From d47f735112080bb020731e620ac9549a1200e759 Mon Sep 17 00:00:00 2001 From: Christian Kellner Date: Thu, 1 Jul 2021 15:40:08 +0000 Subject: [PATCH] tools/mpp: create PkgInfo class for package info Instead of passing dictionaries around that are inconvenient to use in code and even more in the `mpp-format-*` directives, use a simple class to represent package information. Use that in the `pkginfo` dict that can be accessed via `mpp-format-*`. Use the `evra` property instead of string manipulation in the `fedora-boot.mpp.json` and `-ostree-bootiso.mpp.json` manifest. --- test/data/manifests/fedora-boot.mpp.json | 2 +- .../manifests/fedora-ostree-bootiso.mpp.json | 4 +- tools/osbuild-mpp | 54 +++++++++++++------ 3 files changed, 40 insertions(+), 20 deletions(-) diff --git a/test/data/manifests/fedora-boot.mpp.json b/test/data/manifests/fedora-boot.mpp.json index 71afac0e..a792f429 100644 --- a/test/data/manifests/fedora-boot.mpp.json +++ b/test/data/manifests/fedora-boot.mpp.json @@ -71,7 +71,7 @@ "kernel_opts": "ro biosdevname=0 net.ifnames=0", "legacy": "i386-pc", "saved_entry": { - "mpp-format-string": "{'f'*32}-{rpms['stages']['kernel-core'][12:]}" + "mpp-format-string": "{'f'*32}-{rpms['stages']['kernel-core'].evra}" } } }, diff --git a/test/data/manifests/fedora-ostree-bootiso.mpp.json b/test/data/manifests/fedora-ostree-bootiso.mpp.json index d9335e72..358caa30 100644 --- a/test/data/manifests/fedora-ostree-bootiso.mpp.json +++ b/test/data/manifests/fedora-ostree-bootiso.mpp.json @@ -341,7 +341,7 @@ "type": "org.osbuild.dracut", "options": { "kernel": [ - {"mpp-format-string": "{rpms['ostree-tree']['kernel'][7:-4]}" } + {"mpp-format-string": "{rpms['ostree-tree']['kernel'].evra}" } ], "add_modules": [ "anaconda", @@ -455,7 +455,7 @@ "version": "34" }, "isolabel": "Fedora-34-X86_64", - "kernel": {"mpp-format-string": "{rpms['ostree-tree']['kernel'][7:-4]}" }, + "kernel": {"mpp-format-string": "{rpms['ostree-tree']['kernel'].evra}" }, "efi": { "architectures": [ "IA32", diff --git a/tools/osbuild-mpp b/tools/osbuild-mpp index 3fb283ca..4f3fc168 100755 --- a/tools/osbuild-mpp +++ b/tools/osbuild-mpp @@ -144,6 +144,36 @@ def element_enter(element, key, default): return element[key] +class PkgInfo: + def __init__(self, checksum, name, evr, arch): + self.checksum = checksum + self.name = name + self.evr = evr + self.arch = arch + self.url = None + self.secrets = None + + @classmethod + def from_dnf_package(cls, pkg: dnf.package.Package): + checksum_type = hawkey.chksum_name(pkg.chksum[0]) + checksum_hex = pkg.chksum[1].hex() + + checksum = f"{checksum_type}:{checksum_hex}" + + return cls(checksum, pkg.name, pkg.evr, pkg.arch) + + @property + def evra(self): + return f"{self.evr}.{self.arch}" + + @property + def nevra(self): + return f"{self.name}-{self.evra}" + + def __str__(self): + return self.nevra + + class DepSolver: def __init__(self, cachedir, persistdir): self.cachedir = cachedir @@ -267,9 +297,6 @@ class DepSolver: if tsi.action not in dnf.transaction.FORWARD_ACTIONS: continue - checksum_type = hawkey.chksum_name(tsi.pkg.chksum[0]) - checksum_hex = tsi.pkg.chksum[1].hex() - path = tsi.pkg.relativepath reponame = tsi.pkg.reponame baseurl = self.base.repos[reponame].baseurl[0] # self.expand_baseurl(baseurls[reponame]) @@ -277,17 +304,11 @@ class DepSolver: # relative to `baseurl`. Strip any leading slashes, but ensure there's # exactly one between `baseurl` and the path. url = urllib.parse.urljoin(baseurl + "/", path.lstrip("/")) - secret = self.secrets.get(reponame) - pkg = { - "checksum": f"{checksum_type}:{checksum_hex}", - "name": tsi.pkg.name, - "nevra": str(tsi.pkg), - "url": url, - } + pkg = PkgInfo.from_dnf_package(tsi.pkg) + pkg.url = url + pkg.secrets = self.secrets.get(reponame) - if secret: - pkg["secrets"] = secret deps.append(pkg) return deps @@ -371,15 +392,14 @@ class ManifestFile: pkginfos = {} for dep in deps: - name, checksum, url = dep["name"], dep["checksum"], dep["url"] + name, checksum, url = dep.name, dep.checksum, dep.url - pkginfos[name] = dep["nevra"] + pkginfos[name] = dep - secretes = dep.get("secrets") - if secretes: + if dep.secrets: data = { "url": url, - "secrets": { "name": secretes } + "secrets": {"name": dep.secrets} } else: data = url