2
Serializer code for cobbler.
3
Experimental: couchdb version
5
Copyright 2006-2009, Red Hat, Inc
6
Michael DeHaan <mdehaan@redhat.com>
8
This program is free software; you can redistribute it and/or modify
9
it under the terms of the GNU General Public License as published by
10
the Free Software Foundation; either version 2 of the License, or
11
(at your option) any later version.
13
This program is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
GNU General Public License for more details.
18
You should have received a copy of the GNU General Public License
19
along with this program; if not, write to the Free Software
20
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24
import distutils.sysconfig
33
plib = distutils.sysconfig.get_python_lib()
34
mod_path="%s/cobbler" % plib
35
sys.path.insert(0, mod_path)
39
from cexceptions import *
43
typez = [ "distro", "profile", "system", "image", "repo" ]
44
couchdb = couch.Couch('127.0.0.1')
53
The mandatory cobbler module registration hook.
55
# FIXME: only run this if enabled.
58
def serialize_item(obj, item):
60
datastruct = item.to_datastruct()
61
# blindly prevent conflict resolution
62
couchdb.openDoc(obj.collection_type(), item.name)
63
data = couchdb.saveDoc(obj.collection_type(),
64
simplejson.dumps(datastruct, encoding="utf-8"),
66
data = simplejson.loads(data)
69
def serialize_delete(obj, item):
71
couchdb.deleteDoc(obj.collection_type(),
75
def deserialize_item_raw(collection_type, item_name):
77
data = couchdb.openDoc(collection_type, item_name)
78
return simplejson.loads(data, encoding="utf-8")
82
Save an object to disk. Object must "implement" Serializable.
83
FIXME: Return False on access/permission errors.
84
This should NOT be used by API if serialize_item is available.
87
ctype = obj.collection_type()
88
if ctype == "settings":
94
def deserialize_raw(collection_type):
96
contents = simplejson.loads(couchdb.listDoc(collection_type))
98
if contents.has_key("error") and contents.get("reason","").find("Missing") != -1:
99
# no items in the DB yet
101
for x in contents["rows"]:
102
items.append(x["key"])
104
if collection_type == "settings":
105
fd = open("/etc/cobbler/settings")
106
datastruct = yaml.load(fd.read())
112
data = couchdb.openDoc(collection_type, f)
113
datastruct = simplejson.loads(data, encoding='utf-8')
114
results.append(datastruct)
117
def deserialize(obj,topological=True):
119
Populate an existing object with the contents of datastruct.
120
Object must "implement" Serializable.
123
datastruct = deserialize_raw(obj.collection_type())
124
if topological and type(datastruct) == list:
125
datastruct.sort(__depth_cmp)
126
obj.from_datastruct(datastruct)
129
def __depth_cmp(item1, item2):
130
d1 = item1.get("depth",1)
131
d2 = item2.get("depth",1)
134
if __name__ == "__main__":
135
print deserialize_item_raw("distro","D1")