import re
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
MANIFEST = ROOT / "Cargo.toml"
WORKFLOW = ROOT / ".github" / "workflows" / "release.yml"
EXPECTED_TARGETS = {
"aarch64-apple-darwin",
"x86_64-apple-darwin",
"x86_64-unknown-linux-musl",
"aarch64-unknown-linux-musl",
}
def matrix_targets(text: str) -> set:
block = re.search(
r"^\s*matrix:\s*$(.*?)(?=^\s{0,6}\S)",
text,
re.M | re.S,
)
if not block:
fail("no build matrix in release.yml")
return set(re.findall(r"^\s*-\s*target:\s*(\S+)\s*$", block.group(1), re.M))
def fail(msg: str) -> None:
print(f"binstall contract: {msg}", file=sys.stderr)
sys.exit(1)
def binstall_block(text: str) -> dict:
m = re.search(
r"^\[package\.metadata\.binstall\]\s*$(.*?)(?=^\[|\Z)",
text,
re.M | re.S,
)
if not m:
fail("no [package.metadata.binstall] section in Cargo.toml")
out = {}
for line in m.group(1).splitlines():
line = line.strip()
if not line or line.startswith("#"):
continue
k, _, v = line.partition("=")
out[k.strip()] = v.strip().strip('"')
return out
def main() -> None:
if not WORKFLOW.exists():
fail(f"{WORKFLOW.relative_to(ROOT)} is missing, but Cargo.toml "
"promises binstall artifacts")
manifest = MANIFEST.read_text()
workflow = WORKFLOW.read_text()
meta = binstall_block(manifest)
for key in ("pkg-url", "pkg-fmt", "bin-dir"):
if key not in meta:
fail(f"[package.metadata.binstall] has no {key}")
url_archive = meta["pkg-url"].rsplit("/", 1)[-1]
if "{ target }" not in url_archive:
fail(f"pkg-url's archive name does not vary by target: {url_archive}")
prefix, suffix = url_archive.split("{ target }", 1)
packaged = re.search(r'^\s*name="([^"]+)"\s*$', workflow, re.M)
if not packaged:
fail("release.yml has no `name=\"...\"` line in its Package step; "
"cannot tell what the archive is called")
packaged_name = packaged.group(1).replace("${{ matrix.target }}",
"{ target }")
if packaged_name != url_archive[: -len(suffix)]:
fail(f"release.yml packages {packaged_name!r} but pkg-url expects "
f"{url_archive[: -len(suffix)]!r}; binstall would 404")
if not suffix:
fail("pkg-url's archive name has no extension")
if "tar -czf" not in workflow:
fail("release.yml no longer produces a tar.gz, but pkg-url expects "
f"{suffix}")
if suffix != ".tar.gz":
fail(f"release.yml writes .tar.gz but pkg-url expects {suffix}")
if suffix == ".tar.gz" and meta["pkg-fmt"] != "tgz":
fail(f'pkg-fmt is {meta["pkg-fmt"]!r} for a .tar.gz archive; want "tgz"')
bin_dir = meta["bin-dir"]
if "/" not in bin_dir:
fail(f"bin-dir has no directory component: {bin_dir!r}; the archive "
"wraps the binary in a directory named for the target")
archive_dir = bin_dir.split("/", 1)[0]
expected_dir = url_archive[: -len(suffix)]
if archive_dir != expected_dir:
fail(f"bin-dir's directory {archive_dir!r} does not match the archive "
f"name {expected_dir!r}; binstall would look in the wrong folder")
tar = re.search(r'tar -czf\s+"?([^"\s]+)"?\s+-C\s+(\S+)\s+"?([^"\s]+)"?',
workflow)
if not tar:
fail("release.yml has no recognisable `tar -czf <out> -C <dir> <member>` "
"line; cannot tell what the archive contains")
out = tar.group(1).replace("$name", expected_dir)
want_out = f"dist/{expected_dir}{suffix}"
if out != want_out:
fail(f"release.yml writes {out!r} but pkg-url expects {want_out!r}")
member = tar.group(3).replace("$name", expected_dir)
if member != expected_dir:
fail(f"release.yml archives {member!r} but bin-dir expects the binary "
f"under {expected_dir!r}/; binstall would extract the tarball and "
"look in a directory that is not in it")
if "/v{ version }/" not in meta["pkg-url"]:
fail("pkg-url does not use the v-prefixed tag the workflow triggers on")
if 'tags:' not in workflow or '"v*"' not in workflow:
fail("release.yml does not trigger on v* tags, but pkg-url assumes it")
built = matrix_targets(workflow)
missing = sorted(EXPECTED_TARGETS - built)
if missing:
fail("release.yml does not build " + ", ".join(missing))
print(f"binstall contract: Cargo.toml and "
f"{WORKFLOW.relative_to(ROOT)} agree "
f"({len(EXPECTED_TARGETS)} targets, {expected_dir}{suffix})")
if __name__ == "__main__":
main()