PyXR

c:\python24\lib \ whichdb.py



0001 # !/usr/bin/env python
0002 """Guess which db package to use to open a db file."""
0003 
0004 import os
0005 import struct
0006 import sys
0007 
0008 try:
0009     import dbm
0010     _dbmerror = dbm.error
0011 except ImportError:
0012     dbm = None
0013     # just some sort of valid exception which might be raised in the
0014     # dbm test
0015     _dbmerror = IOError
0016 
0017 def whichdb(filename):
0018     """Guess which db package to use to open a db file.
0019 
0020     Return values:
0021 
0022     - None if the database file can't be read;
0023     - empty string if the file can be read but can't be recognized
0024     - the module name (e.g. "dbm" or "gdbm") if recognized.
0025 
0026     Importing the given module may still fail, and opening the
0027     database using that module may still fail.
0028     """
0029 
0030     # Check for dbm first -- this has a .pag and a .dir file
0031     try:
0032         f = open(filename + os.extsep + "pag", "rb")
0033         f.close()
0034         # dbm linked with gdbm on OS/2 doesn't have .dir file
0035         if not (dbm.library == "GNU gdbm" and sys.platform == "os2emx"):
0036             f = open(filename + os.extsep + "dir", "rb")
0037             f.close()
0038         return "dbm"
0039     except IOError:
0040         # some dbm emulations based on Berkeley DB generate a .db file
0041         # some do not, but they should be caught by the dbhash checks
0042         try:
0043             f = open(filename + os.extsep + "db", "rb")
0044             f.close()
0045             # guarantee we can actually open the file using dbm
0046             # kind of overkill, but since we are dealing with emulations
0047             # it seems like a prudent step
0048             if dbm is not None:
0049                 d = dbm.open(filename)
0050                 d.close()
0051                 return "dbm"
0052         except (IOError, _dbmerror):
0053             pass
0054 
0055     # Check for dumbdbm next -- this has a .dir and a .dat file
0056     try:
0057         # First check for presence of files
0058         os.stat(filename + os.extsep + "dat")
0059         size = os.stat(filename + os.extsep + "dir").st_size
0060         # dumbdbm files with no keys are empty
0061         if size == 0:
0062             return "dumbdbm"
0063         f = open(filename + os.extsep + "dir", "rb")
0064         try:
0065             if f.read(1) in ["'", '"']:
0066                 return "dumbdbm"
0067         finally:
0068             f.close()
0069     except (OSError, IOError):
0070         pass
0071 
0072     # See if the file exists, return None if not
0073     try:
0074         f = open(filename, "rb")
0075     except IOError:
0076         return None
0077 
0078     # Read the start of the file -- the magic number
0079     s16 = f.read(16)
0080     f.close()
0081     s = s16[0:4]
0082 
0083     # Return "" if not at least 4 bytes
0084     if len(s) != 4:
0085         return ""
0086 
0087     # Convert to 4-byte int in native byte order -- return "" if impossible
0088     try:
0089         (magic,) = struct.unpack("=l", s)
0090     except struct.error:
0091         return ""
0092 
0093     # Check for GNU dbm
0094     if magic == 0x13579ace:
0095         return "gdbm"
0096 
0097     # Check for old Berkeley db hash file format v2
0098     if magic in (0x00061561, 0x61150600):
0099         return "bsddb185"
0100 
0101     # Later versions of Berkeley db hash file have a 12-byte pad in
0102     # front of the file type
0103     try:
0104         (magic,) = struct.unpack("=l", s16[-4:])
0105     except struct.error:
0106         return ""
0107 
0108     # Check for BSD hash
0109     if magic in (0x00061561, 0x61150600):
0110         return "dbhash"
0111 
0112     # Unknown
0113     return ""
0114 
0115 if __name__ == "__main__":
0116     for filename in sys.argv[1:]:
0117         print whichdb(filename) or "UNKNOWN", filename
0118 

Generated by PyXR 0.9.4
SourceForge.net Logo