| 1 | """Publish a signed runtime closure from a verified release archive inventory.""" |
| 2 | |
| 3 | import argparse |
| 4 | from collections import defaultdict |
| 5 | import csv |
| 6 | import hashlib |
| 7 | from pathlib import Path |
| 8 | import subprocess |
| 9 | |
| 10 | |
| 11 | def records(file): |
| 12 | with file.open() as stream: |
| 13 | return list(csv.DictReader(stream, delimiter="\t")) |
| 14 | |
| 15 | |
| 16 | def runtime_packages(plan, coverage, roots): |
| 17 | packages = {row["package"]: row for row in records(coverage)} |
| 18 | edges = defaultdict(set) |
| 19 | for row in records(plan / "edges.tsv"): |
| 20 | if row["kind"] == "depend": |
| 21 | edges[row["package"]].add(row["selected_provider"]) |
| 22 | selected = set() |
| 23 | pending = list(roots) |
| 24 | while pending: |
| 25 | name = pending.pop() |
| 26 | if name in selected: |
| 27 | continue |
| 28 | if name not in packages: |
| 29 | raise ValueError(f"unknown package or unresolved runtime provider: {name}") |
| 30 | selected.add(name) |
| 31 | pending.extend(edges[name] - selected) |
| 32 | return {name: packages[name] for name in sorted(selected)} |
| 33 | |
| 34 | |
| 35 | def publish(args): |
| 36 | selected = runtime_packages(args.plan, args.coverage, args.roots) |
| 37 | if args.dependencies_only: |
| 38 | for name in set(args.roots): |
| 39 | del selected[name] |
| 40 | blocked = [f"{name} ({row['status']})" for name, row in selected.items() |
| 41 | if row["status"] != "verified"] |
| 42 | if blocked: |
| 43 | raise ValueError(f"runtime closure has unverified archives: {', '.join(blocked)}") |
| 44 | archives = [] |
| 45 | ordered = {row["package"]: row["version"] for row in records(args.plan / "order.tsv")} |
| 46 | for name, row in selected.items(): |
| 47 | version = ordered[name] |
| 48 | path = (args.workspace / row["archive"]).resolve(strict=True) |
| 49 | if row["version"] != version or path.name not in ( |
| 50 | f"{name}-{version}-{args.arch}.pkg.tar.zst", f"{name}-{version}-any.pkg.tar.zst"): |
| 51 | raise ValueError(f"{name}: archive differs from the release plan") |
| 52 | with path.open("rb") as stream: |
| 53 | actual = hashlib.file_digest(stream, "sha256").hexdigest() |
| 54 | if actual != row["archive_sha256"]: |
| 55 | raise ValueError(f"{name}: archive bytes changed after verification") |
| 56 | archives.append(path) |
| 57 | if not archives: |
| 58 | raise ValueError("empty runtime closure") |
| 59 | subprocess.run(["feed", "publish", args.keys, args.repo, args.arch, *archives], check=True) |
| 60 | |
| 61 | |
| 62 | if __name__ == "__main__": |
| 63 | parser = argparse.ArgumentParser(description=__doc__) |
| 64 | parser.add_argument("arch", choices=("x86_64", "aarch64")) |
| 65 | parser.add_argument("workspace", type=Path, help="base directory for paths in the archive inventory") |
| 66 | parser.add_argument("plan", type=Path, help="directory containing order.tsv and edges.tsv") |
| 67 | parser.add_argument("coverage", type=Path, help="verified inventory produced by release-archives.py") |
| 68 | parser.add_argument("keys", type=Path, help="feed signing state") |
| 69 | parser.add_argument("repo", type=Path, help="new repository directory") |
| 70 | parser.add_argument("roots", nargs="+", help="packages to install by name") |
| 71 | parser.add_argument("--dependencies-only", action="store_true", |
| 72 | help="omit the named roots while preparing their signed build dependencies") |
| 73 | publish(parser.parse_args()) |
| 74 | |