croft-software 0.2.3

VSCode-style TUI written in Rust
#!/usr/bin/env python3
"""Whether Cargo.toml's binstall metadata still describes what release.yml builds.

`cargo binstall croft-software` resolves a URL and an in-archive path from
`[package.metadata.binstall]`, and nothing verifies those against the workflow
that produces the archives. The two drift silently: rename the archive in the
workflow and the manifest still points at the old name, so binstall 404s for
every user while every test and every CI job stays green. The failure lands on
users at install time, which is the worst place for it (#376).

So this asserts the contract between the two files:

  * the archive basename in `pkg-url` matches the name release.yml packages
  * `bin-dir`'s directory component matches the directory inside the tarball
  * the tag shape in `pkg-url` matches the workflow's trigger and its
    tag-to-version arithmetic
  * every target in `bin-dir`'s expansion is one the workflow actually builds

Exit 0 when they agree, 1 with a message naming the drift when they do not.

Deliberately string-level rather than a TOML/YAML parse: the point is to catch
a human editing one file and not the other, and both files are read here the
way a reader reads them. A parser would also need a dependency this repo's
gates do not have.
"""

import re
import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parent.parent
MANIFEST = ROOT / "Cargo.toml"
WORKFLOW = ROOT / ".github" / "workflows" / "release.yml"

# The four triples the issue names. A target added to the workflow without
# being added here is fine; a target here that the workflow does not build
# means binstall promises a platform no release carries.
EXPECTED_TARGETS = {
    "aarch64-apple-darwin",
    "x86_64-apple-darwin",
    "x86_64-unknown-linux-musl",
    "aarch64-unknown-linux-musl",
}


def matrix_targets(text: str) -> set[str]:
    """The triples the build matrix actually names.

    Searching the whole file for each triple answers a different question:
    whether the NAME appears anywhere. It appears in the artifact-kind
    assertion's case arms, and it would appear in a comment, so a target
    dropped from the matrix left this check green while no release carried it
    (#554). The matrix block is the corpus; a mention outside it is not a
    build.
    """
    lines = text.splitlines()
    matrix_indent = None
    section = None
    item_indent = None
    out = set()
    for line in lines:
        if matrix_indent is None:
            m = re.match(r"^(\s*)matrix:\s*$", line)
            if m:
                matrix_indent = len(m.group(1))
            continue

        indent = len(line) - len(line.lstrip())
        if line.strip() and indent <= matrix_indent:
            break
        m = re.match(r"^\s*([A-Za-z_-]+):\s*$", line)
        if m and indent == matrix_indent + 2:
            section = m.group(1)
            item_indent = None
            continue
        if section == "include":
            # A list ITEM starts at the dash; `target:` is one of its keys and
            # YAML does not order them, so `- os: ...` with `target:` on a
            # later line is the same entry. Matching only `- target:` drops
            # such an entry and the checker then reports a target it builds as
            # missing - or misses one that really went away (#554 again).
            m = re.match(r"^(\s*)-\s*(.*)$", line)
            if m:
                item_indent = len(m.group(1))
                rest = m.group(2)
            else:
                rest = line.strip()
                if item_indent is None or indent <= item_indent:
                    continue
            k = re.match(r"^target:\s*(\S+)\s*$", rest)
            if k:
                out.add(k.group(1))
        elif section == "target":
            m = re.match(r"^\s*-\s*(\S+)\s*$", line)
            if m:
                out.add(m.group(1))

    if matrix_indent is None:
        fail("no build matrix in release.yml")
    return out


def fail(msg: str) -> None:
    print(f"binstall contract: {msg}", file=sys.stderr)
    sys.exit(1)


def binstall_block(text: str) -> dict:
    """The key/value pairs under [package.metadata.binstall]."""
    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}")

    # 1. The archive name. Both sides spell it with the target substituted,
    #    so compare the shape either side of the placeholder.
    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)
    # Anchor on the Package step's own assignment rather than on the prefix
    # appearing somewhere in the file: "croft-" occurs in artifact names, the
    # collect glob and the checksum lines, so a substring search over the
    # whole workflow passes even when the packaged name has changed.
    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")
    # The archive command is REQUIRED, not conditional. Written as
    # `if "tar -czf" in workflow and suffix != ".tar.gz"`, the guard skipped
    # itself exactly when the workflow stopped producing a tar.gz — the
    # drift it exists to catch removes the substring that arms it.
    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}")

    # 2. pkg-fmt must match the extension, or binstall unpacks with the wrong
    #    reader and reports a corrupt download.
    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"')

    # 3. bin-dir's directory must match the directory the workflow puts inside
    #    the tarball. `tar -czf "$name.tar.gz" -C dist "$name"` means the
    #    archive root is $name, so bin-dir must start with the same shape.
    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")

    # And the layout INSIDE the tarball, which is a separate claim from the
    # archive's name. `tar -czf x.tar.gz -C dist "$name"` wraps the binary in
    # a directory; `-C dist croft` puts it at the root. Both produce an
    # identically-named archive, so comparing names cannot tell them apart —
    # and bin-dir is resolved against the extracted tree, not the file name.
    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")
    # The OUTPUT path, which the first version captured and then discarded.
    # `tar -czf dist/$name.zip` writes a file binstall will never request,
    # and every name-based check still agrees because the archive NAME is
    # built from $name either way.
    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")

    # 4. The tag shape. pkg-url hard-codes the `v` prefix, and the workflow
    #    both triggers on it and strips it to find the notes file.
    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")

    # 5. Every expected target is actually built.
    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()