"""Install the latest receipted native packages together and audit their complete root.""" import argparse import csv import hashlib import re import subprocess from pathlib import Path def archives(location, arch): if location.is_dir() or location.suffix == ".sha256": receipts = sorted(location.glob("*.sha256")) if location.is_dir() else [location] for receipt in receipts: for line in receipt.read_text().splitlines(): digest, name = line.split(maxsplit=1) if name.endswith(".pkg.tar.zst"): if Path(name).name != name: raise ValueError(f"expected a package basename in {receipt}") yield receipt.parent / name, digest else: with location.open() as index: for row in csv.DictReader(index, delimiter="\t"): if Path(row["archive"]).name != row["archive"]: raise ValueError(f"invalid package name in {location}") if row["arch"] not in (arch, "any"): continue path = location.parent / arch / row["archive"] if row["arch"] == "any" and not path.is_file(): # Data packages in a single-target feed live in its architecture directory. siblings = [directory / row["archive"] for directory in location.parent.iterdir() if directory.is_dir() and (directory / row["archive"]).is_file()] if not siblings: raise ValueError(f"missing architecture-independent package {row['archive']}") for sibling in siblings: yield sibling, row["sha256"] else: yield path, row["sha256"] def collect(sources, arch): selected = {} for source in sources: for archive, expected in archives(source, arch): if not archive.name.endswith((f"-{arch}.pkg.tar.zst", "-any.pkg.tar.zst")): continue with archive.open("rb") as stream: digest = hashlib.file_digest(stream, "sha256").hexdigest() if digest != expected: raise ValueError(f"checksum mismatch for {archive}") metadata = subprocess.check_output(["bsdtar", "-xOf", archive, ".PKGINFO"], text=True) fields = dict(line.split(" = ", 1) for line in metadata.splitlines() if " = " in line) name, version, machine = (fields[k] for k in ("pkgname", "pkgver", "arch")) if machine not in ("any", arch) or not re.fullmatch(r"[A-Za-z0-9@._+][A-Za-z0-9@._+-]*", name): raise ValueError(f"invalid target package metadata: {archive}") if archive.name != f"{name}-{version}-{machine}.pkg.tar.zst": raise ValueError(f"archive filename does not match its metadata: {archive}") if name in selected: old, old_digest, _ = selected[name] newer = int(subprocess.check_output(["vercmp", version, old], text=True)) if newer == 0 and digest != old_digest: raise ValueError(f"distinct archives have the same {name} version {version}") if newer <= 0: continue selected[name] = version, digest, archive if not selected: raise ValueError("no packages with checksums for this architecture") return selected def run(args): args.work.mkdir() selected = collect(args.sources, args.arch) names = sorted(selected) with (args.work / "selection.tsv").open("w") as output: table = csv.writer(output, delimiter="\t", lineterminator="\n") table.writerow(["name", "version", "sha256", "archive"]) table.writerows((name, *selected[name]) for name in names) repo, client = args.work / "repo", args.work / "client" with (args.work / "publish.log").open("w") as log: subprocess.run(["feed", "publish", args.keys, repo, args.arch, *[selected[n][2] for n in names]], check=True, stdout=log, stderr=subprocess.STDOUT) if args.publish_only: return for label, command in [ ("install", ["feed", "install", args.keys, repo, args.arch, client, "--fakeroot", *(args.install or names)]), ("audit", [args.auditor, args.arch, client]), ]: with (args.work / (label + ".log")).open("w") as log: subprocess.run(command, check=True, stdout=log, stderr=subprocess.STDOUT) print((args.work / "audit.log").read_text(), end="") if __name__ == "__main__": parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("arch", choices=["x86_64", "aarch64"]) for option in ["keys", "auditor", "work"]: parser.add_argument(option, type=lambda x: Path(x).resolve()) parser.add_argument("sources", nargs="+", type=lambda x: Path(x).resolve(), help="artifact directory, individual .sha256 receipt, or signed feed's packages.tsv") mode = parser.add_mutually_exclusive_group() mode.add_argument("--install", metavar="NAME", action="append", help="install this root and its dependencies; repeat for additional roots (default: every package)") mode.add_argument("--publish-only", action="store_true", help="publish the signed repository for a separate clean install") run(parser.parse_args())