+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)
+
+ """
+ privatetrans = False
+
+ if session is None:
+ session = DBConn().session()
+ privatetrans = True
+
+ q = session.query(Architecture).filter_by(arch_string=architecture)
+
+ if q.count() == 0:
+ ret = None
+ else:
+ ret = q.one()
+
+ if privatetrans:
+ session.close()
+
+ return ret
+
+__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)
+ """
+ privatetrans = False
+
+ if session is None:
+ session = DBConn().session()
+ privatetrans = True
+
+ q = session.query(Suite)
+ q = q.join(SuiteArchitecture)
+ q = q.join(Architecture).filter_by(arch_string=architecture).order_by('suite_name')
+
+ ret = q.all()
+
+ if privatetrans:
+ session.close()
+
+ return ret
+
+__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()
+
+ privatetrans = False
+ if session is None:
+ session = DBConn().session()
+ privatetrans = True
+
+ q = session.query(Archive).filter_by(archive_name=archive)
+
+ if q.count() == 0:
+ ret = None
+ else:
+ ret = q.one()
+
+ if privatetrans:
+ session.close()
+
+ return ret
+
+__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
+ """
+
+ privatetrans = False
+ if session is None:
+ session = DBConn().session()
+ privatetrans = True
+
+ ret = session.query(Suite).join(BinAssociation).join(DBBinary).filter_by(package=package).all()
+
+ session.close()
+
+ return ret
+
+__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)
+ """
+ privatetrans = False
+ if session is None:
+ session = DBConn().session()
+ privatetrans = True
+
+ q = session.query(DBBinary).filter_by(binary_id=id)
+
+ if q.count() == 0:
+ ret = None
+ else:
+ ret = q.one()
+
+ if privatetrans:
+ session.close()
+
+ return ret
+
+__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)
+ """
+ privatetrans = False
+ if session is None:
+ session = DBConn().session()
+ privatetrans = True
+
+ 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))
+
+ ret = q.all()
+
+ if privatetrans:
+ session.close()
+
+ return ret
+
+__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)
+ """
+ privatetrans = False
+ if session is None:
+ session = DBConn().session()
+ privatetrans = True
+
+ ret = session.query(DBBinary).filter_by(source_id=source_id).all()
+
+ if privatetrans:
+ session.close()
+
+ return ret
+
+
+__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
+ privatetrans = False
+ if session is None:
+ session = DBConn().session()
+ privatetrans = True
+
+ 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"""
+
+ ret = session.execute(sql, {'package': package, 'suitename': suitename})
+
+ if privatetrans:
+ session.close()
+
+ return ret
+
+__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}
+
+ privatetrans = False
+ if session is None:
+ session = DBConn().session()
+ privatetrans = True
+
+ ret = session.execute(query, vals)
+
+ if privatetrans:
+ session.close()
+
+ return ret
+
+__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()
+
+ privatetrans = False
+ if session is None:
+ session = DBConn().session()
+ privatetrans = True
+
+ q = session.query(Component).filter_by(component_name=component)
+
+ if q.count() == 0:
+ ret = None
+ else:
+ ret = q.one()
+
+ if privatetrans:
+ session.close()
+
+ return ret
+
+__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
+
+ q = session.query(ContentFilename).filter_by(filename=filename)
+ if q.count() < 1:
+ cf = ContentFilename()
+ cf.filename = filename
+ session.add(cf)
+ if privatetrans:
+ session.commit()
+ else:
+ session.flush()
+ ret = cf.cafilename_id
+ else:
+ ret = q.one().cafilename_id
+
+ if privatetrans:
+ session.close()
+
+ return ret
+
+__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)
+ """
+
+ privatetrans = False
+ if session is None:
+ session = DBConn().session()
+ privatetrans = True
+
+ # 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"
+
+ ret = session.execute(contents_q, vals)
+
+ if privatetrans:
+ session.close()
+
+ return ret
+
+__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=None):
+ """
+ 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
+
+ q = session.query(ContentFilepath).filter_by(filepath=filepath)
+ if q.count() < 1:
+ cf = ContentFilepath()
+ cf.filepath = filepath
+ session.add(cf)
+ if privatetrans:
+ session.commit()
+ else:
+ session.flush()
+ ret = cf.cafilepath_id
+ else:
+ ret = q.one().cafilepath_id
+
+ if privatetrans:
+ session.close()
+
+ return ret
+
+__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:
+ # Insert paths
+ pathcache = {}
+ for fullpath in fullpaths:
+ # Get the necessary IDs ...
+ (path, file) = os.path.split(fullpath)
+
+ filepath_id = get_or_set_contents_path_id(path, session)
+ filename_id = get_or_set_contents_file_id(file, session)
+
+ pathcache[fullpath] = (filepath_id, filename_id)
+
+ for fullpath, dat in pathcache.items():
+ ca = ContentAssociation()
+ ca.binary_id = binary_id
+ ca.filepath_id = dat[0]
+ ca.filename_id = dat[1]
+ session.add(ca)
+
+ # Only commit if we set up the session ourself
+ if privatetrans:
+ session.commit()
+ session.close()
+ else:
+ session.flush()
+
+ return True
+
+ except:
+ traceback.print_exc()
+
+ # Only rollback if we set up the session ourself
+ if privatetrans:
+ session.rollback()
+ session.close()
+
+ 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
+ """
+
+ privatetrans = False
+ if session is None:
+ session = DBConn().session()
+ privatetrans = True
+
+ 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)
+
+ ret = q.all()
+
+ if privatetrans:
+ session.close()
+
+ return ret
+
+__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
+ """
+
+ privatetrans = False
+ if session is None:
+ session = DBConn().session()
+ privatetrans = True
+
+ q = session.query(PoolFile).filter_by(filename=filename)
+ q = q.join(Location).filter_by(location_id=location_id)
+
+ ret = None
+
+ if q.count() > 1:
+ ret = (None, None)
+ elif q.count() < 1:
+ ret = (False, None)
+ else:
+ obj = q.one()
+ if obj.md5sum != md5sum or obj.filesize != filesize:
+ ret = (False, obj)
+
+ if ret is None:
+ ret = (True, obj)
+
+ if privatetrans:
+ session.close()
+
+ return ret
+
+__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
+ """
+
+ privatetrans = False
+ if session is None:
+ session = DBConn().session()
+ privatetrans = True
+
+ q = session.query(PoolFile).filter_by(file_id=file_id)
+
+ if q.count() > 0:
+ ret = q.one()
+ else:
+ ret = None
+
+ if privatetrans:
+ session.close()
+
+ return ret
+
+__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
+ """
+
+ privatetrans = False
+ if session is None:
+ session = DBConn().session()
+ privatetrans = True
+
+ q = session.query(PoolFile).filter_by(filename=filename)
+
+ if location_id is not None:
+ q = q.join(Location).filter_by(location_id=location_id)
+
+ ret = q.all()
+
+ if privatetrans:
+ session.close()
+
+ return ret
+
+__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
+ """
+
+ privatetrans = False
+ if session is None:
+ session = DBConn().session()
+ privatetrans = True
+
+ # TODO: There must be a way of properly using bind parameters with %FOO%
+ q = session.query(PoolFile).filter(PoolFile.filename.like('%%%s%%' % filename))
+
+ ret = q.all()
+
+ if privatetrans:
+ session.close()
+
+ return ret
+
+__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
+
+ 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()
+ ret = fingerprint
+ else:
+ ret = q.one()
+
+ if privatetrans:
+ session.close()
+
+ return ret
+
+__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
+ """
+
+ privatetrans = False
+ if session is None:
+ session = DBConn().session()
+ privatetrans = True
+
+ 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:
+ ret = None
+ else:
+ ret = q.one()
+
+ if privatetrans:
+ session.close()
+
+ return ret
+
+__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
+
+ 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()
+ ret = maintainer
+ else:
+ ret = q.one()
+
+ if privatetrans:
+ session.close()
+
+ return ret
+
+__all__.append('get_or_set_maintainer')
+
+def get_maintainer(maintainer_id, session=None):
+ """
+ Return the name of the maintainer behind C{maintainer_id} or None if that
+ maintainer_id is invalid.
+
+ @type maintainer_id: int
+ @param maintainer_id: the id of the maintainer
+
+ @rtype: Maintainer
+ @return: the Maintainer with this C{maintainer_id}
+ """
+
+ privatetrans = False
+ if session is None:
+ session = DBConn().session()
+ privatetrans = True
+
+ try:
+ return session.query(Maintainer).get(maintainer_id)
+ finally:
+ if privatetrans:
+ session.close()
+
+__all__.append('get_maintainer')
+
+################################################################################
+
+class NewComment(object):
+ def __init__(self, *args, **kwargs):
+ pass
+
+ def __repr__(self):
+ return '''<NewComment for '%s %s' (%s)>''' % (self.package, self.version, self.comment_id)
+
+__all__.append('NewComment')
+
+def has_new_comment(package, version, session=None):
+ """
+ Returns true if the given combination of C{package}, C{version} has a comment.
+
+ @type package: string
+ @param package: name of the package
+
+ @type version: string
+ @param version: package version
+
+ @type session: Session
+ @param session: Optional SQLA session object (a temporary one will be
+ generated if not supplied)
+
+ @rtype: boolean
+ @return: true/false
+ """
+
+ privatetrans = False
+ if session is None:
+ session = DBConn().session()
+ privatetrans = True
+
+ q = session.query(NewComment)
+ q = q.filter_by(package=package)
+ q = q.filter_by(version=version)
+
+ ret = q.count() > 0
+
+ if privatetrans:
+ session.close()
+
+ return ret
+
+__all__.append('has_new_comment')
+
+def get_new_comments(package=None, version=None, comment_id=None, session=None):
+ """
+ Returns (possibly empty) list of NewComment objects for the given
+ parameters
+
+ @type package: string (optional)
+ @param package: name of the package
+
+ @type version: string (optional)
+ @param version: package version
+
+ @type comment_id: int (optional)
+ @param comment_id: An id of a comment
+
+ @type session: Session
+ @param session: Optional SQLA session object (a temporary one will be
+ generated if not supplied)
+
+ @rtype: list
+ @return: A (possibly empty) list of NewComment objects will be returned
+
+ """
+
+ privatetrans = False
+ if session is None:
+ session = DBConn().session()
+ privatetrans = True
+
+ q = session.query(NewComment)
+ if package is not None: q = q.filter_by(package=package)
+ if version is not None: q = q.filter_by(version=version)
+ if comment_id is not None: q = q.filter_by(comment_id=comment_id)
+
+ ret = q.all()
+
+ if privatetrans:
+ session.close()
+
+ return ret
+
+__all__.append('get_new_comments')
+
+################################################################################
+
+class Override(object):
+ def __init__(self, *args, **kwargs):
+ pass
+
+ def __repr__(self):
+ return '<Override %s (%s)>' % (self.package, self.suite_id)
+
+__all__.append('Override')
+
+def get_override(package, suite=None, component=None, overridetype=None, session=None):
+ """
+ Returns Override object for the given parameters
+
+ @type package: string
+ @param package: The name of the package
+
+ @type suite: string, list or None
+ @param suite: The name of the suite (or suites if a list) to limit to. If
+ None, don't limit. Defaults to None.
+
+ @type component: string, list or None
+ @param component: The name of the component (or components if a list) to
+ limit to. If None, don't limit. Defaults to None.
+
+ @type overridetype: string, list or None
+ @param overridetype: The name of the overridetype (or overridetypes if a list) to
+ limit to. If None, don't limit. Defaults to None.
+
+ @type session: Session
+ @param session: Optional SQLA session object (a temporary one will be
+ generated if not supplied)
+
+ @rtype: list
+ @return: A (possibly empty) list of Override objects will be returned
+
+ """
+ privatetrans = False
+ if session is None:
+ session = DBConn().session()
+ privatetrans = True
+
+ q = session.query(Override)
+ q = q.filter_by(package=package)
+
+ if suite is not None:
+ if not isinstance(suite, list): suite = [suite]
+ q = q.join(Suite).filter(Suite.suite_name.in_(suite))
+
+ if component is not None:
+ if not isinstance(component, list): component = [component]
+ q = q.join(Component).filter(Component.component_name.in_(component))
+
+ if overridetype is not None:
+ if not isinstance(overridetype, list): overridetype = [overridetype]
+ q = q.join(OverrideType).filter(OverrideType.overridetype.in_(overridetype))
+
+ ret = q.all()
+
+ if privatetrans:
+ session.close()
+
+ return ret
+
+__all__.append('get_override')
+