Skip to content

Commit 96531b0

Browse files
author
Richard Jones
committed
I fixed the problems with anydbm using the dbm module at the backend.
It turns out the dbm module modifies the file name to append ".db" and my check to determine if we're opening an existing or new db just tested os.path.exists() on the filename. Well, no longer! We now perform a much better check _and_ cope with the anydbm implementation module changing too! I also fixed the backends __init__ so only ImportError is squashed.
1 parent 57c4560 commit 96531b0

File tree

3 files changed

+54
-13
lines changed

3 files changed

+54
-13
lines changed

CHANGES.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ Fixed:
3636
. fixed doc/index.html to include the quoting in the mail alias.
3737
. fixed the backends __init__ so we can pydoc the backend modules
3838
. web i/f reports "note added" if there are no changes but a note is entered
39+
. we were assuming database files created by anydbm had the same name, but
40+
this is not the case for dbm. We now perform a much better check _and_
41+
cope with the anydbm implementation module changing too!
3942

4043

4144
2001-11-23 - 0.3.0

roundup/backends/__init__.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717
#
18-
# $Id: __init__.py,v 1.8 2001-12-10 22:20:01 richard Exp $
18+
# $Id: __init__.py,v 1.9 2001-12-12 02:30:51 richard Exp $
1919

2020
__all__ = []
2121

@@ -30,25 +30,32 @@
3030
__all__.append('anydbm')
3131
except AssertionError:
3232
del back_anydbm
33-
except:
33+
except ImportError:
3434
pass
3535

3636
try:
3737
import back_bsddb
3838
bsddb = back_bsddb
3939
__all__.append('bsddb')
40-
except:
40+
except ImportError:
4141
pass
4242

4343
try:
4444
import back_bsddb3
4545
bsddb3 = back_bsddb3
4646
__all__.append('bsddb3')
47-
except:
47+
except ImportError:
4848
pass
4949

5050
#
5151
# $Log: not supported by cvs2svn $
52+
# Revision 1.8 2001/12/10 22:20:01 richard
53+
# Enabled transaction support in the bsddb backend. It uses the anydbm code
54+
# where possible, only replacing methods where the db is opened (it uses the
55+
# btree opener specifically.)
56+
# Also cleaned up some change note generation.
57+
# Made the backends package work with pydoc too.
58+
#
5259
# Revision 1.7 2001/12/10 00:57:38 richard
5360
# From CHANGES:
5461
# . Added the "display" command to the admin tool - displays a node's values

roundup/backends/back_anydbm.py

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515
# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717
#
18-
#$Id: back_anydbm.py,v 1.14 2001-12-10 22:20:01 richard Exp $
18+
#$Id: back_anydbm.py,v 1.15 2001-12-12 02:30:51 richard Exp $
1919
'''
2020
This module defines a backend that saves the hyperdatabase in a database
2121
chosen by anydbm. It is guaranteed to always be available in python
2222
versions >2.1.1 (the dumbdbm fallback in 2.1.1 and earlier has several
2323
serious bugs, and is not available)
2424
'''
2525

26-
import anydbm, os, marshal
26+
import whichdb, anydbm, os, marshal
2727
from roundup import hyperdb, date, password
2828

2929
#
@@ -88,22 +88,46 @@ def getclass(self, classname):
8888
# Class DBs
8989
#
9090
def clear(self):
91+
'''Delete all database contents
92+
'''
9193
for cn in self.classes.keys():
92-
db = os.path.join(self.dir, 'nodes.%s'%cn)
93-
anydbm.open(db, 'n')
94-
db = os.path.join(self.dir, 'journals.%s'%cn)
95-
anydbm.open(db, 'n')
94+
for type in 'nodes', 'journals':
95+
path = os.path.join(self.dir, 'journals.%s'%cn)
96+
if os.path.exists(path):
97+
os.remove(path)
98+
elif os.path.exists(path+'.db'): # dbm appends .db
99+
os.remove(path+'.db')
96100

97101
def getclassdb(self, classname, mode='r'):
98102
''' grab a connection to the class db that will be used for
99103
multiple actions
100104
'''
105+
# determine which DB wrote the class file
101106
path = os.path.join(os.getcwd(), self.dir, 'nodes.%s'%classname)
102-
if os.path.exists(path):
103-
return anydbm.open(path, mode)
104-
else:
107+
db_type = whichdb.whichdb(path)
108+
if not db_type:
109+
# dbm appends ".db"
110+
db_type = whichdb.whichdb(path+'.db')
111+
db_type = whichdb.whichdb(path)
112+
113+
# if we can't identify it and it exists...
114+
if not db_type and os.path.exists(path) or os.path.exists(path+'.db'):
115+
raise hyperdb.DatabaseError, \
116+
"Couldn't identify the database type"
117+
118+
# new database? let anydbm pick the best dbm
119+
if not db_type:
105120
return anydbm.open(path, 'n')
106121

122+
# open the database with the correct module
123+
try:
124+
dbm = __import__(db_type)
125+
except:
126+
raise hyperdb.DatabaseError, \
127+
"Couldn't open database - the required module '%s'"\
128+
"is not available"%db_type
129+
return dbm.open(path, mode)
130+
107131
#
108132
# Nodes
109133
#
@@ -254,6 +278,13 @@ def rollback(self):
254278

255279
#
256280
#$Log: not supported by cvs2svn $
281+
#Revision 1.14 2001/12/10 22:20:01 richard
282+
#Enabled transaction support in the bsddb backend. It uses the anydbm code
283+
#where possible, only replacing methods where the db is opened (it uses the
284+
#btree opener specifically.)
285+
#Also cleaned up some change note generation.
286+
#Made the backends package work with pydoc too.
287+
#
257288
#Revision 1.13 2001/12/02 05:06:16 richard
258289
#. We now use weakrefs in the Classes to keep the database reference, so
259290
# the close() method on the database is no longer needed.

0 commit comments

Comments
 (0)