Raymond Hettinger | e54e726 | 2004-10-20 07:17:16 +0000 | [diff] [blame] | 1 | # !/usr/bin/env python |
Guido van Rossum | 8d12a1b | 1996-07-30 16:30:15 +0000 | [diff] [blame] | 2 | """Guess which db package to use to open a db file.""" |
| 3 | |
Guido van Rossum | d74fb6b | 2001-03-02 06:43:49 +0000 | [diff] [blame] | 4 | import os |
Skip Montanaro | 404378f | 2002-08-02 17:12:15 +0000 | [diff] [blame] | 5 | import struct |
Andrew MacIntyre | a1e93e8 | 2003-07-11 12:16:48 +0000 | [diff] [blame] | 6 | import sys |
Skip Montanaro | 404378f | 2002-08-02 17:12:15 +0000 | [diff] [blame] | 7 | |
| 8 | try: |
| 9 | import dbm |
| 10 | _dbmerror = dbm.error |
| 11 | except ImportError: |
| 12 | dbm = None |
| 13 | # just some sort of valid exception which might be raised in the |
| 14 | # dbm test |
| 15 | _dbmerror = IOError |
Guido van Rossum | d74fb6b | 2001-03-02 06:43:49 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | 8d12a1b | 1996-07-30 16:30:15 +0000 | [diff] [blame] | 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 | # Check for dbm first -- this has a .pag and a .dir file |
| 31 | try: |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 32 | f = open(filename + os.extsep + "pag", "rb") |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 33 | f.close() |
Andrew MacIntyre | a1e93e8 | 2003-07-11 12:16:48 +0000 | [diff] [blame] | 34 | # dbm linked with gdbm on OS/2 doesn't have .dir file |
| 35 | if not (dbm.library == "GNU gdbm" and sys.platform == "os2emx"): |
| 36 | f = open(filename + os.extsep + "dir", "rb") |
| 37 | f.close() |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 38 | return "dbm" |
Guido van Rossum | 8d12a1b | 1996-07-30 16:30:15 +0000 | [diff] [blame] | 39 | except IOError: |
Skip Montanaro | 404378f | 2002-08-02 17:12:15 +0000 | [diff] [blame] | 40 | # some dbm emulations based on Berkeley DB generate a .db file |
| 41 | # some do not, but they should be caught by the dbhash checks |
| 42 | try: |
| 43 | f = open(filename + os.extsep + "db", "rb") |
| 44 | f.close() |
| 45 | # guarantee we can actually open the file using dbm |
| 46 | # kind of overkill, but since we are dealing with emulations |
| 47 | # it seems like a prudent step |
| 48 | if dbm is not None: |
| 49 | d = dbm.open(filename) |
| 50 | d.close() |
| 51 | return "dbm" |
| 52 | except (IOError, _dbmerror): |
| 53 | pass |
Guido van Rossum | 8d12a1b | 1996-07-30 16:30:15 +0000 | [diff] [blame] | 54 | |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 55 | # Check for dumbdbm next -- this has a .dir and a .dat file |
Thomas Wouters | 3027b15 | 2000-08-04 08:46:59 +0000 | [diff] [blame] | 56 | try: |
Martin v. Löwis | 17fb507 | 2003-06-14 08:16:34 +0000 | [diff] [blame] | 57 | # First check for presence of files |
Martin v. Löwis | 5b8bfdf | 2003-06-21 13:54:55 +0000 | [diff] [blame] | 58 | os.stat(filename + os.extsep + "dat") |
| 59 | size = os.stat(filename + os.extsep + "dir").st_size |
Martin v. Löwis | 17fb507 | 2003-06-14 08:16:34 +0000 | [diff] [blame] | 60 | # dumbdbm files with no keys are empty |
Martin v. Löwis | 5b8bfdf | 2003-06-21 13:54:55 +0000 | [diff] [blame] | 61 | if size == 0: |
Martin v. Löwis | 17fb507 | 2003-06-14 08:16:34 +0000 | [diff] [blame] | 62 | return "dumbdbm" |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 63 | f = open(filename + os.extsep + "dir", "rb") |
Moshe Zadka | 7a4409c | 2000-07-29 05:31:40 +0000 | [diff] [blame] | 64 | try: |
Raymond Hettinger | dbecd93 | 2005-02-06 06:57:08 +0000 | [diff] [blame] | 65 | if f.read(1) in ("'", '"'): |
Moshe Zadka | 7a4409c | 2000-07-29 05:31:40 +0000 | [diff] [blame] | 66 | return "dumbdbm" |
| 67 | finally: |
| 68 | f.close() |
Martin v. Löwis | 17fb507 | 2003-06-14 08:16:34 +0000 | [diff] [blame] | 69 | except (OSError, IOError): |
Moshe Zadka | 7a4409c | 2000-07-29 05:31:40 +0000 | [diff] [blame] | 70 | pass |
| 71 | |
Guido van Rossum | 8d12a1b | 1996-07-30 16:30:15 +0000 | [diff] [blame] | 72 | # See if the file exists, return None if not |
| 73 | try: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 74 | f = open(filename, "rb") |
Guido van Rossum | 8d12a1b | 1996-07-30 16:30:15 +0000 | [diff] [blame] | 75 | except IOError: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 76 | return None |
Guido van Rossum | 8d12a1b | 1996-07-30 16:30:15 +0000 | [diff] [blame] | 77 | |
Guido van Rossum | cf09a39 | 1999-06-08 13:13:16 +0000 | [diff] [blame] | 78 | # Read the start of the file -- the magic number |
| 79 | s16 = f.read(16) |
Guido van Rossum | 8d12a1b | 1996-07-30 16:30:15 +0000 | [diff] [blame] | 80 | f.close() |
Guido van Rossum | cf09a39 | 1999-06-08 13:13:16 +0000 | [diff] [blame] | 81 | s = s16[0:4] |
Guido van Rossum | 8d12a1b | 1996-07-30 16:30:15 +0000 | [diff] [blame] | 82 | |
| 83 | # Return "" if not at least 4 bytes |
| 84 | if len(s) != 4: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 85 | return "" |
Guido van Rossum | 8d12a1b | 1996-07-30 16:30:15 +0000 | [diff] [blame] | 86 | |
| 87 | # Convert to 4-byte int in native byte order -- return "" if impossible |
| 88 | try: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 89 | (magic,) = struct.unpack("=l", s) |
Guido van Rossum | 8d12a1b | 1996-07-30 16:30:15 +0000 | [diff] [blame] | 90 | except struct.error: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 91 | return "" |
Guido van Rossum | 8d12a1b | 1996-07-30 16:30:15 +0000 | [diff] [blame] | 92 | |
| 93 | # Check for GNU dbm |
| 94 | if magic == 0x13579ace: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 95 | return "gdbm" |
Guido van Rossum | 8d12a1b | 1996-07-30 16:30:15 +0000 | [diff] [blame] | 96 | |
Skip Montanaro | 02cd365 | 2003-05-06 20:42:10 +0000 | [diff] [blame] | 97 | # Check for old Berkeley db hash file format v2 |
Guido van Rossum | b86ba12 | 1998-04-28 15:41:03 +0000 | [diff] [blame] | 98 | if magic in (0x00061561, 0x61150600): |
Skip Montanaro | 02cd365 | 2003-05-06 20:42:10 +0000 | [diff] [blame] | 99 | return "bsddb185" |
Guido van Rossum | 8d12a1b | 1996-07-30 16:30:15 +0000 | [diff] [blame] | 100 | |
Skip Montanaro | 02cd365 | 2003-05-06 20:42:10 +0000 | [diff] [blame] | 101 | # Later versions of Berkeley db hash file have a 12-byte pad in |
| 102 | # front of the file type |
Guido van Rossum | cf09a39 | 1999-06-08 13:13:16 +0000 | [diff] [blame] | 103 | try: |
Fred Drake | 13a2c27 | 2000-02-10 17:17:14 +0000 | [diff] [blame] | 104 | (magic,) = struct.unpack("=l", s16[-4:]) |
Guido van Rossum | cf09a39 | 1999-06-08 13:13:16 +0000 | [diff] [blame] | 105 | except struct.error: |
| 106 | return "" |
| 107 | |
| 108 | # Check for BSD hash |
| 109 | if magic in (0x00061561, 0x61150600): |
| 110 | return "dbhash" |
| 111 | |
Guido van Rossum | 8d12a1b | 1996-07-30 16:30:15 +0000 | [diff] [blame] | 112 | # Unknown |
| 113 | return "" |
Raymond Hettinger | e54e726 | 2004-10-20 07:17:16 +0000 | [diff] [blame] | 114 | |
| 115 | if __name__ == "__main__": |
| 116 | for filename in sys.argv[1:]: |
| 117 | print whichdb(filename) or "UNKNOWN", filename |