#!/usr/bin/env python3 """Copy only checksum-verified delivery movies to immutable R2 keys and upload them.""" import hashlib import json from pathlib import Path import shutil import subprocess ROOT = Path(__file__).resolve().parents[2] OUT = ROOT / 'output/video-refresh-20261023/workspace/delivery' MIRROR = Path('/Volumes/Music/Video/astra_video/website-media/jotley') UPLOADER = MIRROR.parent / 'r2_media.py' def main(): assert Path('/Volumes/Music').is_mount() and MIRROR.is_dir() published_path = OUT / 'verified-published-files.json' published = json.loads(published_path.read_text()) if published_path.exists() else [] previous = {item['key']: item for item in published} files = [] for path in sorted(OUT.rglob('*.mp4')): if 'qc' in path.parts: continue receipt = path.with_suffix('.verification.json') if not receipt.exists(): continue # Another export may still be writing its movie. evidence = json.loads(receipt.read_text()) digest = hashlib.file_digest(path.open('rb'), 'sha256').hexdigest() assert evidence.get('full_decode') and evidence['sha256'] == digest, path relative = path.relative_to(OUT).as_posix() key = relative.removeprefix('website/') if relative.startswith('website/') else 'social/20261023/' + path.relative_to(OUT / 'social').as_posix() dest = MIRROR / key dest.parent.mkdir(parents=True, exist_ok=True) if dest.exists(): assert hashlib.file_digest(dest.open('rb'), 'sha256').hexdigest() == digest, f'Immutable key already differs: {key}' else: shutil.copy2(path, dest) files.append({'key': key, 'sha256': digest, 'bytes': path.stat().st_size}) (OUT / 'uploaded-files.json').write_text(json.dumps(files, indent=2) + '\n') keys = [item['key'] for item in files if item['key'] not in previous or previous[item['key']]['sha256'] != item['sha256']] if keys: subprocess.run(['python3', '-u', str(UPLOADER), 'upload', 'jotley', *keys], check=True) subprocess.run(['python3', '-u', str(UPLOADER), 'verify', 'jotley', *keys], check=True) published_path.write_text(json.dumps(files, indent=2) + '\n') print(f'Uploaded and verified {len(keys)} new finished movies; {len(files)} verified in total', flush=True) if __name__ == '__main__': main()