|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import csv |
| 4 | +import gzip |
| 5 | +import logging |
| 6 | +import tempfile |
| 7 | +import xml.etree.ElementTree as ET |
| 8 | +from collections import namedtuple |
| 9 | +from typing import Iterable, Iterator |
| 10 | +from urllib.request import urlopen |
| 11 | + |
| 12 | +import rpm # type: ignore |
| 13 | + |
| 14 | +import repoquery |
| 15 | + |
| 16 | +ARCH = "x86_64" |
| 17 | +XCP_VERSION = "8.3" |
| 18 | + |
| 19 | +class EVR: |
| 20 | + def __init__(self, e: str, v: str, r: str): |
| 21 | + self._evr = ('0' if e in [None, 'None'] else e, v, r) |
| 22 | + |
| 23 | + def __eq__(self, other): |
| 24 | + if isinstance(other, EVR): |
| 25 | + return self._evr == other._evr |
| 26 | + else: |
| 27 | + return self._evr == other |
| 28 | + |
| 29 | + def __gt__(self, other): |
| 30 | + if isinstance(other, EVR): |
| 31 | + return rpm.labelCompare(self._evr, other._evr) > 0 # type: ignore |
| 32 | + else: |
| 33 | + return self._evr > other |
| 34 | + |
| 35 | + def __lt__(self, other): |
| 36 | + return other > self |
| 37 | + |
| 38 | + def __str__(self): |
| 39 | + if self._evr[0] != '0': |
| 40 | + return f'{self._evr[0]}:{self._evr[1]}-{self._evr[2]}' |
| 41 | + else: |
| 42 | + return f'{self._evr[1]}-{self._evr[2]}' |
| 43 | + |
| 44 | +# Filters an iterator of (n, e, v, r) for newest evr of each `n`. |
| 45 | +# Older versions are allowed to appear before the newer ones. |
| 46 | +def filter_best_evr(nevrs: Iterable[tuple[str, str, str, str]]) -> Iterator[tuple[str, str, str, str]]: |
| 47 | + best: dict[str, tuple[str, str, str]] = {} |
| 48 | + for (n, e, v, r) in nevrs: |
| 49 | + if n not in best or rpm.labelCompare(best[n], (e, v, r)) < 0: # type: ignore |
| 50 | + best[n] = (e, v, r) |
| 51 | + yield (n, e, v, r) |
| 52 | + # else (e, v, r) is older than a previously-seen version, drop |
| 53 | + |
| 54 | +def collect_data_xcpng() -> dict[str, EVR]: |
| 55 | + with (tempfile.NamedTemporaryFile() as dnfconf, |
| 56 | + tempfile.TemporaryDirectory() as yumrepod): |
| 57 | + repoquery.setup_xcpng_yum_repos(yum_repo_d=yumrepod, |
| 58 | + sections=['base', 'updates'], |
| 59 | + bin_arch=None, |
| 60 | + version=XCP_VERSION) |
| 61 | + repoquery.dnf_setup(dnf_conf=dnfconf.name, yum_repo_d=yumrepod) |
| 62 | + |
| 63 | + xcp_nevr = { |
| 64 | + n: EVR(e, v, r) |
| 65 | + for (n, e, v, r) |
| 66 | + in filter_best_evr(repoquery.rpm_parse_nevr(nevr, f".xcpng{XCP_VERSION}") |
| 67 | + for nevr in repoquery.all_srpms())} |
| 68 | + |
| 69 | + return xcp_nevr |
| 70 | + |
| 71 | +def collect_data_xs8(): |
| 72 | + with (tempfile.NamedTemporaryFile() as dnfconf, |
| 73 | + tempfile.TemporaryDirectory() as yumrepod): |
| 74 | + |
| 75 | + repoquery.setup_xs8_yum_repos(yum_repo_d=yumrepod, |
| 76 | + sections=['base', 'normal'], |
| 77 | + ) |
| 78 | + repoquery.dnf_setup(dnf_conf=dnfconf.name, yum_repo_d=yumrepod) |
| 79 | + logging.debug("fill cache with XS info") |
| 80 | + repoquery.fill_srpm_binrpms_cache() |
| 81 | + |
| 82 | + logging.debug("get all XS SRPMs") |
| 83 | + xs8_srpms = {nevr for nevr in repoquery.all_srpms()} |
| 84 | + xs8_rpms_sources = {nevr for nevr in repoquery.SRPM_BINRPMS_CACHE} |
| 85 | + |
| 86 | + xs8_srpms_set = {n: EVR(e, v, r) |
| 87 | + for (n, e, v, r) |
| 88 | + in filter_best_evr(repoquery.rpm_parse_nevr(nevr, ".xs8") |
| 89 | + for nevr in xs8_srpms)} |
| 90 | + xs8_rpms_sources_set = {n: EVR(e, v, r) |
| 91 | + for (n, e, v, r) |
| 92 | + in filter_best_evr(repoquery.rpm_parse_nevr(nevr, ".xs8") |
| 93 | + for nevr in xs8_rpms_sources)} |
| 94 | + |
| 95 | + return (xs8_srpms_set, xs8_rpms_sources_set) |
| 96 | + |
| 97 | +def read_package_status_metadata(): |
| 98 | + with open('package_status.csv', newline='') as csvfile: |
| 99 | + csvreader = csv.reader(csvfile, delimiter=';', quotechar='|') |
| 100 | + headers = next(csvreader) |
| 101 | + assert headers == ["SRPM_name", "status", "comment"], f"unexpected headers {headers!r}" |
| 102 | + PackageStatus = namedtuple("PackageStatus", headers[1:]) # type: ignore[misc] |
| 103 | + return {row[0]: PackageStatus(*row[1:]) |
| 104 | + for row in csvreader} |
| 105 | + |
| 106 | +def get_xs8_rpm_updates(): |
| 107 | + NS = {'repo': 'http://linux.duke.edu/metadata/repo'} |
| 108 | + BASE_URL = 'http://repos/repos/XS8/normal/xs8p-normal' |
| 109 | + |
| 110 | + # read the update info path from repomd.xml |
| 111 | + with urlopen(f'{BASE_URL}/repodata/repomd.xml') as f: |
| 112 | + repomd = f.read() |
| 113 | + data = ET.fromstring(repomd).find("repo:data[@type='updateinfo']", NS) |
| 114 | + assert data is not None |
| 115 | + location = data.find('repo:location', NS) |
| 116 | + assert location is not None |
| 117 | + path = location.attrib['href'] |
| 118 | + |
| 119 | + # read the update info file |
| 120 | + res = {} |
| 121 | + with urlopen(f'{BASE_URL}/{path}') as cf, gzip.open(cf, 'rb') as f: |
| 122 | + updateinfo = f.read() |
| 123 | + updates = ET.fromstring(updateinfo).findall('update') |
| 124 | + for update in updates: |
| 125 | + update_id = update.find('id') |
| 126 | + assert update_id is not None |
| 127 | + update_id = update_id.text |
| 128 | + pkglist = update.find('pkglist') |
| 129 | + assert pkglist is not None |
| 130 | + collection = pkglist.find('collection') |
| 131 | + assert collection is not None |
| 132 | + packages = collection.findall('package') |
| 133 | + for package in packages: |
| 134 | + evr = EVR(package.attrib['epoch'], package.attrib['version'], package.attrib['release']) |
| 135 | + rpm = f'{package.attrib["name"]}-{evr}' |
| 136 | + srpm = repoquery.rpm_source_package(rpm, default=rpm) |
| 137 | + res[srpm] = update_id |
| 138 | + return res |
0 commit comments