ยปCore Development>Code coverage>Lib/whichdb.py

Python code coverage for Lib/whichdb.py

#countcontent
1n/a# !/usr/bin/env python
21"""Guess which db package to use to open a db file."""
3n/a
41import os
51import struct
61import sys
7n/a
81try:
91 import dbm
101 _dbmerror = dbm.error
110except ImportError:
120 dbm = None
13n/a # just some sort of valid exception which might be raised in the
14n/a # dbm test
150 _dbmerror = IOError
16n/a
171def whichdb(filename):
18n/a """Guess which db package to use to open a db file.
19n/a
20n/a Return values:
21n/a
22n/a - None if the database file can't be read;
23n/a - empty string if the file can be read but can't be recognized
24n/a - the module name (e.g. "dbm" or "gdbm") if recognized.
25n/a
26n/a Importing the given module may still fail, and opening the
27n/a database using that module may still fail.
28n/a """
29n/a
30n/a # Check for dbm first -- this has a .pag and a .dir file
3196 try:
3296 f = open(filename + os.extsep + "pag", "rb")
332 f.close()
34n/a # dbm linked with gdbm on OS/2 doesn't have .dir file
352 if not (dbm.library == "GNU gdbm" and sys.platform == "os2emx"):
362 f = open(filename + os.extsep + "dir", "rb")
372 f.close()
382 return "dbm"
3994 except IOError:
40n/a # some dbm emulations based on Berkeley DB generate a .db file
41n/a # some do not, but they should be caught by the dbhash checks
4294 try:
4394 f = open(filename + os.extsep + "db", "rb")
440 f.close()
45n/a # guarantee we can actually open the file using dbm
46n/a # kind of overkill, but since we are dealing with emulations
47n/a # it seems like a prudent step
480 if dbm is not None:
490 d = dbm.open(filename)
500 d.close()
510 return "dbm"
5294 except (IOError, _dbmerror):
5394 pass
54n/a
55n/a # Check for dumbdbm next -- this has a .dir and a .dat file
5694 try:
57n/a # First check for presence of files
5894 os.stat(filename + os.extsep + "dat")
592 size = os.stat(filename + os.extsep + "dir").st_size
60n/a # dumbdbm files with no keys are empty
612 if size == 0:
621 return "dumbdbm"
631 f = open(filename + os.extsep + "dir", "rb")
641 try:
651 if f.read(1) in ("'", '"'):
661 return "dumbdbm"
67n/a finally:
681 f.close()
6992 except (OSError, IOError):
7092 pass
71n/a
72n/a # See if the file exists, return None if not
7392 try:
7492 f = open(filename, "rb")
7585 except IOError:
7685 return None
77n/a
78n/a # Read the start of the file -- the magic number
797 s16 = f.read(16)
807 f.close()
817 s = s16[0:4]
82n/a
83n/a # Return "" if not at least 4 bytes
847 if len(s) != 4:
850 return ""
86n/a
87n/a # Convert to 4-byte int in native byte order -- return "" if impossible
887 try:
897 (magic,) = struct.unpack("=l", s)
900 except struct.error:
910 return ""
92n/a
93n/a # Check for GNU dbm
947 if magic == 0x13579ace:
952 return "gdbm"
96n/a
97n/a # Check for old Berkeley db hash file format v2
985 if magic in (0x00061561, 0x61150600):
990 return "bsddb185"
100n/a
101n/a # Later versions of Berkeley db hash file have a 12-byte pad in
102n/a # front of the file type
1035 try:
1045 (magic,) = struct.unpack("=l", s16[-4:])
1050 except struct.error:
1060 return ""
107n/a
108n/a # Check for BSD hash
1095 if magic in (0x00061561, 0x61150600):
1105 return "dbhash"
111n/a
112n/a # Unknown
1130 return ""
114n/a
1151if __name__ == "__main__":
1160 for filename in sys.argv[1:]:
1170 print whichdb(filename) or "UNKNOWN", filename