X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=daklib%2Fdbconn.py;h=43c0b6985e7ebf723423cce58f6a1c59d353bfe3;hb=652db9e053efe60d9e461def19a620fa980d2e74;hp=fca8e1e0ec3ed347bf17cbb5cb23f4207bd07b14;hpb=28d6d3160080ea84655f6e44834aa04567746e2e;p=dak.git diff --git a/daklib/dbconn.py b/daklib/dbconn.py index fca8e1e0..43c0b698 100755 --- a/daklib/dbconn.py +++ b/daklib/dbconn.py @@ -603,6 +603,41 @@ class DSCFile(object): __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): @@ -660,6 +695,29 @@ def check_poolfile(filename, filesize, md5sum, location_id, session=None): __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): """ @@ -720,6 +778,49 @@ class Fingerprint(object): __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): @@ -795,6 +896,49 @@ class Maintainer(object): __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') + ################################################################################ class NewComment(object): @@ -1246,15 +1390,15 @@ class QueueBuild(object): __all__.append('QueueBuild') -def get_queue_build(filename, suite_id, session=None): +def get_queue_build(filename, suite, session=None): """ - Returns QueueBuild object for given C{filename} and C{suite id}. + Returns QueueBuild object for given C{filename} and C{suite}. @type filename: string @param filename: The name of the file - @type suiteid: int - @param suiteid: Suite ID + @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 @@ -1266,7 +1410,12 @@ def get_queue_build(filename, suite_id, session=None): """ if session is None: session = DBConn().session() - q = session.query(QueueBuild).filter_by(filename=filename).filter_by(suite_id=suite_id) + 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) + if q.count() == 0: return None return q.one() @@ -1745,6 +1894,8 @@ def get_or_set_uid(uidname, session=None): session.add(uid) if privatetrans: session.commit() + else: + session.flush() return uid else: return q.one()