#!/usr/bin/env python3 # -*- coding: utf-8 -*- """check-guide-version.py — ADVISORY: is each user guide still checked against the image the app runs? python3 scripts/check-guide-version.py # every app python3 scripts/check-guide-version.py sonarr plex # named apps only python3 scripts/check-guide-version.py --root DIR # another checkout (the decoy harness) `app_info.guide.checked_version` names the image tag the guide's author checked every label against (controller >= 0.315.0 renders a guide only when it is set — `02-controller-module-map.md`, „The catalog's user guide"). When the app's image moves, the guide may describe a screen that changed. This gate says so, naming the app and both versions. IT NEVER FAILS A PUSH (exit 0 always). An image move is decided by its test record (`check-test-record*.py`); a guide that may be stale is a reason to re-read it, not a reason to hold back a security fix. Refusing here would make every Renovate-style bump wait on prose, and a gate that blocks the wrong thing gets bypassed. Which tag is „the app's": the compose service named like the template directory (or whose container_name is), else — when no service is — every service's tag. A sidecar sharing the checked tag by coincidence is the cost of that fallback; it is stated rather than hidden. STANDARD LIBRARY ONLY: the CI runner has python3 and git and nothing else (the copy gate's first six pushes turned CI red by importing yaml). The two values are read by indentation from files whose shape the other gates already hold: `checked_version:` at four spaces under ` guide:` under top-level `app_info:` — so the English block (six spaces and deeper) and a comment are never read as the fact. """ import io import os import re import sys ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) def strip_comment(line): # A `#` starts a comment only outside quotes; the values read here never contain one. out, q = [], None for c in line: if q: if c == q: q = None elif c in "'\"": q = c elif c == "#": break out.append(c) return "".join(out).rstrip() def unquote(v): v = v.strip() if len(v) >= 2 and v[0] == v[-1] and v[0] in "'\"": return v[1:-1] return v def checked_version(felhom_yml): """(present, value) — present is False when the template carries no guide block at all.""" state = None # None → in app_info → in guide present, value = False, None for raw in io.open(felhom_yml, encoding="utf-8").read().split("\n"): line = strip_comment(raw) if not line.strip(): continue ind = len(line) - len(line.lstrip(" ")) if ind == 0: state = "app_info" if line.strip() == "app_info:" else None continue if state == "app_info" and ind == 2: state = "guide" if line.strip() == "guide:" else "app_info" if state == "guide": present = True continue if state == "guide": if ind <= 2: state = "app_info" continue m = re.match(r"^ checked_version:\s*(.*)$", line) if m: value = unquote(m.group(1)) return present, value def image_tags(compose, slug): """{service: tag} for every image: line, and the services that are „the app" by name.""" tags, cname, svc, in_services = {}, {}, None, False for raw in io.open(compose, encoding="utf-8").read().split("\n"): line = strip_comment(raw) if not line.strip(): continue ind = len(line) - len(line.lstrip(" ")) if ind == 0: in_services = line.strip() == "services:" svc = None continue if not in_services: continue if ind == 2 and line.strip().endswith(":"): svc = line.strip()[:-1] continue if svc and ind == 4: m = re.match(r"^\s+image:\s*(\S+)", line) if m: ref = unquote(m.group(1)).split("@")[0] last = ref.rsplit("/", 1)[-1] tags[svc] = last.split(":", 1)[1] if ":" in last else "" m = re.match(r"^\s+container_name:\s*(\S+)", line) if m: cname[svc] = unquote(m.group(1)) main = [s for s in tags if s == slug or cname.get(s) == slug] return tags, main def main(argv): global ROOT if "--root" in argv: ROOT = os.path.abspath(argv[argv.index("--root") + 1]) skip = {argv.index("--root") + 1} if "--root" in argv else set() scope = [a for i, a in enumerate(argv) if i >= 1 and not a.startswith("-") and i not in skip] tdir = os.path.join(ROOT, "templates") apps = sorted(d for d in os.listdir(tdir) if os.path.exists(os.path.join(tdir, d, ".felhom.yml"))) if scope: apps = [a for a in apps if a in scope] shown, hidden, stale, none = [], [], [], 0 for app in apps: present, cv = checked_version(os.path.join(tdir, app, ".felhom.yml")) if not present: none += 1 continue if not cv: hidden.append(app) continue compose = os.path.join(tdir, app, "docker-compose.yml") if not os.path.exists(compose): print("WARN %s: guide checked against %s, but the template has no docker-compose.yml to compare" % (app, cv)) stale.append(app) continue tags, main_svcs = image_tags(compose, app) cands = [tags[s] for s in main_svcs] or list(tags.values()) if cv in cands: shown.append(app) continue stale.append(app) now = ", ".join("%s=%s" % (s, tags[s]) for s in (main_svcs or sorted(tags))) print("WARN %s: the guide was checked against %s, the image is now %s — re-check the guide's labels " "and set checked_version again (the box still shows it)" % (app, cv, now or "(no image line)")) print("guide-version: %d app(s) · %d guide(s) current · %d may be STALE · %d hidden (checked_version empty) · " "%d without a guide" % (len(apps), len(shown), len(stale), len(hidden), none)) if hidden: print("guide-version: hidden — %s" % ", ".join(hidden)) print("guide-version: advisory — never fails a push") return 0 if __name__ == "__main__": sys.exit(main(sys.argv))