PulseCanvas / app · pulsecanvas.ai / scripts / audit-cream-library.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Account for the pinned upstream tree and verify every bundled Git blob.""" |
| 3 | import argparse |
| 4 | import collections |
| 5 | import hashlib |
| 6 | import json |
| 7 | from pathlib import Path |
| 8 | |
| 9 | root = Path(__file__).resolve().parents[1] |
| 10 | parser = argparse.ArgumentParser(description=__doc__) |
| 11 | parser.add_argument('--tree', type=Path, required=True, help='GitHub recursive Git tree JSON at the pin') |
| 12 | parser.add_argument('--output', type=Path, required=True) |
| 13 | args = parser.parse_args() |
| 14 | assets = root / 'Sources/PulseCanvas/Resources/ProjectM' |
| 15 | tree = json.loads(args.tree.read_text()) |
| 16 | manifest = json.loads((assets / 'preset-manifest.json').read_text()) |
| 17 | assert tree['sha'] == manifest['upstreamCommit'] and not tree.get('truncated') |
| 18 | upstream = {x['path']: x['sha'] for x in tree['tree'] if x['type'] == 'blob' and x['path'].endswith('.milk')} |
| 19 | catalog = json.loads((assets / 'catalog.json').read_text()) |
| 20 | retired = json.loads((assets / 'retired-presets.json').read_text()) |
| 21 | active = {x['path'] for x in catalog} |
| 22 | excluded = {x['id'].removeprefix('projectm:') for x in retired} |
| 23 | assert len(upstream) == 9795 and len(active) == len(catalog) |
| 24 | assert active.isdisjoint(excluded) and active | excluded == set(upstream), 'Upstream accounting differs' |
| 25 | for path in active: |
| 26 | raw = (assets / 'Presets' / path).read_bytes() |
| 27 | git_blob = hashlib.sha1(b'blob ' + str(len(raw)).encode() + b'\0' + raw).hexdigest() |
| 28 | assert git_blob == upstream[path], f'Changed upstream bytes: {path}' |
| 29 | for local, original in [('Preset-README.md', 'README.md'), ('Preset-notice.md', 'LICENSE.md')]: |
| 30 | raw = (assets / local).read_bytes() |
| 31 | blob = next(x['sha'] for x in tree['tree'] if x['path'] == original) |
| 32 | assert hashlib.sha1(b'blob ' + str(len(raw)).encode() + b'\0' + raw).hexdigest() == blob |
| 33 | curation = json.loads((assets / 'preset-curation.json').read_text()) |
| 34 | removed = {x for batch in curation['batches'] for x in batch['removedIDs']} |
| 35 | assert all(x.removeprefix('projectm:') in excluded for x in removed) |
| 36 | receipt = json.loads((assets / 'preset-admissions.json').read_text()) |
| 37 | report = dict(schemaVersion=1, upstreamCommit=tree['sha'], upstreamMilkCount=len(upstream), |
| 38 | bundledCount=len(active), retiredCount=len(excluded), missingCount=0, |
| 39 | exactUpstreamBlobMatches=len(active), upstreamNoticesUnchanged=True, |
| 40 | ownerReviewedRejectionsPreserved=len(removed), restoredCount=len(receipt['presets']), |
| 41 | restoredReceipt='Sources/PulseCanvas/Resources/ProjectM/preset-admissions.json', |
| 42 | exclusionsByReason=dict(sorted(collections.Counter(x['reason'] for x in retired).items())), |
| 43 | rightsStatus='Technical compatibility evidence only; individual preset and texture permissions remain unresolved.') |
| 44 | args.output.parent.mkdir(parents=True, exist_ok=True) |
| 45 | args.output.write_text(json.dumps(report, indent=2) + '\n') |
| 46 | print(json.dumps({k: report[k] for k in ['bundledCount', 'retiredCount', 'missingCount', 'restoredCount']})) |