From 1a1121150877b44b452bc48f0d50d1f8bd4e62f1 Mon Sep 17 00:00:00 2001 From: Mika Date: Sun, 29 Mar 2026 10:41:48 +0000 Subject: [PATCH] Add artifact_2/src/artifact_2/cli.py --- artifact_2/src/artifact_2/cli.py | 68 ++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 artifact_2/src/artifact_2/cli.py diff --git a/artifact_2/src/artifact_2/cli.py b/artifact_2/src/artifact_2/cli.py new file mode 100644 index 0000000..714df88 --- /dev/null +++ b/artifact_2/src/artifact_2/cli.py @@ -0,0 +1,68 @@ +import argparse +import json +import sys +from pathlib import Path +from typing import Any, List + +from artifact_2.core import LogEntry, check_timestamp_consistency + + +def _load_log_entries(file_path: Path) -> List[LogEntry]: + if not file_path.exists(): + raise FileNotFoundError(f"Input file not found: {file_path}") + with file_path.open("r", encoding="utf-8") as f: + try: + data = json.load(f) + except json.JSONDecodeError as e: + raise ValueError(f"Invalid JSON format: {e}") from e + + if not isinstance(data, list): + raise ValueError("Input JSON must be a list of log entries.") + + entries: List[LogEntry] = [] + for idx, item in enumerate(data): + if not isinstance(item, dict): + raise ValueError(f"Log entry at index {idx} must be an object.") + try: + entry = LogEntry( + epoch_ms=int(item["epoch_ms"]), + monotonic_ns=int(item["monotonic_ns"]), + tz_offset_minutes=int(item["tz_offset_minutes"]), + run_id=str(item["run_id"]), + step_id=str(item["step_id"]), + ) + except KeyError as e: + raise ValueError(f"Missing required field in log entry: {e}") from e + entries.append(entry) + + return entries + + +def _parse_args(argv: List[str]) -> argparse.Namespace: + parser = argparse.ArgumentParser( + description="Check timestamp consistency in log entries." + ) + parser.add_argument( + "--input", + required=True, + help="Path to the JSON file containing log entries.", + ) + return parser.parse_args(argv) + + +def main(argv: List[str] | None = None) -> None: + argv = argv if argv is not None else sys.argv[1:] + args = _parse_args(argv) + input_path = Path(args.input) + + try: + log_entries = _load_log_entries(input_path) + result = check_timestamp_consistency(log_entries) + print("OK" if result else "Inconsistent") + except Exception as e: + print(f"Error: {e}", file=sys.stderr) + sys.exit(1) + + +if __name__ == "__main__": + main() \ No newline at end of file