#!/usr/bin/env python3
import json,subprocess,sys
from pathlib import Path
PROJECT=Path(__file__).resolve().parents[1];VALIDATOR=PROJECT/'script/validate-production.py';REPORT=PROJECT/'log/production-verification.json';FINAL=PROJECT/'output/final.mp4';UPLOAD=PROJECT/'output/final-upload.mp4';TRANS=PROJECT/'log/upload-transcode.json';META=PROJECT/'output/info.txt'
result=subprocess.run([sys.executable,str(VALIDATOR)],cwd=PROJECT)
if result.returncode:raise SystemExit(result.returncode)
report=json.loads(REPORT.read_text())
if report.get('verified') is not True or not all(x.exists() for x in (FINAL,UPLOAD,TRANS,META)):raise SystemExit('Publish blocked: artifacts missing')
trans=json.loads(TRANS.read_text());server=trans.get('server_verification',{})
if trans.get('verified') is not True or trans.get('status')!='completed' or trans.get('source_canon_sha256')!=report.get('source_canon_sha256') or server.get('verified') is not True or server.get('under_one_gb') is not True or int(server.get('output_bytes',0))!=UPLOAD.stat().st_size or UPLOAD.stat().st_size>=1_000_000_000:raise SystemExit('Publish blocked: upload copy not verified below 1 GB')
print(json.dumps({'publish_ready':True,'source_canon_sha256':report['source_canon_sha256'],'master_video':str(FINAL),'video':str(UPLOAD),'video_bytes':UPLOAD.stat().st_size,'transcode_job_id':trans['job_id'],'metadata':str(META)},ensure_ascii=False))
