Added 'list_only' option (and modified 'run()' to respect it).
[python/dscho.git] / Lib / whichdb.py
blobaa1761cc58a30e31eaa8cad371262d00ade92b45
1 """Guess which db package to use to open a db file."""
3 import struct
5 def whichdb(filename):
6 """Guess which db package to use to open a db file.
8 Return values:
10 - None if the database file can't be read;
11 - empty string if the file can be read but can't be recognized
12 - the module name (e.g. "dbm" or "gdbm") if recognized.
14 Importing the given module may still fail, and opening the
15 database using that module may still fail.
16 """
18 # Check for dbm first -- this has a .pag and a .dir file
19 try:
20 f = open(filename + ".pag", "rb")
21 f.close()
22 f = open(filename + ".dir", "rb")
23 f.close()
24 return "dbm"
25 except IOError:
26 pass
28 # See if the file exists, return None if not
29 try:
30 f = open(filename, "rb")
31 except IOError:
32 return None
34 # Read the start of the file -- the magic number
35 s16 = f.read(16)
36 f.close()
37 s = s16[0:4]
39 # Return "" if not at least 4 bytes
40 if len(s) != 4:
41 return ""
43 # Convert to 4-byte int in native byte order -- return "" if impossible
44 try:
45 (magic,) = struct.unpack("=l", s)
46 except struct.error:
47 return ""
49 # Check for GNU dbm
50 if magic == 0x13579ace:
51 return "gdbm"
53 # Check for BSD hash
54 if magic in (0x00061561, 0x61150600):
55 return "dbhash"
57 # BSD hash v2 has a 12-byte NULL pad in front of the file type
58 try:
59 (magic,) = struct.unpack("=l", s16[-4:])
60 except struct.error:
61 return ""
63 # Check for BSD hash
64 if magic in (0x00061561, 0x61150600):
65 return "dbhash"
67 # Unknown
68 return ""