import psycopg2
import traceback
+from inspect import getargspec
+
from sqlalchemy import create_engine, Table, MetaData, select
from sqlalchemy.orm import sessionmaker, mapper, relation
################################################################################
+def session_wrapper(fn):
+ 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()
+
+ try:
+ return fn(*args, **kwargs)
+ finally:
+ if private_transaction:
+ # We created a session; close it.
+ kwargs['session'].close()
+
+ return wrapped
+
+################################################################################
+
class Architecture(object):
def __init__(self, *args, **kwargs):
pass
__all__.append('Architecture')
+@session_wrapper
def get_architecture(architecture, session=None):
"""
Returns database id for given C{architecture}.
@rtype: Architecture
@return: Architecture object for the given arch (None if not present)
-
"""
- privatetrans = False
-
- if session is None:
- session = DBConn().session()
- privatetrans = True
q = session.query(Architecture).filter_by(arch_string=architecture)
else:
ret = q.one()
- if privatetrans:
- session.close()
-
return ret
__all__.append('get_architecture')
+@session_wrapper
def get_architecture_suites(architecture, session=None):
"""
Returns list of Suite objects for given C{architecture} name
@rtype: list
@return: list of Suite objects for the given name (may be empty)
"""
- privatetrans = False
-
- if session is None:
- session = DBConn().session()
- privatetrans = True
q = session.query(Suite)
q = q.join(SuiteArchitecture)
ret = q.all()
- if privatetrans:
- session.close()
-
return ret
__all__.append('get_architecture_suites')
__all__.append('Archive')
+@session_wrapper
def get_archive(archive, session=None):
"""
returns database id for given c{archive}.
"""
archive = archive.lower()
- privatetrans = False
- if session is None:
- session = DBConn().session()
- privatetrans = True
-
q = session.query(Archive).filter_by(archive_name=archive)
if q.count() == 0:
else:
ret = q.one()
- if privatetrans:
- session.close()
-
return ret
__all__.append('get_archive')
__all__.append('DBBinary')
+@session_wrapper
def get_suites_binary_in(package, session=None):
"""
Returns list of Suite objects which given C{package} name is in
@return: list of Suite objects for the given package
"""
- privatetrans = False
- if session is None:
- session = DBConn().session()
- privatetrans = True
-
ret = session.query(Suite).join(BinAssociation).join(DBBinary).filter_by(package=package).all()
- session.close()
-
return ret
__all__.append('get_suites_binary_in')
+@session_wrapper
def get_binary_from_id(id, session=None):
"""
Returns DBBinary object for given C{id}
@rtype: DBBinary
@return: DBBinary object for the given binary (None if not present)
"""
- privatetrans = False
- if session is None:
- session = DBConn().session()
- privatetrans = True
q = session.query(DBBinary).filter_by(binary_id=id)
else:
ret = q.one()
- if privatetrans:
- session.close()
-
return ret
__all__.append('get_binary_from_id')
+@session_wrapper
def get_binaries_from_name(package, version=None, architecture=None, session=None):
"""
Returns list of DBBinary objects for given C{package} name
@rtype: list
@return: list of DBBinary objects for the given name (may be empty)
"""
- privatetrans = False
- if session is None:
- session = DBConn().session()
- privatetrans = True
q = session.query(DBBinary).filter_by(package=package)
ret = q.all()
- if privatetrans:
- session.close()
-
return ret
__all__.append('get_binaries_from_name')
+@session_wrapper
def get_binaries_from_source_id(source_id, session=None):
"""
Returns list of DBBinary objects for given C{source_id}
@rtype: list
@return: list of DBBinary objects for the given name (may be empty)
"""
- privatetrans = False
- if session is None:
- session = DBConn().session()
- privatetrans = True
ret = session.query(DBBinary).filter_by(source_id=source_id).all()
- if privatetrans:
- session.close()
-
return ret
-
__all__.append('get_binaries_from_source_id')
-
+@session_wrapper
def get_binary_from_name_suite(package, suitename, session=None):
### For dak examine-package
### XXX: Doesn't use object API yet
- privatetrans = False
- if session is None:
- session = DBConn().session()
- privatetrans = True
sql = """SELECT DISTINCT(b.package), b.version, c.name, su.suite_name
FROM binaries b, files fi, location l, component c, bin_associations ba, suite su
ret = session.execute(sql, {'package': package, 'suitename': suitename})
- if privatetrans:
- session.close()
-
return ret
__all__.append('get_binary_from_name_suite')
+@session_wrapper
def get_binary_components(package, suitename, arch, session=None):
# Check for packages that have moved from one component to another
query = """SELECT c.name FROM binaries b, bin_associations ba, suite s, location l, component c, architecture a, files f
vals = {'package': package, 'suitename': suitename, 'arch': arch}
- privatetrans = False
- if session is None:
- session = DBConn().session()
- privatetrans = True
-
ret = session.execute(query, vals)
- if privatetrans:
- session.close()
-
return ret
__all__.append('get_binary_components')
__all__.append('Component')
+@session_wrapper
def get_component(component, session=None):
"""
Returns database id for given C{component}.
"""
component = component.lower()
- privatetrans = False
- if session is None:
- session = DBConn().session()
- privatetrans = True
-
q = session.query(Component).filter_by(component_name=component)
if q.count() == 0:
else:
ret = q.one()
- if privatetrans:
- session.close()
-
return ret
__all__.append('get_component')
__all__.append('get_or_set_contents_file_id')
+@session_wrapper
def get_contents(suite, overridetype, section=None, session=None):
"""
Returns contents for a suite / overridetype combination, limiting
package, arch_id)
"""
- privatetrans = False
- if session is None:
- session = DBConn().session()
- privatetrans = True
-
# find me all of the contents for a given suite
contents_q = """SELECT (p.path||'/'||n.file) AS fn,
s.section,
ret = session.execute(contents_q, vals)
- if privatetrans:
- session.close()
-
return ret
__all__.append('get_contents')
__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
@return: Possibly empty list of DSCFiles
"""
- privatetrans = False
- if session is None:
- session = DBConn().session()
- privatetrans = True
-
q = session.query(DSCFile)
if dscfile_id is not None:
ret = q.all()
- if privatetrans:
- session.close()
-
return ret
__all__.append('get_dscfiles')
__all__.append('PoolFile')
+@session_wrapper
def check_poolfile(filename, filesize, md5sum, location_id, session=None):
"""
Returns a tuple:
(False, PoolFile object) if file found with size/md5sum mismatch
"""
- privatetrans = False
- if session is None:
- session = DBConn().session()
- privatetrans = True
-
q = session.query(PoolFile).filter_by(filename=filename)
q = q.join(Location).filter_by(location_id=location_id)
if ret is None:
ret = (True, obj)
- if privatetrans:
- session.close()
-
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
@return: either the PoolFile object or None
"""
- privatetrans = False
- if session is None:
- session = DBConn().session()
- privatetrans = True
-
q = session.query(PoolFile).filter_by(file_id=file_id)
if q.count() > 0:
else:
ret = None
- if privatetrans:
- session.close()
-
return ret
__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
@return: array of PoolFile objects
"""
- privatetrans = False
- if session is None:
- session = DBConn().session()
- privatetrans = True
-
q = session.query(PoolFile).filter_by(filename=filename)
if location_id is not None:
ret = q.all()
- if privatetrans:
- session.close()
-
return ret
__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
@return: array of PoolFile objects
"""
- privatetrans = False
- if session is None:
- session = DBConn().session()
- privatetrans = True
-
# 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()
- if privatetrans:
- session.close()
-
return ret
__all__.append('get_poolfile_like_name')
__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
@return: Either a Location object or None if one can't be found
"""
- privatetrans = False
- if session is None:
- session = DBConn().session()
- privatetrans = True
-
q = session.query(Location).filter_by(path=location)
if archive is not None:
else:
ret = q.one()
- if privatetrans:
- session.close()
-
return ret
__all__.append('get_location')
__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.
@return: true/false
"""
- privatetrans = False
- if session is None:
- session = DBConn().session()
- privatetrans = True
-
q = session.query(NewComment)
q = q.filter_by(package=package)
q = q.filter_by(version=version)
ret = q.count() > 0
- if privatetrans:
- session.close()
-
return ret
__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
@rtype: list
@return: A (possibly empty) list of NewComment objects will be returned
-
"""
- privatetrans = False
- if session is None:
- session = DBConn().session()
- privatetrans = True
-
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)
ret = q.all()
- if privatetrans:
- session.close()
-
return ret
__all__.append('get_new_comments')
__all__.append('Override')
+@session_wrapper
def get_override(package, suite=None, component=None, overridetype=None, session=None):
"""
Returns Override object for the given parameters
@rtype: list
@return: A (possibly empty) list of Override objects will be returned
-
"""
- privatetrans = False
- if session is None:
- session = DBConn().session()
- privatetrans = True
q = session.query(Override)
q = q.filter_by(package=package)
ret = q.all()
- if privatetrans:
- session.close()
-
return ret
__all__.append('get_override')
__all__.append('OverrideType')
+@session_wrapper
def get_override_type(override_type, session=None):
"""
Returns OverrideType object for given C{override type}.
@rtype: int
@return: the database id for the given override type
-
"""
- privatetrans = False
- if session is None:
- session = DBConn().session()
- privatetrans = True
q = session.query(OverrideType).filter_by(overridetype=override_type)
else:
ret = q.one()
- if privatetrans:
- session.close()
-
return ret
__all__.append('get_override_type')
__all__.append('Priority')
+@session_wrapper
def get_priority(priority, session=None):
"""
Returns Priority object for given C{priority name}.
@rtype: Priority
@return: Priority object for the given priority
-
"""
- privatetrans = False
- if session is None:
- session = DBConn().session()
- privatetrans = True
q = session.query(Priority).filter_by(priority=priority)
else:
ret = q.one()
- if privatetrans:
- session.close()
-
return ret
__all__.append('get_priority')
+@session_wrapper
def get_priorities(session=None):
"""
Returns dictionary of priority names -> id mappings
@rtype: dictionary
@return: dictionary of priority names -> id mappings
"""
- privatetrans = False
- if session is None:
- session = DBConn().session()
- privatetrans = True
ret = {}
q = session.query(Priority)
for x in q.all():
ret[x.priority] = x.priority_id
- if privatetrans:
- session.close()
-
return ret
__all__.append('get_priorities')
__all__.append('Queue')
+@session_wrapper
def get_queue(queuename, session=None):
"""
Returns Queue object for given C{queue name}.
@rtype: Queue
@return: Queue object for the given queue
-
"""
- privatetrans = False
- if session is None:
- session = DBConn().session()
- privatetrans = True
q = session.query(Queue).filter_by(queue_name=queuename)
if q.count() == 0:
else:
ret = q.one()
- if privatetrans:
- session.close()
-
return ret
__all__.append('get_queue')
__all__.append('QueueBuild')
+@session_wrapper
def get_queue_build(filename, suite, session=None):
"""
Returns QueueBuild object for given C{filename} and C{suite}.
@rtype: Queue
@return: Queue object for the given queue
-
"""
- privatetrans = False
- if session is None:
- session = DBConn().session()
- privatetrans = True
if isinstance(suite, int):
q = session.query(QueueBuild).filter_by(filename=filename).filter_by(suite_id=suite)
else:
ret = q.one()
- if privatetrans:
- session.close()
-
return ret
__all__.append('get_queue_build')
__all__.append('Section')
+@session_wrapper
def get_section(section, session=None):
"""
Returns Section object for given C{section name}.
@rtype: Section
@return: Section object for the given section name
-
"""
- privatetrans = False
- if session is None:
- session = DBConn().session()
- privatetrans = True
q = session.query(Section).filter_by(section=section)
if q.count() == 0:
else:
ret = q.one()
- if privatetrans:
- session.close()
-
return ret
__all__.append('get_section')
+@session_wrapper
def get_sections(session=None):
"""
Returns dictionary of section names -> id mappings
@rtype: dictionary
@return: dictionary of section names -> id mappings
"""
- privatetrans = False
- if session is None:
- session = DBConn().session()
- privatetrans = True
ret = {}
q = session.query(Section)
for x in q.all():
ret[x.section] = x.section_id
- if privatetrans:
- session.close()
-
return ret
__all__.append('get_sections')
__all__.append('DBSource')
+@session_wrapper
def source_exists(source, source_version, suites = ["any"], session=None):
"""
Ensure that source exists somewhere in the archive for the binary
"""
- privatetrans = False
- if session is None:
- session = DBConn().session()
- privatetrans = True
-
cnf = Config()
ret = 1
# No source found so return not ok
ret = 0
- if privatetrans:
- session.close()
-
return ret
__all__.append('source_exists')
+@session_wrapper
def get_suites_source_in(source, session=None):
"""
Returns list of Suite objects which given C{source} name is in
@return: list of Suite objects for the given source
"""
- privatetrans = False
- if session is None:
- session = DBConn().session()
- privatetrans = True
-
ret = session.query(Suite).join(SrcAssociation).join(DBSource).filter_by(source=source).all()
- if privatetrans:
- session.close()
-
return ret
__all__.append('get_suites_source_in')
+@session_wrapper
def get_sources_from_name(source, version=None, dm_upload_allowed=None, session=None):
"""
Returns list of DBSource objects for given C{source} name and other parameters
@rtype: list
@return: list of DBSource objects for the given name (may be empty)
"""
- privatetrans = False
- if session is None:
- session = DBConn().session()
- privatetrans = True
q = session.query(DBSource).filter_by(source=source)
ret = q.all()
- if privatetrans:
- session.close()
-
return ret
__all__.append('get_sources_from_name')
+@session_wrapper
def get_source_in_suite(source, suite, session=None):
"""
Returns list of DBSource objects for a combination of C{source} and C{suite}.
@return: the version for I{source} in I{suite}
"""
- privatetrans = False
- if session is None:
- session = DBConn().session()
- privatetrans = True
q = session.query(SrcAssociation)
q = q.join('source').filter_by(source=source)
# ???: Maybe we should just return the SrcAssociation object instead
ret = q.one().source
- if privatetrans:
- session.close()
-
return ret
__all__.append('get_source_in_suite')
__all__.append('Suite')
+@session_wrapper
def get_suite_architecture(suite, architecture, session=None):
"""
Returns a SuiteArchitecture object given C{suite} and ${arch} or None if it
@return: the SuiteArchitecture object or None
"""
- privatetrans = False
- if session is None:
- session = DBConn().session()
- privatetrans = True
-
q = session.query(SuiteArchitecture)
q = q.join(Architecture).filter_by(arch_string=architecture)
q = q.join(Suite).filter_by(suite_name=suite)
else:
ret = q.one()
- if privatetrans:
- session.close()
-
return ret
__all__.append('get_suite_architecture')
+@session_wrapper
def get_suite(suite, session=None):
"""
Returns Suite object for given C{suite name}.
@rtype: Suite
@return: Suite object for the requested suite name (None if not presenT)
-
"""
- privatetrans = False
- if session is None:
- session = DBConn().session()
- privatetrans = True
q = session.query(Suite).filter_by(suite_name=suite)
else:
ret = q.one()
- if privatetrans:
- session.close()
-
return ret
__all__.append('get_suite')
__all__.append('SuiteArchitecture')
+@session_wrapper
def get_suite_architectures(suite, skipsrc=False, skipall=False, session=None):
"""
Returns list of Architecture objects for given C{suite} name
@return: list of Architecture objects for the given name (may be empty)
"""
- privatetrans = False
- if session is None:
- session = DBConn().session()
- privatetrans = True
-
q = session.query(Architecture)
q = q.join(SuiteArchitecture)
q = q.join(Suite).filter_by(suite_name=suite)
ret = q.all()
- if privatetrans:
- session.close()
-
return ret
__all__.append('get_suite_architectures')
__all__.append('get_or_set_uid')
-
+@session_wrapper
def get_uid_from_fingerprint(fpr, session=None):
- privatetrans = False
- if session is None:
- session = DBConn().session()
- privatetrans = True
-
q = session.query(Uid)
q = q.join(Fingerprint).filter_by(fingerprint=fpr)
else:
ret = q.one()
- if privatetrans:
- session.close()
-
return ret
__all__.append('get_uid_from_fingerprint')