Branchwell

PulseCanvas / app · pulsecanvas.ai / scripts / audit-cream-library.py

Raw 2.8 KB on 23ffa4e102ea595ec83f2b18683b75332fc815c3
1#!/usr/bin/env python3
2"""Account for the pinned upstream tree and verify every bundled Git blob."""
3import argparse
4import collections
5import hashlib
6import json
7from pathlib import Path
8
9root = Path(__file__).resolve().parents[1]
10parser = argparse.ArgumentParser(description=__doc__)
11parser.add_argument('--tree', type=Path, required=True, help='GitHub recursive Git tree JSON at the pin')
12parser.add_argument('--output', type=Path, required=True)
13args = parser.parse_args()
14assets = root / 'Sources/PulseCanvas/Resources/ProjectM'
15tree = json.loads(args.tree.read_text())
16manifest = json.loads((assets / 'preset-manifest.json').read_text())
17assert tree['sha'] == manifest['upstreamCommit'] and not tree.get('truncated')
18upstream = {x['path']: x['sha'] for x in tree['tree'] if x['type'] == 'blob' and x['path'].endswith('.milk')}
19catalog = json.loads((assets / 'catalog.json').read_text())
20retired = json.loads((assets / 'retired-presets.json').read_text())
21active = {x['path'] for x in catalog}
22excluded = {x['id'].removeprefix('projectm:') for x in retired}
23assert len(upstream) == 9795 and len(active) == len(catalog)
24assert active.isdisjoint(excluded) and active | excluded == set(upstream), 'Upstream accounting differs'
25for 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}'
29for 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
33curation = json.loads((assets / 'preset-curation.json').read_text())
34removed = {x for batch in curation['batches'] for x in batch['removedIDs']}
35assert all(x.removeprefix('projectm:') in excluded for x in removed)
36receipt = json.loads((assets / 'preset-admissions.json').read_text())
37report = 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.')
44args.output.parent.mkdir(parents=True, exist_ok=True)
45args.output.write_text(json.dumps(report, indent=2) + '\n')
46print(json.dumps({k: report[k] for k in ['bundledCount', 'retiredCount', 'missingCount', 'restoredCount']}))