Branchwell

Jotley / website / scripts / video-refresh / upload-verified.py

1#!/usr/bin/env python3
2"""Copy only checksum-verified delivery movies to immutable R2 keys and upload them."""
3import hashlib
4import json
5from pathlib import Path
6import shutil
7import subprocess
8
9ROOT = Path(__file__).resolve().parents[2]
10OUT = ROOT / 'output/video-refresh-20261023/workspace/delivery'
11MIRROR = Path('/Volumes/Music/Video/astra_video/website-media/jotley')
12UPLOADER = MIRROR.parent / 'r2_media.py'
13
14
15def main():
16 assert Path('/Volumes/Music').is_mount() and MIRROR.is_dir()
17 published_path = OUT / 'verified-published-files.json'
18 published = json.loads(published_path.read_text()) if published_path.exists() else []
19 previous = {item['key']: item for item in published}
20 files = []
21 for path in sorted(OUT.rglob('*.mp4')):
22 if 'qc' in path.parts:
23 continue
24 receipt = path.with_suffix('.verification.json')
25 if not receipt.exists():
26 continue # Another export may still be writing its movie.
27 evidence = json.loads(receipt.read_text())
28 digest = hashlib.file_digest(path.open('rb'), 'sha256').hexdigest()
29 assert evidence.get('full_decode') and evidence['sha256'] == digest, path
30 relative = path.relative_to(OUT).as_posix()
31 key = relative.removeprefix('website/') if relative.startswith('website/') else 'social/20261023/' + path.relative_to(OUT / 'social').as_posix()
32 dest = MIRROR / key
33 dest.parent.mkdir(parents=True, exist_ok=True)
34 if dest.exists():
35 assert hashlib.file_digest(dest.open('rb'), 'sha256').hexdigest() == digest, f'Immutable key already differs: {key}'
36 else:
37 shutil.copy2(path, dest)
38 files.append({'key': key, 'sha256': digest, 'bytes': path.stat().st_size})
39 (OUT / 'uploaded-files.json').write_text(json.dumps(files, indent=2) + '\n')
40 keys = [item['key'] for item in files if item['key'] not in previous or previous[item['key']]['sha256'] != item['sha256']]
41 if keys:
42 subprocess.run(['python3', '-u', str(UPLOADER), 'upload', 'jotley', *keys], check=True)
43 subprocess.run(['python3', '-u', str(UPLOADER), 'verify', 'jotley', *keys], check=True)
44 published_path.write_text(json.dumps(files, indent=2) + '\n')
45 print(f'Uploaded and verified {len(keys)} new finished movies; {len(files)} verified in total', flush=True)
46
47
48if __name__ == '__main__':
49 main()