X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=daklib%2Fdbconn.py;h=ca90ba8882bedaa6dc316d94efd44fb2c09086ae;hb=282d1ce6f56cb8ec1f37b6d9e5c2b9b856141a0e;hp=04afe7c4f352d473faf4c022a8156b4e6e59f9a4;hpb=1f7d2e0d967c3ae513c31c1ce3a1acd7db694909;p=dak.git diff --git a/daklib/dbconn.py b/daklib/dbconn.py index 04afe7c4..ca90ba88 100755 --- a/daklib/dbconn.py +++ b/daklib/dbconn.py @@ -59,21 +59,45 @@ __all__ = ['IntegrityError', 'SQLAlchemyError'] ################################################################################ def session_wrapper(fn): + """ + Wrapper around common ".., session=None):" handling. If the wrapped + function is called without passing 'session', we create a local one + and destroy it when the function ends. + + Also attaches a commit_or_flush method to the session; if we created a + local session, this is a synonym for session.commit(), otherwise it is a + synonym for session.flush(). + """ + def wrapped(*args, **kwargs): private_transaction = False - session = kwargs.get('session') - # No session specified as last argument or in kwargs, create one. - if session is None and len(args) == len(getargspec(fn)[0]) - 1: - private_transaction = True - kwargs['session'] = DBConn().session() + # Find the session object + try: + session = kwargs['session'] + except KeyError: + if len(args) <= len(getargspec(fn)[0]) - 1: + # No session specified as last argument or in kwargs + private_transaction = True + session = kwargs['session'] = DBConn().session() + else: + # Session is last argument in args + session = args[-1] + + if private_transaction: + session.commit_or_flush = session.commit + else: + session.commit_or_flush = session.flush try: return fn(*args, **kwargs) finally: if private_transaction: # We created a session; close it. - kwargs['session'].close() + session.close() + + wrapped.__doc__ = fn.__doc__ + wrapped.func_name = fn.func_name return wrapped @@ -165,7 +189,7 @@ __all__.append('Archive') @session_wrapper def get_archive(archive, session=None): """ - returns database id for given c{archive}. + returns database id for given C{archive}. @type archive: string @param archive: the name of the arhive @@ -223,9 +247,7 @@ def get_suites_binary_in(package, session=None): @return: list of Suite objects for the given package """ - ret = session.query(Suite).join(BinAssociation).join(DBBinary).filter_by(package=package).all() - - return ret + return session.query(Suite).join(BinAssociation).join(DBBinary).filter_by(package=package).all() __all__.append('get_suites_binary_in') @@ -308,9 +330,7 @@ def get_binaries_from_source_id(source_id, session=None): @return: list of DBBinary objects for the given name (may be empty) """ - ret = session.query(DBBinary).filter_by(source_id=source_id).all() - - return ret + return session.query(DBBinary).filter_by(source_id=source_id).all() __all__.append('get_binaries_from_source_id') @@ -330,9 +350,7 @@ def get_binary_from_name_suite(package, suitename, session=None): AND su.suite_name=:suitename ORDER BY b.version DESC""" - ret = session.execute(sql, {'package': package, 'suitename': suitename}) - - return ret + return session.execute(sql, {'package': package, 'suitename': suitename}) __all__.append('get_binary_from_name_suite') @@ -349,9 +367,7 @@ def get_binary_components(package, suitename, arch, session=None): vals = {'package': package, 'suitename': suitename, 'arch': arch} - ret = session.execute(query, vals) - - return ret + return session.execute(query, vals) __all__.append('get_binary_components') @@ -513,9 +529,7 @@ def get_contents(suite, overridetype, section=None, session=None): contents_q += " ORDER BY fn" - ret = session.execute(contents_q, vals) - - return ret + return session.execute(contents_q, vals) __all__.append('get_contents') @@ -686,9 +700,7 @@ def get_dscfiles(dscfile_id=None, source_id=None, poolfile_id=None, session=None if poolfile_id is not None: q = q.filter_by(poolfile_id=poolfile_id) - ret = q.all() - - return ret + return q.all() __all__.append('get_dscfiles') @@ -795,9 +807,7 @@ def get_poolfile_by_name(filename, location_id=None, session=None): if location_id is not None: q = q.join(Location).filter_by(location_id=location_id) - ret = q.all() - - return ret + return q.all() __all__.append('get_poolfile_by_name') @@ -816,9 +826,7 @@ def get_poolfile_like_name(filename, session=None): # 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() - - return ret + return q.all() __all__.append('get_poolfile_like_name') @@ -1088,9 +1096,7 @@ def has_new_comment(package, version, session=None): q = q.filter_by(package=package) q = q.filter_by(version=version) - ret = q.count() > 0 - - return ret + return bool(q.count() > 0) __all__.append('has_new_comment') @@ -1122,9 +1128,7 @@ def get_new_comments(package=None, version=None, comment_id=None, session=None): 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() - - return ret + return q.all() __all__.append('get_new_comments') @@ -1182,9 +1186,7 @@ def get_override(package, suite=None, component=None, overridetype=None, session if not isinstance(overridetype, list): overridetype = [overridetype] q = q.join(OverrideType).filter(OverrideType.overridetype.in_(overridetype)) - ret = q.all() - - return ret + return q.all() __all__.append('get_override') @@ -1223,8 +1225,6 @@ def get_override_type(override_type, session=None): except NoResultFound: return None - return ret - __all__.append('get_override_type') ################################################################################ @@ -1450,6 +1450,7 @@ class Queue(object): # 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 @@ -1737,9 +1738,7 @@ def get_suites_source_in(source, session=None): @return: list of Suite objects for the given source """ - ret = session.query(Suite).join(SrcAssociation).join(DBSource).filter_by(source=source).all() - - return ret + return session.query(Suite).join(SrcAssociation).join(DBSource).filter_by(source=source).all() __all__.append('get_suites_source_in') @@ -1774,9 +1773,7 @@ def get_sources_from_name(source, version=None, dm_upload_allowed=None, session= if dm_upload_allowed is not None: q = q.filter_by(dm_upload_allowed=dm_upload_allowed) - ret = q.all() - - return ret + return q.all() __all__.append('get_sources_from_name') @@ -1928,7 +1925,7 @@ def get_suite(suite, session=None): generated if not supplied) @rtype: Suite - @return: Suite object for the requested suite name (None if not presenT) + @return: Suite object for the requested suite name (None if not present) """ q = session.query(Suite).filter_by(suite_name=suite) @@ -1987,9 +1984,7 @@ def get_suite_architectures(suite, skipsrc=False, skipall=False, session=None): q = q.order_by('arch_string') - ret = q.all() - - return ret + return q.all() __all__.append('get_suite_architectures')