blob: 0609e4990c2f8ea5e134b8b9b9a75664528fa0fa [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
Andrew Svetlovf7a17b42012-12-25 16:47:37 +020045error = (error, OSError)
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()
Jesus Ceaf1af7052012-10-05 02:48:46 +0200109 f = io.open(filename + ".dir", "rb")
110 f.close()
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000111 return "dbm.ndbm"
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200112 except OSError:
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000113 # some dbm emulations based on Berkeley DB generate a .db file
114 # some do not, but they should be caught by the bsd checks
115 try:
116 f = io.open(filename + ".db", "rb")
117 f.close()
118 # guarantee we can actually open the file using dbm
119 # kind of overkill, but since we are dealing with emulations
120 # it seems like a prudent step
121 if ndbm is not None:
122 d = ndbm.open(filename)
123 d.close()
124 return "dbm.ndbm"
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200125 except OSError:
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000126 pass
127
128 # Check for dumbdbm next -- this has a .dir and a .dat file
129 try:
130 # First check for presence of files
131 os.stat(filename + ".dat")
132 size = os.stat(filename + ".dir").st_size
133 # dumbdbm files with no keys are empty
134 if size == 0:
135 return "dbm.dumb"
136 f = io.open(filename + ".dir", "rb")
137 try:
138 if f.read(1) in (b"'", b'"'):
139 return "dbm.dumb"
140 finally:
141 f.close()
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200142 except OSError:
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000143 pass
144
145 # See if the file exists, return None if not
146 try:
147 f = io.open(filename, "rb")
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200148 except OSError:
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000149 return None
150
151 # Read the start of the file -- the magic number
152 s16 = f.read(16)
153 f.close()
154 s = s16[0:4]
155
156 # Return "" if not at least 4 bytes
157 if len(s) != 4:
158 return ""
159
160 # Convert to 4-byte int in native byte order -- return "" if impossible
161 try:
162 (magic,) = struct.unpack("=l", s)
163 except struct.error:
164 return ""
165
166 # Check for GNU dbm
Jesus Ceabc566b02011-09-19 17:08:18 +0200167 if magic in (0x13579ace, 0x13579acd, 0x13579acf):
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000168 return "dbm.gnu"
169
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000170 # Later versions of Berkeley db hash file have a 12-byte pad in
171 # front of the file type
172 try:
173 (magic,) = struct.unpack("=l", s16[-4:])
174 except struct.error:
175 return ""
176
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000177 # Unknown
178 return ""
179
180
181if __name__ == "__main__":
182 for filename in sys.argv[1:]:
183 print(whichdb(filename) or "UNKNOWN", filename)