|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Build and locally verify the D3 representative daily preview artifact.""" |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import argparse |
| 6 | +import hashlib |
| 7 | +import json |
| 8 | +import re |
| 9 | +import shutil |
| 10 | +import sys |
| 11 | +import tempfile |
| 12 | +from pathlib import Path |
| 13 | + |
| 14 | +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src")) |
| 15 | + |
| 16 | +from quant_advisor_research.advisory_report import build_advisory_report |
| 17 | +from quant_advisor_research.preview_bundle import PreviewBundleError, build_preview_bundle, read_preview_bundle |
| 18 | + |
| 19 | + |
| 20 | +BASE_SHA_PATTERN = re.compile(r"[0-9a-f]{40}") |
| 21 | +SOURCE_KIND = "repository_representative_fixture" |
| 22 | +CHECKS = [ |
| 23 | + "exact_three_files", |
| 24 | + "canonical_json_readback", |
| 25 | + "manifest_hashes", |
| 26 | + "manifest_source_pair", |
| 27 | + "relative_html_links", |
| 28 | + "repeat_build_bytes", |
| 29 | +] |
| 30 | + |
| 31 | + |
| 32 | +def _require_base_sha(value: str) -> str: |
| 33 | + if BASE_SHA_PATTERN.fullmatch(value) is None: |
| 34 | + raise PreviewBundleError("base_sha_invalid") |
| 35 | + return value |
| 36 | + |
| 37 | + |
| 38 | +def _sha256(path: Path) -> str: |
| 39 | + return hashlib.sha256(path.read_bytes()).hexdigest() |
| 40 | + |
| 41 | + |
| 42 | +def _evidence( |
| 43 | + *, output: Path, report: dict[str, object], manifest: dict[str, object], base_sha: str, repeat_equal: bool |
| 44 | +) -> dict[str, object]: |
| 45 | + return { |
| 46 | + "source_kind": SOURCE_KIND, |
| 47 | + "base_sha": base_sha, |
| 48 | + "bundle_contract": "qar.preview_bundle.v1", |
| 49 | + "source": { |
| 50 | + "cadence": report["cadence"], |
| 51 | + "as_of": report["as_of"], |
| 52 | + "generated_at": report["generated_at"], |
| 53 | + "schema_version": report["schema_version"], |
| 54 | + }, |
| 55 | + "provenance": { |
| 56 | + "political_events": "examples/political_events.example.csv", |
| 57 | + "political_watchlist": "examples/political_watchlist.example.csv", |
| 58 | + }, |
| 59 | + "files": sorted(path.name for path in output.iterdir()), |
| 60 | + "sha256": {name: _sha256(output / name) for name in ("manifest.json", "report.html", "report.json")}, |
| 61 | + "manifest_source": manifest["source"], |
| 62 | + "html_links": ["report.json", "manifest.json"], |
| 63 | + "checks": CHECKS, |
| 64 | + "repeat_build_bytes": repeat_equal, |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | +def build(args: argparse.Namespace) -> None: |
| 69 | + base_sha = _require_base_sha(args.base_sha) |
| 70 | + output = Path(args.artifact_dir) |
| 71 | + events = Path(args.political_events) |
| 72 | + watchlist = Path(args.political_watchlist) |
| 73 | + if not events.is_file() or not watchlist.is_file(): |
| 74 | + raise PreviewBundleError("fixture_missing") |
| 75 | + report = build_advisory_report( |
| 76 | + as_of=args.as_of, |
| 77 | + cadence=args.cadence, |
| 78 | + political_events_path=events, |
| 79 | + political_watchlist_path=watchlist, |
| 80 | + ) |
| 81 | + build_preview_bundle(report, output) |
| 82 | + evidence = read_preview_bundle(output) |
| 83 | + repeat_parent = Path(tempfile.mkdtemp(prefix=f".{output.name}.repeat-", dir=output.parent)) |
| 84 | + try: |
| 85 | + repeat_output = repeat_parent / "preview" |
| 86 | + build_preview_bundle(report, repeat_output) |
| 87 | + repeat_equal = all( |
| 88 | + (output / name).read_bytes() == (repeat_output / name).read_bytes() |
| 89 | + for name in ("manifest.json", "report.html", "report.json") |
| 90 | + ) |
| 91 | + finally: |
| 92 | + shutil.rmtree(repeat_parent, ignore_errors=True) |
| 93 | + if not repeat_equal: |
| 94 | + raise PreviewBundleError("repeat_build_non_deterministic") |
| 95 | + payload = _evidence( |
| 96 | + output=output, |
| 97 | + report=dict(evidence.report), |
| 98 | + manifest=dict(evidence.manifest), |
| 99 | + base_sha=base_sha, |
| 100 | + repeat_equal=repeat_equal, |
| 101 | + ) |
| 102 | + evidence_path = Path(args.evidence_path) |
| 103 | + if evidence_path.exists(): |
| 104 | + raise PreviewBundleError("evidence_exists") |
| 105 | + evidence_path.write_text(json.dumps(payload, ensure_ascii=False, indent=2) + "\n", encoding="utf-8") |
| 106 | + print(json.dumps(payload, ensure_ascii=False, sort_keys=True)) |
| 107 | + |
| 108 | + |
| 109 | +def parser() -> argparse.ArgumentParser: |
| 110 | + result = argparse.ArgumentParser() |
| 111 | + result.add_argument("--as-of", required=True) |
| 112 | + result.add_argument("--cadence", choices=("daily", "weekly", "monthly"), default="daily") |
| 113 | + result.add_argument("--political-events", required=True) |
| 114 | + result.add_argument("--political-watchlist", required=True) |
| 115 | + result.add_argument("--artifact-dir", required=True) |
| 116 | + result.add_argument("--evidence-path", required=True) |
| 117 | + result.add_argument("--base-sha", required=True) |
| 118 | + return result |
| 119 | + |
| 120 | + |
| 121 | +def main() -> None: |
| 122 | + try: |
| 123 | + build(parser().parse_args()) |
| 124 | + except PreviewBundleError as exc: |
| 125 | + print(f"daily_preview_build_failed:{exc.code}", file=sys.stderr) |
| 126 | + raise SystemExit(1) from None |
| 127 | + except (OSError, TypeError, ValueError, UnicodeError): |
| 128 | + print("daily_preview_build_failed", file=sys.stderr) |
| 129 | + raise SystemExit(1) from None |
| 130 | + |
| 131 | + |
| 132 | +if __name__ == "__main__": |
| 133 | + main() |
0 commit comments