+from singleton import Singleton
+from textutils import fix_maintainer
+
+################################################################################
+
+__all__ = ['IntegrityError', 'SQLAlchemyError']
+
+################################################################################
+
+class Architecture(object):
+ def __init__(self, *args, **kwargs):
+ pass
+
+ def __eq__(self, val):
+ if isinstance(val, str):
+ return (self.arch_string== val)
+ # This signals to use the normal comparison operator
+ return NotImplemented
+
+ def __ne__(self, val):
+ if isinstance(val, str):
+ return (self.arch_string != val)
+ # This signals to use the normal comparison operator
+ return NotImplemented
+
+ def __repr__(self):
+ return '<Architecture %s>' % self.arch_string
+
+__all__.append('Architecture')
+
+def get_architecture(architecture, session=None):
+ """
+ Returns database id for given C{architecture}.
+
+ @type architecture: string
+ @param architecture: The name of the architecture
+
+ @type session: Session
+ @param session: Optional SQLA session object (a temporary one will be
+ generated if not supplied)
+
+ @rtype: Architecture
+ @return: Architecture object for the given arch (None if not present)
+
+ """
+ if session is None:
+ session = DBConn().session()
+ q = session.query(Architecture).filter_by(arch_string=architecture)
+ if q.count() == 0:
+ return None
+ return q.one()
+
+__all__.append('get_architecture')
+
+def get_architecture_suites(architecture, session=None):
+ """
+ Returns list of Suite objects for given C{architecture} name
+
+ @type source: str
+ @param source: Architecture name to search for
+
+ @type session: Session
+ @param session: Optional SQL session object (a temporary one will be
+ generated if not supplied)
+
+ @rtype: list
+ @return: list of Suite objects for the given name (may be empty)
+ """
+
+ if session is None:
+ session = DBConn().session()
+
+ q = session.query(Suite)
+ q = q.join(SuiteArchitecture)
+ q = q.join(Architecture).filter_by(arch_string=architecture).order_by('suite_name')
+ return q.all()
+
+__all__.append('get_architecture_suites')
+
+################################################################################
+
+class Archive(object):
+ def __init__(self, *args, **kwargs):
+ pass
+
+ def __repr__(self):
+ return '<Archive %s>' % self.name
+
+__all__.append('Archive')
+
+def get_archive(archive, session=None):
+ """
+ returns database id for given c{archive}.
+
+ @type archive: string
+ @param archive: the name of the arhive
+
+ @type session: Session
+ @param session: Optional SQLA session object (a temporary one will be
+ generated if not supplied)
+
+ @rtype: Archive
+ @return: Archive object for the given name (None if not present)
+
+ """
+ archive = archive.lower()
+ if session is None:
+ session = DBConn().session()
+ q = session.query(Archive).filter_by(archive_name=archive)
+ if q.count() == 0:
+ return None
+ return q.one()
+
+__all__.append('get_archive')
+
+################################################################################
+
+class BinAssociation(object):
+ def __init__(self, *args, **kwargs):
+ pass
+
+ def __repr__(self):
+ return '<BinAssociation %s (%s, %s)>' % (self.ba_id, self.binary, self.suite)
+
+__all__.append('BinAssociation')
+
+################################################################################
+
+class DBBinary(object):
+ def __init__(self, *args, **kwargs):
+ pass
+
+ def __repr__(self):
+ return '<DBBinary %s (%s, %s)>' % (self.package, self.version, self.architecture)
+
+__all__.append('DBBinary')
+
+def get_suites_binary_in(package, session=None):
+ """
+ Returns list of Suite objects which given C{package} name is in
+
+ @type source: str
+ @param source: DBBinary package name to search for
+
+ @rtype: list
+ @return: list of Suite objects for the given package
+ """
+
+ if session is None:
+ session = DBConn().session()
+
+ return session.query(Suite).join(BinAssociation).join(DBBinary).filter_by(package=package).all()
+
+__all__.append('get_suites_binary_in')
+
+def get_binary_from_id(id, session=None):
+ """
+ Returns DBBinary object for given C{id}
+
+ @type id: int
+ @param id: Id of the required binary
+
+ @type session: Session
+ @param session: Optional SQLA session object (a temporary one will be
+ generated if not supplied)
+
+ @rtype: DBBinary
+ @return: DBBinary object for the given binary (None if not present)
+ """
+ if session is None:
+ session = DBConn().session()
+ q = session.query(DBBinary).filter_by(binary_id=id)
+ if q.count() == 0:
+ return None
+ return q.one()
+
+__all__.append('get_binary_from_id')
+
+def get_binaries_from_name(package, version=None, architecture=None, session=None):
+ """
+ Returns list of DBBinary objects for given C{package} name
+
+ @type package: str
+ @param package: DBBinary package name to search for
+
+ @type version: str or None
+ @param version: Version to search for (or None)
+
+ @type package: str, list or None
+ @param package: Architectures to limit to (or None if no limit)
+
+ @type session: Session
+ @param session: Optional SQL session object (a temporary one will be
+ generated if not supplied)
+
+ @rtype: list
+ @return: list of DBBinary objects for the given name (may be empty)
+ """
+ if session is None:
+ session = DBConn().session()
+
+ q = session.query(DBBinary).filter_by(package=package)
+
+ if version is not None:
+ q = q.filter_by(version=version)
+
+ if architecture is not None:
+ if not isinstance(architecture, list):
+ architecture = [architecture]
+ q = q.join(Architecture).filter(Architecture.arch_string.in_(architecture))
+
+ return q.all()
+
+__all__.append('get_binaries_from_name')
+
+def get_binaries_from_source_id(source_id, session=None):
+ """
+ Returns list of DBBinary objects for given C{source_id}
+
+ @type source_id: int
+ @param source_id: source_id to search for
+
+ @type session: Session
+ @param session: Optional SQL session object (a temporary one will be
+ generated if not supplied)
+
+ @rtype: list
+ @return: list of DBBinary objects for the given name (may be empty)
+ """
+ if session is None:
+ session = DBConn().session()
+ return session.query(DBBinary).filter_by(source_id=source_id).all()
+
+__all__.append('get_binaries_from_source_id')
+
+
+def get_binary_from_name_suite(package, suitename, session=None):
+ ### For dak examine-package
+ ### XXX: Doesn't use object API yet
+ if session is None:
+ session = DBConn().session()
+
+ sql = """SELECT DISTINCT(b.package), b.version, c.name, su.suite_name
+ FROM binaries b, files fi, location l, component c, bin_associations ba, suite su
+ WHERE b.package=:package
+ AND b.file = fi.id
+ AND fi.location = l.id
+ AND l.component = c.id
+ AND ba.bin=b.id
+ AND ba.suite = su.id
+ AND su.suite_name=:suitename
+ ORDER BY b.version DESC"""
+
+ return session.execute(sql, {'package': package, 'suitename': suitename})
+
+__all__.append('get_binary_from_name_suite')
+
+def get_binary_components(package, suitename, arch, session=None):
+# Check for packages that have moved from one component to another
+ query = """SELECT c.name FROM binaries b, bin_associations ba, suite s, location l, component c, architecture a, files f
+ WHERE b.package=:package AND s.suite_name=:suitename
+ AND (a.arch_string = :arch OR a.arch_string = 'all')
+ AND ba.bin = b.id AND ba.suite = s.id AND b.architecture = a.id
+ AND f.location = l.id
+ AND l.component = c.id
+ AND b.file = f.id"""
+
+ vals = {'package': package, 'suitename': suitename, 'arch': arch}
+
+ if session is None:
+ session = DBConn().session()
+ return session.execute(query, vals)
+
+__all__.append('get_binary_components')
+
+################################################################################
+
+class Component(object):
+ def __init__(self, *args, **kwargs):
+ pass
+
+ def __eq__(self, val):
+ if isinstance(val, str):
+ return (self.component_name == val)
+ # This signals to use the normal comparison operator
+ return NotImplemented
+
+ def __ne__(self, val):
+ if isinstance(val, str):
+ return (self.component_name != val)
+ # This signals to use the normal comparison operator
+ return NotImplemented
+
+ def __repr__(self):
+ return '<Component %s>' % self.component_name
+
+
+__all__.append('Component')
+
+def get_component(component, session=None):
+ """
+ Returns database id for given C{component}.
+
+ @type component: string
+ @param component: The name of the override type
+
+ @rtype: int
+ @return: the database id for the given component
+
+ """
+ component = component.lower()
+ if session is None:
+ session = DBConn().session()
+ q = session.query(Component).filter_by(component_name=component)
+ if q.count() == 0:
+ return None
+ return q.one()
+
+__all__.append('get_component')
+
+################################################################################
+
+class DBConfig(object):
+ def __init__(self, *args, **kwargs):
+ pass
+
+ def __repr__(self):
+ return '<DBConfig %s>' % self.name
+
+__all__.append('DBConfig')
+
+################################################################################
+
+class ContentFilename(object):
+ def __init__(self, *args, **kwargs):
+ pass
+
+ def __repr__(self):
+ return '<ContentFilename %s>' % self.filename
+
+__all__.append('ContentFilename')
+
+def get_or_set_contents_file_id(filename, session=None):
+ """
+ Returns database id for given filename.
+
+ If no matching file is found, a row is inserted.
+
+ @type filename: string
+ @param filename: The filename
+ @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: int
+ @return: the database id for the given component
+ """
+ privatetrans = False
+ if session is None:
+ session = DBConn().session()
+ privatetrans = True
+
+ try:
+ q = session.query(ContentFilename).filter_by(filename=filename)
+ if q.count() < 1:
+ cf = ContentFilename()
+ cf.filename = filename
+ session.add(cf)
+ if privatetrans:
+ session.commit()
+ return cf.cafilename_id
+ else:
+ return q.one().cafilename_id
+
+ except:
+ traceback.print_exc()
+ raise
+
+__all__.append('get_or_set_contents_file_id')
+
+def get_contents(suite, overridetype, section=None, session=None):
+ """
+ Returns contents for a suite / overridetype combination, limiting
+ to a section if not None.
+
+ @type suite: Suite
+ @param suite: Suite object
+
+ @type overridetype: OverrideType
+ @param overridetype: OverrideType object
+
+ @type section: Section
+ @param section: Optional section object to limit results to
+
+ @type session: SQLAlchemy
+ @param session: Optional SQL session object (a temporary one will be
+ generated if not supplied)
+
+ @rtype: ResultsProxy
+ @return: ResultsProxy object set up to return tuples of (filename, section,
+ package, arch_id)
+ """
+
+ if session is None:
+ session = DBConn().session()
+
+ # find me all of the contents for a given suite
+ contents_q = """SELECT (p.path||'/'||n.file) AS fn,
+ s.section,
+ b.package,
+ b.architecture
+ FROM content_associations c join content_file_paths p ON (c.filepath=p.id)
+ JOIN content_file_names n ON (c.filename=n.id)
+ JOIN binaries b ON (b.id=c.binary_pkg)
+ JOIN override o ON (o.package=b.package)
+ JOIN section s ON (s.id=o.section)
+ WHERE o.suite = :suiteid AND o.type = :overridetypeid
+ AND b.type=:overridetypename"""
+
+ vals = {'suiteid': suite.suite_id,
+ 'overridetypeid': overridetype.overridetype_id,
+ 'overridetypename': overridetype.overridetype}
+
+ if section is not None:
+ contents_q += " AND s.id = :sectionid"
+ vals['sectionid'] = section.section_id
+
+ contents_q += " ORDER BY fn"
+
+ return session.execute(contents_q, vals)
+
+__all__.append('get_contents')
+
+################################################################################
+
+class ContentFilepath(object):
+ def __init__(self, *args, **kwargs):
+ pass
+
+ def __repr__(self):
+ return '<ContentFilepath %s>' % self.filepath
+
+__all__.append('ContentFilepath')
+
+def get_or_set_contents_path_id(filepath, session):
+ """
+ Returns database id for given path.
+
+ If no matching file is found, a row is inserted.
+
+ @type filename: string
+ @param filename: The filepath
+ @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: int
+ @return: the database id for the given path
+ """
+ privatetrans = False
+ if session is None:
+ session = DBConn().session()
+ privatetrans = True
+
+ try:
+ q = session.query(ContentFilepath).filter_by(filepath=filepath)
+ if q.count() < 1:
+ cf = ContentFilepath()
+ cf.filepath = filepath
+ session.add(cf)
+ if privatetrans:
+ session.commit()
+ return cf.cafilepath_id
+ else:
+ return q.one().cafilepath_id
+
+ except:
+ traceback.print_exc()
+ raise
+
+__all__.append('get_or_set_contents_path_id')
+
+################################################################################
+
+class ContentAssociation(object):
+ def __init__(self, *args, **kwargs):
+ pass
+
+ def __repr__(self):
+ return '<ContentAssociation %s>' % self.ca_id
+
+__all__.append('ContentAssociation')
+
+def insert_content_paths(binary_id, fullpaths, session=None):
+ """
+ Make sure given path is associated with given binary id
+
+ @type binary_id: int
+ @param binary_id: the id of the binary
+ @type fullpaths: list
+ @param fullpaths: the list of paths of the file being associated with the binary
+ @type session: SQLAlchemy session
+ @param session: Optional SQLAlchemy session. If this is passed, the caller
+ is responsible for ensuring a transaction has begun and committing the
+ results or rolling back based on the result code. If not passed, a commit
+ will be performed at the end of the function, otherwise the caller is
+ responsible for commiting.
+
+ @return: True upon success
+ """
+
+ privatetrans = False
+
+ if session is None:
+ session = DBConn().session()
+ privatetrans = True
+
+ try:
+ for fullpath in fullpaths:
+ (path, file) = os.path.split(fullpath)
+
+ # Get the necessary IDs ...
+ ca = ContentAssociation()
+ ca.binary_id = binary_id
+ ca.filename_id = get_or_set_contents_file_id(file)
+ ca.filepath_id = get_or_set_contents_path_id(path)
+ session.add(ca)
+
+ # Only commit if we set up the session ourself
+ if privatetrans:
+ session.commit()
+
+ return True
+ except:
+ traceback.print_exc()
+
+ # Only rollback if we set up the session ourself
+ if privatetrans:
+ session.rollback()
+
+ return False
+
+__all__.append('insert_content_paths')
+
+################################################################################
+
+class DSCFile(object):
+ def __init__(self, *args, **kwargs):
+ pass
+
+ def __repr__(self):
+ return '<DSCFile %s>' % self.dscfile_id
+
+__all__.append('DSCFile')
+
+def get_dscfiles(dscfile_id=None, source_id=None, poolfile_id=None, session=None):
+ """
+ Returns a list of DSCFiles which may be empty
+
+ @type dscfile_id: int (optional)
+ @param dscfile_id: the dscfile_id of the DSCFiles to find
+
+ @type source_id: int (optional)
+ @param source_id: the source id related to the DSCFiles to find
+
+ @type poolfile_id: int (optional)
+ @param poolfile_id: the poolfile id related to the DSCFiles to find
+
+ @rtype: list
+ @return: Possibly empty list of DSCFiles
+ """
+
+ if session is None:
+ session = DBConn().session()
+
+ q = session.query(DSCFile)
+
+ if dscfile_id is not None:
+ q = q.filter_by(dscfile_id=dscfile_id)
+
+ if source_id is not None:
+ q = q.filter_by(source_id=source_id)
+
+ if poolfile_id is not None:
+ q = q.filter_by(poolfile_id=poolfile_id)
+
+ return q.all()
+
+__all__.append('get_dscfiles')
+
+################################################################################
+
+class PoolFile(object):
+ def __init__(self, *args, **kwargs):
+ pass
+
+ def __repr__(self):
+ return '<PoolFile %s>' % self.filename
+
+__all__.append('PoolFile')
+
+def check_poolfile(filename, filesize, md5sum, location_id, session=None):
+ """
+ Returns a tuple:
+ (ValidFileFound [boolean or None], PoolFile object or None)
+
+ @type filename: string
+ @param filename: the filename of the file to check against the DB
+
+ @type filesize: int
+ @param filesize: the size of the file to check against the DB
+
+ @type md5sum: string
+ @param md5sum: the md5sum of the file to check against the DB
+
+ @type location_id: int
+ @param location_id: the id of the location to look in
+
+ @rtype: tuple
+ @return: Tuple of length 2.
+ If more than one file found with that name:
+ (None, None)
+ If valid pool file found: (True, PoolFile object)
+ If valid pool file not found:
+ (False, None) if no file found
+ (False, PoolFile object) if file found with size/md5sum mismatch
+ """
+
+ if session is None:
+ session = DBConn().session()
+
+ q = session.query(PoolFile).filter_by(filename=filename)
+ q = q.join(Location).filter_by(location_id=location_id)
+
+ if q.count() > 1:
+ return (None, None)
+ if q.count() < 1:
+ return (False, None)
+
+ obj = q.one()
+ if obj.md5sum != md5sum or obj.filesize != filesize:
+ return (False, obj)
+
+ return (True, obj)
+
+__all__.append('check_poolfile')
+
+def get_poolfile_by_id(file_id, session=None):
+ """
+ Returns a PoolFile objects or None for the given id
+
+ @type file_id: int
+ @param file_id: the id of the file to look for
+
+ @rtype: PoolFile or None
+ @return: either the PoolFile object or None
+ """
+
+ if session is None:
+ session = DBConn().session()
+
+ q = session.query(PoolFile).filter_by(file_id=file_id)
+
+ if q.count() > 0:
+ return q.one()
+
+ return None
+
+__all__.append('get_poolfile_by_id')
+
+
+def get_poolfile_by_name(filename, location_id=None, session=None):
+ """
+ Returns an array of PoolFile objects for the given filename and
+ (optionally) location_id
+
+ @type filename: string
+ @param filename: the filename of the file to check against the DB
+
+ @type location_id: int
+ @param location_id: the id of the location to look in (optional)
+
+ @rtype: array
+ @return: array of PoolFile objects
+ """
+
+ if session is None:
+ session = DBConn().session()
+
+ q = session.query(PoolFile).filter_by(filename=filename)
+
+ if location_id is not None:
+ q = q.join(Location).filter_by(location_id=location_id)
+
+ return q.all()
+
+__all__.append('get_poolfile_by_name')
+
+def get_poolfile_like_name(filename, session=None):
+ """
+ Returns an array of PoolFile objects which are like the given name
+
+ @type filename: string
+ @param filename: the filename of the file to check against the DB
+
+ @rtype: array
+ @return: array of PoolFile objects
+ """
+
+ if session is None:
+ session = DBConn().session()
+
+ # TODO: There must be a way of properly using bind parameters with %FOO%
+ q = session.query(PoolFile).filter(PoolFile.filename.like('%%%s%%' % filename))
+
+ return q.all()
+
+__all__.append('get_poolfile_like_name')
+
+################################################################################
+
+class Fingerprint(object):
+ def __init__(self, *args, **kwargs):
+ pass
+
+ def __repr__(self):
+ return '<Fingerprint %s>' % self.fingerprint
+
+__all__.append('Fingerprint')
+
+def get_or_set_fingerprint(fpr, session=None):
+ """
+ Returns Fingerprint object for given fpr.
+
+ If no matching fpr is found, a row is inserted.
+
+ @type fpr: string
+ @param fpr: The fpr to find / 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.
+ A flush will be performed either way.
+
+ @rtype: Fingerprint
+ @return: the Fingerprint object for the given fpr
+ """
+ privatetrans = False
+ if session is None:
+ session = DBConn().session()
+ privatetrans = True
+
+ try:
+ q = session.query(Fingerprint).filter_by(fingerprint=fpr)
+ if q.count() < 1:
+ fingerprint = Fingerprint()
+ fingerprint.fingerprint = fpr
+ session.add(fingerprint)
+ if privatetrans:
+ session.commit()
+ else:
+ session.flush()
+ return fingerprint
+ else:
+ return q.one()
+
+ except:
+ traceback.print_exc()
+ raise
+
+__all__.append('get_or_set_fingerprint')
+
+################################################################################
+
+class Keyring(object):
+ def __init__(self, *args, **kwargs):
+ pass
+
+ def __repr__(self):
+ return '<Keyring %s>' % self.keyring_name
+
+__all__.append('Keyring')
+
+################################################################################
+
+class Location(object):
+ def __init__(self, *args, **kwargs):
+ pass
+
+ def __repr__(self):
+ return '<Location %s (%s)>' % (self.path, self.location_id)
+
+__all__.append('Location')
+
+def get_location(location, component=None, archive=None, session=None):
+ """
+ Returns Location object for the given combination of location, component
+ and archive
+
+ @type location: string
+ @param location: the path of the location, e.g. I{/srv/ftp.debian.org/ftp/pool/}
+
+ @type component: string
+ @param component: the component name (if None, no restriction applied)
+
+ @type archive: string
+ @param archive_id: the archive name (if None, no restriction applied)
+
+ @rtype: Location / None
+ @return: Either a Location object or None if one can't be found
+ """
+
+ if session is None:
+ session = DBConn().session()
+
+ q = session.query(Location).filter_by(path=location)
+
+ if archive is not None:
+ q = q.join(Archive).filter_by(archive_name=archive)
+
+ if component is not None:
+ q = q.join(Component).filter_by(component_name=component)
+
+ if q.count() < 1:
+ return None
+ else:
+ return q.one()
+
+__all__.append('get_location')
+
+################################################################################
+
+class Maintainer(object):
+ def __init__(self, *args, **kwargs):
+ pass
+
+ def __repr__(self):
+ return '''<Maintainer '%s' (%s)>''' % (self.name, self.maintainer_id)
+
+ def get_split_maintainer(self):
+ if not hasattr(self, 'name') or self.name is None:
+ return ('', '', '', '')
+
+ return fix_maintainer(self.name.strip())
+
+__all__.append('Maintainer')
+
+def get_or_set_maintainer(name, session=None):
+ """
+ Returns Maintainer object for given maintainer name.
+
+ If no matching maintainer name is found, a row is inserted.
+
+ @type name: string
+ @param name: The maintainer name 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.
+ A flush will be performed either way.
+
+ @rtype: Maintainer
+ @return: the Maintainer object for the given maintainer
+ """
+ privatetrans = False
+ if session is None:
+ session = DBConn().session()
+ privatetrans = True
+
+ try:
+ q = session.query(Maintainer).filter_by(name=name)
+ if q.count() < 1:
+ maintainer = Maintainer()
+ maintainer.name = name
+ session.add(maintainer)
+ if privatetrans:
+ session.commit()
+ else:
+ session.flush()
+ return maintainer
+ else:
+ return q.one()
+
+ except:
+ traceback.print_exc()
+ raise
+
+__all__.append('get_or_set_maintainer')