X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=daklib%2Fdbconn.py;h=f32a94bb91d550fa82886943a80c83d620ec613e;hb=4db4a1577f7971458d41966083b82e46d50b8a04;hp=5aa40210fc5b62684101b5bf995e4b14aec013cc;hpb=d5c3522f3b94dd7a1377bc6cd5848a3a733044fb;p=dak.git diff --git a/daklib/dbconn.py b/daklib/dbconn.py index 5aa40210..f32a94bb 100755 --- a/daklib/dbconn.py +++ b/daklib/dbconn.py @@ -417,6 +417,9 @@ def get_or_set_contents_file_id(filename, session=None): session.add(cf) if privatetrans: session.commit() + session.close() + else: + session.flush() return cf.cafilename_id else: return q.one().cafilename_id @@ -491,7 +494,7 @@ class ContentFilepath(object): __all__.append('ContentFilepath') -def get_or_set_contents_path_id(filepath, session): +def get_or_set_contents_path_id(filepath, session=None): """ Returns database id for given path. @@ -520,6 +523,9 @@ def get_or_set_contents_path_id(filepath, session): session.add(cf) if privatetrans: session.commit() + session.close() + else: + session.flush() return cf.cafilepath_id else: return q.one().cafilepath_id @@ -566,27 +572,40 @@ def insert_content_paths(binary_id, fullpaths, session=None): privatetrans = True try: + # Insert paths + pathcache = {} for fullpath in fullpaths: + # Get the necessary IDs ... (path, file) = os.path.split(fullpath) - # Get the necessary IDs ... + 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.filename_id = get_or_set_contents_file_id(file) - ca.filepath_id = get_or_set_contents_path_id(path) + 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 @@ -603,6 +622,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 +714,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): """ @@ -1098,23 +1175,32 @@ def insert_pending_content_paths(package, fullpaths, session=None): 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.filename_id = get_or_set_contents_file_id(file, session) - pca.filepath_id = get_or_set_contents_path_id(path, session) + 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() + else: + session.flush() return True except: @@ -1175,6 +1261,29 @@ def get_priority(priority, session=None): __all__.append('get_priority') +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 + """ + if session is None: + session = DBConn().session() + + 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): @@ -1238,7 +1347,7 @@ class Queue(object): dest = os.path.join(dest_dir, file_entry) # TODO: Move into database as above - if Cnf.FindB("Dinstall::SecurityQueueBuild"): + if conf.FindB("Dinstall::SecurityQueueBuild"): # Copy it since the original won't be readable by www-data utils.copy(src, dest) else: @@ -1332,15 +1441,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 @@ -1352,7 +1461,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() @@ -1406,6 +1520,29 @@ def get_section(section, session=None): __all__.append('get_section') +def get_sections(session=None): + """ + Returns dictionary of section 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 section names -> id mappings + """ + if session is None: + session = DBConn().session() + + ret = {} + q = session.query(Section) + for x in q.all(): + ret[x.section] = x.section_id + + return ret + +__all__.append('get_sections') + ################################################################################ class DBSource(object):