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 | |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 45 | error = (error, OSError) |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 46 | |
Ezio Melotti | b08495b | 2013-07-07 13:15:08 +0200 | [diff] [blame] | 47 | try: |
| 48 | from dbm import ndbm |
| 49 | except ImportError: |
| 50 | ndbm = None |
| 51 | |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 52 | |
Éric Araujo | f8e1b60 | 2011-04-20 18:52:55 +0200 | [diff] [blame] | 53 | def open(file, flag='r', mode=0o666): |
| 54 | """Open or create database at path given by *file*. |
| 55 | |
| 56 | Optional argument *flag* can be 'r' (default) for read-only access, 'w' |
| 57 | for read-write access of an existing database, 'c' for read-write access |
| 58 | to a new or existing database, and 'n' for read-write access to a new |
| 59 | database. |
| 60 | |
| 61 | Note: 'r' and 'w' fail if the database doesn't exist; 'c' creates it |
| 62 | only if it doesn't exist; and 'n' always creates a new database. |
| 63 | """ |
Georg Brandl | b17acad | 2008-05-28 08:43:17 +0000 | [diff] [blame] | 64 | global _defaultmod |
| 65 | if _defaultmod is None: |
| 66 | for name in _names: |
| 67 | try: |
| 68 | mod = __import__(name, fromlist=['open']) |
| 69 | except ImportError: |
| 70 | continue |
| 71 | if not _defaultmod: |
| 72 | _defaultmod = mod |
| 73 | _modules[name] = mod |
| 74 | if not _defaultmod: |
| 75 | raise ImportError("no dbm clone found; tried %s" % _names) |
| 76 | |
briancurtin | 525c25d | 2011-03-14 16:03:54 -0400 | [diff] [blame] | 77 | # guess the type of an existing database, if not creating a new one |
| 78 | result = whichdb(file) if 'n' not in flag else None |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 79 | if result is None: |
briancurtin | 525c25d | 2011-03-14 16:03:54 -0400 | [diff] [blame] | 80 | # 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] | 81 | if 'c' in flag or 'n' in flag: |
| 82 | # file doesn't exist and the new flag was used so use default type |
| 83 | mod = _defaultmod |
| 84 | else: |
Amaury Forgeot d'Arc | b5cf301 | 2008-09-25 22:27:43 +0000 | [diff] [blame] | 85 | raise error[0]("need 'c' or 'n' flag to open new db") |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 86 | elif result == "": |
| 87 | # db type cannot be determined |
Amaury Forgeot d'Arc | b5cf301 | 2008-09-25 22:27:43 +0000 | [diff] [blame] | 88 | raise error[0]("db type could not be determined") |
Georg Brandl | b17acad | 2008-05-28 08:43:17 +0000 | [diff] [blame] | 89 | elif result not in _modules: |
Amaury Forgeot d'Arc | b5cf301 | 2008-09-25 22:27:43 +0000 | [diff] [blame] | 90 | raise error[0]("db type is {0}, but the module is not " |
| 91 | "available".format(result)) |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 92 | else: |
| 93 | mod = _modules[result] |
| 94 | return mod.open(file, flag, mode) |
| 95 | |
| 96 | |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 97 | def whichdb(filename): |
| 98 | """Guess which db package to use to open a db file. |
| 99 | |
| 100 | Return values: |
| 101 | |
| 102 | - None if the database file can't be read; |
| 103 | - empty string if the file can be read but can't be recognized |
| 104 | - the name of the dbm submodule (e.g. "ndbm" or "gnu") if recognized. |
| 105 | |
| 106 | Importing the given module may still fail, and opening the |
| 107 | database using that module may still fail. |
| 108 | """ |
| 109 | |
| 110 | # Check for ndbm first -- this has a .pag and a .dir file |
| 111 | try: |
| 112 | f = io.open(filename + ".pag", "rb") |
| 113 | f.close() |
Jesus Cea | f1af705 | 2012-10-05 02:48:46 +0200 | [diff] [blame] | 114 | f = io.open(filename + ".dir", "rb") |
| 115 | f.close() |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 116 | return "dbm.ndbm" |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 117 | except OSError: |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 118 | # some dbm emulations based on Berkeley DB generate a .db file |
| 119 | # some do not, but they should be caught by the bsd checks |
| 120 | try: |
| 121 | f = io.open(filename + ".db", "rb") |
| 122 | f.close() |
| 123 | # guarantee we can actually open the file using dbm |
| 124 | # kind of overkill, but since we are dealing with emulations |
| 125 | # it seems like a prudent step |
| 126 | if ndbm is not None: |
| 127 | d = ndbm.open(filename) |
| 128 | d.close() |
| 129 | return "dbm.ndbm" |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 130 | except OSError: |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 131 | pass |
| 132 | |
| 133 | # Check for dumbdbm next -- this has a .dir and a .dat file |
| 134 | try: |
| 135 | # First check for presence of files |
| 136 | os.stat(filename + ".dat") |
| 137 | size = os.stat(filename + ".dir").st_size |
| 138 | # dumbdbm files with no keys are empty |
| 139 | if size == 0: |
| 140 | return "dbm.dumb" |
| 141 | f = io.open(filename + ".dir", "rb") |
| 142 | try: |
| 143 | if f.read(1) in (b"'", b'"'): |
| 144 | return "dbm.dumb" |
| 145 | finally: |
| 146 | f.close() |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 147 | except OSError: |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 148 | pass |
| 149 | |
| 150 | # See if the file exists, return None if not |
| 151 | try: |
| 152 | f = io.open(filename, "rb") |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 153 | except OSError: |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 154 | return None |
| 155 | |
Serhiy Storchaka | 46ba6c8 | 2015-04-04 11:01:02 +0300 | [diff] [blame] | 156 | with f: |
| 157 | # Read the start of the file -- the magic number |
| 158 | s16 = f.read(16) |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 159 | s = s16[0:4] |
| 160 | |
| 161 | # Return "" if not at least 4 bytes |
| 162 | if len(s) != 4: |
| 163 | return "" |
| 164 | |
| 165 | # Convert to 4-byte int in native byte order -- return "" if impossible |
| 166 | try: |
| 167 | (magic,) = struct.unpack("=l", s) |
| 168 | except struct.error: |
| 169 | return "" |
| 170 | |
| 171 | # Check for GNU dbm |
Jesus Cea | bc566b0 | 2011-09-19 17:08:18 +0200 | [diff] [blame] | 172 | if magic in (0x13579ace, 0x13579acd, 0x13579acf): |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 173 | return "dbm.gnu" |
| 174 | |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 175 | # Later versions of Berkeley db hash file have a 12-byte pad in |
| 176 | # front of the file type |
| 177 | try: |
| 178 | (magic,) = struct.unpack("=l", s16[-4:]) |
| 179 | except struct.error: |
| 180 | return "" |
| 181 | |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 182 | # Unknown |
| 183 | return "" |
| 184 | |
| 185 | |
| 186 | if __name__ == "__main__": |
| 187 | for filename in sys.argv[1:]: |
| 188 | print(whichdb(filename) or "UNKNOWN", filename) |