| 1 |
|
| 2 |
"""Guess which db package to use to open a db file.""" |
| 3 |
|
| 4 |
import os |
| 5 |
import struct |
| 6 |
import sys |
| 7 |
|
| 8 |
try: |
| 9 |
import dbm |
| 10 |
_dbmerror = dbm.error |
| 11 |
except ImportError: |
| 12 |
dbm = None |
| 13 |
|
| 14 |
|
| 15 |
_dbmerror = IOError |
| 16 |
|
| 17 |
def whichdb(filename): |
| 18 |
"""Guess which db package to use to open a db file. |
| 19 |
|
| 20 |
Return values: |
| 21 |
|
| 22 |
- None if the database file can't be read; |
| 23 |
- empty string if the file can be read but can't be recognized |
| 24 |
- the module name (e.g. "dbm" or "gdbm") if recognized. |
| 25 |
|
| 26 |
Importing the given module may still fail, and opening the |
| 27 |
database using that module may still fail. |
| 28 |
""" |
| 29 |
|
| 30 |
|
| 31 |
try: |
| 32 |
f = open(filename + os.extsep + "pag", "rb") |
| 33 |
f.close() |
| 34 |
|
| 35 |
if not (dbm.library == "GNU gdbm" and sys.platform == "os2emx"): |
| 36 |
f = open(filename + os.extsep + "dir", "rb") |
| 37 |
f.close() |
| 38 |
return "dbm" |
| 39 |
except IOError: |
| 40 |
|
| 41 |
|
| 42 |
try: |
| 43 |
f = open(filename + os.extsep + "db", "rb") |
| 44 |
f.close() |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
if dbm is not None: |
| 49 |
d = dbm.open(filename) |
| 50 |
d.close() |
| 51 |
return "dbm" |
| 52 |
except (IOError, _dbmerror): |
| 53 |
pass |
| 54 |
|
| 55 |
|
| 56 |
try: |
| 57 |
|
| 58 |
os.stat(filename + os.extsep + "dat") |
| 59 |
size = os.stat(filename + os.extsep + "dir").st_size |
| 60 |
|
| 61 |
if size == 0: |
| 62 |
return "dumbdbm" |
| 63 |
f = open(filename + os.extsep + "dir", "rb") |
| 64 |
try: |
| 65 |
if f.read(1) in ("'", '"'): |
| 66 |
return "dumbdbm" |
| 67 |
finally: |
| 68 |
f.close() |
| 69 |
except (OSError, IOError): |
| 70 |
pass |
| 71 |
|
| 72 |
|
| 73 |
try: |
| 74 |
f = open(filename, "rb") |
| 75 |
except IOError: |
| 76 |
return None |
| 77 |
|
| 78 |
|
| 79 |
s16 = f.read(16) |
| 80 |
f.close() |
| 81 |
s = s16[0:4] |
| 82 |
|
| 83 |
|
| 84 |
if len(s) != 4: |
| 85 |
return "" |
| 86 |
|
| 87 |
|
| 88 |
try: |
| 89 |
(magic,) = struct.unpack("=l", s) |
| 90 |
except struct.error: |
| 91 |
return "" |
| 92 |
|
| 93 |
|
| 94 |
if magic in (0x13579ace, 0x13579acd, 0x13579acf): |
| 95 |
return "gdbm" |
| 96 |
|
| 97 |
|
| 98 |
if magic in (0x00061561, 0x61150600): |
| 99 |
return "bsddb185" |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
try: |
| 104 |
(magic,) = struct.unpack("=l", s16[-4:]) |
| 105 |
except struct.error: |
| 106 |
return "" |
| 107 |
|
| 108 |
|
| 109 |
if magic in (0x00061561, 0x61150600): |
| 110 |
return "dbhash" |
| 111 |
|
| 112 |
|
| 113 |
return "" |
| 114 |
|
| 115 |
if __name__ == "__main__": |
| 116 |
for filename in sys.argv[1:]: |
| 117 |
print whichdb(filename) or "UNKNOWN", filename |