From 314b4841a812d2974836e39ae1d1c78626835712 Mon Sep 17 00:00:00 2001 From: "Brian C. Lane" Date: Thu, 7 Dec 2023 17:17:00 -0800 Subject: [PATCH] ltmpl: Filter out other arches, clean up naming Because of the way the template filter is implemented it was picking up i686 packages along with the x86_64 ones. This adds an arch filter that only includes the basearch packages and 'noarch'. Also clean up the naming in install_pkg --- src/pylorax/ltmpl.py | 33 ++++++++++++++++++--------------- src/pylorax/treebuilder.py | 6 ++++-- tests/pylorax/test_ltmpl.py | 3 ++- 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/pylorax/ltmpl.py b/src/pylorax/ltmpl.py index 37ffe1484..c38945254 100644 --- a/src/pylorax/ltmpl.py +++ b/src/pylorax/ltmpl.py @@ -124,7 +124,7 @@ class TemplateRunner(object): This class parses and executes Lorax templates. Sample usage: # install a bunch of packages - runner = LoraxTemplateRunner(inroot=rundir, outroot=rundir, dbo=dnf_obj) + runner = LoraxTemplateRunner(inroot=rundir, outroot=rundir, dbo=dnf_obj, basearch="x86_64") runner.run("install-packages.ltmpl") NOTES: @@ -250,6 +250,9 @@ def _pkgver(self, pkg_spec): else: raise RuntimeError(f"Unknown comparison '{pcv[1]}' operator") + # Filter out other arches, list should include basearch and noarch + query.filter_arch(self._filter_arches) + # MUST be added last. Otherwise it will only return the latest, not the latest of the # filtered results. query.filter_latest_evr() @@ -320,27 +323,23 @@ def installpkg(self, *pkgs): # the filtering is done the hard way. # Get the latest package, or package matching the selected version - ## TODO -- rename this, it is no longer names, it is package objects - pkgnames = self._pkgver(pkg) - if not pkgnames: -## XXX How does it translate exceptions? -# raise dnf.exceptions.PackageNotFoundError("no package matched", pkg) + pkgobjs = self._pkgver(pkg) + if not pkgobjs: raise RuntimeError(f"no package matched {pkg}") - # XXX DEBUG - I am seeing 2 entries for the same package name when the sack - # isn't setup correctly, might just leave this here... - if len(pkgnames) == 2 and pkgnames[0].get_nevra() == pkgnames[1].get_nevra(): + # Catch problems with sack setup, there should not be duplicate packages + if len(pkgobjs) == 2 and pkgobjs[0].get_nevra() == pkgobjs[1].get_nevra(): raise RuntimeError("Duplicate packages found in _pkgver request") # Apply excludes to the name only for exclude in excludes: - pkgnames = [p for p in pkgnames if not fnmatch.fnmatch(p.get_name(), exclude)] + pkgobjs = [p for p in pkgobjs if not fnmatch.fnmatch(p.get_name(), exclude)] # If the request is a glob, expand it in the log if any(g for g in ['*','?','.'] if g in pkg): - logger.info("installpkg: %s expands to %s", pkg, ",".join(p.get_nevra() for p in pkgnames)) + logger.info("installpkg: %s expands to %s", pkg, ",".join(p.get_nevra() for p in pkgobjs)) - for p in pkgnames: + for p in pkgobjs: try: self.goal.add_rpm_install(p) except Exception as e: # pylint: disable=broad-except @@ -363,7 +362,7 @@ class LoraxTemplateRunner(TemplateRunner, InstallpkgMixin): This class parses and executes Lorax templates. Sample usage: # install a bunch of packages - runner = LoraxTemplateRunner(inroot=rundir, outroot=rundir, dbo=dnf_obj) + runner = LoraxTemplateRunner(inroot=rundir, outroot=rundir, dbo=dnf_obj, basearch="x86_64") runner.run("install-packages.ltmpl") # modify a runtime dir @@ -390,7 +389,7 @@ class LoraxTemplateRunner(TemplateRunner, InstallpkgMixin): * Commands should raise exceptions for errors - don't use sys.exit() ''' def __init__(self, inroot, outroot, dbo=None, fatalerrors=True, - templatedir=None, defaults=None): + templatedir=None, defaults=None, basearch=None): self.inroot = inroot self.outroot = outroot self.dbo = dbo @@ -403,6 +402,11 @@ def __init__(self, inroot, outroot, dbo=None, fatalerrors=True, glob=lambda g: list(rglob(g, root=inroot))) self.results = DataHolder(treeinfo=dict()) # just treeinfo for now + # Setup arch filter for package query + self._filter_arches = ["noarch"] + if basearch: + self._filter_arches.append(basearch) + super(LoraxTemplateRunner, self).__init__(fatalerrors, templatedir, defaults, builtins) # TODO: set up custom logger with a filter to add line info @@ -980,5 +984,4 @@ def __init__(self, dbo, fatalerrors=True, templatedir=None, defaults=None): self.goal = dnf5.base.Goal(self.dbo) self.pkgs = [] self.pkgnames = [] - super(LiveTemplateRunner, self).__init__(fatalerrors, templatedir, defaults) diff --git a/src/pylorax/treebuilder.py b/src/pylorax/treebuilder.py index 736d46a17..5c045278c 100644 --- a/src/pylorax/treebuilder.py +++ b/src/pylorax/treebuilder.py @@ -84,7 +84,8 @@ def __init__(self, product, arch, dbo=None, templatedir=None, raise RuntimeError("No root directory passed to RuntimeBuilder") self._runner = LoraxTemplateRunner(inroot=root, outroot=root, - dbo=dbo, templatedir=templatedir) + dbo=dbo, templatedir=templatedir, + basearch=arch.basearch) self.add_templates = add_templates or [] self.add_template_vars = add_template_vars or {} self._installpkgs = installpkgs or [] @@ -280,7 +281,8 @@ def __init__(self, product, arch, inroot, outroot, runtime, isolabel, domacboot= isolabel=isolabel, udev=udev_escape, domacboot=domacboot, doupgrade=doupgrade, workdir=workdir, lower=string_lower, extra_boot_args=extra_boot_args) - self._runner = LoraxTemplateRunner(inroot, outroot, templatedir=templatedir) + self._runner = LoraxTemplateRunner(inroot, outroot, templatedir=templatedir, + basearch=arch.basearch) self._runner.defaults = self.vars self.add_templates = add_templates or [] self.add_template_vars = add_template_vars or {} diff --git a/tests/pylorax/test_ltmpl.py b/tests/pylorax/test_ltmpl.py index e99459bda..f73db04e2 100644 --- a/tests/pylorax/test_ltmpl.py +++ b/tests/pylorax/test_ltmpl.py @@ -154,7 +154,8 @@ def setUpClass(self): self.runner = LoraxTemplateRunner(inroot=self.root_dir, outroot=self.root_dir, dbo=self.dnfbase, - templatedir="./tests/pylorax/templates") + templatedir="./tests/pylorax/templates", + basearch="x86_64") @classmethod def tearDownClass(self):