def __init__(self, *args, **config): super(database,self).__init__(*args, **config) default_db = config.get("dbtype","anydbm") if not default_db.startswith("."): default_db = '.' + default_db self._db_path = os.path.join(self.location, fs_template.gen_label(self.location, self.label)+default_db) self.__db = None try: # dbm.open() will not work with bytes in python-3.1: # TypeError: can't concat bytes to str self.__db = anydbm_module.open(self._db_path, 'w', self._perms) except anydbm_module.error: # XXX handle this at some point try: self._ensure_dirs() self._ensure_dirs(self._db_path) except (OSError, IOError) as e: raise cache_errors.InitializationError(self.__class__, e) # try again if failed try: if self.__db == None: # dbm.open() will not work with bytes in python-3.1: # TypeError: can't concat bytes to str self.__db = anydbm_module.open(self._db_path, 'c', self._perms) except anydbm_module.error as e: raise cache_errors.InitializationError(self.__class__, e) self._ensure_access(self._db_path)
def _initdb_con(self, config): """ensure needed tables are in place. If the derived class needs a different set of table creation commands, overload the approriate SCHEMA_ attributes. If it needs additional execution beyond, override""" self._dbconnect(config) if not self._table_exists(self.SCHEMA_PACKAGE_NAME): if self.readonly: raise cache_errors.ReadOnlyRestriction( "table %s doesn't exist" % self.SCHEMA_PACKAGE_NAME ) try: self.con.execute(self.SCHEMA_PACKAGE_CREATE) except self._BaseError as e: raise cache_errors.InitializationError(self.__class__, e) if not self._table_exists(self.SCHEMA_VALUES_NAME): if self.readonly: raise cache_errors.ReadOnlyRestriction( "table %s doesn't exist" % self.SCHEMA_VALUES_NAME ) try: self.con.execute(self.SCHEMA_VALUES_CREATE) except self._BaseError as e: raise cache_errors.InitializationError(self.__class__, e)
def _db_init_connection(self): config = self._config self._dbpath = self.location + ".sqlite" # if os.path.exists(self._dbpath): # os.unlink(self._dbpath) connection_kwargs = {} connection_kwargs["timeout"] = config["timeout"] try: if not self.readonly: self._ensure_dirs() connection = self._db_module.connect(database=_unicode_decode( self._dbpath), **connection_kwargs) cursor = connection.cursor() self._db_connection_info = self._connection_info_entry( connection, cursor, portage.getpid()) self._db_cursor.execute("PRAGMA encoding = %s" % self._db_escape_string("UTF-8")) if not self.readonly and not self._ensure_access(self._dbpath): raise cache_errors.InitializationError( self.__class__, "can't ensure perms on %s" % self._dbpath) self._db_init_cache_size(config["cache_bytes"]) self._db_init_synchronous(config["synchronous"]) self._db_init_structures() except self._db_error as e: raise cache_errors.InitializationError(self.__class__, e)
def _db_init_synchronous(self, synchronous): cursor = self._db_cursor cursor.execute("PRAGMA synchronous = %d" % synchronous) cursor.execute("PRAGMA synchronous") actual_synchronous=int(cursor.fetchone()[0]) del cursor if actual_synchronous!=synchronous: raise cache_errors.InitializationError(self.__class__,"actual synchronous = "+actual_synchronous+" does does not match requested value of "+synchronous)
def _import_sqlite(self): # sqlite3 is optional with >=python-2.5 try: import sqlite3 as db_module except ImportError as e: raise cache_errors.InitializationError(self.__class__, e) self._db_module = db_module self._db_error = db_module.Error
def __init__(self, *args, **config): super(database,self).__init__(*args, **config) default_db = config.get("dbtype","anydbm") if not default_db.startswith("."): default_db = '.' + default_db self._db_path = os.path.join(self.location, fs_template.gen_label(self.location, self.label)+default_db) self.__db = None mode = "w" if dbm.whichdb(self._db_path) in ("dbm.gnu", "gdbm"): # Allow multiple concurrent writers (see bug #53607). mode += "u" try: # dbm.open() will not work with bytes in python-3.1: # TypeError: can't concat bytes to str self.__db = dbm.open(self._db_path, mode, self._perms) except dbm.error: # XXX handle this at some point try: self._ensure_dirs() self._ensure_dirs(self._db_path) except (OSError, IOError) as e: raise cache_errors.InitializationError(self.__class__, e) # try again if failed try: if self.__db == None: # dbm.open() will not work with bytes in python-3.1: # TypeError: can't concat bytes to str if gdbm is None: self.__db = dbm.open(self._db_path, "c", self._perms) else: # Prefer gdbm type if available, since it allows # multiple concurrent writers (see bug #53607). self.__db = gdbm.open(self._db_path, "cu", self._perms) except dbm.error as e: raise cache_errors.InitializationError(self.__class__, e) self._ensure_access(self._db_path)
def _db_init_cache_size(self, cache_bytes): cursor = self._db_cursor cursor.execute("PRAGMA page_size") page_size=int(cursor.fetchone()[0]) # number of pages, sqlite default is 2000 cache_size = cache_bytes // page_size cursor.execute("PRAGMA cache_size = %d" % cache_size) cursor.execute("PRAGMA cache_size") actual_cache_size = int(cursor.fetchone()[0]) del cursor if actual_cache_size != cache_size: raise cache_errors.InitializationError(self.__class__,"actual cache_size = "+actual_cache_size+" does does not match requested size of "+cache_size)