Files
app-catalog-felhom.eu/scripts/check-download-binds.py
T

144 lines
6.3 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""check-download-binds.py — a download client's download folder is never backed up (`excluded`).
python3 scripts/check-download-binds.py # every template under templates/
python3 scripts/check-download-binds.py --root DIR # another checkout (the decoy harness)
WHY (R-1019, 2026-10-20; felhom.eu `architecture/15-media-library.md` §7). A download folder is large and can be
fetched again. Declared `optional`, it is mirrored to the household's second drive every night (Tier 2 = mandatory +
optional) and can fill it with films; left UNLISTED, the controller classes a writable `${…_PATH}` bind MANDATORY and
it goes off-site too (`stacks/classify_binds.go`; measured 2026-10-14 for Transmission's watch folder). So every
download folder is declared `excluded`: no tier copies it (`appbackup.tierKeeps`).
WHAT IS A DOWNLOAD FOLDER — judged by the FACT, not by an app name: a `${USERDATA_PATH}/<folder>` bind of
templates/<app>/docker-compose.yml whose CONTAINER target is `/downloads` or `/watch` (or below them) — where every
download client of the catalog writes (qBittorrent, Transmission, NZBGet, SABnzbd on 2026-10-20). The same folder
bound a second time elsewhere (qBittorrent binds it at its own path too) is the same folder. Its class is read from
.felhom.yml `backup: userdata:` (`- path: <folder>` + `class:`); it must be `excluded`. NOT judged, on purpose:
steps/<key>.yml — a ladder step carries the compose only; the backup classes live in .felhom.yml alone.
STANDARD LIBRARY ONLY (the CI runner has no PyYAML); comments never count.
Exit 0 clean · 1 a download folder is optional, mandatory or unlisted · 3 nothing to judge.
"""
import io
import os
import re
import sys
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DOWNLOAD_TARGETS = ("/downloads", "/watch")
# R-1283 (operator D44 #25, 2026-10-25; felhom.eu `architecture/15-media-library.md` §7): a download folder the operator
# ruled worth a second-drive copy. It may be `optional` (Tier 2 copies it, off-site does not) — never mandatory and never
# unlisted. Only by a ruling named here; every other download client stays `excluded`.
OPTIONAL_BY_RULING = {
"metube": "a MeTube video may be gone from its source site, so it cannot be fetched again (D44 #25, R-1283)",
}
BIND = re.compile(r"""^\s*-\s*["']?\$\{USERDATA_PATH\}/([^:"'\s]+):([^:"'\s]+)""")
def strip_comment(line):
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 is_download_target(target):
t = target.rstrip("/")
return any(t == d or t.startswith(d + "/") for d in DOWNLOAD_TARGETS)
def download_folders(compose):
"""The userdata folders a compose binds at a download target (sorted, de-duplicated)."""
out = set()
with io.open(compose, encoding="utf-8") as f:
for raw in f:
m = BIND.match(strip_comment(raw.rstrip("\n")))
if m and is_download_target(m.group(2)):
out.add(m.group(1).rstrip("/"))
return sorted(out)
def userdata_classes(felhom):
"""{path: class} from .felhom.yml `backup:` → `userdata:` — a line reader for the catalog's own shape."""
out, in_backup, in_userdata, cur = {}, False, False, None
if not os.path.exists(felhom):
return out
with io.open(felhom, encoding="utf-8") as f:
for raw in f:
line = strip_comment(raw.rstrip("\n"))
if not line.strip():
continue
ind = len(line) - len(line.lstrip(" "))
if ind == 0:
in_backup, in_userdata, cur = line.startswith("backup:"), False, None
continue
if not in_backup:
continue
if ind == 2:
in_userdata, cur = line.strip() == "userdata:", None
continue
if not in_userdata:
continue
m = re.match(r"^\s*-\s*path:\s*['\"]?([^'\"]+?)['\"]?\s*$", line)
if m:
cur = m.group(1).strip().rstrip("/")
out[cur] = ""
continue
m = re.match(r"^\s*class:\s*['\"]?([A-Za-z]+)['\"]?\s*$", line)
if m and cur is not None:
out[cur] = m.group(1)
return out
def main(argv):
root = ROOT
for a in argv:
if a.startswith("--root="):
root = a.split("=", 1)[1]
tdir = os.path.join(root, "templates")
if not os.path.isdir(tdir):
print("download-binds: nothing to judge — no templates/ under %s" % root)
return 3
judged, bad = [], []
for app in sorted(os.listdir(tdir)):
compose = os.path.join(tdir, app, "docker-compose.yml")
if not os.path.isfile(compose):
continue
folders = download_folders(compose)
if not folders:
continue
classes = userdata_classes(os.path.join(tdir, app, ".felhom.yml"))
for folder in folders:
cls = classes.get(folder)
judged.append("%s:%s=%s" % (app, folder, cls or "UNLISTED"))
if cls == "optional" and app in OPTIONAL_BY_RULING:
continue # the ruling's one allowed class besides excluded (R-1283)
if cls != "excluded":
why = ("UNLISTED in backup.userdata — the controller classes it MANDATORY (every tier, off-site included)"
if cls is None else "declared `%s` — %s" % (cls, "a nightly copy on the second drive" if cls == "optional" else "copied by the backup"))
bad.append("templates/%s: the download folder %s is %s; declare it `class: excluded` (R-1019)" % (app, folder, why))
if not judged:
print("download-binds: nothing to judge — no template binds a folder at %s" % " or ".join(DOWNLOAD_TARGETS))
return 3
if bad:
print("download-binds gate FAILED (%d of %d download folder(s)):" % (len(bad), len(judged)))
for b in bad:
print(" " + b)
return 1
print("download-binds gate OK — %d download folder(s), every one `excluded` or `optional` by a named ruling (%s): %s"
% (len(judged), ", ".join(sorted(OPTIONAL_BY_RULING)), ", ".join(judged)))
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))