|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Copyright 2026 The Fuchsia Authors |
| 4 | +# |
| 5 | +# Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0 |
| 6 | +# <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT |
| 7 | +# license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option. |
| 8 | +# This file may not be copied, modified, or distributed except according to |
| 9 | +# those terms. |
| 10 | + |
| 11 | +"""Update anneal/Cargo.toml's exocrate archive URLs and hashes.""" |
| 12 | + |
| 13 | +import argparse |
| 14 | +import json |
| 15 | +import re |
| 16 | +from pathlib import Path |
| 17 | + |
| 18 | +EXPECTED_PLATFORMS = { |
| 19 | + ("linux", "x86_64"), |
| 20 | + ("linux", "aarch64"), |
| 21 | + ("macos", "x86_64"), |
| 22 | + ("macos", "aarch64"), |
| 23 | +} |
| 24 | + |
| 25 | +EXPECTED_TARGETS = { |
| 26 | + ("linux", "x86_64"): "linux-x86_64", |
| 27 | + ("linux", "aarch64"): "linux-aarch64", |
| 28 | + ("macos", "x86_64"): "macos-x86_64", |
| 29 | + ("macos", "aarch64"): "macos-aarch64", |
| 30 | +} |
| 31 | + |
| 32 | +SECTION_RE = re.compile(r"^\[package\.metadata\.exocrate\.([^.]+)\.([^\]]+)\]$") |
| 33 | +SHA_RE = re.compile(r'^\s*sha256\s*=\s*"[0-9a-fA-F]{64}"\s*$') |
| 34 | +URL_RE = re.compile(r'^\s*url\s*=\s*".*"\s*$') |
| 35 | + |
| 36 | + |
| 37 | +def load_metadata( |
| 38 | + paths: list[Path], expected_release_tag: str | None |
| 39 | +) -> dict[tuple[str, str], dict[str, str]]: |
| 40 | + metadata = {} |
| 41 | + for path in paths: |
| 42 | + value = json.loads(path.read_text()) |
| 43 | + try: |
| 44 | + target = value["target"] |
| 45 | + os_name = value["os"] |
| 46 | + arch = value["arch"] |
| 47 | + sha256 = value["sha256"] |
| 48 | + url = value["url"] |
| 49 | + except KeyError as e: |
| 50 | + raise SystemExit(f"{path}: missing required key {e.args[0]!r}") from e |
| 51 | + if not all(isinstance(field, str) for field in (target, os_name, arch, sha256, url)): |
| 52 | + raise SystemExit(f"{path}: target, os, arch, sha256, and url must all be strings") |
| 53 | + if not re.fullmatch(r"[0-9a-f]{64}", sha256): |
| 54 | + raise SystemExit(f"{path}: sha256 must be 64 lowercase hex characters") |
| 55 | + platform = (os_name, arch) |
| 56 | + expected_target = EXPECTED_TARGETS.get(platform) |
| 57 | + if expected_target is not None and target != expected_target: |
| 58 | + raise SystemExit( |
| 59 | + f"{path}: target {target!r} does not match platform " |
| 60 | + f"{os_name}.{arch}; expected {expected_target!r}" |
| 61 | + ) |
| 62 | + if expected_release_tag is not None: |
| 63 | + expected_url_component = f"/releases/download/{expected_release_tag}/" |
| 64 | + if expected_url_component not in url: |
| 65 | + raise SystemExit( |
| 66 | + f"{path}: url does not point at release tag " |
| 67 | + f"{expected_release_tag!r}: {url}" |
| 68 | + ) |
| 69 | + if platform in metadata: |
| 70 | + raise SystemExit(f"duplicate metadata for {os_name}.{arch}") |
| 71 | + metadata[platform] = {"sha256": sha256, "url": url} |
| 72 | + return metadata |
| 73 | + |
| 74 | + |
| 75 | +def metadata_files(metadata_dir: Path) -> list[Path]: |
| 76 | + return sorted(path for path in metadata_dir.glob("*.json") if path.is_file()) |
| 77 | + |
| 78 | + |
| 79 | +def update_manifest(cargo_toml: Path, metadata: dict[tuple[str, str], dict[str, str]]) -> None: |
| 80 | + lines = cargo_toml.read_text().splitlines() |
| 81 | + seen = set() |
| 82 | + updated = {platform: set() for platform in metadata} |
| 83 | + current_platform = None |
| 84 | + |
| 85 | + for i, line in enumerate(lines): |
| 86 | + section = SECTION_RE.match(line) |
| 87 | + if section: |
| 88 | + platform = (section.group(1), section.group(2)) |
| 89 | + current_platform = platform if platform in metadata else None |
| 90 | + if current_platform is not None: |
| 91 | + seen.add(current_platform) |
| 92 | + continue |
| 93 | + |
| 94 | + if current_platform is None: |
| 95 | + continue |
| 96 | + |
| 97 | + values = metadata[current_platform] |
| 98 | + if SHA_RE.match(line): |
| 99 | + lines[i] = f'sha256 = "{values["sha256"]}"' |
| 100 | + updated[current_platform].add("sha256") |
| 101 | + elif URL_RE.match(line): |
| 102 | + lines[i] = f'url = "{values["url"]}"' |
| 103 | + updated[current_platform].add("url") |
| 104 | + |
| 105 | + missing_sections = set(metadata) - seen |
| 106 | + if missing_sections: |
| 107 | + formatted = ", ".join(f"{os_name}.{arch}" for os_name, arch in sorted(missing_sections)) |
| 108 | + raise SystemExit(f"Cargo.toml is missing exocrate sections for: {formatted}") |
| 109 | + |
| 110 | + incomplete = { |
| 111 | + platform: {"sha256", "url"} - fields |
| 112 | + for platform, fields in updated.items() |
| 113 | + if {"sha256", "url"} - fields |
| 114 | + } |
| 115 | + if incomplete: |
| 116 | + formatted = ", ".join( |
| 117 | + f"{os_name}.{arch} missing {','.join(sorted(fields))}" |
| 118 | + for (os_name, arch), fields in sorted(incomplete.items()) |
| 119 | + ) |
| 120 | + raise SystemExit(f"Cargo.toml has incomplete exocrate sections: {formatted}") |
| 121 | + |
| 122 | + cargo_toml.write_text("\n".join(lines) + "\n") |
| 123 | + |
| 124 | + |
| 125 | +def main() -> None: |
| 126 | + parser = argparse.ArgumentParser() |
| 127 | + parser.add_argument("--cargo-toml", default="anneal/Cargo.toml", type=Path) |
| 128 | + parser.add_argument("--metadata-dir", type=Path) |
| 129 | + parser.add_argument("--metadata", action="append", default=[], type=Path) |
| 130 | + parser.add_argument("--expected-release-tag") |
| 131 | + parser.add_argument("--require-all", action="store_true") |
| 132 | + args = parser.parse_args() |
| 133 | + |
| 134 | + paths = list(args.metadata) |
| 135 | + if args.metadata_dir is not None: |
| 136 | + paths.extend(metadata_files(args.metadata_dir)) |
| 137 | + if not paths: |
| 138 | + raise SystemExit("no metadata JSON files provided") |
| 139 | + |
| 140 | + metadata = load_metadata(paths, args.expected_release_tag) |
| 141 | + if args.require_all and set(metadata) != EXPECTED_PLATFORMS: |
| 142 | + missing = EXPECTED_PLATFORMS - set(metadata) |
| 143 | + extra = set(metadata) - EXPECTED_PLATFORMS |
| 144 | + messages = [] |
| 145 | + if missing: |
| 146 | + messages.append("missing " + ", ".join(f"{os_name}.{arch}" for os_name, arch in sorted(missing))) |
| 147 | + if extra: |
| 148 | + messages.append("unexpected " + ", ".join(f"{os_name}.{arch}" for os_name, arch in sorted(extra))) |
| 149 | + raise SystemExit("; ".join(messages)) |
| 150 | + |
| 151 | + update_manifest(args.cargo_toml, metadata) |
| 152 | + |
| 153 | + |
| 154 | +if __name__ == "__main__": |
| 155 | + main() |
0 commit comments