|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Build the dedicated weekly artifact after dispatch validation.""" |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import argparse |
| 7 | +import json |
| 8 | +import sys |
| 9 | +from datetime import date, datetime |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src")) |
| 13 | + |
| 14 | +from political_event_tracking_research.weekly_artifact import WeeklyArtifactError, build_weekly_artifact # noqa: E402 |
| 15 | + |
| 16 | + |
| 17 | +def main() -> None: |
| 18 | + parser = argparse.ArgumentParser(description=__doc__) |
| 19 | + parser.add_argument("--period-start", required=True) |
| 20 | + parser.add_argument("--as-of", required=True) |
| 21 | + parser.add_argument("--generated-at", required=True) |
| 22 | + parser.add_argument("--workflow-ref", required=True) |
| 23 | + parser.add_argument("--source-run-id", required=True) |
| 24 | + parser.add_argument("--producer-ref", required=True) |
| 25 | + parser.add_argument("--source-events", required=True, type=Path) |
| 26 | + parser.add_argument("--watchlist", required=True, type=Path) |
| 27 | + parser.add_argument("--feed-status", required=True, type=Path) |
| 28 | + parser.add_argument("--output-dir", required=True, type=Path) |
| 29 | + parser.add_argument("--run-mode", choices=("scheduled", "manual"), required=True) |
| 30 | + args = parser.parse_args() |
| 31 | + try: |
| 32 | + period_start = date.fromisoformat(args.period_start) |
| 33 | + as_of = date.fromisoformat(args.as_of) |
| 34 | + generated_at = datetime.fromisoformat(args.generated_at.replace("Z", "+00:00")) |
| 35 | + feed_status = json.loads(args.feed_status.read_text(encoding="utf-8")) |
| 36 | + files = build_weekly_artifact( |
| 37 | + period_start=period_start, |
| 38 | + as_of=as_of, |
| 39 | + generated_at=generated_at, |
| 40 | + workflow_ref=args.workflow_ref, |
| 41 | + source_run_id=args.source_run_id, |
| 42 | + producer_ref=args.producer_ref, |
| 43 | + source_events=args.source_events.read_bytes(), |
| 44 | + watchlist=args.watchlist.read_bytes(), |
| 45 | + feed_status=feed_status, |
| 46 | + source_provenance="official_rss_source_pipeline_v1", |
| 47 | + run_mode=args.run_mode, |
| 48 | + ) |
| 49 | + args.output_dir.mkdir(parents=True, exist_ok=True) |
| 50 | + if any(args.output_dir.iterdir()): |
| 51 | + raise WeeklyArtifactError("artifact_output_not_empty") |
| 52 | + for name, content in files.items(): |
| 53 | + (args.output_dir / name).write_bytes(content) |
| 54 | + except WeeklyArtifactError as exc: |
| 55 | + raise SystemExit(exc.code) from None |
| 56 | + except (OSError, UnicodeError, json.JSONDecodeError, TypeError, ValueError, OverflowError): |
| 57 | + raise SystemExit("weekly_artifact_input_invalid") from None |
| 58 | + |
| 59 | + |
| 60 | +if __name__ == "__main__": |
| 61 | + main() |
0 commit comments