|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Script for generating yum repository metadata in a reproducible manner. |
| 4 | +
|
| 5 | +Files are copied into public/ and metadata is generated there. All RPMs |
| 6 | +have their mtime fixed to a specific timestamp, so the generated XML/SQLite |
| 7 | +files will be reproducible. |
| 8 | +""" |
| 9 | + |
| 10 | +import os |
| 11 | +import shutil |
| 12 | +import subprocess |
| 13 | +import sys |
| 14 | +import time |
| 15 | +from pathlib import Path |
| 16 | +import xml.etree.ElementTree as ET |
| 17 | + |
| 18 | + |
| 19 | +def fetch_reproduce_timestamp(public: Path) -> int: |
| 20 | + repomd = next(public.glob("workstation/dom0/*/repodata/repomd.xml")) |
| 21 | + tree = ET.parse(repomd) |
| 22 | + root = tree.getroot() |
| 23 | + revision = root.find( |
| 24 | + "repo:revision", {"repo": "http://linux.duke.edu/metadata/repo"} |
| 25 | + ) |
| 26 | + print(f"Will use a timestamp of {revision.text} (from {repomd})") |
| 27 | + return int(revision.text) |
| 28 | + |
| 29 | + |
| 30 | +def main(): |
| 31 | + root = Path(__file__).parent.parent |
| 32 | + public = root / "public" |
| 33 | + workstation = root / "workstation" |
| 34 | + if "--reproduce" in sys.argv: |
| 35 | + try: |
| 36 | + timestamp = fetch_reproduce_timestamp(public) |
| 37 | + except Exception as err: |
| 38 | + raise RuntimeError("Failed to fetch timestamp from repomd.xml") from err |
| 39 | + else: |
| 40 | + # Use the current time |
| 41 | + timestamp = int(time.time()) |
| 42 | + # Reset public, copy the workstation/ tree into it |
| 43 | + print("Creating public/ (from scratch)") |
| 44 | + if public.exists(): |
| 45 | + shutil.rmtree(public) |
| 46 | + public.mkdir() |
| 47 | + shutil.copytree(workstation, public / "workstation") |
| 48 | + for rpm in public.glob("**/*.rpm"): |
| 49 | + os.utime(rpm, (timestamp, timestamp)) |
| 50 | + # Folders are public/workstation/dom0/fXX, run createrepo_c in each one |
| 51 | + for folder in public.glob("*/*/*/"): |
| 52 | + if not folder.is_dir(): |
| 53 | + continue |
| 54 | + print(f"Generating metadata for {folder}") |
| 55 | + # The <revision> and <timestamp> fields are set to the current UNIX time |
| 56 | + # unless we explicitly override them. Use our fixed time to ensure it's |
| 57 | + # consistent regardless of how long this command takes to run. |
| 58 | + subprocess.check_call( |
| 59 | + [ |
| 60 | + "createrepo_c", |
| 61 | + "--revision", |
| 62 | + str(timestamp), |
| 63 | + "--set-timestamp-to-revision", |
| 64 | + str(folder), |
| 65 | + ] |
| 66 | + ) |
| 67 | + print("Done!") |
| 68 | + |
| 69 | + |
| 70 | +if __name__ == "__main__": |
| 71 | + main() |
0 commit comments