From: Torsten Werner Date: Wed, 26 Jan 2011 23:07:48 +0000 (+0100) Subject: Test and refactor get_component_by_package_suite(). X-Git-Url: https://git.decadent.org.uk/gitweb/?a=commitdiff_plain;h=d5e281a9a28d2b2762fc8636863035538cc5703d;p=dak.git Test and refactor get_component_by_package_suite(). Signed-off-by: Torsten Werner --- diff --git a/dak/examine_package.py b/dak/examine_package.py index 0e1b34ff..0d98497e 100755 --- a/dak/examine_package.py +++ b/dak/examine_package.py @@ -60,7 +60,7 @@ import commands import threading from daklib import utils -from daklib.dbconn import DBConn, get_binary_from_name_suite +from daklib.dbconn import DBConn, get_component_by_package_suite from daklib.regexes import html_escaping, re_html_escaping, re_version, re_spacestrip, \ re_contrib, re_nonfree, re_localhost, re_newlinespace, \ re_package, re_doc_directory @@ -325,9 +325,9 @@ def read_changes_or_dsc (suite, filename, session = None): def create_depends_string (suite, depends_tree, session = None): result = "" if suite == 'experimental': - suite_where = "in ('experimental','unstable')" + suite_list = ['experimental','unstable'] else: - suite_where = "= '%s'" % suite + suite_list = [suite] comma_count = 1 for l in depends_tree: @@ -339,17 +339,15 @@ def create_depends_string (suite, depends_tree, session = None): result += " | " # doesn't do version lookup yet. - res = get_binary_from_name_suite(d['name'], suite_where, session) - if res.rowcount > 0: - i = res.fetchone() - + component = get_component_by_package_suite(d['name'], suite_list, session) + if component is not None: adepends = d['name'] if d['version'] != '' : adepends += " (%s)" % (d['version']) - if i[2] == "contrib": + if component == "contrib": result += colour_output(adepends, "contrib") - elif i[2] == "non-free": + elif component == "non-free": result += colour_output(adepends, "nonfree") else : result += colour_output(adepends, "main") diff --git a/daklib/dbconn.py b/daklib/dbconn.py index e1e21d2b..97b76e0a 100755 --- a/daklib/dbconn.py +++ b/daklib/dbconn.py @@ -472,24 +472,16 @@ def get_suites_binary_in(package, session=None): __all__.append('get_suites_binary_in') @session_wrapper -def get_binary_from_name_suite(package, suitename, session=None): +def get_component_by_package_suite(package, suite_list, session=None): ### For dak examine-package - ### XXX: Doesn't use object API yet - - 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 - WHERE b.package='%(package)s' - AND b.file = fi.id - AND fi.location = l.id - AND l.component = c.id - AND ba.bin=b.id - AND ba.suite = su.id - AND su.suite_name %(suitename)s - ORDER BY b.version DESC""" - - return session.execute(sql % {'package': package, 'suitename': suitename}) - -__all__.append('get_binary_from_name_suite') + + return session.query(Component.component_name). \ + join(Component.location, Location.files, PoolFile.binary). \ + filter_by(package = package). \ + join(DBBinary.suites).filter(Suite.suite_name.in_(suite_list)). \ + limit(1).scalar() + +__all__.append('get_component_by_package_suite') @session_wrapper def get_binary_components(package, suitename, arch, session=None): diff --git a/tests/dbtest_packages.py b/tests/dbtest_packages.py index 12ab5960..7015c7fb 100755 --- a/tests/dbtest_packages.py +++ b/tests/dbtest_packages.py @@ -6,7 +6,8 @@ from daklib.dbconn import Architecture, Suite, get_suite_architectures, \ get_architecture_suites, Maintainer, DBSource, Location, PoolFile, \ check_poolfile, get_poolfile_like_name, get_source_in_suite, \ get_suites_source_in, add_dsc_to_db, source_exists, DBBinary, \ - get_suites_binary_in, add_deb_to_db, Component + get_suites_binary_in, add_deb_to_db, Component, \ + get_component_by_package_suite from daklib.queue_install import package_to_suite from daklib.queue import get_newest_source, get_suite_version_by_source, \ get_source_by_package_and_suite, get_suite_version_by_package @@ -523,5 +524,15 @@ class PackageTestCase(DBDakTestCase): self.assertEqual(self.loc['main'], self.comp['main'].location) self.assertEqual(self.loc['contrib'], self.comp['contrib'].location) + def test_get_component_by_package_suite(self): + 'test get_component_by_package_suite()' + + result = get_component_by_package_suite('hello', ['sid'], self.session) + self.assertEqual('main', result) + result = get_component_by_package_suite('hello', ['hamm'], self.session) + self.assertEqual(None, result) + result = get_component_by_package_suite('foobar', ['sid'], self.session) + self.assertEqual(None, result) + if __name__ == '__main__': unittest.main()