From 81fa48093a8d0aa5d61f652be29f3c74adc05524 Mon Sep 17 00:00:00 2001 From: Owen W. Taylor Date: May 16 2018 15:32:30 +0000 Subject: Use RPM metadata as the basis for summary and description If only one package is specified on the `fedmod rpm2module` command line, use the metadata from that RPM for the summary and description for the module. --- diff --git a/src/_fedmod/_depchase.py b/src/_fedmod/_depchase.py index dc69031..fa70363 100644 --- a/src/_fedmod/_depchase.py +++ b/src/_fedmod/_depchase.py @@ -228,10 +228,10 @@ def print_reldeps(pool, pkg): for dep in s.lookup_deparray(reltype): print("{}: {}".format(relstr, dep)) -def get_srpm_for_rpm(pool, pkg): +def _get_rpm(pool, pkg): sel = pool.select(pkg, solv.Selection.SELECTION_NAME | solv.Selection.SELECTION_DOTARCH) if sel.isempty(): - return f"unknown-component-{pkg}" + raise RuntimeError("Couldn't find package {}".format(pkg)) found = sel.solvables() num_results = len(found) s = None @@ -246,7 +246,10 @@ def get_srpm_for_rpm(pool, pkg): s = second if s is None: raise RuntimeError("More matching solvables were found, {}".format(found)) - s = found[0] + return found[0] + +def get_srpm_for_rpm(pool, pkg): + s = _get_rpm(pool, pkg) return get_sourcepkg(s, only_name=True) @@ -269,3 +272,11 @@ def get_rpms_for_srpms(pool, pkgnames): if p.lookup_sourcepkg() in sources: result.add(p.name) return result + +def get_rpm_metadata(pool, pkg): + s = _get_rpm(pool, pkg) + + return { + 'summary': s.lookup_str(solv.SOLVABLE_SUMMARY), + 'description': s.lookup_str(solv.SOLVABLE_DESCRIPTION), + } diff --git a/src/_fedmod/module_generator.py b/src/_fedmod/module_generator.py index e061786..f90c17c 100644 --- a/src/_fedmod/module_generator.py +++ b/src/_fedmod/module_generator.py @@ -1,4 +1,5 @@ import logging +import re from gi.repository import Modulemd @@ -8,6 +9,15 @@ def _name_only(rpm_name): name, version, release = rpm_name.rsplit("-", 2) return name +def _unwrap_description(desc): + def replace(m): + if len(m.group(0)) == 1: + return ' ' + else: + return m.group(0)[1:] + + return re.sub(r'\n+', replace, desc) + def _categorise_deps(all_rpm_deps): module_deps = set() remaining_rpm_deps = set() @@ -23,7 +33,6 @@ class ModuleGenerator(object): def __init__(self, pkgs): self.pkgs = pkgs - self.core_srpm = None self.mmd = Modulemd.Module(mdversion=2) self._pool = _depchase.make_pool("x86_64") @@ -40,12 +49,6 @@ class ModuleGenerator(object): self.module_run_deps = module_run_deps self.run_srpms = set() - def _set_core_srpm(self): - """Set core SRPM based on first listed package""" - pool = self._pool - pkg = self.pkgs[0] - self.core_srpm = _name_only(_depchase.get_srpm_for_rpm(pool, pkg)) - def _save_module_md(self, output_fname): """ Function writes modulemd to either stdout or the given filename @@ -66,12 +69,22 @@ class ModuleGenerator(object): """ self.mmd.props.module_licenses.add("MIT") - if self.core_srpm is not None: - self.mmd.props.summary = f"Generated module for {self.core_srpm}" + # FIXME: This and: + # https://fedoraproject.org/wiki/Module:Guidelines?rd=Fedora_Packaging_Guidelines_for_Modules + # + # should be improved so that it's clear what type of information should be in the + # resulting summary and description. + if len(self.pkgs) == 1: + # If we're generating a module for a single package, use the summary and + # description from the package. This isn't fully correct, but gives the + # module maintainer a starting place to edit from. + metadata = _depchase.get_rpm_metadata(self._pool, self.pkgs[0]) + self.mmd.props.summary = metadata['summary'].strip() + self.mmd.props.description = _unwrap_description(metadata['description'].strip()) else: self.mmd.props.summary = f"Generated module for {self.pkgs}" + self.mmd.props.description = "Module auto-generated by fedmod" - self.mmd.props.description = "Module auto-generated by fedmod" # Declare the public API for pkg in self.api_srpms: @@ -108,8 +121,6 @@ class ModuleGenerator(object): return 0 def run(self, output_fname): - if len(self.pkgs) == 1: - self._set_core_srpm() self._calculate_dependencies() self._update_module_md() self._save_module_md(output_fname) diff --git a/tests/test_module_generator.py b/tests/test_module_generator.py index cbe3003..1baca60 100644 --- a/tests/test_module_generator.py +++ b/tests/test_module_generator.py @@ -25,8 +25,8 @@ class TestSinglePackageInput(object): modmd = _generate_modulemd(input_rpms) # Expected description for 'grep' - assert modmd.props.summary == "Generated module for grep" - assert modmd.props.description == "Module auto-generated by fedmod" + assert modmd.props.summary == "Pattern matching utilities" + assert modmd.props.description.startswith("The GNU versions of commonly used grep utilities") # Expected licenses for 'grep' assert modmd.props.module_licenses.get() == ['MIT']