Files
alkane-toolkit/ci/gen-badges.py
T
noahandClaude Opus 4.8 749cdc4eed item 12: CI verify/release split, dependency audit, README + badges
CI is now two workflows: verify.yml (pull requests + non-main pushes) runs the
clippy gate, build, unit tests, microbenchmarks, the headless self-test, and a
best-effort dependency audit; release.yml (main) repeats the clippy gate + audit
before its existing build/test/selftest/package/release, then refreshes the
committed README badges and pushes any change back (best-effort, and a badges-
only push does not bump the version, so it does not loop).

Security: cargo audit found a build-time-only advisory (h2 0.3 in the wx-rs
build script's reqwest 0.11 subtree, never linked into the runtime binary and
never executed on Linux). The vendored/offline tree can't upgrade it without
re-vendoring, so it is documented and accepted in .cargo/audit.toml (mirrored in
deny.toml with the project's permissive-license policy).

Badges: ci/gen-badges.py emits self-contained shields-style SVGs into badges/
(tests, coverage 36%, bench, LOC 12.5k, clippy clean, dependencies, license) --
committed and referenced by relative path so a self-hosted Gitea renders them
with no external service. README rewritten around what the toolkit actually does
today (discover / monitor / operate / install), with build, CLI, dev, and
architecture sections and the dual-license note.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Djv3oncM4LxTS5tb61Ako6
2026-09-22 18:17:31 -06:00

166 lines
5.3 KiB
Python

#!/usr/bin/env python3
"""Generate self-contained shields-style SVG badges into badges/.
Self-hosted Gitea renders a README's relative image links straight from the
repo, so committing the SVGs (rather than depending on an external badge
service that would have to reach a possibly-private Gitea) is the robust choice.
CI regenerates them from live metrics and commits any changes back.
Usage:
gen-badges.py --tests N --coverage PCT --bench "label" [--out badges]
Metrics not passed on the command line are computed here (LOC from the source
tree, dependency count from Cargo.lock, license from Cargo.toml).
"""
import argparse
import os
import re
import sys
# Approximate per-character width at 11px DejaVu Sans, in 1/10 px, matching the
# shields "flat" look closely enough for a fixed-width monospace-ish render.
CHAR_W = 6.5
PAD = 6.0
COLORS = {
"green": "#4c1",
"brightgreen": "#4c1",
"yellowgreen": "#a4a61d",
"yellow": "#dfb317",
"orange": "#fe7d37",
"red": "#e05d44",
"blue": "#007ec6",
"lightgrey": "#9f9f9f",
"grey": "#555",
}
def text_width(s: str) -> float:
return len(s) * CHAR_W + 2 * PAD
def badge_svg(label: str, message: str, color: str) -> str:
color = COLORS.get(color, color)
lw = round(text_width(label), 1)
mw = round(text_width(message), 1)
total = round(lw + mw, 1)
lx = round(lw / 2, 1)
mx = round(lw + mw / 2, 1)
# Text is drawn twice (a dark shadow then the light glyphs) for the shields look.
return f'''<svg xmlns="http://www.w3.org/2000/svg" width="{total}" height="20" role="img" aria-label="{label}: {message}">
<title>{label}: {message}</title>
<linearGradient id="s" x2="0" y2="100%">
<stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
<stop offset="1" stop-opacity=".1"/>
</linearGradient>
<clipPath id="r"><rect width="{total}" height="20" rx="3" fill="#fff"/></clipPath>
<g clip-path="url(#r)">
<rect width="{lw}" height="20" fill="#555"/>
<rect x="{lw}" width="{mw}" height="20" fill="{color}"/>
<rect width="{total}" height="20" fill="url(#s)"/>
</g>
<g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" font-size="11">
<text x="{lx}" y="15" fill="#010101" fill-opacity=".3">{label}</text>
<text x="{lx}" y="14">{label}</text>
<text x="{mx}" y="15" fill="#010101" fill-opacity=".3">{message}</text>
<text x="{mx}" y="14">{message}</text>
</g>
</svg>
'''
def count_loc(root: str) -> int:
total = 0
for base in ("crates",):
for dirpath, _dirs, files in os.walk(os.path.join(root, base)):
for f in files:
if f.endswith(".rs"):
try:
with open(os.path.join(dirpath, f), encoding="utf-8", errors="ignore") as fh:
total += sum(1 for _ in fh)
except OSError:
pass
return total
def count_deps(root: str) -> int:
lock = os.path.join(root, "Cargo.lock")
try:
with open(lock, encoding="utf-8") as fh:
return sum(1 for line in fh if line.startswith("name = "))
except OSError:
return 0
def read_license(root: str) -> str:
try:
with open(os.path.join(root, "Cargo.toml"), encoding="utf-8") as fh:
for line in fh:
m = re.match(r'\s*license\s*=\s*"([^"]+)"', line)
if m:
return m.group(1)
except OSError:
pass
return "unknown"
def loc_color(n: int) -> str:
return "blue"
def cov_color(pct: float) -> str:
if pct >= 80:
return "brightgreen"
if pct >= 60:
return "yellowgreen"
if pct >= 40:
return "yellow"
if pct >= 20:
return "orange"
return "red"
def human(n: int) -> str:
if n >= 1000:
return f"{n / 1000:.1f}k".replace(".0k", "k")
return str(n)
def main() -> int:
ap = argparse.ArgumentParser()
ap.add_argument("--out", default="badges")
ap.add_argument("--root", default=".")
ap.add_argument("--tests", type=int, default=None)
ap.add_argument("--coverage", type=float, default=None)
ap.add_argument("--bench", default=None)
ap.add_argument("--clippy", default="clean")
args = ap.parse_args()
os.makedirs(os.path.join(args.root, args.out), exist_ok=True)
def write(name, label, message, color):
path = os.path.join(args.root, args.out, name + ".svg")
with open(path, "w", encoding="utf-8") as fh:
fh.write(badge_svg(label, message, color))
print(f"wrote {path}: {label}={message}")
loc = count_loc(args.root)
deps = count_deps(args.root)
lic = read_license(args.root)
write("loc", "lines of code", human(loc), loc_color(loc))
write("license", "license", lic, "blue")
write("dependencies", "dependencies", str(deps), "blue")
write("quality", "clippy", args.clippy, "brightgreen" if args.clippy == "clean" else "orange")
if args.tests is not None:
write("tests", "tests", f"{args.tests} passing", "brightgreen")
if args.coverage is not None:
write("coverage", "coverage", f"{args.coverage:.0f}%", cov_color(args.coverage))
if args.bench is not None:
write("bench", "bench", args.bench, "blue")
return 0
if __name__ == "__main__":
sys.exit(main())