import sys, re from pathlib import Path from datetime import date wiki = Path(sys.argv[1]) report = Path(sys.argv[2]) threshold = int(sys.argv[3]) today = date.today() stale, fresh, nodate = [], [], [] for f in sorted(wiki.rglob('*.md')): rel = f.relative_to(wiki) if str(rel).startswith('raw/'): continue content = f.read_text(errors='ignore') upd = re.search(r'^updated:\s*(\S+)', content[:800], re.M) if not upd: nodate.append(str(rel)) continue try: d = date.fromisoformat(upd.group(1)) except ValueError: nodate.append(str(rel)) continue age = (today - d).days (stale if age > threshold else fresh).append((str(rel), age, upd.group(1))) stale.sort(key=lambda x: -x[1]) lines = [ f"# Wiki freshness audit — {today.isoformat()}", f"Threshold: >{threshold} days since `updated`", "", f"## STALE ({len(stale)})", "", ] for rel, age, d in stale: lines.append(f"- [{age:>3}d] {rel} (updated {d})") lines += [f"", f"## FRESH ({len(fresh)})", ""] for rel, age, d in fresh: lines.append(f"- [{age:>3}d] {rel}") lines += [f"", f"## NO updated DATE ({len(nodate)})", ""] for rel in nodate: lines.append(f"- {rel}") lines += [ "", f"TOTAL: {len(stale)} stale, {len(fresh)} fresh, {len(nodate)} no-date of {len(stale)+len(fresh)+len(nodate)} pages", ] report.parent.mkdir(parents=True, exist_ok=True) report.write_text('\n'.join(lines)) print(f"[2/3] audit: {len(stale)} stale (>={threshold}d), {len(fresh)} fresh, {len(nodate)} no-date -> {report}")