diff --git a/ognibuild/debian/file_search.py b/ognibuild/debian/file_search.py index ca9aa00..8f493df 100644 --- a/ognibuild/debian/file_search.py +++ b/ognibuild/debian/file_search.py @@ -280,8 +280,20 @@ def get_package_for_paths( logging.warning( "More than 1 packages found that contain %r: %r", path, candidates ) - # Euhr. Pick the one with the shortest name? - return sorted(candidates, key=len)[0] + try: + from .udd import UDD + except ModuleNotFoundError: + logging.warning('Unable to import UDD, not ranking by popcon') + return sorted(candidates, key=len)[0] + udd = UDD() + udd.connect() + winner = udd.get_most_popular(candidates) + if winner is None: + logging.warning( + 'No relevant popcon information found, not ranking by popcon') + return sorted(candidates, key=len)[0] + logging.info('Picked winner using popcon') + return winner else: return candidates.pop() diff --git a/ognibuild/debian/udd.py b/ognibuild/debian/udd.py new file mode 100644 index 0000000..e45a432 --- /dev/null +++ b/ognibuild/debian/udd.py @@ -0,0 +1,39 @@ +#!/usr/bin/python3 +# Copyright (C) 2021 Jelmer Vernooij +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +"""Support for accessing UDD.""" + + +class UDD(object): + + def connect(self): + import psycopg2 + + self._conn = psycopg2.connect( + database="udd", + user="udd-mirror", + password="udd-mirror", + port=5432, + host="udd-mirror.debian.net", + ) + + def get_most_popular(self, packages): + cursor = self._conn.cursor() + cursor.execute( + 'SELECT package FROM popcon WHERE package IN %s ORDER BY insts DESC LIMIT 1', + (tuple(packages), )) + return cursor.fetchone()[0]