home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 December / CHIP_CD_2004-12.iso / bonus / oo / OOo_1.1.3_ru_RU_infra_WinIntel_install.exe / $PLUGINSDIR / f_0372 / python-core-2.2.2 / lib / whichdb.py < prev    next >
Text File  |  2004-10-09  |  2KB  |  84 lines

  1. """Guess which db package to use to open a db file."""
  2.  
  3. import os
  4.  
  5. def whichdb(filename):
  6.     """Guess which db package to use to open a db file.
  7.  
  8.     Return values:
  9.  
  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.
  13.  
  14.     Importing the given module may still fail, and opening the
  15.     database using that module may still fail.
  16.     """
  17.  
  18.     import struct
  19.  
  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
  29.  
  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
  42.  
  43.     # See if the file exists, return None if not
  44.     try:
  45.         f = open(filename, "rb")
  46.     except IOError:
  47.         return None
  48.  
  49.     # Read the start of the file -- the magic number
  50.     s16 = f.read(16)
  51.     f.close()
  52.     s = s16[0:4]
  53.  
  54.     # Return "" if not at least 4 bytes
  55.     if len(s) != 4:
  56.         return ""
  57.  
  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 ""
  63.  
  64.     # Check for GNU dbm
  65.     if magic == 0x13579ace:
  66.         return "gdbm"
  67.  
  68.     # Check for BSD hash
  69.     if magic in (0x00061561, 0x61150600):
  70.         return "dbhash"
  71.  
  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 ""
  77.  
  78.     # Check for BSD hash
  79.     if magic in (0x00061561, 0x61150600):
  80.         return "dbhash"
  81.  
  82.     # Unknown
  83.     return ""
  84.