Re-commit Ping's patch to the cgi and cgitb documentation, using the
[python/dscho.git] / Lib / whichdb.py
blob8b31003d9b186d20e941e1df23dfc20a3c815809
1 """Guess which db package to use to open a db file."""
3 import os
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 import struct
20 # Check for dbm first -- this has a .pag and a .dir file
21 try:
22 f = open(filename + os.extsep + "pag", "rb")
23 f.close()
24 f = open(filename + os.extsep + "dir", "rb")
25 f.close()
26 return "dbm"
27 except IOError:
28 pass
30 # Check for dumbdbm next -- this has a .dir and and a .dat file
31 try:
32 f = open(filename + os.extsep + "dat", "rb")
33 f.close()
34 f = open(filename + os.extsep + "dir", "rb")
35 try:
36 if f.read(1) in ["'", '"']:
37 return "dumbdbm"
38 finally:
39 f.close()
40 except IOError:
41 pass
43 # See if the file exists, return None if not
44 try:
45 f = open(filename, "rb")
46 except IOError:
47 return None
49 # Read the start of the file -- the magic number
50 s16 = f.read(16)
51 f.close()
52 s = s16[0:4]
54 # Return "" if not at least 4 bytes
55 if len(s) != 4:
56 return ""
58 # Convert to 4-byte int in native byte order -- return "" if impossible
59 try:
60 (magic,) = struct.unpack("=l", s)
61 except struct.error:
62 return ""
64 # Check for GNU dbm
65 if magic == 0x13579ace:
66 return "gdbm"
68 # Check for BSD hash
69 if magic in (0x00061561, 0x61150600):
70 return "dbhash"
72 # BSD hash v2 has a 12-byte NULL pad in front of the file type
73 try:
74 (magic,) = struct.unpack("=l", s16[-4:])
75 except struct.error:
76 return ""
78 # Check for BSD hash
79 if magic in (0x00061561, 0x61150600):
80 return "dbhash"
82 # Unknown
83 return ""