Skip to content

Commit 1caeeb0

Browse files
author
Richard Jones
committed
Moved over to using marshal in the bsddb and anydbm backends.
roundup-admin now has a "freshen" command that'll load/save all nodes (not retired - mod hyperdb.Class.list() so it lists retired nodes)
1 parent 7787330 commit 1caeeb0

File tree

3 files changed

+80
-36
lines changed

3 files changed

+80
-36
lines changed

roundup-admin

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#! /usr/bin/python
22

3-
# $Id: roundup-admin,v 1.1 2001-07-23 03:46:48 richard Exp $
3+
# $Id: roundup-admin,v 1.2 2001-07-23 08:20:44 richard Exp $
44

55
import sys
66
if int(sys.version[0]) < 2:
@@ -254,6 +254,16 @@ def main():
254254
classname, nodeid = roundupdb.splitDesignator(designator)
255255
db.getclass(classname).retire(nodeid)
256256

257+
elif command == 'freshen':
258+
n, db = determineLogin(instance, argv, n)
259+
for classname, cl in db.classes.items():
260+
properties = cl.properties.keys()
261+
for nodeid in cl.list():
262+
node = {}
263+
for name in properties:
264+
node[name] = cl.get(nodeid, name)
265+
db.setnode(classname, nodeid, node)
266+
257267
else:
258268
print "Unknown command '%s'"%command
259269
usage()
@@ -267,6 +277,9 @@ if __name__ == '__main__':
267277

268278
#
269279
# $Log: not supported by cvs2svn $
280+
# Revision 1.1 2001/07/23 03:46:48 richard
281+
# moving the bin files to facilitate out-of-the-boxness
282+
#
270283
# Revision 1.1 2001/07/22 11:15:45 richard
271284
# More Grande Splite stuff
272285
#

roundup/backends/back_anydbm.py

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#$Id: back_anydbm.py,v 1.1 2001-07-23 07:22:13 richard Exp $
1+
#$Id: back_anydbm.py,v 1.2 2001-07-23 08:20:44 richard Exp $
22

3-
import anydbm, os, cPickle
3+
import anydbm, os, marshal
44
from roundup import hyperdb, date
55

66
#
@@ -68,11 +68,24 @@ def getclassdb(self, classname, mode='r'):
6868
path = os.path.join(os.getcwd(), self.dir, 'nodes.%s'%classname)
6969
return anydbm.open(path, mode)
7070

71+
#
72+
# Nodes
73+
#
7174
def addnode(self, classname, nodeid, node):
7275
''' add the specified node to its class's db
7376
'''
7477
db = self.getclassdb(classname, 'c')
75-
db[nodeid] = cPickle.dumps(node, 1)
78+
79+
# convert the instance data to builtin types
80+
properties = self.classes[classname].properties
81+
for key in properties.keys():
82+
if properties[key].isDateType:
83+
node[key] = node[key].get_tuple()
84+
elif properties[key].isIntervalType:
85+
node[key] = node[key].get_tuple()
86+
87+
# now save the marshalled data
88+
db[nodeid] = marshal.dumps(node)
7689
db.close()
7790
setnode = addnode
7891

@@ -82,7 +95,16 @@ def getnode(self, classname, nodeid, cldb=None):
8295
db = cldb or self.getclassdb(classname)
8396
if not db.has_key(nodeid):
8497
raise IndexError, nodeid
85-
res = cPickle.loads(db[nodeid])
98+
res = marshal.loads(db[nodeid])
99+
100+
# convert the marshalled data to instances
101+
properties = self.classes[classname].properties
102+
for key in res.keys():
103+
if properties[key].isDateType:
104+
res[key] = date.Date(res[key])
105+
elif properties[key].isIntervalType:
106+
res[key] = date.Interval(res[key])
107+
86108
if not cldb: db.close()
87109
return res
88110

