def run(self): if self._schema_name is None: raise MythDBError('Schema update failed, variable name not set') if self.db.settings.NULL[self._schema_name] is None: self.db.settings.NULL[self._schema_name] = self.create() origschema = int(self.db.settings.NULL[self._schema_name]) schema = origschema try: while True: newschema = getattr(self, 'up%d' % schema)() self.log(MythLog.GENERAL, MythLog.INFO, 'successfully updated from %d to %d' %\ (schema, newschema)) schema = newschema self.db.settings.NULL[self._schema_name] = schema except AttributeError as e: self.log(MythLog.GENERAL, MythLog.CRIT, 'failed at %d' % schema, 'no handler method') raise MythDBError('Schema update failed, ' "SchemaUpdate has no function 'up%s'" % schema) except StopIteration: if schema != origschema: self.log(MythLog.GENERAL, MythLog.NOTICE, '%s update complete' % self._schema_name) pass except Exception as e: raise MythDBError(MythError.DB_SCHEMAUPDATE, e.args)
def __init__(self, parent, field_name, editable=True): self._parent = weakref.proxy(parent) self._field = field_name field = parent._db.tablefields[parent._table][self._field].type if field[:4] != 'set(': raise MythDBError("ParseSet error. "+\ "Field '%s' not of type 'set()'" % self._field) self._enum = dict([(t,2**i) for i,t in enumerate([type.strip("'")\ for type in field[4:-1].split(',')])]) self._static = not editable
def __init__(self, path=None, setting=None, db=None, useshell=True, prefix=''): DBCache.__init__(self, db=db) self.log = MythLog(self.logmodule, db=self) self.path = None if setting is not None: # pull local setting from database host = self.gethostname() self.path = self.settings[host][setting] if self.path is None: # see if that setting is applied globally self.path = self.settings['NULL'][setting] if self.path is None: # not set globally either, use supplied default self.path = path if self.path is None: # no default supplied, just error out raise MythDBError(MythError.DB_SETTING, setting, host) if self.path is None: # setting not given, use path from argument if path is None: raise MythError('Invalid input to System()') self.path = path if prefix: self.path = os.path.join(prefix, self.path) cmd = self.path.split()[0] if self.path.startswith('/'): # test full given path if not os.access(cmd, os.F_OK): raise MythFileError('Defined executable path does not exist.') else: # search command from PATH for folder in os.environ['PATH'].split(':'): if os.access(os.path.join(folder, cmd), os.F_OK): self.path = os.path.join(folder, self.path) break else: raise MythFileError('Defined executable path does not exist.') self.returncode = 0 self.stderr = '' self.useshell = useshell
def __init__(self, table=None, tableto=None, fields=None, \ fieldsto=None, fieldsfrom=None): if (table is None) or (tableto is None) or \ ((fields is None) and \ ((fieldsto is None) or (fieldsfrom is None))): raise MythDBError('Invalid input to databaseSearch.Join.') self.table = table self.tableto = tableto if fields: self.fieldsfrom = fields self.fieldsto = fields else: self.fieldsfrom = fieldsfrom self.fieldsto = fieldsto
def run(self): if self._schema_name is None: raise MythDBError('Schema update failed, variable name not set') if self.db.settings.NULL[self._schema_name] is None: self.db.settings.NULL[self._schema_name] = self.create() origschema = int(self.db.settings.NULL[self._schema_name]) schema = origschema try: while True: newschema = getattr(self, 'up%d' % schema)() self.log(MythLog.GENERAL, MythLog.INFO, 'successfully updated from %d to %d' %\ (schema, newschema)) schema = newschema self.db.settings.NULL[self._schema_name] = schema except AttributeError, e: self.log(MythLog.GENERAL, MythLog.CRIT, 'failed at %d' % schema, 'no handler method') raise MythDBError('Schema update failed, ' "SchemaUpdate has no function 'up%s'" % schema)
def execute(self, query, args=None): """ Execute a query. query -- string, query to execute on server args -- optional sequence, parameters to use with query. Returns long integer rows affected, if any """ query = self._sanitize(query) self.log_query(query, args) try: if args: return super(LoggedCursor, self).execute(query, args) return super(LoggedCursor, self).execute(query) except Exception, e: raise MythDBError(MythDBError.DB_RAW, e.args)
def executemany(self, query, args): """ Execute a multi-row query. query -- string, query to execute on server args -- Sequence of sequences, parameters to use with query. Returns long integer rows affected, if any. This method improves performance on multiple-row INSERT and REPLACE. Otherwise it is equivalent to looping over args with execute(). """ query = self._sanitize(query) self.log_query(query, args) try: return super(LoggedCursor, self).executemany(query, args) except Exception, e: raise MythDBError(MythDBError.DB_RAW, e.args)
def execute(self, query, args=None): """ Execute a query. query -- string, query to execute on server args -- optional sequence or mapping, parameters to use with query. Note: If args is a sequence, then %s must be used as the parameter placeholder in the query. If a mapping is used, %(key)s must be used as the placeholder. Returns long integer rows affected, if any """ self.ping() query = self._sanitize(query) self.log_query(query, args) try: if args is None: return super(LoggedCursor, self).execute(query) return super(LoggedCursor, self).execute(query, args) except Exception, e: raise MythDBError(MythDBError.DB_RAW, e.args)
def create(self): raise MythDBError('Schema creation failed, method not defined.')
def __init__(self, backend=None, blockshutdown=False, events=False, db=None): self.db = DBCache(db) self.log = MythLog(self.logmodule, db=self.db) self.hostname = None self.sendcommands = True self.blockshutdown = blockshutdown self.receiveevents = events if backend is None: # no backend given, use master self.host = self.db.settings.NULL.MasterServerIP self.hostname = self.db._gethostfromaddr(self.host) else: backend = backend.strip('[]') query = "SELECT hostname FROM settings WHERE value=? AND data=?" if self._reip.match(backend): # given backend is IP address self.host = backend self.hostname = self.db._gethostfromaddr( backend, 'BackendServerIP') elif check_ipv6(backend): # given backend is IPv6 address self.host = backend self.hostname = self.db._gethostfromaddr( backend, 'BackendServerIP6') else: # given backend is hostname, pull address from database self.hostname = backend self.host = self.db._getpreferredaddr(backend) # lookup port from database self.port = int(self.db.settings[self.hostname].BackendServerPort) if not self.port: raise MythDBError(MythError.DB_SETTING, 'BackendServerPort', self.port) self._ident = '%s:%d' % (self.host, self.port) if self._ident in self._shared: # existing connection found self._conn = self._shared[self._ident] if self.sendcommands: if self._conn.command is None: self._conn.command = self._newcmdconn() elif self.blockshutdown: # upref block of shutdown # issue command to backend if needed self._conn.blockshutdown += 1 if self._conn.blockshutdown == 1: self._conn.command.blockShutdown() if self.receiveevents: if self._conn.event is None: self._conn.event = self._neweventconn() else: # no existing connection, create new self._conn = self._ConnHolder() if self.sendcommands: self._conn.command = self._newcmdconn() if self.blockshutdown: self._conn.blockshutdown = 1 if self.receiveevents: self._conn.event = self._neweventconn() self._shared[self._ident] = self._conn self._events = self._listhandlers() for func in self._events: self.registerevent(func)
self.db.settings.NULL[self._schema_name] = schema except AttributeError, e: self.log(MythLog.GENERAL, MythLog.CRIT, 'failed at %d' % schema, 'no handler method') raise MythDBError('Schema update failed, ' "SchemaUpdate has no function 'up%s'" % schema) except StopIteration: if schema != origschema: self.log(MythLog.GENERAL, MythLog.NOTICE, '%s update complete' % self._schema_name) pass except Exception, e: raise MythDBError(MythError.DB_SCHEMAUPDATE, e.args) def create(self): raise MythDBError('Schema creation failed, method not defined.') class databaseSearch(object): # decorator class for database searches """ Decorator class Decorated functions must accept three keywords: init, key, value. 'init' will be set once to True, to initialize the decorator. During initialization, the decorated class must return a tuple of the following format (<table name>, -- Primary table to pull data from.
def __init__(self, backend=None, blockshutdown=False, events=False, db=None): self.db = DBCache(db) self.log = MythLog(self.logmodule, db=self.db) self.hostname = None self.sendcommands = True self.blockshutdown = blockshutdown self.receiveevents = events if backend is None: # use master backend backend = self.db.getMasterBackend() else: backend = backend.strip('[]') # assume backend is hostname from settings host = self.db._getpreferredaddr(backend) if host: port = int(self.db.settings[backend].BackendServerPort) self.hostname = backend else: # assume ip address self.hostname = self.db._gethostfromaddr(backend) host = backend port = int(self.db.settings[self.hostname].BackendServerPort) # resolve ip address from name reshost, resport = resolve_ip(host,port) if not reshost: raise MythDBError(MythError.DB_SETTING, backend+': BackendServerAddr') if not resport: raise MythDBError(MythError.DB_SETTING, backend+': BackendServerPort') self.host = host self.port = port self._ident = '%s:%d' % (self.host, self.port) if self._ident in self._shared: # existing connection found self._conn = self._shared[self._ident] if self.sendcommands: if self._conn.command is None: self._conn.command = self._newcmdconn() elif self.blockshutdown: # upref block of shutdown # issue command to backend if needed self._conn.blockshutdown += 1 if self._conn.blockshutdown == 1: self._conn.command.blockShutdown() if self.receiveevents: if self._conn.event is None: self._conn.event = self._neweventconn() else: # no existing connection, create new self._conn = self._ConnHolder() if self.sendcommands: self._conn.command = self._newcmdconn() if self.blockshutdown: self._conn.blockshutdown = 1 if self.receiveevents: self._conn.event = self._neweventconn() self._shared[self._ident] = self._conn self._events = self._listhandlers() for func in self._events: self.registerevent(func)