X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=daklib%2Fdbconn.py;h=fdfa96ca19e52d19c9199c4c7c11d82fc52b140e;hb=aa91d788fde273baf28aa39cda300cdae6f11d63;hp=b993d43fa1e2ead90c072ef318f06d8f163191d5;hpb=32e38e7021820d57d03deaa5f2a7d305771a00ec;p=dak.git diff --git a/daklib/dbconn.py b/daklib/dbconn.py index b993d43f..fdfa96ca 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): @@ -614,6 +649,76 @@ class PoolFile(object): __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 @@ -629,7 +734,7 @@ def get_poolfile_by_name(filename, location_id=None, session=None): @return: array of PoolFile objects """ - if session is not None: + if session is None: session = DBConn().session() q = session.query(PoolFile).filter_by(filename=filename) @@ -652,7 +757,7 @@ def get_poolfile_like_name(filename, session=None): @return: array of PoolFile objects """ - if session is not None: + if session is None: session = DBConn().session() # TODO: There must be a way of properly using bind parameters with %FOO% @@ -673,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): @@ -748,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): @@ -1698,6 +1889,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()