1 """Guess which db package to use to open a db file."""
10 def whichdb(filename
):
11 """Guess which db package to use to open a db file.
15 - None if the database file can't be read;
16 - empty string if the file can be read but can't be recognized
17 - the module name (e.g. "dbm" or "gdbm") if recognized.
19 Importing the given module may still fail, and opening the
20 database using that module may still fail.
25 # Check for dbm first -- this has a .pag and a .dir file
27 f
= open(filename
+ endsep
+ "pag", "rb")
29 f
= open(filename
+ endsep
+ "dir", "rb")
35 # Check for dumbdbm next -- this has a .dir and and a .dat file
37 f
= open(filename
+ endsep
+ "dat", "rb")
39 f
= open(filename
+ endsep
+ "dir", "rb")
41 if f
.read(1) in ["'", '"']:
48 # See if the file exists, return None if not
50 f
= open(filename
, "rb")
54 # Read the start of the file -- the magic number
59 # Return "" if not at least 4 bytes
63 # Convert to 4-byte int in native byte order -- return "" if impossible
65 (magic
,) = struct
.unpack("=l", s
)
70 if magic
== 0x13579ace:
74 if magic
in (0x00061561, 0x61150600):
77 # BSD hash v2 has a 12-byte NULL pad in front of the file type
79 (magic
,) = struct
.unpack("=l", s16
[-4:])
84 if magic
in (0x00061561, 0x61150600):