import json
import pathlib
import re
import sys
def refusing_by_default(rules_path: str) -> list[str]:
entries = json.loads(pathlib.Path(rules_path).read_text())
assert isinstance(entries, list), f"{rules_path} is not a JSON array"
ids = sorted(
e["id"]
for e in entries
if e["kind"] == "rule" and e["default_stance"] == "deny"
)
assert ids, f"no rule ships at deny in {rules_path} — wrong file?"
return ids
def rewrite_refusing(text: str, ids: list[str]) -> str:
method = re.compile(
r"(?P<head> def refusing_by_default\n )%w\[[^\]\n]*\](?P<tail>\n end\n)"
)
text, n = method.subn(
lambda m: f"{m.group('head')}%w[{' '.join(ids)}]{m.group('tail')}", text
)
assert n == 1, (
f"expected one `def refusing_by_default` holding a %w[...] line, found {n}"
)
return text
def main() -> None:
assert len(sys.argv) == 5, __doc__.strip().splitlines()[-1]
version, sums_path, rules_path, formula_path = sys.argv[1:5]
version = version.lstrip("v")
refusing = refusing_by_default(rules_path)
sums: dict[str, tuple[str, str]] = {}
for line in pathlib.Path(sums_path).read_text().splitlines():
parts = line.split()
if len(parts) != 2:
continue
sha, name = parts
m = re.fullmatch(r"amont-agent-([0-9.]+)-(.+?)\.(?:tar\.gz|zip)", name)
if not m:
continue
assert m.group(1) == version, (
f"SHA256SUMS is for {m.group(1)}, not {version} — wrong release?"
)
sums[m.group(2)] = (sha, name)
assert sums, f"no amont-agent checksums parsed from {sums_path}"
formula = pathlib.Path(formula_path)
text = formula.read_text()
text, n_version = re.subn(
r'version "[0-9.]+"', f'version "{version}"', text, count=1
)
assert n_version == 1, "the formula carries no version line"
def rewrite(m: "re.Match[str]") -> str:
target = m.group("target")
assert target in sums, (
f"the formula wants {target} and this release did not publish it"
)
sha, name = sums[target]
return (
f'url "https://github.com/fredericrous/amont-agent/releases/'
f'download/v{version}/{name}"'
f'{m.group("mid")}sha256 "{sha}"'
)
pair = re.compile(
r'url "https://github\.com/fredericrous/amont-agent/releases/'
r'download/v[0-9.]+/amont-agent-[0-9.]+-(?P<target>[a-z0-9_-]+)\.tar\.gz"'
r'(?P<mid>\s+)sha256 "[0-9a-f]{64}"'
)
text, n = pair.subn(rewrite, text)
assert n == 4, f"expected 4 url/sha pairs in the formula, rewrote {n}"
stale = set(re.findall(r"amont-agent-([0-9.]+)-", text)) - {version}
assert not stale, f"stale release versions survived the rewrite: {stale}"
text = rewrite_refusing(text, refusing)
formula.write_text(text)
print(f"formula -> {version} ({n} targets; refusing: {' '.join(refusing)})")
if __name__ == "__main__":
main()