#!/usr/bin/env python3 """Checks the shape of construction files, as the Submission format in README.md states it: JSON objects with a nonempty list "values" of at most 16384 heights (numbers, "p/q" strings, or integers over a positive integer "denominator"). It does not check admissibility and computes no score. python3 tools/check_format.py ...""" import json import pathlib import re import sys RATIONAL = re.compile(r"^\s*[+-]?\d+(\.\d+)?([eE][+-]?\d+)?(\s*/\s*\d+)?\s*$") def problems(path): try: data = json.loads(path.read_text()) except Exception as exc: return [f"not JSON ({exc})"] if not isinstance(data, dict) or "values" not in data: return None # not a construction: the check skips it values, out = data["values"], [] if not isinstance(values, list) or not values: return ['"values" must be a nonempty list'] if len(values) > 16384: out.append(f'"values" has {len(values)} heights; at most 16384') den = data.get("denominator") if den is not None and (isinstance(den, bool) or not isinstance(den, int) or den <= 0): out.append('"denominator" must be a positive integer') for i, v in enumerate(values): ok = (isinstance(v, (int, float)) and not isinstance(v, bool) and v == v and v not in (float("inf"), float("-inf"))) \ or (isinstance(v, str) and RATIONAL.match(v)) if not ok: out.append(f"height {i} is not a finite number or a 'p/q' string: {v!r}") break return out bad = 0 for arg in sys.argv[1:] or ["."]: root = pathlib.Path(arg) for path in sorted(root.rglob("*.json")) if root.is_dir() else [root]: found = problems(path) if found is None: continue print(f"{path}: {'ok' if not found else '; '.join(found)}") bad += bool(found) sys.exit(1 if bad else 0)