|
| 1 | +import logging |
| 2 | +import os |
| 3 | +import re |
| 4 | +import subprocess |
| 5 | +from typing import Iterable, Sequence |
| 6 | + |
| 7 | +XCPNG_YUMREPO_TMPL = """ |
| 8 | +[xcpng-{section}{suffix}] |
| 9 | +name=xcpng - {section}{suffix} |
| 10 | +baseurl=https://updates.xcp-ng.org/8/{version}/{section}/{rpmarch}/ |
| 11 | +gpgkey=https://xcp-ng.org/RPM-GPG-KEY-xcpng |
| 12 | +failovermethod=priority |
| 13 | +skip_if_unavailable=False |
| 14 | +""" |
| 15 | + |
| 16 | +def setup_xcpng_yum_repos(*, yum_repo_d: str, sections: Iterable[str], |
| 17 | + bin_arch: str | None, version: str) -> None: |
| 18 | + with open(os.path.join(yum_repo_d, "xcpng.repo"), "w") as yumrepoconf: |
| 19 | + for section in sections: |
| 20 | + # binaries |
| 21 | + block = XCPNG_YUMREPO_TMPL.format(rpmarch=bin_arch, |
| 22 | + section=section, |
| 23 | + version=version, |
| 24 | + suffix='', |
| 25 | + ) |
| 26 | + yumrepoconf.write(block) |
| 27 | + # sources |
| 28 | + block = XCPNG_YUMREPO_TMPL.format(rpmarch='Source', |
| 29 | + section=section, |
| 30 | + version=version, |
| 31 | + suffix='-src', |
| 32 | + ) |
| 33 | + yumrepoconf.write(block) |
| 34 | + |
| 35 | +DNF_BASE_CMD = None |
| 36 | +def dnf_setup(*, dnf_conf: str, yum_repo_d: str) -> None: |
| 37 | + global DNF_BASE_CMD |
| 38 | + DNF_BASE_CMD = ['dnf', '--quiet', |
| 39 | + '--releasever', 'WTF', |
| 40 | + '--config', dnf_conf, |
| 41 | + f'--setopt=reposdir={yum_repo_d}', |
| 42 | + ] |
| 43 | + |
| 44 | +BINRPM_SOURCE_CACHE: dict[str, str] = {} |
| 45 | +def rpm_source_package(rpmname: str) -> str: |
| 46 | + return BINRPM_SOURCE_CACHE[rpmname] |
| 47 | + |
| 48 | +def run_repoquery(args: list[str]) -> Sequence[str]: |
| 49 | + assert DNF_BASE_CMD is not None |
| 50 | + cmd = DNF_BASE_CMD + ['repoquery'] + args |
| 51 | + logging.debug('$ %s', ' '.join(cmd)) |
| 52 | + ret = subprocess.check_output(cmd, universal_newlines=True).strip().split() |
| 53 | + logging.debug('> %s', ret) |
| 54 | + return ret |
| 55 | + |
| 56 | +SRPM_BINRPMS_CACHE: dict[str, set[str]] = {} # binrpm-nevr -> srpm-nevr |
| 57 | +def fill_srpm_binrpms_cache() -> None: |
| 58 | + # HACK: get nevr for what dnf outputs as %{sourcerpm} |
| 59 | + logging.debug("get epoch info for SRPMs") |
| 60 | + args = [ |
| 61 | + '--disablerepo=*', '--enablerepo=*-src', '*', |
| 62 | + '--qf', '%{name}-%{version}-%{release}.src.rpm,%{name}-%{evr}', |
| 63 | + '--latest-limit=1', |
| 64 | + ] |
| 65 | + SRPM_NEVR_CACHE = { # sourcerpm -> srpm-nevr |
| 66 | + sourcerpm: nevr |
| 67 | + for sourcerpm, nevr in (line.split(',') |
| 68 | + for line in run_repoquery(args)) |
| 69 | + } |
| 70 | + |
| 71 | + # binary -> source mapping |
| 72 | + logging.debug("get binary to source mapping") |
| 73 | + global SRPM_BINRPMS_CACHE, BINRPM_SOURCE_CACHE |
| 74 | + args = [ |
| 75 | + '--disablerepo=*-src', '*', |
| 76 | + '--qf', '%{name}-%{evr},%{sourcerpm}', # FIXME no epoch in sourcerpm, why does it work? |
| 77 | + '--latest-limit=1', |
| 78 | + ] |
| 79 | + BINRPM_SOURCE_CACHE = { |
| 80 | + # packages without source are not in SRPM_NEVR_CACHE, fallback to sourcerpm |
| 81 | + binrpm: SRPM_NEVR_CACHE.get(sourcerpm, srpm_strip_src_rpm(sourcerpm)) |
| 82 | + for binrpm, sourcerpm in (line.split(',') |
| 83 | + for line in run_repoquery(args)) |
| 84 | + } |
| 85 | + |
| 86 | + # reverse mapping source -> binaries |
| 87 | + SRPM_BINRPMS_CACHE = {} |
| 88 | + for binrpm, srpm in BINRPM_SOURCE_CACHE.items(): |
| 89 | + binrpms = SRPM_BINRPMS_CACHE.get(srpm, set()) |
| 90 | + if not binrpms: |
| 91 | + SRPM_BINRPMS_CACHE[srpm] = binrpms |
| 92 | + binrpms.add(binrpm) |
| 93 | + |
| 94 | +def srpm_nevr(rpmname: str) -> str: |
| 95 | + args = [ |
| 96 | + '--disablerepo=*', '--enablerepo=*-src', |
| 97 | + '--qf=%{name}-%{evr}', # to get the epoch only when non-zero |
| 98 | + '--latest-limit=1', |
| 99 | + rpmname, |
| 100 | + ] |
| 101 | + ret = run_repoquery(args) |
| 102 | + assert ret, f"Found no SRPM named {rpmname}" |
| 103 | + assert len(ret) == 1 # ensured by --latest-limit=1 ? |
| 104 | + return ret[0] |
| 105 | + |
| 106 | +# dnf insists on spitting .src.rpm names it cannot take as input itself |
| 107 | +def srpm_strip_src_rpm(srpmname: str) -> str: |
| 108 | + SUFFIX = ".src.rpm" |
| 109 | + assert srpmname.endswith(SUFFIX), f"{srpmname} does not end in .src.rpm" |
| 110 | + nrv = srpmname[:-len(SUFFIX)] |
| 111 | + return nrv |
| 112 | + |
| 113 | +def rpm_requires(rpmname: str) -> Sequence[str]: |
| 114 | + args = [ |
| 115 | + '--disablerepo=*-src', # else requires of same-name SRPM are included |
| 116 | + '--qf=%{name}-%{evr}', # to avoid getting the arch and explicit zero epoch |
| 117 | + '--resolve', |
| 118 | + '--requires', rpmname, |
| 119 | + ] |
| 120 | + ret = run_repoquery(args) |
| 121 | + return ret |
| 122 | + |
| 123 | +def srpm_requires(srpmname: str) -> set[str]: |
| 124 | + args = [ |
| 125 | + '--qf=%{name}-%{evr}', # to avoid getting the arch |
| 126 | + '--resolve', |
| 127 | + '--requires', f"{srpmname}.src", |
| 128 | + ] |
| 129 | + ret = set(run_repoquery(args)) |
| 130 | + return ret |
| 131 | + |
| 132 | +def srpm_binrpms(srpmname: str) -> set[str]: |
| 133 | + ret = SRPM_BINRPMS_CACHE.get(srpmname, None) |
| 134 | + if ret is None: # FIXME should not happen |
| 135 | + logging.error("%r not found in cache", srpmname) |
| 136 | + assert False |
| 137 | + return [] |
| 138 | + logging.debug("binrpms for %s: %s", srpmname, ret) |
| 139 | + return ret |
| 140 | + |
| 141 | +UPSTREAM_REGEX = re.compile(r'\.el[0-9]+(_[0-9]+)?(\..*|)$') |
| 142 | +RPM_NVR_SPLIT_REGEX = re.compile(r'^(.+)-([^-]+)-([^-]+)$') |
| 143 | +def is_pristine_upstream(rpmname:str) -> bool: |
| 144 | + if re.search(UPSTREAM_REGEX, rpmname): |
| 145 | + return True |
| 146 | + return False |
0 commit comments