@@ -117,22 +139,35 @@ def addjournal(self, classname, nodeid, action, params):
117139
'link' or 'unlink' -- 'params' is (classname, nodeid, propname)
118140
'retire' -- 'params' is None
119141
'''
120-
entry = (nodeid, date.Date(), self.journaltag, action, params)
142+
entry = (nodeid, date.Date().get_tuple(), self.journaltag, action,
143+
params)
121144
db = anydbm.open(os.path.join(self.dir, 'journals.%s'%classname), 'c')
122145
if db.has_key(nodeid):
123146
s = db[nodeid]
124-
l = cPickle.loads(db[nodeid])
147+
l = marshal.loads(db[nodeid])
125148
l.append(entry)
126149
else:
127150
l = [entry]
128-
db[nodeid] = cPickle.dumps(l)
151+
db[nodeid] = marshal.dumps(l)
129152
db.close()
130153

131154
def getjournal(self, classname, nodeid):
132155
''' get the journal for id
133156
'''
134-
db = anydbm.open(os.path.join(self.dir, 'journals.%s'%classname), 'r')
135-
res = cPickle.loads(db[nodeid])
157+
# attempt to open the journal - in some rare cases, the journal may
158+
# not exist
159+
try:
160+
db = anydbm.open(os.path.join(self.dir, 'journals.%s'%classname),
161+
'r')
162+
except anydbm.open, error:
163+
if error.args[0] != 2: raise
164+
return []
165+
journal = marshal.loads(db[nodeid])
166+
res = []
167+
for entry in journal:
168+
(nodeid, date_stamp, self.journaltag, action, params) = entry
169+
date_obj = date.Date(date_stamp)
170+
res.append((nodeid, date_obj, self.journaltag, action, params))
136171
db.close()
137172
return res
138173

@@ -162,7 +197,4 @@ def rollback(self):
162197

163198
#
164199
#$Log: not supported by cvs2svn $
165-
#Revision 1.1 2001/07/23 07:15:57 richard
166-
#Moved the backends into the backends package. Anydbm hasn't been tested at all.
167-
#
168200
#

roundup/backends/back_bsddb.py

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
#$Id: back_bsddb.py,v 1.2 2001-07-23 07:56:05 richard Exp $
1+
#$Id: back_bsddb.py,v 1.3 2001-07-23 08:20:44 richard Exp $
22

33
import bsddb, os, marshal
4-
# handle the older cPickle'd data
5-
import cPickle
64
from roundup import hyperdb, date
75

86
#
@@ -77,14 +75,17 @@ def addnode(self, classname, nodeid, node):
7775
''' add the specified node to its class's db
7876
'''
7977
db = self.getclassdb(classname, 'c')
78+
8079
# convert the instance data to builtin types
8180
properties = self.classes[classname].properties
82-
for key in res.keys():
81+
for key in properties.keys():
8382
if properties[key].isDateType:
84-
res[key] = res[key].get_tuple()
83+
node[key] = node[key].get_tuple()
8584
elif properties[key].isIntervalType:
86-
res[key] = res[key].get_tuple()
87-
db[nodeid] = marshal.dumps(node, 1)
85+
node[key] = node[key].get_tuple()
86+
87+
# now save the marshalled data
88+
db[nodeid] = marshal.dumps(node)
8889
db.close()
8990
setnode = addnode
9091

@@ -94,20 +95,15 @@ def getnode(self, classname, nodeid, cldb=None):
9495
db = cldb or self.getclassdb(classname)
9596
if not db.has_key(nodeid):
9697
raise IndexError, nodeid
97-
try:
98-
res = marshal.loads(db[nodeid])
99-
# convert the marshalled data to instances
100-
properties = self.classes[classname].properties
101-
for key in res.keys():
102-
if properties[key].isDateType:
103-
res[key] = date.Date(res[key])
104-
elif properties[key].isIntervalType:
105-
res[key] = date.Interval(res[key])
106-
except ValueError, message:
107-
if str(message) != 'bad marshal data':
108-
raise
109-
# handle the older cPickle'd data
110-
res = cPickle.loads(db[nodeid])
98+
res = marshal.loads(db[nodeid])
99+
100+
# convert the marshalled data to instances
101+
properties = self.classes[classname].properties
102+
for key in res.keys():
103+
if properties[key].isDateType:
104+
res[key] = date.Date(res[key])
105+
elif properties[key].isIntervalType:
106+
res[key] = date.Interval(res[key])
111107

112108
if not cldb: db.close()
113109
return res
@@ -143,7 +139,7 @@ def addjournal(self, classname, nodeid, action, params):
143139
'link' or 'unlink' -- 'params' is (classname, nodeid, propname)
144140
'retire' -- 'params' is None
145141
'''
146-
entry = (nodeid, date.Date().journal_tuple(), self.journaltag, action,
142+
entry = (nodeid, date.Date().get_tuple(), self.journaltag, action,
147143
params)
148144
db = bsddb.btopen(os.path.join(self.dir, 'journals.%s'%classname), 'c')
149145
if db.has_key(nodeid):
@@ -170,7 +166,7 @@ def getjournal(self, classname, nodeid):
170166
res = []
171167
for entry in journal:
172168
(nodeid, date_stamp, self.journaltag, action, params) = entry
173-
date_obj = date.Date(set=date_stamp)
169+
date_obj = date.Date(date_stamp)
174170
res.append((nodeid, date_obj, self.journaltag, action, params))
175171
db.close()
176172
return res
@@ -201,6 +197,9 @@ def rollback(self):
201197

202198
#
203199
#$Log: not supported by cvs2svn $
200+
#Revision 1.2 2001/07/23 07:56:05 richard
201+
#Storing only marshallable data in the db - no nasty pickled class references.
202+
#
204203
#Revision 1.1 2001/07/23 07:22:13 richard
205204
#*sigh* some databases have _foo.so as their underlying implementation.
206205
#This time for sure, Rocky.

0 commit comments

Comments
 (0)