blob: 813a29d70922814b20a93ed7cc68df7673d12a30 [file] [log] [blame]
Georg Brandl0a7ac7d2008-05-26 10:29:35 +00001"""Generic interface to all dbm clones.
2
3Use
4
5 import dbm
6 d = dbm.open(file, 'w', 0o666)
7
Victor Stinner1cec3e32010-07-29 16:26:56 +00008The returned object is a dbm.gnu, dbm.ndbm or dbm.dumb object, dependent on the
9type of database being opened (determined by the whichdb function) in the case
10of an existing dbm. If the dbm does not exist and the create or new flag ('c'
11or 'n') was specified, the dbm type will be determined by the availability of
12the modules (tested in the above order).
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000013
14It has the following interface (key and data are strings):
15
16 d[key] = data # store data at key (may override data at
17 # existing key)
18 data = d[key] # retrieve data at key (raise KeyError if no
19 # such key)
20 del d[key] # delete data stored at key (raises KeyError
21 # if no such key)
22 flag = key in d # true if the key exists
23 list = d.keys() # return a list of all existing keys (slow!)
24
25Future versions may change the order in which implementations are
Éric Araujof8e1b602011-04-20 18:52:55 +020026tested for existence, and add interfaces to other dbm-like
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000027implementations.
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000028"""
29
Antoine Pitroua3280292009-10-10 21:28:29 +000030__all__ = ['open', 'whichdb', 'error']
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000031
32import io
33import os
34import struct
35import sys
36
37
38class error(Exception):
39 pass
40
Victor Stinner1cec3e32010-07-29 16:26:56 +000041_names = ['dbm.gnu', 'dbm.ndbm', 'dbm.dumb']
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000042_defaultmod = None
43_modules = {}
44
Georg Brandlb17acad2008-05-28 08:43:17 +000045error = (error, IOError)
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000046
47
Éric Araujof8e1b602011-04-20 18:52:55 +020048def open(file, flag='r', mode=0o666):
49 """Open or create database at path given by *file*.
50
51 Optional argument *flag* can be 'r' (default) for read-only access, 'w'
52 for read-write access of an existing database, 'c' for read-write access
53 to a new or existing database, and 'n' for read-write access to a new
54 database.
55
56 Note: 'r' and 'w' fail if the database doesn't exist; 'c' creates it
57 only if it doesn't exist; and 'n' always creates a new database.
58 """
Georg Brandlb17acad2008-05-28 08:43:17 +000059 global _defaultmod
60 if _defaultmod is None:
61 for name in _names:
62 try:
63 mod = __import__(name, fromlist=['open'])
64 except ImportError:
65 continue
66 if not _defaultmod:
67 _defaultmod = mod
68 _modules[name] = mod
69 if not _defaultmod:
70 raise ImportError("no dbm clone found; tried %s" % _names)
71
briancurtin525c25d2011-03-14 16:03:54 -040072 # guess the type of an existing database, if not creating a new one
73 result = whichdb(file) if 'n' not in flag else None
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000074 if result is None:
briancurtin525c25d2011-03-14 16:03:54 -040075 # db doesn't exist or 'n' flag was specified to create a new db
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000076 if 'c' in flag or 'n' in flag:
77 # file doesn't exist and the new flag was used so use default type
78 mod = _defaultmod
79 else:
Amaury Forgeot d'Arcb5cf3012008-09-25 22:27:43 +000080 raise error[0]("need 'c' or 'n' flag to open new db")
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000081 elif result == "":
82 # db type cannot be determined
Amaury Forgeot d'Arcb5cf3012008-09-25 22:27:43 +000083 raise error[0]("db type could not be determined")
Georg Brandlb17acad2008-05-28 08:43:17 +000084 elif result not in _modules:
Amaury Forgeot d'Arcb5cf3012008-09-25 22:27:43 +000085 raise error[0]("db type is {0}, but the module is not "
86 "available".format(result))
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000087 else:
88 mod = _modules[result]
89 return mod.open(file, flag, mode)
90
91
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000092def whichdb(filename):
93 """Guess which db package to use to open a db file.
94
95 Return values:
96
97 - None if the database file can't be read;
98 - empty string if the file can be read but can't be recognized
99 - the name of the dbm submodule (e.g. "ndbm" or "gnu") if recognized.
100
101 Importing the given module may still fail, and opening the
102 database using that module may still fail.
103 """
104
105 # Check for ndbm first -- this has a .pag and a .dir file
106 try:
107 f = io.open(filename + ".pag", "rb")
108 f.close()
109 # dbm linked with gdbm on OS/2 doesn't have .dir file
110 if not (ndbm.library == "GNU gdbm" and sys.platform == "os2emx"):
111 f = io.open(filename + ".dir", "rb")
112 f.close()
113 return "dbm.ndbm"
114 except IOError:
115 # some dbm emulations based on Berkeley DB generate a .db file
116 # some do not, but they should be caught by the bsd checks
117 try:
118 f = io.open(filename + ".db", "rb")
119 f.close()
120 # guarantee we can actually open the file using dbm
121 # kind of overkill, but since we are dealing with emulations
122 # it seems like a prudent step
123 if ndbm is not None:
124 d = ndbm.open(filename)
125 d.close()
126 return "dbm.ndbm"
Georg Brandlb17acad2008-05-28 08:43:17 +0000127 except IOError:
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000128 pass
129
130 # Check for dumbdbm next -- this has a .dir and a .dat file
131 try:
132 # First check for presence of files
133 os.stat(filename + ".dat")
134 size = os.stat(filename + ".dir").st_size
135 # dumbdbm files with no keys are empty
136 if size == 0:
137 return "dbm.dumb"
138 f = io.open(filename + ".dir", "rb")
139 try:
140 if f.read(1) in (b"'", b'"'):
141 return "dbm.dumb"
142 finally:
143 f.close()
144 except (OSError, IOError):
145 pass
146
147 # See if the file exists, return None if not
148 try:
149 f = io.open(filename, "rb")
150 except IOError:
151 return None
152
153 # Read the start of the file -- the magic number
154 s16 = f.read(16)
155 f.close()
156 s = s16[0:4]
157
158 # Return "" if not at least 4 bytes
159 if len(s) != 4:
160 return ""
161
162 # Convert to 4-byte int in native byte order -- return "" if impossible
163 try:
164 (magic,) = struct.unpack("=l", s)
165 except struct.error:
166 return ""
167
168 # Check for GNU dbm
Jesus Ceabc566b02011-09-19 17:08:18 +0200169 if magic in (0x13579ace, 0x13579acd, 0x13579acf):
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000170 return "dbm.gnu"
171
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000172 # Later versions of Berkeley db hash file have a 12-byte pad in
173 # front of the file type
174 try:
175 (magic,) = struct.unpack("=l", s16[-4:])
176 except struct.error:
177 return ""
178
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000179 # Unknown
180 return ""
181
182
183if __name__ == "__main__":
184 for filename in sys.argv[1:]:
185 print(whichdb(filename) or "UNKNOWN", filename)