Skip to content

Commit

Permalink
ltmpl: Filter out other arches, clean up naming
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bcl committed Dec 11, 2023
1 parent a88393b commit 314b484
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
33 changes: 18 additions & 15 deletions src/pylorax/ltmpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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)
6 changes: 4 additions & 2 deletions src/pylorax/treebuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 []
Expand Down Expand Up @@ -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 {}
Expand Down
3 changes: 2 additions & 1 deletion tests/pylorax/test_ltmpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 314b484

Please sign in to comment.