#!/usr/bin/env python3 """Versioned public-brand exchange. Explicit consumption in a source checkout only.""" import argparse,hashlib,json,shutil,subprocess from pathlib import Path,PurePosixPath CSS={'css/public-site.css','css/public-art-direction.css','css/public-visual-system.css','favicon.svg'} def sha(p):return hashlib.sha256(p.read_bytes()).hexdigest() def allowed(name):return name in CSS or name.startswith('images/public/') p=argparse.ArgumentParser();s=p.add_subparsers(dest='command',required=True) q=s.add_parser('publish');q.add_argument('--source',required=True);q.add_argument('--output',required=True);q.add_argument('--version',required=True) q=s.add_parser('consume');q.add_argument('package');q.add_argument('--target-webroot',required=True);q.add_argument('--manifest-sha256',required=True) a=p.parse_args() if a.command=='publish': root=Path(a.source).resolve();out=Path(a.output).resolve();out.mkdir(exist_ok=False);web=root/'PlotDirector.Website/wwwroot' files={str(f.relative_to(web)):sha(f) for f in sorted(web.rglob('*')) if f.is_file() and allowed(str(f.relative_to(web)))} for name in files: dest=out/name;dest.parent.mkdir(parents=True,exist_ok=True);shutil.copyfile(web/name,dest) manifest={'format':1,'version':a.version,'sourceCommit':subprocess.check_output(['git','rev-parse','HEAD'],cwd=root,text=True).strip(),'files':files} (out/'manifest.json').write_text(json.dumps(manifest,indent=2)+'\n');print(sha(out/'manifest.json')) else: source=Path(a.package).resolve();target=Path(a.target_webroot).resolve() if not (target.parent/'PlotLine.csproj').is_file():raise ValueError('Application source webroot required; deployed directories forbidden') if sha(source/'manifest.json')!=a.manifest_sha256:raise ValueError('Approved manifest digest mismatch') m=json.loads((source/'manifest.json').read_text()) if m['format']!=1:raise ValueError('Unsupported brand format') for name,digest in m['files'].items(): path=PurePosixPath(name) if path.is_absolute() or '..' in path.parts or '\\' in name or not allowed(name) or (source/name).is_symlink() or sha(source/name)!=digest:raise ValueError('Invalid brand member') for name in m['files']: dest=target/name;dest.parent.mkdir(parents=True,exist_ok=True);shutil.copyfile(source/name,dest) print(json.dumps({'consumed':m['version'],'sourceCommit':m['sourceCommit'],'files':len(m['files'])}))