+ q = session.query(Suite).filter_by(suite_name=suite)
+
+ try:
+ return q.one()
+ except NoResultFound:
+ return None
+
+__all__.append('get_suite')
+
+################################################################################
+
+@session_wrapper
+def get_suite_architectures(suite, skipsrc=False, skipall=False, session=None):
+ """
+ Returns list of Architecture objects for given C{suite} name. The list is
+ empty if suite does not exist.
+
+ @type suite: str
+ @param suite: Suite name to search for
+
+ @type skipsrc: boolean
+ @param skipsrc: Whether to skip returning the 'source' architecture entry
+ (Default False)
+
+ @type skipall: boolean
+ @param skipall: Whether to skip returning the 'all' architecture entry
+ (Default False)
+
+ @type session: Session
+ @param session: Optional SQL session object (a temporary one will be
+ generated if not supplied)
+
+ @rtype: list
+ @return: list of Architecture objects for the given name (may be empty)
+ """
+
+ try:
+ return get_suite(suite, session).get_architectures(skipsrc, skipall)
+ except AttributeError:
+ return []
+
+__all__.append('get_suite_architectures')
+
+################################################################################
+
+class Uid(ORMObject):
+ def __init__(self, uid = None, name = None):
+ self.uid = uid
+ self.name = name
+
+ def __eq__(self, val):
+ if isinstance(val, str):
+ return (self.uid == val)
+ # This signals to use the normal comparison operator
+ return NotImplemented
+
+ def __ne__(self, val):
+ if isinstance(val, str):
+ return (self.uid != val)
+ # This signals to use the normal comparison operator
+ return NotImplemented
+
+ def properties(self):
+ return ['uid', 'name', 'fingerprint']
+
+ def not_null_constraints(self):
+ return ['uid']
+
+__all__.append('Uid')
+
+@session_wrapper
+def get_or_set_uid(uidname, session=None):
+ """
+ Returns uid object for given uidname.
+
+ If no matching uidname is found, a row is inserted.
+
+ @type uidname: string
+ @param uidname: The uid to add
+
+ @type session: SQLAlchemy
+ @param session: Optional SQL session object (a temporary one will be
+ generated if not supplied). If not passed, a commit will be performed at
+ the end of the function, otherwise the caller is responsible for commiting.
+
+ @rtype: Uid
+ @return: the uid object for the given uidname
+ """
+
+ q = session.query(Uid).filter_by(uid=uidname)
+
+ try:
+ ret = q.one()
+ except NoResultFound:
+ uid = Uid()
+ uid.uid = uidname
+ session.add(uid)
+ session.commit_or_flush()
+ ret = uid
+
+ return ret
+
+__all__.append('get_or_set_uid')
+
+@session_wrapper
+def get_uid_from_fingerprint(fpr, session=None):
+ q = session.query(Uid)
+ q = q.join(Fingerprint).filter_by(fingerprint=fpr)
+
+ try:
+ return q.one()
+ except NoResultFound:
+ return None
+
+__all__.append('get_uid_from_fingerprint')
+
+################################################################################
+
+class MetadataKey(ORMObject):
+ def __init__(self, key = None):
+ self.key = key
+
+ def properties(self):
+ return ['key']
+
+ def not_null_constraints(self):
+ return ['key']
+
+__all__.append('MetadataKey')
+
+@session_wrapper
+def get_or_set_metadatakey(keyname, session=None):
+ """
+ Returns MetadataKey object for given uidname.
+
+ If no matching keyname is found, a row is inserted.
+
+ @type uidname: string
+ @param uidname: The keyname to add
+
+ @type session: SQLAlchemy
+ @param session: Optional SQL session object (a temporary one will be
+ generated if not supplied). If not passed, a commit will be performed at
+ the end of the function, otherwise the caller is responsible for commiting.
+
+ @rtype: MetadataKey
+ @return: the metadatakey object for the given keyname
+ """
+
+ q = session.query(MetadataKey).filter_by(key=keyname)
+
+ try:
+ ret = q.one()
+ except NoResultFound:
+ ret = MetadataKey(keyname)
+ session.add(ret)
+ session.commit_or_flush()
+
+ return ret
+
+__all__.append('get_or_set_metadatakey')
+
+################################################################################
+
+class BinaryMetadata(ORMObject):
+ def __init__(self, key = None, value = None, binary = None):
+ self.key = key
+ self.value = value
+ self.binary = binary
+
+ def properties(self):
+ return ['binary', 'key', 'value']
+
+ def not_null_constraints(self):
+ return ['value']
+
+__all__.append('BinaryMetadata')
+
+################################################################################
+
+class SourceMetadata(ORMObject):
+ def __init__(self, key = None, value = None, source = None):
+ self.key = key
+ self.value = value
+ self.source = source
+
+ def properties(self):
+ return ['source', 'key', 'value']
+
+ def not_null_constraints(self):
+ return ['value']
+
+__all__.append('SourceMetadata')
+
+################################################################################
+
+class VersionCheck(ORMObject):
+ def __init__(self, *args, **kwargs):
+ pass
+
+ def properties(self):
+ #return ['suite_id', 'check', 'reference_id']
+ return ['check']
+
+ def not_null_constraints(self):
+ return ['suite', 'check', 'reference']
+
+__all__.append('VersionCheck')
+
+@session_wrapper
+def get_version_checks(suite_name, check = None, session = None):
+ suite = get_suite(suite_name, session)
+ if not suite:
+ # Make sure that what we return is iterable so that list comprehensions
+ # involving this don't cause a traceback
+ return []
+ q = session.query(VersionCheck).filter_by(suite=suite)
+ if check:
+ q = q.filter_by(check=check)
+ return q.all()
+
+__all__.append('get_version_checks')
+
+################################################################################
+
+class DBConn(object):
+ """
+ database module init.
+ """
+ __shared_state = {}
+
+ def __init__(self, *args, **kwargs):
+ self.__dict__ = self.__shared_state
+
+ if not getattr(self, 'initialised', False):
+ self.initialised = True
+ self.debug = kwargs.has_key('debug')
+ self.__createconn()
+
+ def __setuptables(self):
+ tables = (
+ 'acl',
+ 'acl_architecture_map',
+ 'acl_fingerprint_map',
+ 'acl_per_source',
+ 'architecture',
+ 'archive',
+ 'bin_associations',
+ 'bin_contents',
+ 'binaries',
+ 'binaries_metadata',
+ 'build_queue',
+ 'changelogs_text',
+ 'changes',
+ 'component',
+ 'config',
+ 'dsc_files',
+ 'external_overrides',
+ 'extra_src_references',
+ 'files',
+ 'files_archive_map',
+ 'fingerprint',
+ 'keyrings',
+ 'maintainer',
+ 'metadata_keys',
+ 'new_comments',
+ # TODO: the maintainer column in table override should be removed.
+ 'override',
+ 'override_type',
+ 'policy_queue',
+ 'policy_queue_upload',
+ 'policy_queue_upload_binaries_map',
+ 'policy_queue_byhand_file',
+ 'priority',
+ 'section',
+ 'signature_history',
+ 'source',
+ 'source_metadata',
+ 'src_associations',
+ 'src_contents',
+ 'src_format',
+ 'src_uploaders',
+ 'suite',
+ 'suite_acl_map',
+ 'suite_architectures',
+ 'suite_build_queue_copy',
+ 'suite_src_formats',
+ 'uid',
+ 'version_check',
+ )
+
+ views = (
+ 'almost_obsolete_all_associations',
+ 'almost_obsolete_src_associations',
+ 'any_associations_source',
+ 'bin_associations_binaries',
+ 'binaries_suite_arch',
+ 'changelogs',
+ 'file_arch_suite',
+ 'newest_all_associations',
+ 'newest_any_associations',
+ 'newest_source',
+ 'newest_src_association',
+ 'obsolete_all_associations',
+ 'obsolete_any_associations',
+ 'obsolete_any_by_all_associations',
+ 'obsolete_src_associations',
+ 'source_suite',
+ 'src_associations_bin',
+ 'src_associations_src',
+ 'suite_arch_by_name',
+ )
+
+ for table_name in tables:
+ table = Table(table_name, self.db_meta, \
+ autoload=True, useexisting=True)
+ setattr(self, 'tbl_%s' % table_name, table)
+
+ for view_name in views:
+ view = Table(view_name, self.db_meta, autoload=True)
+ setattr(self, 'view_%s' % view_name, view)
+
+ def __setupmappers(self):
+ mapper(Architecture, self.tbl_architecture,
+ properties = dict(arch_id = self.tbl_architecture.c.id,
+ suites = relation(Suite, secondary=self.tbl_suite_architectures,
+ order_by=self.tbl_suite.c.suite_name,
+ backref=backref('architectures', order_by=self.tbl_architecture.c.arch_string))),
+ extension = validator)
+
+ mapper(ACL, self.tbl_acl,
+ properties = dict(
+ architectures = relation(Architecture, secondary=self.tbl_acl_architecture_map, collection_class=set),
+ fingerprints = relation(Fingerprint, secondary=self.tbl_acl_fingerprint_map, collection_class=set),
+ match_keyring = relation(Keyring, primaryjoin=(self.tbl_acl.c.match_keyring_id == self.tbl_keyrings.c.id)),
+ per_source = relation(ACLPerSource, collection_class=set),
+ ))
+
+ mapper(ACLPerSource, self.tbl_acl_per_source,
+ properties = dict(
+ acl = relation(ACL),
+ fingerprint = relation(Fingerprint, primaryjoin=(self.tbl_acl_per_source.c.fingerprint_id == self.tbl_fingerprint.c.id)),
+ created_by = relation(Fingerprint, primaryjoin=(self.tbl_acl_per_source.c.created_by_id == self.tbl_fingerprint.c.id)),
+ ))
+
+ mapper(Archive, self.tbl_archive,
+ properties = dict(archive_id = self.tbl_archive.c.id,
+ archive_name = self.tbl_archive.c.name))
+
+ mapper(ArchiveFile, self.tbl_files_archive_map,
+ properties = dict(archive = relation(Archive, backref='files'),
+ component = relation(Component),
+ file = relation(PoolFile, backref='archives')))
+
+ mapper(BuildQueue, self.tbl_build_queue,
+ properties = dict(queue_id = self.tbl_build_queue.c.id,
+ suite = relation(Suite, primaryjoin=(self.tbl_build_queue.c.suite_id==self.tbl_suite.c.id))))
+
+ mapper(DBBinary, self.tbl_binaries,
+ properties = dict(binary_id = self.tbl_binaries.c.id,
+ package = self.tbl_binaries.c.package,
+ version = self.tbl_binaries.c.version,
+ maintainer_id = self.tbl_binaries.c.maintainer,
+ maintainer = relation(Maintainer),
+ source_id = self.tbl_binaries.c.source,
+ source = relation(DBSource, backref='binaries'),
+ arch_id = self.tbl_binaries.c.architecture,
+ architecture = relation(Architecture),
+ poolfile_id = self.tbl_binaries.c.file,
+ poolfile = relation(PoolFile),
+ binarytype = self.tbl_binaries.c.type,
+ fingerprint_id = self.tbl_binaries.c.sig_fpr,
+ fingerprint = relation(Fingerprint),
+ install_date = self.tbl_binaries.c.install_date,
+ suites = relation(Suite, secondary=self.tbl_bin_associations,
+ backref=backref('binaries', lazy='dynamic')),
+ extra_sources = relation(DBSource, secondary=self.tbl_extra_src_references,
+ backref=backref('extra_binary_references', lazy='dynamic')),
+ key = relation(BinaryMetadata, cascade='all',
+ collection_class=attribute_mapped_collection('key'))),
+ extension = validator)
+
+ mapper(Component, self.tbl_component,
+ properties = dict(component_id = self.tbl_component.c.id,
+ component_name = self.tbl_component.c.name),
+ extension = validator)
+
+ mapper(DBConfig, self.tbl_config,
+ properties = dict(config_id = self.tbl_config.c.id))
+
+ mapper(DSCFile, self.tbl_dsc_files,
+ properties = dict(dscfile_id = self.tbl_dsc_files.c.id,
+ source_id = self.tbl_dsc_files.c.source,
+ source = relation(DBSource),
+ poolfile_id = self.tbl_dsc_files.c.file,
+ poolfile = relation(PoolFile)))
+
+ mapper(ExternalOverride, self.tbl_external_overrides,
+ properties = dict(
+ suite_id = self.tbl_external_overrides.c.suite,
+ suite = relation(Suite),
+ component_id = self.tbl_external_overrides.c.component,
+ component = relation(Component)))
+
+ mapper(PoolFile, self.tbl_files,
+ properties = dict(file_id = self.tbl_files.c.id,
+ filesize = self.tbl_files.c.size),
+ extension = validator)
+
+ mapper(Fingerprint, self.tbl_fingerprint,
+ properties = dict(fingerprint_id = self.tbl_fingerprint.c.id,
+ uid_id = self.tbl_fingerprint.c.uid,
+ uid = relation(Uid),
+ keyring_id = self.tbl_fingerprint.c.keyring,
+ keyring = relation(Keyring),
+ acl = relation(ACL)),
+ extension = validator)
+
+ mapper(Keyring, self.tbl_keyrings,
+ properties = dict(keyring_name = self.tbl_keyrings.c.name,
+ keyring_id = self.tbl_keyrings.c.id,
+ acl = relation(ACL, primaryjoin=(self.tbl_keyrings.c.acl_id == self.tbl_acl.c.id)))),
+
+ mapper(DBChange, self.tbl_changes,
+ properties = dict(change_id = self.tbl_changes.c.id,
+ seen = self.tbl_changes.c.seen,
+ source = self.tbl_changes.c.source,
+ binaries = self.tbl_changes.c.binaries,
+ architecture = self.tbl_changes.c.architecture,
+ distribution = self.tbl_changes.c.distribution,
+ urgency = self.tbl_changes.c.urgency,
+ maintainer = self.tbl_changes.c.maintainer,
+ changedby = self.tbl_changes.c.changedby,
+ date = self.tbl_changes.c.date,
+ version = self.tbl_changes.c.version))
+
+ mapper(Maintainer, self.tbl_maintainer,
+ properties = dict(maintainer_id = self.tbl_maintainer.c.id,
+ maintains_sources = relation(DBSource, backref='maintainer',
+ primaryjoin=(self.tbl_maintainer.c.id==self.tbl_source.c.maintainer)),
+ changed_sources = relation(DBSource, backref='changedby',
+ primaryjoin=(self.tbl_maintainer.c.id==self.tbl_source.c.changedby))),
+ extension = validator)
+
+ mapper(NewComment, self.tbl_new_comments,
+ properties = dict(comment_id = self.tbl_new_comments.c.id,
+ policy_queue = relation(PolicyQueue)))
+
+ mapper(Override, self.tbl_override,
+ properties = dict(suite_id = self.tbl_override.c.suite,
+ suite = relation(Suite, \
+ backref=backref('overrides', lazy='dynamic')),
+ package = self.tbl_override.c.package,
+ component_id = self.tbl_override.c.component,
+ component = relation(Component, \
+ backref=backref('overrides', lazy='dynamic')),
+ priority_id = self.tbl_override.c.priority,
+ priority = relation(Priority, \
+ backref=backref('overrides', lazy='dynamic')),
+ section_id = self.tbl_override.c.section,
+ section = relation(Section, \
+ backref=backref('overrides', lazy='dynamic')),
+ overridetype_id = self.tbl_override.c.type,
+ overridetype = relation(OverrideType, \
+ backref=backref('overrides', lazy='dynamic'))))
+
+ mapper(OverrideType, self.tbl_override_type,
+ properties = dict(overridetype = self.tbl_override_type.c.type,
+ overridetype_id = self.tbl_override_type.c.id))
+
+ mapper(PolicyQueue, self.tbl_policy_queue,
+ properties = dict(policy_queue_id = self.tbl_policy_queue.c.id,
+ suite = relation(Suite, primaryjoin=(self.tbl_policy_queue.c.suite_id == self.tbl_suite.c.id))))
+
+ mapper(PolicyQueueUpload, self.tbl_policy_queue_upload,
+ properties = dict(
+ changes = relation(DBChange),
+ policy_queue = relation(PolicyQueue, backref='uploads'),
+ target_suite = relation(Suite),
+ source = relation(DBSource),
+ binaries = relation(DBBinary, secondary=self.tbl_policy_queue_upload_binaries_map),
+ ))
+
+ mapper(PolicyQueueByhandFile, self.tbl_policy_queue_byhand_file,
+ properties = dict(
+ upload = relation(PolicyQueueUpload, backref='byhand'),
+ )
+ )
+
+ mapper(Priority, self.tbl_priority,
+ properties = dict(priority_id = self.tbl_priority.c.id))
+
+ mapper(Section, self.tbl_section,
+ properties = dict(section_id = self.tbl_section.c.id,
+ section=self.tbl_section.c.section))
+
+ mapper(SignatureHistory, self.tbl_signature_history)
+
+ mapper(DBSource, self.tbl_source,
+ properties = dict(source_id = self.tbl_source.c.id,
+ version = self.tbl_source.c.version,
+ maintainer_id = self.tbl_source.c.maintainer,
+ poolfile_id = self.tbl_source.c.file,
+ poolfile = relation(PoolFile),
+ fingerprint_id = self.tbl_source.c.sig_fpr,
+ fingerprint = relation(Fingerprint),
+ changedby_id = self.tbl_source.c.changedby,
+ srcfiles = relation(DSCFile,
+ primaryjoin=(self.tbl_source.c.id==self.tbl_dsc_files.c.source)),
+ suites = relation(Suite, secondary=self.tbl_src_associations,
+ backref=backref('sources', lazy='dynamic')),
+ uploaders = relation(Maintainer,
+ secondary=self.tbl_src_uploaders),
+ key = relation(SourceMetadata, cascade='all',
+ collection_class=attribute_mapped_collection('key'))),
+ extension = validator)
+
+ mapper(SrcFormat, self.tbl_src_format,
+ properties = dict(src_format_id = self.tbl_src_format.c.id,
+ format_name = self.tbl_src_format.c.format_name))
+
+ mapper(Suite, self.tbl_suite,
+ properties = dict(suite_id = self.tbl_suite.c.id,
+ policy_queue = relation(PolicyQueue, primaryjoin=(self.tbl_suite.c.policy_queue_id == self.tbl_policy_queue.c.id)),
+ new_queue = relation(PolicyQueue, primaryjoin=(self.tbl_suite.c.new_queue_id == self.tbl_policy_queue.c.id)),
+ copy_queues = relation(BuildQueue,
+ secondary=self.tbl_suite_build_queue_copy),
+ srcformats = relation(SrcFormat, secondary=self.tbl_suite_src_formats,
+ backref=backref('suites', lazy='dynamic')),
+ archive = relation(Archive, backref='suites'),
+ acls = relation(ACL, secondary=self.tbl_suite_acl_map, collection_class=set)),
+ extension = validator)
+
+ mapper(Uid, self.tbl_uid,
+ properties = dict(uid_id = self.tbl_uid.c.id,
+ fingerprint = relation(Fingerprint)),
+ extension = validator)
+
+ mapper(BinContents, self.tbl_bin_contents,
+ properties = dict(
+ binary = relation(DBBinary,
+ backref=backref('contents', lazy='dynamic', cascade='all')),
+ file = self.tbl_bin_contents.c.file))
+
+ mapper(SrcContents, self.tbl_src_contents,
+ properties = dict(
+ source = relation(DBSource,
+ backref=backref('contents', lazy='dynamic', cascade='all')),
+ file = self.tbl_src_contents.c.file))
+
+ mapper(MetadataKey, self.tbl_metadata_keys,
+ properties = dict(
+ key_id = self.tbl_metadata_keys.c.key_id,
+ key = self.tbl_metadata_keys.c.key))
+
+ mapper(BinaryMetadata, self.tbl_binaries_metadata,
+ properties = dict(
+ binary_id = self.tbl_binaries_metadata.c.bin_id,
+ binary = relation(DBBinary),
+ key_id = self.tbl_binaries_metadata.c.key_id,
+ key = relation(MetadataKey),
+ value = self.tbl_binaries_metadata.c.value))
+
+ mapper(SourceMetadata, self.tbl_source_metadata,
+ properties = dict(
+ source_id = self.tbl_source_metadata.c.src_id,
+ source = relation(DBSource),
+ key_id = self.tbl_source_metadata.c.key_id,
+ key = relation(MetadataKey),
+ value = self.tbl_source_metadata.c.value))
+
+ mapper(VersionCheck, self.tbl_version_check,
+ properties = dict(
+ suite_id = self.tbl_version_check.c.suite,
+ suite = relation(Suite, primaryjoin=self.tbl_version_check.c.suite==self.tbl_suite.c.id),
+ reference_id = self.tbl_version_check.c.reference,
+ reference = relation(Suite, primaryjoin=self.tbl_version_check.c.reference==self.tbl_suite.c.id, lazy='joined')))
+
+ ## Connection functions
+ def __createconn(self):
+ from config import Config
+ cnf = Config()
+ if cnf.has_key("DB::Service"):
+ connstr = "postgresql://service=%s" % cnf["DB::Service"]
+ elif cnf.has_key("DB::Host"):
+ # TCP/IP
+ connstr = "postgresql://%s" % cnf["DB::Host"]
+ if cnf.has_key("DB::Port") and cnf["DB::Port"] != "-1":
+ connstr += ":%s" % cnf["DB::Port"]
+ connstr += "/%s" % cnf["DB::Name"]
+ else:
+ # Unix Socket
+ connstr = "postgresql:///%s" % cnf["DB::Name"]
+ if cnf.has_key("DB::Port") and cnf["DB::Port"] != "-1":
+ connstr += "?port=%s" % cnf["DB::Port"]
+
+ engine_args = { 'echo': self.debug }
+ if cnf.has_key('DB::PoolSize'):
+ engine_args['pool_size'] = int(cnf['DB::PoolSize'])
+ if cnf.has_key('DB::MaxOverflow'):
+ engine_args['max_overflow'] = int(cnf['DB::MaxOverflow'])
+ if sa_major_version in ('0.6', '0.7') and cnf.has_key('DB::Unicode') and \
+ cnf['DB::Unicode'] == 'false':
+ engine_args['use_native_unicode'] = False
+
+ # Monkey patch a new dialect in in order to support service= syntax
+ import sqlalchemy.dialects.postgresql
+ from sqlalchemy.dialects.postgresql.psycopg2 import PGDialect_psycopg2
+ class PGDialect_psycopg2_dak(PGDialect_psycopg2):
+ def create_connect_args(self, url):
+ if str(url).startswith('postgresql://service='):
+ # Eww
+ servicename = str(url)[21:]
+ return (['service=%s' % servicename], {})
+ else:
+ return PGDialect_psycopg2.create_connect_args(self, url)
+
+ sqlalchemy.dialects.postgresql.base.dialect = PGDialect_psycopg2_dak
+
+ try:
+ self.db_pg = create_engine(connstr, **engine_args)
+ self.db_meta = MetaData()
+ self.db_meta.bind = self.db_pg
+ self.db_smaker = sessionmaker(bind=self.db_pg,
+ autoflush=True,
+ autocommit=False)
+
+ self.__setuptables()
+ self.__setupmappers()
+
+ except OperationalError as e:
+ import utils
+ utils.fubar("Cannot connect to database (%s)" % str(e))
+
+ self.pid = os.getpid()
+
+ def session(self, work_mem = 0):
+ '''
+ Returns a new session object. If a work_mem parameter is provided a new
+ transaction is started and the work_mem parameter is set for this
+ transaction. The work_mem parameter is measured in MB. A default value
+ will be used if the parameter is not set.
+ '''
+ # reinitialize DBConn in new processes
+ if self.pid != os.getpid():
+ clear_mappers()
+ self.__createconn()
+ session = self.db_smaker()
+ if work_mem > 0:
+ session.execute("SET LOCAL work_mem TO '%d MB'" % work_mem)
+ return session
+
+__all__.append('DBConn')