From 2f2fec655e42702f28790bac3b5bdb89cdab3083 Mon Sep 17 00:00:00 2001 From: Adam Piasecki Date: Wed, 14 Jun 2023 14:23:29 +0100 Subject: [PATCH] scripts: Move download-overrides.py to coreos-assembler/main/src This script will be now used in cosa. It makes more sense to move it, to avoid fetching it from the automation repo. See: https://github.com/coreos/fedora-coreos-tracker/issues/1185 See: https://github.com/coreos/coreos-assembler/pull/3510 --- src/download-overrides.py | 71 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100755 src/download-overrides.py diff --git a/src/download-overrides.py b/src/download-overrides.py new file mode 100755 index 0000000000..98fe9b33b8 --- /dev/null +++ b/src/download-overrides.py @@ -0,0 +1,71 @@ +#!/usr/bin/python3 + +import dnf.subject +import hawkey +import os +import yaml +import subprocess +import sys + +arch = os.uname().machine + +# this was partially copied from coreos-koji-tagger +def get_rpminfo(string: str) -> str: + form = hawkey.FORM_NEVRA + + # get a hawkey.Subject object for the string + subject = dnf.subject.Subject(string) # returns hawkey.Subject + + # get a list of hawkey.NEVRA objects that are the possibilities + nevras = subject.get_nevra_possibilities(forms=form) + + # return the first hawkey.NEVRA item in the list of possibilities + rpminfo = nevras[0] + return rpminfo + +def is_override_lockfile(filename: str) -> bool: + return (filename == "manifest-lock.overrides.yaml" or + filename == f'manifest-lock.overrides.{arch}.yaml') + +def assert_epochs_match(overrides_epoch: int, rpmfile_epoch: str): + # normalize the input into a string + if overrides_epoch is None: + normalized_overrides_epoch = '(none)' # matches rpm -qp --queryformat='%{E}' + else: + normalized_overrides_epoch = str(overrides_epoch) + if normalized_overrides_epoch != rpmfile_epoch: + raise Exception(f"Epoch mismatch between downloaded rpm ({rpmfile_epoch})" + f" and overrides file entry ({overrides_epoch})") + +assert os.path.isdir("builds"), "Missing builds/ dir; is this a cosa workdir?" + +rpms = set() +os.makedirs('overrides/rpm', exist_ok=True) +for filename in os.listdir(os.path.join("src/config")): + if is_override_lockfile(filename): + with open(f'src/config/{filename}') as f: + lockfile = yaml.safe_load(f) + if lockfile is None or 'packages' not in lockfile: + continue + for pkg, pkgobj in lockfile['packages'].items(): + if 'evr' in pkgobj: + rpminfo = get_rpminfo(f"{pkg}-{pkgobj['evr']}.{arch}") + else: + rpminfo = get_rpminfo(f"{pkg}-{pkgobj['evra']}") + rpmnvra = f"{rpminfo.name}-{rpminfo.version}-{rpminfo.release}.{rpminfo.arch}" + rpms.add(rpmnvra) + subprocess.check_call(['koji', 'download-build', '--rpm', rpmnvra], cwd='overrides/rpm') + # Make sure the epoch matches what was in the overrides file + # otherwise we can get errors: https://github.com/coreos/fedora-coreos-config/pull/293 + cp = subprocess.run(['rpm', '-qp', '--queryformat', '%{E}', f'{rpmnvra}.rpm'], + check=True, + capture_output=True, + cwd='overrides/rpm') + rpmfile_epoch = cp.stdout.decode('utf-8') + assert_epochs_match(rpminfo.epoch, rpmfile_epoch) + +if not rpms: + print("No overrides; exiting.") +else: + for rpm in rpms: + print(f'Downloaded {rpm} to overrides dir')