| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  | """Generic interface to all dbm clones.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Use | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         import dbm | 
					
						
							|  |  |  |         d = dbm.open(file, 'w', 0o666) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-29 16:26:56 +00:00
										 |  |  | The returned object is a dbm.gnu, dbm.ndbm or dbm.dumb object, dependent on the | 
					
						
							|  |  |  | type of database being opened (determined by the whichdb function) in the case | 
					
						
							|  |  |  | of an existing dbm. If the dbm does not exist and the create or new flag ('c' | 
					
						
							|  |  |  | or 'n') was specified, the dbm type will be determined by the availability of | 
					
						
							|  |  |  | the modules (tested in the above order). | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | It has the following interface (key and data are strings): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         d[key] = data   # store data at key (may override data at | 
					
						
							|  |  |  |                         # existing key) | 
					
						
							|  |  |  |         data = d[key]   # retrieve data at key (raise KeyError if no | 
					
						
							|  |  |  |                         # such key) | 
					
						
							|  |  |  |         del d[key]      # delete data stored at key (raises KeyError | 
					
						
							|  |  |  |                         # if no such key) | 
					
						
							|  |  |  |         flag = key in d # true if the key exists | 
					
						
							|  |  |  |         list = d.keys() # return a list of all existing keys (slow!) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Future versions may change the order in which implementations are | 
					
						
							| 
									
										
										
										
											2011-04-20 18:52:55 +02:00
										 |  |  | tested for existence, and add interfaces to other dbm-like | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  | implementations. | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-10 21:28:29 +00:00
										 |  |  | __all__ = ['open', 'whichdb', 'error'] | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | import io | 
					
						
							|  |  |  | import os | 
					
						
							|  |  |  | import struct | 
					
						
							|  |  |  | import sys | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class error(Exception): | 
					
						
							|  |  |  |     pass | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-29 16:26:56 +00:00
										 |  |  | _names = ['dbm.gnu', 'dbm.ndbm', 'dbm.dumb'] | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  | _defaultmod = None | 
					
						
							|  |  |  | _modules = {} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-12-25 16:47:37 +02:00
										 |  |  | error = (error, OSError) | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-07 13:15:08 +02:00
										 |  |  | try: | 
					
						
							|  |  |  |     from dbm import ndbm | 
					
						
							|  |  |  | except ImportError: | 
					
						
							|  |  |  |     ndbm = None | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-04-20 18:52:55 +02:00
										 |  |  | def open(file, flag='r', mode=0o666): | 
					
						
							|  |  |  |     """Open or create database at path given by *file*.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Optional argument *flag* can be 'r' (default) for read-only access, 'w' | 
					
						
							|  |  |  |     for read-write access of an existing database, 'c' for read-write access | 
					
						
							|  |  |  |     to a new or existing database, and 'n' for read-write access to a new | 
					
						
							|  |  |  |     database. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Note: 'r' and 'w' fail if the database doesn't exist; 'c' creates it | 
					
						
							|  |  |  |     only if it doesn't exist; and 'n' always creates a new database. | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2008-05-28 08:43:17 +00:00
										 |  |  |     global _defaultmod | 
					
						
							|  |  |  |     if _defaultmod is None: | 
					
						
							|  |  |  |         for name in _names: | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 mod = __import__(name, fromlist=['open']) | 
					
						
							|  |  |  |             except ImportError: | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             if not _defaultmod: | 
					
						
							|  |  |  |                 _defaultmod = mod | 
					
						
							|  |  |  |             _modules[name] = mod | 
					
						
							|  |  |  |         if not _defaultmod: | 
					
						
							|  |  |  |             raise ImportError("no dbm clone found; tried %s" % _names) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-14 16:03:54 -04:00
										 |  |  |     # guess the type of an existing database, if not creating a new one | 
					
						
							|  |  |  |     result = whichdb(file) if 'n' not in flag else None | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  |     if result is None: | 
					
						
							| 
									
										
										
										
											2011-03-14 16:03:54 -04:00
										 |  |  |         # db doesn't exist or 'n' flag was specified to create a new db | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  |         if 'c' in flag or 'n' in flag: | 
					
						
							|  |  |  |             # file doesn't exist and the new flag was used so use default type | 
					
						
							|  |  |  |             mod = _defaultmod | 
					
						
							|  |  |  |         else: | 
					
						
							| 
									
										
										
										
											2008-09-25 22:27:43 +00:00
										 |  |  |             raise error[0]("need 'c' or 'n' flag to open new db") | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  |     elif result == "": | 
					
						
							|  |  |  |         # db type cannot be determined | 
					
						
							| 
									
										
										
										
											2008-09-25 22:27:43 +00:00
										 |  |  |         raise error[0]("db type could not be determined") | 
					
						
							| 
									
										
										
										
											2008-05-28 08:43:17 +00:00
										 |  |  |     elif result not in _modules: | 
					
						
							| 
									
										
										
										
											2008-09-25 22:27:43 +00:00
										 |  |  |         raise error[0]("db type is {0}, but the module is not " | 
					
						
							|  |  |  |                        "available".format(result)) | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  |     else: | 
					
						
							|  |  |  |         mod = _modules[result] | 
					
						
							|  |  |  |     return mod.open(file, flag, mode) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def whichdb(filename): | 
					
						
							|  |  |  |     """Guess which db package to use to open a db file.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Return values: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     - None if the database file can't be read; | 
					
						
							|  |  |  |     - empty string if the file can be read but can't be recognized | 
					
						
							|  |  |  |     - the name of the dbm submodule (e.g. "ndbm" or "gnu") if recognized. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Importing the given module may still fail, and opening the | 
					
						
							|  |  |  |     database using that module may still fail. | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Check for ndbm first -- this has a .pag and a .dir file | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         f = io.open(filename + ".pag", "rb") | 
					
						
							|  |  |  |         f.close() | 
					
						
							| 
									
										
										
										
											2012-10-05 02:48:46 +02:00
										 |  |  |         f = io.open(filename + ".dir", "rb") | 
					
						
							|  |  |  |         f.close() | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  |         return "dbm.ndbm" | 
					
						
							| 
									
										
										
										
											2012-12-25 16:47:37 +02:00
										 |  |  |     except OSError: | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  |         # some dbm emulations based on Berkeley DB generate a .db file | 
					
						
							|  |  |  |         # some do not, but they should be caught by the bsd checks | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             f = io.open(filename + ".db", "rb") | 
					
						
							|  |  |  |             f.close() | 
					
						
							|  |  |  |             # guarantee we can actually open the file using dbm | 
					
						
							|  |  |  |             # kind of overkill, but since we are dealing with emulations | 
					
						
							|  |  |  |             # it seems like a prudent step | 
					
						
							|  |  |  |             if ndbm is not None: | 
					
						
							|  |  |  |                 d = ndbm.open(filename) | 
					
						
							|  |  |  |                 d.close() | 
					
						
							|  |  |  |                 return "dbm.ndbm" | 
					
						
							| 
									
										
										
										
											2012-12-25 16:47:37 +02:00
										 |  |  |         except OSError: | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  |             pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Check for dumbdbm next -- this has a .dir and a .dat file | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         # First check for presence of files | 
					
						
							|  |  |  |         os.stat(filename + ".dat") | 
					
						
							|  |  |  |         size = os.stat(filename + ".dir").st_size | 
					
						
							|  |  |  |         # dumbdbm files with no keys are empty | 
					
						
							|  |  |  |         if size == 0: | 
					
						
							|  |  |  |             return "dbm.dumb" | 
					
						
							|  |  |  |         f = io.open(filename + ".dir", "rb") | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             if f.read(1) in (b"'", b'"'): | 
					
						
							|  |  |  |                 return "dbm.dumb" | 
					
						
							|  |  |  |         finally: | 
					
						
							|  |  |  |             f.close() | 
					
						
							| 
									
										
										
										
											2012-12-25 16:47:37 +02:00
										 |  |  |     except OSError: | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # See if the file exists, return None if not | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         f = io.open(filename, "rb") | 
					
						
							| 
									
										
										
										
											2012-12-25 16:47:37 +02:00
										 |  |  |     except OSError: | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  |         return None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Read the start of the file -- the magic number | 
					
						
							|  |  |  |     s16 = f.read(16) | 
					
						
							|  |  |  |     f.close() | 
					
						
							|  |  |  |     s = s16[0:4] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Return "" if not at least 4 bytes | 
					
						
							|  |  |  |     if len(s) != 4: | 
					
						
							|  |  |  |         return "" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Convert to 4-byte int in native byte order -- return "" if impossible | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         (magic,) = struct.unpack("=l", s) | 
					
						
							|  |  |  |     except struct.error: | 
					
						
							|  |  |  |         return "" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Check for GNU dbm | 
					
						
							| 
									
										
										
										
											2011-09-19 17:08:18 +02:00
										 |  |  |     if magic in (0x13579ace, 0x13579acd, 0x13579acf): | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  |         return "dbm.gnu" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Later versions of Berkeley db hash file have a 12-byte pad in | 
					
						
							|  |  |  |     # front of the file type | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         (magic,) = struct.unpack("=l", s16[-4:]) | 
					
						
							|  |  |  |     except struct.error: | 
					
						
							|  |  |  |         return "" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Unknown | 
					
						
							|  |  |  |     return "" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == "__main__": | 
					
						
							|  |  |  |     for filename in sys.argv[1:]: | 
					
						
							|  |  |  |         print(whichdb(filename) or "UNKNOWN", filename) |