+ 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')
+
+@session_wrapper
+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
+ """
+
+ 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')
+
+@session_wrapper
+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
+ """
+
+ 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)
+
+ return ret
+
+__all__.append('check_poolfile')
+
+@session_wrapper
+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
+ """
+
+ q = session.query(PoolFile).filter_by(file_id=file_id)
+
+ try:
+ return q.one()
+ except NoResultFound:
+ return None
+
+__all__.append('get_poolfile_by_id')
+
+
+@session_wrapper
+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
+ """
+
+ 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')
+
+@session_wrapper
+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
+ """
+
+ # 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')
+
+@session_wrapper
+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
+ """
+
+ q = session.query(Fingerprint).filter_by(fingerprint=fpr)
+
+ try:
+ ret = q.one()
+ except NoResultFound:
+ fingerprint = Fingerprint()
+ fingerprint.fingerprint = fpr
+ session.add(fingerprint)
+ session.commit_or_flush()
+ ret = fingerprint
+
+ 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')
+
+@session_wrapper
+def get_or_set_keyring(keyring, session=None):
+ """
+ If C{keyring} does not have an entry in the C{keyrings} table yet, create one
+ and return the new Keyring
+ If C{keyring} already has an entry, simply return the existing Keyring
+
+ @type keyring: string
+ @param keyring: the keyring name
+
+ @rtype: Keyring
+ @return: the Keyring object for this keyring
+ """
+
+ q = session.query(Keyring).filter_by(keyring_name=keyring)
+
+ try:
+ return q.one()
+ except NoResultFound:
+ obj = Keyring(keyring_name=keyring)
+ session.add(obj)
+ session.commit_or_flush()
+ return obj
+
+__all__.append('get_or_set_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')
+
+@session_wrapper
+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
+ """
+
+ 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)
+
+ try:
+ return q.one()
+ except NoResultFound:
+ return None
+
+__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')
+
+@session_wrapper
+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
+ """
+
+ q = session.query(Maintainer).filter_by(name=name)
+ try:
+ ret = q.one()
+ except NoResultFound:
+ maintainer = Maintainer()
+ maintainer.name = name
+ session.add(maintainer)
+ session.commit_or_flush()
+ ret = maintainer
+
+ return ret
+
+__all__.append('get_or_set_maintainer')
+
+@session_wrapper
+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}
+ """
+
+ return session.query(Maintainer).get(maintainer_id)
+
+__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')
+
+@session_wrapper
+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
+ """
+
+ q = session.query(NewComment)
+ q = q.filter_by(package=package)
+ q = q.filter_by(version=version)
+
+ return bool(q.count() > 0)
+
+__all__.append('has_new_comment')
+
+@session_wrapper
+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
+ """
+
+ 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)
+
+ return q.all()
+
+__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')
+
+@session_wrapper
+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
+ """
+
+ 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))
+
+ return q.all()
+
+__all__.append('get_override')
+
+
+################################################################################
+
+class OverrideType(object):
+ def __init__(self, *args, **kwargs):
+ pass
+
+ def __repr__(self):
+ return '<OverrideType %s>' % self.overridetype
+
+__all__.append('OverrideType')
+
+@session_wrapper
+def get_override_type(override_type, session=None):
+ """
+ Returns OverrideType object for given C{override type}.
+
+ @type override_type: string
+ @param override_type: The name of the override type
+
+ @type session: Session
+ @param session: Optional SQLA session object (a temporary one will be
+ generated if not supplied)
+
+ @rtype: int
+ @return: the database id for the given override type
+ """
+
+ q = session.query(OverrideType).filter_by(overridetype=override_type)
+
+ try:
+ return q.one()
+ except NoResultFound:
+ return None
+
+__all__.append('get_override_type')
+
+################################################################################
+
+class PendingContentAssociation(object):
+ def __init__(self, *args, **kwargs):
+ pass
+
+ def __repr__(self):
+ return '<PendingContentAssociation %s>' % self.pca_id
+
+__all__.append('PendingContentAssociation')
+
+def insert_pending_content_paths(package, fullpaths, session=None):
+ """
+ Make sure given paths are temporarily associated with given
+ package
+
+ @type package: dict
+ @param package: the package to associate with should have been read in from the binary control file
+ @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
+
+ @return: True upon success, False if there is a problem
+ """
+
+ privatetrans = False
+
+ if session is None:
+ session = DBConn().session()
+ privatetrans = True
+
+ try:
+ arch = get_architecture(package['Architecture'], session)
+ arch_id = arch.arch_id
+
+ # Remove any already existing recorded files for this package
+ q = session.query(PendingContentAssociation)
+ q = q.filter_by(package=package['Package'])
+ q = q.filter_by(version=package['Version'])
+ q = q.filter_by(architecture=arch_id)
+ q.delete()
+
+ # Insert paths
+ pathcache = {}
+ for fullpath in fullpaths:
+ (path, file) = os.path.split(fullpath)
+
+ if path.startswith( "./" ):
+ path = path[2:]
+
+ 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():
+ pca = PendingContentAssociation()
+ pca.package = package['Package']
+ pca.version = package['Version']
+ pca.filepath_id = dat[0]
+ pca.filename_id = dat[1]
+ pca.architecture = arch_id
+ session.add(pca)
+
+ # Only commit if we set up the session ourself
+ if privatetrans:
+ session.commit()
+ session.close()
+ else:
+ session.flush()
+
+ return True
+ except Exception, e:
+ traceback.print_exc()
+
+ # Only rollback if we set up the session ourself
+ if privatetrans:
+ session.rollback()
+ session.close()
+
+ return False
+
+__all__.append('insert_pending_content_paths')
+
+################################################################################
+
+class Priority(object):
+ def __init__(self, *args, **kwargs):
+ pass
+
+ def __eq__(self, val):
+ if isinstance(val, str):
+ return (self.priority == val)
+ # This signals to use the normal comparison operator
+ return NotImplemented
+
+ def __ne__(self, val):
+ if isinstance(val, str):
+ return (self.priority != val)
+ # This signals to use the normal comparison operator
+ return NotImplemented
+
+ def __repr__(self):
+ return '<Priority %s (%s)>' % (self.priority, self.priority_id)
+
+__all__.append('Priority')
+
+@session_wrapper
+def get_priority(priority, session=None):
+ """
+ Returns Priority object for given C{priority name}.
+
+ @type priority: string
+ @param priority: The name of the priority
+
+ @type session: Session
+ @param session: Optional SQLA session object (a temporary one will be
+ generated if not supplied)
+
+ @rtype: Priority
+ @return: Priority object for the given priority
+ """
+
+ q = session.query(Priority).filter_by(priority=priority)
+
+ try:
+ return q.one()
+ except NoResultFound:
+ return None
+
+__all__.append('get_priority')
+
+@session_wrapper
+def get_priorities(session=None):
+ """
+ Returns dictionary of priority names -> id mappings
+
+ @type session: Session
+ @param session: Optional SQL session object (a temporary one will be
+ generated if not supplied)
+
+ @rtype: dictionary
+ @return: dictionary of priority names -> id mappings
+ """
+
+ ret = {}
+ q = session.query(Priority)
+ for x in q.all():
+ ret[x.priority] = x.priority_id
+
+ return ret
+
+__all__.append('get_priorities')
+
+################################################################################
+
+class Queue(object):
+ def __init__(self, *args, **kwargs):
+ pass
+
+ def __repr__(self):
+ return '<Queue %s>' % self.queue_name
+
+ def autobuild_upload(self, changes, srcpath, session=None):
+ """
+ Update queue_build database table used for incoming autobuild support.
+
+ @type changes: Changes
+ @param changes: changes object for the upload to process
+
+ @type srcpath: string
+ @param srcpath: path for the queue file entries/link destinations
+
+ @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.
+
+ @rtype: NoneType or string
+ @return: None if the operation failed, a string describing the error if not
+ """
+
+ privatetrans = False
+ if session is None:
+ session = DBConn().session()
+ privatetrans = True
+
+ # TODO: Remove by moving queue config into the database
+ conf = Config()
+
+ for suitename in changes.changes["distribution"].keys():
+ # TODO: Move into database as:
+ # buildqueuedir TEXT DEFAULT NULL (i.e. NULL is no build)
+ # buildqueuecopy BOOLEAN NOT NULL DEFAULT FALSE (i.e. default is symlink)
+ # This also gets rid of the SecurityQueueBuild hack below
+ if suitename not in conf.ValueList("Dinstall::QueueBuildSuites"):
+ continue
+
+ # Find suite object
+ s = get_suite(suitename, session)
+ if s is None:
+ return "INTERNAL ERROR: Could not find suite %s" % suitename
+
+ # TODO: Get from database as above
+ dest_dir = conf["Dir::QueueBuild"]
+
+ # TODO: Move into database as above
+ if conf.FindB("Dinstall::SecurityQueueBuild"):
+ dest_dir = os.path.join(dest_dir, suitename)
+
+ for file_entry in changes.files.keys():
+ src = os.path.join(srcpath, file_entry)
+ dest = os.path.join(dest_dir, file_entry)
+
+ # TODO: Move into database as above
+ if conf.FindB("Dinstall::SecurityQueueBuild"):
+ # Copy it since the original won't be readable by www-data
+ import utils
+ utils.copy(src, dest)
+ else:
+ # Create a symlink to it
+ os.symlink(src, dest)
+
+ qb = QueueBuild()
+ qb.suite_id = s.suite_id
+ qb.queue_id = self.queue_id
+ qb.filename = dest
+ qb.in_queue = True
+
+ session.add(qb)
+
+ # If the .orig tarballs are in the pool, create a symlink to
+ # them (if one doesn't already exist)
+ for dsc_file in changes.dsc_files.keys():
+ # Skip all files except orig tarballs
+ from daklib.regexes import re_is_orig_source
+ if not re_is_orig_source.match(dsc_file):
+ continue
+ # Skip orig files not identified in the pool
+ if not (changes.orig_files.has_key(dsc_file) and
+ changes.orig_files[dsc_file].has_key("id")):
+ continue
+ orig_file_id = changes.orig_files[dsc_file]["id"]
+ dest = os.path.join(dest_dir, dsc_file)
+
+ # If it doesn't exist, create a symlink
+ if not os.path.exists(dest):
+ q = session.execute("SELECT l.path, f.filename FROM location l, files f WHERE f.id = :id and f.location = l.id",
+ {'id': orig_file_id})
+ res = q.fetchone()
+ if not res:
+ return "[INTERNAL ERROR] Couldn't find id %s in files table." % (orig_file_id)
+
+ src = os.path.join(res[0], res[1])
+ os.symlink(src, dest)
+
+ # Add it to the list of packages for later processing by apt-ftparchive
+ qb = QueueBuild()
+ qb.suite_id = s.suite_id
+ qb.queue_id = self.queue_id
+ qb.filename = dest
+ qb.in_queue = True
+ session.add(qb)
+
+ # If it does, update things to ensure it's not removed prematurely
+ else:
+ qb = get_queue_build(dest, s.suite_id, session)
+ if qb is None:
+ qb.in_queue = True
+ qb.last_used = None
+ session.add(qb)
+
+ if privatetrans:
+ session.commit()
+ session.close()
+
+ return None
+
+__all__.append('Queue')
+
+@session_wrapper
+def get_or_set_queue(queuename, session=None):
+ """
+ Returns Queue object for given C{queue name}, creating it if it does not
+ exist.
+
+ @type queuename: string
+ @param queuename: The name of the queue
+
+ @type session: Session
+ @param session: Optional SQLA session object (a temporary one will be
+ generated if not supplied)
+
+ @rtype: Queue
+ @return: Queue object for the given queue
+ """
+
+ q = session.query(Queue).filter_by(queue_name=queuename)
+
+ try:
+ ret = q.one()
+ except NoResultFound:
+ queue = Queue()
+ queue.queue_name = queuename
+ session.add(queue)
+ session.commit_or_flush()
+ ret = queue
+
+ return ret
+
+__all__.append('get_or_set_queue')
+
+################################################################################
+
+class QueueBuild(object):
+ def __init__(self, *args, **kwargs):
+ pass
+
+ def __repr__(self):
+ return '<QueueBuild %s (%s)>' % (self.filename, self.queue_id)
+
+__all__.append('QueueBuild')
+
+@session_wrapper
+def get_queue_build(filename, suite, session=None):
+ """
+ Returns QueueBuild object for given C{filename} and C{suite}.
+
+ @type filename: string
+ @param filename: The name of the file
+
+ @type suiteid: int or str
+ @param suiteid: Suite name or ID
+
+ @type session: Session
+ @param session: Optional SQLA session object (a temporary one will be
+ generated if not supplied)
+
+ @rtype: Queue
+ @return: Queue object for the given queue
+ """
+
+ if isinstance(suite, int):
+ q = session.query(QueueBuild).filter_by(filename=filename).filter_by(suite_id=suite)
+ else:
+ q = session.query(QueueBuild).filter_by(filename=filename)
+ q = q.join(Suite).filter_by(suite_name=suite)
+
+ try:
+ return q.one()
+ except NoResultFound:
+ return None
+
+__all__.append('get_queue_build')
+
+################################################################################
+
+class Section(object):
+ def __init__(self, *args, **kwargs):
+ pass
+
+ def __eq__(self, val):
+ if isinstance(val, str):
+ return (self.section == val)
+ # This signals to use the normal comparison operator
+ return NotImplemented
+
+ def __ne__(self, val):
+ if isinstance(val, str):
+ return (self.section != val)
+ # This signals to use the normal comparison operator
+ return NotImplemented
+
+ def __repr__(self):