|
| 1 | +"""Prepare an inspected build output using the existing platform runtime policy. |
| 2 | +
|
| 3 | +Receipts describe the declared build inputs and inspection, not authenticity or |
| 4 | +approval. The current build commit comes from Bazel's workspace status file. |
| 5 | +""" |
| 6 | + |
| 7 | +import argparse |
| 8 | +import importlib |
| 9 | +import json |
| 10 | +from pathlib import Path |
| 11 | +import re |
| 12 | +import sys |
| 13 | +import tarfile |
| 14 | +import tempfile |
| 15 | + |
| 16 | +sys.path.insert(0, str(Path(__file__).resolve().parent)) |
| 17 | +from runtime import digest |
| 18 | + |
| 19 | + |
| 20 | +def prepare_built(prefix, build_receipt, status, target, output): |
| 21 | + prefix = prefix.resolve(strict=True) |
| 22 | + if build_receipt.stat().st_size > 1024 * 1024 or status.stat().st_size > 65536: |
| 23 | + raise ValueError("native build metadata exceeds limits") |
| 24 | + build = json.loads(build_receipt.read_text()) |
| 25 | + commits = [ |
| 26 | + line.removeprefix("STABLE_GIT_COMMIT ") |
| 27 | + for line in status.read_text().splitlines() |
| 28 | + if line.startswith("STABLE_GIT_COMMIT ") |
| 29 | + ] |
| 30 | + manifest_hash = digest(Path(__file__).with_name("sources.json")) |
| 31 | + steps = build.get("steps", []) |
| 32 | + if ( |
| 33 | + len(commits) != 1 |
| 34 | + or not re.fullmatch(r"[0-9a-f]{40}", commits[0]) |
| 35 | + or build.get("target") != target |
| 36 | + or build.get("manifest_sha256") != manifest_hash |
| 37 | + or not 1 <= len(steps) <= 128 |
| 38 | + or any(step.get("exit_code") != 0 for step in steps) |
| 39 | + or steps[-1].get("name") != "gst-plugins-good-install" |
| 40 | + ): |
| 41 | + raise ValueError("native build receipt is incomplete or mismatched") |
| 42 | + suffix = target.partition("-")[2] |
| 43 | + modules = { |
| 44 | + "apple-darwin": "macos_runtime", |
| 45 | + "unknown-linux-gnu": "linux_runtime", |
| 46 | + "pc-windows-msvc": "windows_runtime", |
| 47 | + } |
| 48 | + if suffix not in modules or target.partition("-")[0] not in ("aarch64", "x86_64"): |
| 49 | + raise ValueError("unsupported native runtime target") |
| 50 | + platform = importlib.import_module(modules[suffix]) |
| 51 | + records = [] |
| 52 | + for path in sorted(prefix.rglob("*")): |
| 53 | + if not re.fullmatch(r".+\.(?:dylib|dll|so(?:\.[0-9]+)*)", path.name): |
| 54 | + continue |
| 55 | + if path.is_symlink(): |
| 56 | + if not path.resolve(strict=True).is_relative_to(prefix): |
| 57 | + raise ValueError("native library link escapes its prefix") |
| 58 | + continue |
| 59 | + if not path.is_file(): |
| 60 | + continue |
| 61 | + if len(records) >= 128: |
| 62 | + raise ValueError("native inventory exceeds limits") |
| 63 | + platform.inspect(path, target) |
| 64 | + records.append( |
| 65 | + { |
| 66 | + "path": path.relative_to(prefix).as_posix(), |
| 67 | + "target": target, |
| 68 | + "sha256": digest(path), |
| 69 | + } |
| 70 | + ) |
| 71 | + if not records: |
| 72 | + raise ValueError("native prefix contains no libraries") |
| 73 | + with tempfile.TemporaryDirectory(prefix="voice-receipts-") as temporary: |
| 74 | + receipts = Path(temporary) |
| 75 | + (receipts / "inspection").mkdir() |
| 76 | + (receipts / "inspection/binaries.json").write_text(json.dumps(records)) |
| 77 | + (receipts / "ci.json").write_text( |
| 78 | + json.dumps( |
| 79 | + { |
| 80 | + "commit": commits[0], |
| 81 | + "target": target, |
| 82 | + "manifest_sha256": manifest_hash, |
| 83 | + "build_complete": True, |
| 84 | + "inspection_complete": True, |
| 85 | + } |
| 86 | + ) |
| 87 | + ) |
| 88 | + platform.project(prefix, receipts, target, output) |
| 89 | + |
| 90 | + |
| 91 | +def prepare_archive(archive, build_receipt, status, target, output): |
| 92 | + # Archives preserve native aliases through Bazel's cache and sandbox links. |
| 93 | + with tempfile.TemporaryDirectory(prefix="voice-prefix-") as temporary: |
| 94 | + prefix = Path(temporary) |
| 95 | + with tarfile.open(archive) as source: |
| 96 | + source.extractall(prefix, filter="data") |
| 97 | + prepare_built(prefix, build_receipt, status, target, output) |
| 98 | + |
| 99 | + |
| 100 | +if __name__ == "__main__": |
| 101 | + parser = argparse.ArgumentParser(description=__doc__) |
| 102 | + for name in ("prefix", "build-receipt", "status", "output"): |
| 103 | + parser.add_argument("--" + name, type=Path, required=True) |
| 104 | + parser.add_argument("--target", required=True) |
| 105 | + args = parser.parse_args() |
| 106 | + # Executors may leave the TreeArtifact absent or create an empty directory. |
| 107 | + try: |
| 108 | + args.output.rmdir() |
| 109 | + except FileNotFoundError: |
| 110 | + pass |
| 111 | + prepare_archive( |
| 112 | + args.prefix, args.build_receipt, args.status, args.target, args.output |
| 113 | + ) |
0 commit comments