From e4845ea4460624ffe52ed50ff576f8b868876af1 Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Sun, 22 Nov 2026 03:13:26 +0100 Subject: [PATCH] refactor: one table decides what refresh runs --- scripts/freshness/stalecommands.py | 96 +++++++++++++++++++++--------- scripts/freshness/stalefindings.py | 4 +- scripts/freshness/stalejob.py | 6 +- scripts/freshness/staleplan.py | 37 ++++-------- scripts/refresh_stale.py | 12 ++-- tests/test_measurements.py | 2 +- tests/test_refresh_stale.py | 14 ++++- wiki/tools.md | 6 +- 8 files changed, 106 insertions(+), 71 deletions(-) diff --git a/scripts/freshness/stalecommands.py b/scripts/freshness/stalecommands.py index 9935c8e3d..7ff541d7d 100644 --- a/scripts/freshness/stalecommands.py +++ b/scripts/freshness/stalecommands.py @@ -1,21 +1,31 @@ -"""The command that pulls the upstream of each stale subject, by area.""" +"""The commands that refresh each stale subject, by area, and the areas they reach.""" from __future__ import annotations import sys from collections.abc import Callable +from dataclasses import dataclass from functools import lru_cache from core.paths import DATA_DIRS_FILE, REPO_ROOT from model.registry import data_dir_registry_at from freshness.findings import ( - FBNEO_RECIPES, INSTALLER_PIN, MAME_RECIPES, NO_INTRO_GAMES, REDUMP_GAMES, is_unprofiled_cores, + AREAS, FBNEO_RECIPES, INSTALLER_PIN, MAME_RECIPES, NO_INTRO_GAMES, REDUMP_GAMES, + is_unprofiled_cores, ) from freshness.scrapers import rescrape_command DATA_DIRS_REGISTRY = REPO_ROOT / DATA_DIRS_FILE +@dataclass(frozen=True) +class Chain: + """The first command of a refresh and those run after it while each succeeds.""" + + command: list[str] + then: tuple[tuple[str, ...], ...] = () + + @lru_cache(maxsize=1) def _data_dir_keys() -> frozenset[str]: """The entries refresh_data_dirs.py --key accepts.""" @@ -46,8 +56,36 @@ def export_refresh(platform: str) -> list[str]: return [sys.executable, "scripts/export_native.py", "--platform", platform] -def _platform_command(subject: str, registry: dict[str, dict]) -> list[str] | None: - return rescrape_command("platforms", subject, f"platforms/{subject}.yml", registry) +def _platform_chain(subject: str, registry: dict[str, dict]) -> Chain | None: + command = rescrape_command("platforms", subject, f"platforms/{subject}.yml", registry) + if command is None: + return None + # The truth reads the platform file: regenerated first, or the + # native refresh refuses it as older than the profiles. + return Chain(command, ( + tuple(truth_refresh(subject)), + tuple(native_refresh(subject)), + tuple(export_refresh(subject)), + tuple(verify_command(subject)), + )) + + +def native_chain(subject: str, registry: dict[str, dict] | None = None) -> Chain: + """The export of *subject* rebuilt: truth, original, then the export.""" + # export_native refuses a truth older than the profiles, so the truth + # comes first and the original follows. + return Chain( + truth_refresh(subject), + (tuple(native_refresh(subject)), tuple(export_refresh(subject))), + ) + + +def _ci_chain(subject: str, registry: dict[str, dict]) -> Chain | None: + if subject != INSTALLER_PIN: + # PyPI versions and pinned actions are bumped by a human. + return None + # The pin is the hash of the committed install.py, nothing to judge. + return Chain([sys.executable, "scripts/pin_installer.py"]) def _target_command(subject: str, registry: dict[str, dict]) -> list[str] | None: @@ -103,30 +141,32 @@ def _catalog_command(subject: str, registry: dict[str, dict]) -> list[str] | Non return None -_AREA_COMMANDS: dict[str, Callable[[str, dict[str, dict]], list[str] | None]] = { - "platforms": _platform_command, - "targets": _target_command, - "data": _data_command, - "catalogs": _catalog_command, +def _single( + build: Callable[[str, dict[str, dict]], list[str] | None], +) -> Callable[[str, dict[str, dict]], Chain | None]: + def chain(subject: str, registry: dict[str, dict]) -> Chain | None: + command = build(subject, registry) + return None if command is None else Chain(command) + return chain + + +# The areas a command can refresh. A row of any other area, or one its +# builder returns None for, is listed for a human. +_AREA_CHAINS: dict[str, Callable[[str, dict[str, dict]], Chain | None]] = { + "platforms": _platform_chain, + "native": native_chain, + "targets": _single(_target_command), + "data": _single(_data_command), + "catalogs": _single(_catalog_command), + "ci": _ci_chain, } +AUTO_AREAS = tuple(area for area in AREAS if area in _AREA_CHAINS) +# A rom-folder pin is moved by hand in platforms/romfolders/_sources/, +# then `python -m scripts.scraper.romfolders --setup NAME` reads it again. +MANUAL_AREAS = tuple(area for area in AREAS if area not in _AREA_CHAINS) -def command_for( - area: str, subject: str, registry: dict[str, dict] -) -> list[str] | None: - """Map a stale finding to its refresh command, or None if manual.""" - if area == "native": - # export_native refuses a truth older than the profiles, so the - # truth comes first and the original follows (Job.then). - return truth_refresh(subject) - - if area == "ci" and subject == INSTALLER_PIN: - # The pin is the hash of the committed install.py, nothing to judge. - return [sys.executable, "scripts/pin_installer.py"] - - builder = _AREA_COMMANDS.get(area) - if builder: - return builder(subject, registry) - - # coreinfo, ci, profiles: never auto-refresh. - return None +def refresh_chain(area: str, subject: str, registry: dict[str, dict]) -> Chain | None: + """The refresh of a stale finding, or None when a human resolves it.""" + builder = _AREA_CHAINS.get(area) + return builder(subject, registry) if builder else None diff --git a/scripts/freshness/stalefindings.py b/scripts/freshness/stalefindings.py index f88dd7866..ae5e5544a 100644 --- a/scripts/freshness/stalefindings.py +++ b/scripts/freshness/stalefindings.py @@ -8,7 +8,7 @@ import sys from pathlib import Path from core.paths import REPO_ROOT -from freshness.stalejob import AUTO_AREAS, MANUAL_AREAS +from freshness.findings import AREAS CHECK_FRESHNESS = REPO_ROOT / "scripts" / "check_freshness.py" @@ -16,7 +16,7 @@ CHECK_FRESHNESS = REPO_ROOT / "scripts" / "check_freshness.py" def run_check_freshness(areas: tuple[str, ...], extra: list[str]) -> list[dict]: """Run check_freshness.py --json and return its findings.""" cmd = [sys.executable, str(CHECK_FRESHNESS), "--json"] - if areas and set(areas) != set(AUTO_AREAS + MANUAL_AREAS): + if areas and set(areas) != set(AREAS): cmd += ["--only", ",".join(areas)] cmd += extra try: diff --git a/scripts/freshness/stalejob.py b/scripts/freshness/stalejob.py index ae5d1b085..e34b5d7ae 100644 --- a/scripts/freshness/stalejob.py +++ b/scripts/freshness/stalejob.py @@ -1,4 +1,4 @@ -"""The refresh job a stale row becomes, its result, and the areas refreshed or listed.""" +"""The refresh job a stale row becomes and its result.""" from __future__ import annotations @@ -10,10 +10,6 @@ from core.paths import REPO_ROOT LOG_DIR = REPO_ROOT / "tmp" / "refresh_stale" -AUTO_AREAS = ("platforms", "native", "targets", "data", "catalogs") -# A rom-folder pin is moved by hand in platforms/romfolders/_sources/, -# then `python -m scripts.scraper.romfolders --setup NAME` reads it again. -MANUAL_AREAS = ("romfolders", "coreinfo", "ci", "profiles") @dataclass(frozen=True) diff --git a/scripts/freshness/staleplan.py b/scripts/freshness/staleplan.py index 275b2f454..ba235349e 100644 --- a/scripts/freshness/staleplan.py +++ b/scripts/freshness/staleplan.py @@ -5,13 +5,7 @@ from __future__ import annotations import re from core.paths import REPO_ROOT -from freshness.stalecommands import ( - command_for, - export_refresh, - native_refresh, - truth_refresh, - verify_command, -) +from freshness.stalecommands import native_chain, refresh_chain from freshness.stalejob import LOG_DIR, Job from model.platforms import inheritance_chain from model.registry import load_platform_registry @@ -74,7 +68,7 @@ def plan_jobs( for f in findings if f.get("status") == "STALE" and f.get("area") == "platforms" - and command_for("platforms", str(f.get("subject") or ""), registry) + and refresh_chain("platforms", str(f.get("subject") or ""), registry) is not None } for finding in findings: @@ -93,12 +87,12 @@ def plan_jobs( ignored.append(finding) continue - command = command_for(area, subject, registry) - if command is None: + chain = refresh_chain(area, subject, registry) + if chain is None: surfaced.append(finding) continue - key = (area, " ".join(command)) + key = (area, " ".join(chain.command)) if key in seen: # Two findings funneling to the same command (e.g. mame recipes # listed twice) collapse to one job. @@ -106,24 +100,12 @@ def plan_jobs( seen.add(key) log = LOG_DIR / f"{area}__{_slug(subject)}.log" - then: tuple[tuple[str, ...], ...] = () - if area == "platforms": - # The truth reads the platform file: regenerated first, or the - # native refresh refuses it as older than the profiles. - then = ( - tuple(truth_refresh(subject)), - tuple(native_refresh(subject)), - tuple(export_refresh(subject)), - tuple(verify_command(subject)), - ) - elif area == "native": - then = (tuple(native_refresh(subject)), tuple(export_refresh(subject))) after: frozenset[str] = frozenset() if area in ("platforms", "native"): after = frozenset(_ancestors(subject, parents) & rescraped) jobs.append( - Job(area=area, subject=subject, command=command, log_path=log, - then=then, after=after) + Job(area=area, subject=subject, command=chain.command, log_path=log, + then=chain.then, after=after) ) # The export of a child is built from its parent's file: rewritten once @@ -133,12 +115,13 @@ def plan_jobs( after = frozenset(_ancestors(child, parents) & rescraped) if not after or child in covered: continue + chain = native_chain(child) jobs.append(Job( area="native", subject=child, - command=truth_refresh(child), + command=chain.command, log_path=LOG_DIR / f"native__{_slug(child)}.log", - then=(tuple(native_refresh(child)), tuple(export_refresh(child))), + then=chain.then, after=after, )) diff --git a/scripts/refresh_stale.py b/scripts/refresh_stale.py index d2cb01a18..b5b8ff9ca 100755 --- a/scripts/refresh_stale.py +++ b/scripts/refresh_stale.py @@ -27,6 +27,9 @@ Mapping (stale -> command): --source mame --fetch catalogs/fbneo recipes python -m scripts.scraper.romset_dat_importer --source fbneo --fetch + catalogs/no-intro games python -m scripts.scraper.game_dat_importer + catalogs/redump games --source --fetch --refresh --floor + ci/install.py pin python scripts/pin_installer.py A platform that inherits from a rescraped one (retropie and lakka from retroarch) reads the parent's file: its jobs start once the parent's chain @@ -34,8 +37,7 @@ has ended, and its export is rewritten even when no row named it. Surfaced only (no refresh): data rows that are not a _data_dirs.yml key (the registry itself, buildbot assets it does not track), coreinfo gaps, redump/no-intro/tosec packs, -CI pins (PyPI, pinned actions, while scripts/pin_installer.py writes the -install.py SHA-256 pin), profile_sync, rom-folder +the other CI pins (PyPI, pinned actions), profile_sync, rom-folder pins (moved by hand, then python -m scripts.scraper.romfolders --setup NAME reads them again). Each needs a human read (new .info file, manual DAT download, PyPI version bump). @@ -55,7 +57,8 @@ import sys from pathlib import Path from freshness.stalefindings import load_findings, run_check_freshness -from freshness.stalejob import AUTO_AREAS, LOG_DIR, MANUAL_AREAS +from freshness.stalecommands import AUTO_AREAS, MANUAL_AREAS +from freshness.stalejob import LOG_DIR from freshness.staleplan import plan_jobs, platform_registry from freshness.stalereport import plan_payload, plan_text, render, results_payload from freshness.stalerun import github_token_env, run_jobs @@ -66,7 +69,8 @@ def build_parser() -> argparse.ArgumentParser: parser.add_argument( "--only", help="comma-separated areas to check: " + ",".join(AUTO_AREAS) - + " (refreshed), " + ",".join(MANUAL_AREAS) + " (listed for a human)", + + " (refreshed, a row no command covers is listed for a human), " + + ",".join(MANUAL_AREAS) + " (listed for a human)", ) parser.add_argument( "--dry-run", diff --git a/tests/test_measurements.py b/tests/test_measurements.py index 1c1be08e7..855d1366e 100644 --- a/tests/test_measurements.py +++ b/tests/test_measurements.py @@ -652,7 +652,7 @@ class SitePages(unittest.TestCase): class Freshness(unittest.TestCase): def test_no_measurements_area(self) -> None: from freshness.findings import AREAS - from freshness.stalejob import AUTO_AREAS + from freshness.stalecommands import AUTO_AREAS self.assertNotIn("measurements", AREAS) self.assertNotIn("measurements", AUTO_AREAS) diff --git a/tests/test_refresh_stale.py b/tests/test_refresh_stale.py index 05653c01e..335654418 100644 --- a/tests/test_refresh_stale.py +++ b/tests/test_refresh_stale.py @@ -20,9 +20,9 @@ from unittest import mock import yaml from freshness import stalefindings -from freshness.stalecommands import verify_command +from freshness.stalecommands import AUTO_AREAS, MANUAL_AREAS, verify_command from freshness.stalefindings import load_findings -from freshness.stalejob import AUTO_AREAS, LOG_DIR, MANUAL_AREAS, Job, JobResult +from freshness.stalejob import LOG_DIR, Job, JobResult from freshness.staleplan import plan_jobs from freshness.stalereport import render from freshness.stalerun import run_job, run_jobs @@ -187,6 +187,16 @@ class Planning(unittest.TestCase): self.assertEqual([job.command[1:] for job in jobs], [["scripts/pin_installer.py"]]) self.assertEqual(surfaced, []) + def test_the_help_lists_an_area_with_a_refresher_as_refreshed(self): + """--help listed ci for a human while its install.py pin row ran + scripts/pin_installer.py: both now read the planner's table.""" + self.assertIn("ci", AUTO_AREAS) + self.assertNotIn("ci", MANUAL_AREAS) + for area in MANUAL_AREAS: + with self.subTest(area=area): + jobs, _, _ = self._plan(finding(area, "anything")) + self.assertEqual(jobs, []) + class ProducedRows(unittest.TestCase): """The rows the checks write are the ones the planner knows: a renamed diff --git a/wiki/tools.md b/wiki/tools.md index bfe848c88..58c2d8c2f 100644 --- a/wiki/tools.md +++ b/wiki/tools.md @@ -790,10 +790,12 @@ committed. | `targets/` | the target scraper | | `data/` | `refresh_data_dirs.py --key --force` | | `catalogs/mame recipes`, `catalogs/fbneo recipes` | `scraper/romset_dat_importer.py --source --fetch` | +| `catalogs/no-intro games`, `catalogs/redump games` | `scraper/game_dat_importer.py --source --fetch --refresh --floor` | +| `ci/install.py pin` | `pin_installer.py` | Rows that need a decision are listed and never run: a buildbot core with no -profile, a new `.info` file, the Redump, No-Intro and TOSEC packs, the CI -pins, `profile_sync`, and a rom-folder pin (moved by hand in +profile, a new `.info` file, the Redump, No-Intro and TOSEC packs, the other +CI pins (PyPI, pinned actions), `profile_sync`, and a rom-folder pin (moved by hand in `platforms/romfolders/_sources/`, then `python -m scripts.scraper.romfolders --setup NAME` reads it again). `--dry-run --json` prints the plan as JSON. A failed scraper leaves its cached original