"""Capture public website resources and run a supplied independent browser contract. Local staging command; never publishes or submits a production success receipt. """ import argparse import json from pathlib import Path import subprocess import sys ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / 'packages/core')) from chameleon_core.browser_contract import validate_contract from chameleon_core.intake import fetch_public from chameleon_core.website_bundle_worker import acquire def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('source') parser.add_argument('--contract', type=Path, required=True) parser.add_argument('--output', type=Path, required=True) args = parser.parse_args() contract = json.loads(args.contract.read_text(encoding='utf-8')) validate_contract(contract) # A fresh directory avoids mixing this run with previous results. args.output.mkdir(parents=True, exist_ok=False) body, media = fetch_public(args.source) if media.split(';')[0].strip().lower() != 'text/html': raise ValueError('WEBSITE_HTML_REQUIRED') final_url = getattr(body, 'final_url', args.source) result = acquire(final_url, str(body).encode('utf-8')) (args.output / 'acquisition.json').write_text(json.dumps(result), encoding='utf-8') package_path = args.output / 'package.json' package_path.write_text(json.dumps(result['package']), encoding='utf-8') contract_path = args.output / 'contract.json' contract_path.write_text(json.dumps(contract), encoding='utf-8') (args.output / 'source.json').write_text(json.dumps({ 'requested': args.source, 'final_url': final_url, 'redirects': getattr(body, 'redirects', []), 'representation': 'decoded-html-reencoded-utf8', 'mode': 'available-source-replay', 'production_receipt': False, }), encoding='utf-8') completed = subprocess.run([ sys.executable, str(ROOT / 'scripts/verify_repository_pilot.py'), str(package_path.resolve()), '--website-bundle', '--contract', str(contract_path.resolve()), '--output', str((args.output / 'verification').resolve()), ], cwd=ROOT, timeout=90, check=False) return completed.returncode if __name__ == '__main__': raise SystemExit(main())