Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 1 | """Generic interface to all dbm clones. |
| 2 | |
| 3 | Use |
| 4 | |
| 5 | import dbm |
| 6 | d = dbm.open(file, 'w', 0o666) |
| 7 | |
Victor Stinner | 1cec3e3 | 2010-07-29 16:26:56 +0000 | [diff] [blame] | 8 | The returned object is a dbm.gnu, dbm.ndbm or dbm.dumb object, dependent on the |
| 9 | type of database being opened (determined by the whichdb function) in the case |
| 10 | of an existing dbm. If the dbm does not exist and the create or new flag ('c' |
| 11 | or 'n') was specified, the dbm type will be determined by the availability of |
| 12 | the modules (tested in the above order). |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 13 | |
| 14 | It 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 | |
| 25 | Future versions may change the order in which implementations are |
Éric Araujo | f8e1b60 | 2011-04-20 18:52:55 +0200 | [diff] [blame] | 26 | tested for existence, and add interfaces to other dbm-like |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 27 | implementations. |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 28 | """ |
| 29 | |
Antoine Pitrou | a328029 | 2009-10-10 21:28:29 +0000 | [diff] [blame] | 30 | __all__ = ['open', 'whichdb', 'error'] |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 31 | |
| 32 | import io |
| 33 | import os |
| 34 | import struct |
| 35 | import sys |
| 36 | |
| 37 | |
| 38 | class error(Exception): |
| 39 | pass |
| 40 | |
Victor Stinner | 1cec3e3 | 2010-07-29 16:26:56 +0000 | [diff] [blame] | 41 | _names = ['dbm.gnu', 'dbm.ndbm', 'dbm.dumb'] |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 42 | _defaultmod = None |
| 43 | _modules = {} |
| 44 | |
Georg Brandl | b17acad | 2008-05-28 08:43:17 +0000 | [diff] [blame] | 45 | error = (error, IOError) |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 46 | |
| 47 | |
Éric Araujo | f8e1b60 | 2011-04-20 18:52:55 +0200 | [diff] [blame] | 48 | def 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 Brandl | b17acad | 2008-05-28 08:43:17 +0000 | [diff] [blame] | 59 | 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 | |
briancurtin | 525c25d | 2011-03-14 16:03:54 -0400 | [diff] [blame] | 72 | # 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 Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 74 | if result is None: |
briancurtin | 525c25d | 2011-03-14 16:03:54 -0400 | [diff] [blame] | 75 | # db doesn't exist or 'n' flag was specified to create a new db |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 76 | 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'Arc | b5cf301 | 2008-09-25 22:27:43 +0000 | [diff] [blame] | 80 | raise error[0]("need 'c' or 'n' flag to open new db") |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 81 | elif result == "": |
| 82 | # db type cannot be determined |
Amaury Forgeot d'Arc | b5cf301 | 2008-09-25 22:27:43 +0000 | [diff] [blame] | 83 | raise error[0]("db type could not be determined") |
Georg Brandl | b17acad | 2008-05-28 08:43:17 +0000 | [diff] [blame] | 84 | elif result not in _modules: |
Amaury Forgeot d'Arc | b5cf301 | 2008-09-25 22:27:43 +0000 | [diff] [blame] | 85 | raise error[0]("db type is {0}, but the module is not " |
| 86 | "available".format(result)) |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 87 | else: |
| 88 | mod = _modules[result] |
| 89 | return mod.open(file, flag, mode) |
| 90 | |
| 91 | |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 92 | def 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 Cea | f1af705 | 2012-10-05 02:48:46 +0200 | [diff] [blame] | 109 | f = io.open(filename + ".dir", "rb") |
| 110 | f.close() |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 111 | return "dbm.ndbm" |
| 112 | except IOError: |
| 113 | # 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" |
Georg Brandl | b17acad | 2008-05-28 08:43:17 +0000 | [diff] [blame] | 125 | except IOError: |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 126 | 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() |
| 142 | except (OSError, IOError): |
| 143 | pass |
| 144 | |
| 145 | # See if the file exists, return None if not |
| 146 | try: |
| 147 | f = io.open(filename, "rb") |
| 148 | except IOError: |
| 149 | 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 Cea | bc566b0 | 2011-09-19 17:08:18 +0200 | [diff] [blame] | 167 | if magic in (0x13579ace, 0x13579acd, 0x13579acf): |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 168 | return "dbm.gnu" |
| 169 | |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 170 | # 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 Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 177 | # Unknown |
| 178 | return "" |
| 179 | |
| 180 | |
| 181 | if __name__ == "__main__": |
| 182 | for filename in sys.argv[1:]: |
| 183 | print(whichdb(filename) or "UNKNOWN", filename) |