#!/usr/bin/env python3 """Domienia 14 przypadkow CONTENT_DIFF, ktore zostaly zweryfikowane bajtowo (sha256 https == suma w recepturze, albo sha256 http == sha256 https). Te same zasady co w apply-https.py: granica dopasowania + walidacja YAML. """ import json import os import re import sys import yaml R = "/var/lib/pagan-sync/recipes" APPLY = "--apply" in sys.argv verdict = json.load(open("/tmp/content-diff-verdict.json")) audit = json.load(open("/tmp/https-audit.json")) targets = {v[2] for v in verdict["verified"] if v[1] in ("source", "url")} entries = [e for e in audit["CONTENT_DIFF"] if e["raw"] in targets] print(f"do podmiany: {len(entries)} wystapien (zweryfikowanych bajtowo)") def pat(u): return re.compile(re.escape(u) + r'(?=["\'\s\],]|$)') by_file = {} for e in entries: by_file.setdefault(e["recipe"], []).append(e) changed = ok = problems = 0 for rel, items in sorted(by_file.items()): p = f"{R}/{rel}" text = open(p, newline="", encoding="utf-8").read() before = yaml.safe_load(text) new = text n_applied = 0 for it in items: new2, n = pat(it["raw"]).subn("https://" + it["raw"][7:], new, count=1) if n: n_applied += n else: print(f" ! nie znaleziono doslownie: {rel} :: {it['raw']}") problems += 1 new = new2 if not n_applied: continue after = yaml.safe_load(new) # walidacja: zmienily sie tylko source/sources/url bad = [k for k in set(before) | set(after) if k not in ("source", "sources", "url") and before.get(k) != after.get(k)] if bad: print(f" ! {rel}: nieoczekiwana zmiana kluczy {bad}") problems += 1 continue if APPLY: with open(p, "w", newline="", encoding="utf-8") as f: f.write(new) changed += 1 ok += n_applied print(f" {'ZMIENIONO' if APPLY else 'DRY'}: {rel} ({n_applied})") print(f"\nplikow: {changed} | wystapien: {ok} | problemow: {problems} | tryb: {'APPLY' if APPLY else 'DRY-RUN'}")