This commit was manufactured by cvs2svn to create tag 'r23b1-mac'.
[python/dscho.git] / Lib / whichdb.py
blobcc95ed512f52ccfd66bab80a8272b13c3bce557f
1 """Guess which db package to use to open a db file."""
3 import os
4 import struct
6 try:
7 import dbm
8 _dbmerror = dbm.error
9 except ImportError:
10 dbm = None
11 # just some sort of valid exception which might be raised in the
12 # dbm test
13 _dbmerror = IOError
15 def whichdb(filename):
16 """Guess which db package to use to open a db file.
18 Return values:
20 - None if the database file can't be read;
21 - empty string if the file can be read but can't be recognized
22 - the module name (e.g. "dbm" or "gdbm") if recognized.
24 Importing the given module may still fail, and opening the
25 database using that module may still fail.
26 """
28 # Check for dbm first -- this has a .pag and a .dir file
29 try:
30 f = open(filename + os.extsep + "pag", "rb")
31 f.close()
32 f = open(filename + os.extsep + "dir", "rb")
33 f.close()
34 return "dbm"
35 except IOError:
36 # some dbm emulations based on Berkeley DB generate a .db file
37 # some do not, but they should be caught by the dbhash checks
38 try:
39 f = open(filename + os.extsep + "db", "rb")
40 f.close()
41 # guarantee we can actually open the file using dbm
42 # kind of overkill, but since we are dealing with emulations
43 # it seems like a prudent step
44 if dbm is not None:
45 d = dbm.open(filename)
46 d.close()
47 return "dbm"
48 except (IOError, _dbmerror):
49 pass
51 # Check for dumbdbm next -- this has a .dir and and a .dat file
52 try:
53 f = open(filename + os.extsep + "dat", "rb")
54 f.close()
55 f = open(filename + os.extsep + "dir", "rb")
56 try:
57 if f.read(1) in ["'", '"']:
58 return "dumbdbm"
59 finally:
60 f.close()
61 except IOError:
62 pass
64 # See if the file exists, return None if not
65 try:
66 f = open(filename, "rb")
67 except IOError:
68 return None
70 # Read the start of the file -- the magic number
71 s16 = f.read(16)
72 f.close()
73 s = s16[0:4]
75 # Return "" if not at least 4 bytes
76 if len(s) != 4:
77 return ""
79 # Convert to 4-byte int in native byte order -- return "" if impossible
80 try:
81 (magic,) = struct.unpack("=l", s)
82 except struct.error:
83 return ""
85 # Check for GNU dbm
86 if magic == 0x13579ace:
87 return "gdbm"
89 # Check for BSD hash
90 if magic in (0x00061561, 0x61150600):
91 return "dbhash"
93 # BSD hash v2 has a 12-byte NULL pad in front of the file type
94 try:
95 (magic,) = struct.unpack("=l", s16[-4:])
96 except struct.error:
97 return ""
99 # Check for BSD hash
100 if magic in (0x00061561, 0x61150600):
101 return "dbhash"
103 # Unknown
104 return ""