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: 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) self._ensure_access(self._db_path) except (OSError, IOError), e: raise cache_errors.InitializationError(self.__class__, e) # try again if failed try: if self.__db == None: self.__db = anydbm_module.open(self._db_path, "c", self._perms) except andbm_module.error, e: raise cache_errors.InitializationError(self.__class__, e)
def _dbconnect(self, config): self._dbpath = os.path.join(self.location, fs_template.gen_label(self.location, self.label)+".sqldb") try: self.db = sqlite_module.connect(self._dbpath, mode=self._perms, autocommit=False) if not self._ensure_access(self._dbpath): raise cache_errors.InitializationError(self.__class__, "can't ensure perms on %s" % self._dbpath) self.con = self.db.cursor() except self._BaseError, e: raise cache_errors.InitializationError(self.__class__, e)
def _initdb_con(self, config): sql_template.SQLDatabase._initdb_con(self, config) try: self.con.execute("SELECT name FROM sqlite_master WHERE type=\"trigger\" AND name=%s" % \ self._sfilter(self.SCHEMA_DELETE_NAME)) if self.con.rowcount == 0: self.con.execute(self.SCHEMA_DELETE_TRIGGER); self.db.commit() except self._BaseError, e: raise cache_errors.InitializationError(self.__class__, e)
class FsBased(template.database): """template wrapping fs needed options, and providing _ensure_access as a way to attempt to ensure files have the specified owners/perms""" def __init__(self, label, auxdbkeys, basepath=None, gid=-1, perms=0664, **config): """throws InitializationError if needs args aren't specified""" if not gid: raise cache_errors.InitializationError(self.__class__, "must specify gid!") if not basepath: raise cache_errors.InitializationError(self.__class__, "must specify basepath!") self._gid = gid self._base = basepath self._perms = perms super(FsBased, self).__init__(label, auxdbkeys, **config) if self.label.startswith(os.path.sep): # normpath. self.label = os.path.sep + os.path.normpath(self.label).lstrip(os.path.sep)
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, e: raise cache_errors.InitializationError(self.__class__, e)
def __init__(self, *args, **config): super(database,self).__init__(*args, **config) self._db_path = os.path.join(self.location, fs_template.gen_label(self.location, self.label)+".cdb") self.__db = None try: self.__db = cdb_module.init(self._db_path) except cdb_module.error: try: self._ensure_dirs() self._ensure_dirs(self._db_path) self._ensure_access(self._db_path) except (OSError, IOError), e: raise cache_errors.InitializationError(self.__class__, e) try: cm = cdb_module.cdbmake(self._db_path, self._db_path+".tmp") cm.finish() self._ensure_access(self._db_path) self.__db = cdb_module.init(self._db_path) except cdb_module.error, e: raise cache_errors.InitializationError(self.__class__, e)
def __init__(self, label, auxdbkeys, **config): super(database, self).__init__(label, auxdbkeys, **config) default_db = config.get("dbtype", "anydbm") if not default_db.startswith("."): default_db = '.' + default_db self._db_path = os.path.join( self._base, fs_template.gen_label(self._base, self.label) + default_db) print "opening self._db_path=", self._db_path self.__db = None try: self.__db = anydbm_module.open(self._db_path, "w", self._perms) try: self._ensure_dirs() self._ensure_dirs(self._db_path) self._ensure_access(self._db_path) except (OSError, IOError), e: raise cache_errors.InitializationError(self.__clas__, e) # try again if failed if self.__db == None: self.__db = anydbm_module.open(self._db_path, "c", self._perms)
class SQLDatabase(template.database): """template class for RDBM based caches This class is designed such that derivatives don't have to change much code, mostly constant strings. _BaseError must be an exception class that all Exceptions thrown from the derived RDBMS are derived from. SCHEMA_INSERT_CPV_INTO_PACKAGE should be modified dependant on the RDBMS, as should SCHEMA_PACKAGE_CREATE- basically you need to deal with creation of a unique pkgid. If the dbapi2 rdbms class has a method of recovering that id, then modify _insert_cpv to remove the extra select. Creation of a derived class involves supplying _initdb_con, and table_exists. Additionally, the default schemas may have to be modified. """ SCHEMA_PACKAGE_NAME = "package_cache" SCHEMA_PACKAGE_CREATE = "CREATE TABLE %s (\ pkgid INTEGER PRIMARY KEY, label VARCHAR(255), cpv VARCHAR(255), UNIQUE(label, cpv))" % SCHEMA_PACKAGE_NAME SCHEMA_PACKAGE_DROP = "DROP TABLE %s" % SCHEMA_PACKAGE_NAME SCHEMA_VALUES_NAME = "values_cache" SCHEMA_VALUES_CREATE = "CREATE TABLE %s ( pkgid integer references %s (pkgid) on delete cascade, \ key varchar(255), value text, UNIQUE(pkgid, key))" % (SCHEMA_VALUES_NAME, SCHEMA_PACKAGE_NAME) SCHEMA_VALUES_DROP = "DROP TABLE %s" % SCHEMA_VALUES_NAME SCHEMA_INSERT_CPV_INTO_PACKAGE = "INSERT INTO %s (label, cpv) VALUES(%%s, %%s)" % SCHEMA_PACKAGE_NAME _BaseError = () _dbClass = None autocommits = False # boolean indicating if the derived RDBMS class supports replace syntax _supports_replace = False def __init__(self, label, auxdbkeys, **config): """initialize the instance. derived classes shouldn't need to override this""" super(SQLDatabase, self).__init__(label, auxdbkeys, **config) config.setdefault("host", "127.0.0.1") config.setdefault("autocommit", self.autocommits) self._initdb_con(config) self.label = self._sfilter(self.label) def _dbconnect(self, config): """should be overridden if the derived class needs special parameters for initializing the db connection, or cursor""" self.db = self._dbClass(**config) self.con = self.db.cursor() 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, 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, e: raise cache_errors.InitializationError(self.__class__, e)