|
| 1 | +"""Write the website's syntax figures from a coverage summary, or check that they match it.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import argparse |
| 6 | +import datetime |
| 7 | +import json |
| 8 | +import sys |
| 9 | +from pathlib import Path |
| 10 | +from typing import Any |
| 11 | + |
| 12 | +from scripts.website_figures._helpers.figures import figure_problems, summary_figures |
| 13 | +from scripts.website_figures.constants import ( |
| 14 | + BASIS_KEY, |
| 15 | + BASIS_TEMPLATE, |
| 16 | + CHECK_COMMAND, |
| 17 | + FIGURES_FILE, |
| 18 | + FORMS_KEY, |
| 19 | + JSON_INDENT, |
| 20 | + VERIFIED_KEY, |
| 21 | + WRITE_COMMAND, |
| 22 | +) |
| 23 | + |
| 24 | + |
| 25 | +def run_figures(argv: list[str] | None = None) -> int: |
| 26 | + """Write or check the syntax figures against `syntax-coverage summary --json` output.""" |
| 27 | + parser: argparse.ArgumentParser = argparse.ArgumentParser(description=run_figures.__doc__) |
| 28 | + parser.add_argument("command", choices=(WRITE_COMMAND, CHECK_COMMAND)) |
| 29 | + parser.add_argument("summary", type=Path, help="the JSON `syntax-coverage summary` printed") |
| 30 | + parser.add_argument("--figures", type=Path, default=Path(FIGURES_FILE)) |
| 31 | + parser.add_argument("--day", type=datetime.date.fromisoformat, default=datetime.date.today()) |
| 32 | + arguments: argparse.Namespace = parser.parse_args(argv) |
| 33 | + expected: dict[str, str] = summary_figures(json.loads(arguments.summary.read_text())) |
| 34 | + figures: dict[str, Any] = json.loads(arguments.figures.read_text(encoding="utf-8")) |
| 35 | + if arguments.command == WRITE_COMMAND: |
| 36 | + day: datetime.date = arguments.day |
| 37 | + basis: str = BASIS_TEMPLATE.format( |
| 38 | + verified=expected[VERIFIED_KEY], day=f"{day.day} {day:%B %Y}" |
| 39 | + ) |
| 40 | + figures.update({**expected, BASIS_KEY: basis}) |
| 41 | + text: str = json.dumps(figures, indent=JSON_INDENT, ensure_ascii=False) + "\n" |
| 42 | + arguments.figures.write_text(text, encoding="utf-8") |
| 43 | + print(f"website figures: wrote {basis}") |
| 44 | + return 0 |
| 45 | + problems: list[str] = figure_problems(figures=figures, expected=expected) |
| 46 | + for problem in problems: |
| 47 | + print(f"website figures: {problem}", file=sys.stderr) |
| 48 | + if problems: |
| 49 | + print( |
| 50 | + f"website figures: rewrite them with `make website-figures-update`, then commit " |
| 51 | + f"{FIGURES_FILE}", |
| 52 | + file=sys.stderr, |
| 53 | + ) |
| 54 | + return 1 |
| 55 | + print(f"website figures: passed, {expected[VERIFIED_KEY]} of {expected[FORMS_KEY]} forms") |
| 56 | + return 0 |
0 commit comments