~andreserl/+junk/cobbler

« back to all changes in this revision

Viewing changes to .pc/60_yaml_safe_load.patch/cobbler/modules/serializer_couch.py

  • Committer: Andres Rodriguez
  • Date: 2011-12-09 19:21:57 UTC
  • Revision ID: andreserl@ubuntu.com-20111209192157-rul6dqkyaog2f3i5
Reverted changes back to rev50

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
"""
2
 
Serializer code for cobbler.
3
 
Experimental:  couchdb version
4
 
 
5
 
Copyright 2006-2009, Red Hat, Inc
6
 
Michael DeHaan <mdehaan@redhat.com>
7
 
 
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.
12
 
 
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.
17
 
 
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
21
 
02110-1301  USA
22
 
"""
23
 
 
24
 
import distutils.sysconfig
25
 
import os
26
 
import sys
27
 
import glob
28
 
import traceback
29
 
import yaml # PyYAML
30
 
import simplejson
31
 
import exceptions
32
 
 
33
 
plib = distutils.sysconfig.get_python_lib()
34
 
mod_path="%s/cobbler" % plib
35
 
sys.path.insert(0, mod_path)
36
 
 
37
 
from utils import _
38
 
import utils
39
 
from cexceptions import *
40
 
import os
41
 
import couch
42
 
 
43
 
typez = [ "distro", "profile", "system", "image", "repo" ]
44
 
couchdb = couch.Couch('127.0.0.1')
45
 
 
46
 
def __connect():
47
 
   couchdb.connect()
48
 
   for x in typez:
49
 
       couchdb.createDb(x)
50
 
 
51
 
def register():
52
 
    """
53
 
    The mandatory cobbler module registration hook.
54
 
    """
55
 
    # FIXME: only run this if enabled.
56
 
    return "serializer"
57
 
 
58
 
def serialize_item(obj, item):
59
 
    __connect()
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"),
65
 
                  item.name)
66
 
    data = simplejson.loads(data)
67
 
    return True
68
 
 
69
 
def serialize_delete(obj, item):
70
 
    __connect()
71
 
    couchdb.deleteDoc(obj.collection_type(),
72
 
                    item.name)
73
 
    return True
74
 
 
75
 
def deserialize_item_raw(collection_type, item_name):
76
 
    __connect()
77
 
    data = couchdb.openDoc(collection_type, item_name)
78
 
    return simplejson.loads(data, encoding="utf-8")
79
 
 
80
 
def serialize(obj):
81
 
    """
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.
85
 
    """
86
 
    __connect()
87
 
    ctype = obj.collection_type()
88
 
    if ctype == "settings":
89
 
        return True
90
 
    for x in obj:
91
 
        serialize_item(obj,x)
92
 
    return True
93
 
 
94
 
def deserialize_raw(collection_type):
95
 
    __connect()
96
 
    contents = simplejson.loads(couchdb.listDoc(collection_type))
97
 
    items = []
98
 
    if contents.has_key("error") and contents.get("reason","").find("Missing") != -1:
99
 
        # no items in the DB yet
100
 
        return []
101
 
    for x in contents["rows"]:
102
 
       items.append(x["key"])
103
 
 
104
 
    if collection_type == "settings":
105
 
         fd = open("/etc/cobbler/settings")
106
 
         datastruct = yaml.load(fd.read())
107
 
         fd.close()
108
 
         return datastruct
109
 
    else:
110
 
         results = []
111
 
         for f in items:
112
 
             data = couchdb.openDoc(collection_type, f)
113
 
             datastruct = simplejson.loads(data, encoding='utf-8')
114
 
             results.append(datastruct)
115
 
         return results    
116
 
 
117
 
def deserialize(obj,topological=True):
118
 
    """
119
 
    Populate an existing object with the contents of datastruct.
120
 
    Object must "implement" Serializable.  
121
 
    """
122
 
    __connect()
123
 
    datastruct = deserialize_raw(obj.collection_type())
124
 
    if topological and type(datastruct) == list:
125
 
       datastruct.sort(__depth_cmp)
126
 
    obj.from_datastruct(datastruct)
127
 
    return True
128
 
 
129
 
def __depth_cmp(item1, item2):
130
 
    d1 = item1.get("depth",1)
131
 
    d2 = item2.get("depth",1)
132
 
    return cmp(d1,d2)
133
 
 
134
 
if __name__ == "__main__":
135
 
    print deserialize_item_raw("distro","D1")
136