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