anvil/tools/yumfind
Joshua Harlow 3a2e9e0f72 Show the packages found when installing at DEBUG level.
When attempting to ensure all desired packages at the
right versions are still findable not only print out
that we could find them, but also DEBUG the
information retrieved about the package, which will be
useful in cases where someone wants to look at the debug
output to verify whats going to be installed.

Change-Id: I78de10091740d7c383f415889eaa3aeaad27dcf8
2013-08-06 19:23:43 -07:00

108 lines
3.4 KiB
Python
Executable File

#!/usr/bin/python
# 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 Library 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# Parts Copyright 2007 Red Hat, Inc
import warnings
warnings.filterwarnings("ignore")
from optparse import OptionParser
import collections
import json
import pkg_resources
from yum import YumBase
def format_rpm(pkg):
details = {
'arch': pkg.arch,
'epoch': pkg.epoch,
'name': pkg.name,
'release': pkg.release,
'version': pkg.version,
}
if hasattr(pkg, 'repo') and pkg.repo:
details['repo'] = pkg.repo.name
return details
def parse_py_req(text):
if not text:
return None
return pkg_resources.Requirement.parse(text)
def find_matches(base, rpm_name, py_req=None):
rpms = base.doPackageLists(patterns=[rpm_name], ignore_case=True,
showdups=True)
all_rpms = []
all_rpms.extend(rpms.available)
all_rpms.extend(rpms.installed)
all_rpms.extend(rpms.extras)
all_rpms.extend(rpms.reinstall_available)
yum_map = collections.defaultdict(list)
for rpm in all_rpms:
for provides in rpm.provides:
yum_map[provides[0]].append((rpm.version, rpm))
rpm_matches = []
for (version, rpm) in yum_map.get(rpm_name, []):
if py_req is None or version in py_req:
rpm_matches.append(rpm)
return rpm_matches
if __name__ == '__main__':
parser = OptionParser()
parser.add_option("-p", dest="packages", action="append",
help="rpm-name,python-requirement(optional) to attempt to match",
metavar="RPM-REQ")
parser.add_option("-j", '--json', dest="is_json", action="store_true",
default=False,
help="output the package details in json"
" (which is easier to parse)")
(options, args) = parser.parse_args()
if not options.packages:
parser.error("Option 'packages' is required")
examine_what = []
for p in options.packages:
try:
(rpm_name, python_req) = p.split(",", 1)
rpm_name = rpm_name.strip()
python_req = python_req.strip()
examine_what.append((rpm_name, python_req))
except ValueError:
rpm_name = p.strip()
examine_what.append((rpm_name, None))
base = YumBase()
base.doConfigSetup(debuglevel=-1, errorlevel=-1)
for (rpm_name, py_req) in examine_what:
req = parse_py_req(py_req)
matches = find_matches(base, rpm_name, req)
if matches:
# Pick the newest match.
pkg = sorted(matches)[-1]
if options.is_json:
print(json.dumps(format_rpm(pkg)))
else:
print(pkg)
else:
# Nothing found.
print("")