From 5d8320f7358f2c67ac9cf62f31ba48ec59756d9b Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Mon, 23 Nov 2026 23:43:57 +0100 Subject: [PATCH] fix: watch the source rom folders are read from --- scripts/freshness/romfolders.py | 98 ++++++++++++++++++-------------- scripts/model/romfolders.py | 17 ++++++ scripts/scraper/romfolders/es.py | 14 ++--- tests/test_check_freshness.py | 30 +++++++++- tests/test_romfolders_card.py | 23 +++++++- wiki/tools.md | 5 +- 6 files changed, 133 insertions(+), 54 deletions(-) diff --git a/scripts/freshness/romfolders.py b/scripts/freshness/romfolders.py index 9beefe878..4b1970379 100644 --- a/scripts/freshness/romfolders.py +++ b/scripts/freshness/romfolders.py @@ -6,6 +6,7 @@ from pathlib import Path from core import upstream from model.registry import load_platform_registry +from model.romfolders import row_sources from model.sources import source_yaml_file from model.sourcerevision import scraped_revision from freshness.findings import OK, SKIPPED, STALE, UNKNOWN, Finding @@ -19,31 +20,39 @@ def romfolder_sources(platforms_dir: Path) -> dict[str, dict]: return out -def _os_source(doc: dict) -> dict: - """The source the setup is pinned on: `os`, else the first declared. - The others follow from it (an EmulationStation pinned by a package.mk).""" +def watched_sources(doc: dict) -> list[str]: + """The declared sources whose pins are compared: the setup's own pin + (`os`, else the first declared), then every source its rows are read + from. A source that only follows another (an EmulationStation pinned by + a package.mk) is not compared on its own.""" sources = doc.get("sources") or {} - return sources.get("os") or next(iter(sources.values()), {}) + own = ["os"] if "os" in sources else list(sources)[:1] + return list(dict.fromkeys(own + [k for k in row_sources(doc) if k in sources])) -def platform_revision(setup: str, doc: dict, platforms_dir: Path) -> str: +def _subject(setup: str, doc: dict, key: str) -> str: + """The setup names the first watched pin, `setup:key` each other one.""" + return setup if key == watched_sources(doc)[0] else f"{setup}:{key}" + + +def platform_revision(setup: str, source: dict, platforms_dir: Path) -> str: """The revision the setup's own platform file was scraped at, when the setup is a registered platform read from the same repository; '' else.""" if setup not in load_platform_registry(str(platforms_dir)): return "" - repo = upstream.parse_repo(str(_os_source(doc).get("repo") or "")) + repo = upstream.parse_repo(str(source.get("repo") or "")) if repo is None: return "" return scraped_revision(platforms_dir / f"{setup}.yml", repo.slug) -def romfolder_pin_drift(setup: str, doc: dict, platforms_dir: Path) -> str | None: +def romfolder_pin_drift(setup: str, source: dict, platforms_dir: Path) -> str | None: """Why the folders and the BIOS of one platform come from two revisions, None when they do not or when only the forge can tell (a tag on one - side, a commit on the other).""" - revision = platform_revision(setup, doc, platforms_dir) - os_source = _os_source(doc) - tag, commit = os_source.get("tag"), str(os_source.get("commit") or "") + side, a commit on the other). `source` is one declared source of the + setup: only one read from the platform file's repository can drift.""" + revision = platform_revision(setup, source, platforms_dir) + tag, commit = source.get("tag"), str(source.get("commit") or "") if not revision or revision in (tag, commit): return None if tag or len(revision) == len(commit): @@ -60,58 +69,61 @@ def check_romfolders(platforms_dir: Path, cache_dir: str, offline: bool) -> list compared with its project's newest release, or with its branch head when the project publishes none. A pin is moved by hand, then `python -m scripts.scraper.romfolders --setup NAME` reads it again. + Each watched source is one row, the second and later named `setup:key`. """ - findings: list[Finding] = [] - for setup, doc in romfolder_sources(platforms_dir).items(): - os_source = _os_source(doc) - pin = str(os_source.get("tag") or str(os_source.get("commit") or "")[:12]) - drift = romfolder_pin_drift(setup, doc, platforms_dir) - if drift: - findings.append(Finding("romfolders", setup, STALE, local=pin, detail=drift)) - continue - revision = platform_revision(setup, doc, platforms_dir) - repo = upstream.parse_repo(str(os_source.get("repo") or "")) - commit = str(os_source.get("commit") or "") - if revision and revision in (os_source.get("tag"), commit): - findings.append(Finding("romfolders", setup, OK, local=pin, - detail=f"read where platforms/{setup}.yml is")) - continue - if offline: - findings.append(Finding("romfolders", setup, SKIPPED, local=pin, detail="offline")) - continue - if repo is None or not commit: - findings.append(Finding("romfolders", setup, UNKNOWN, local=pin, detail="no forge to ask")) - continue - findings.append(_romfolder_upstream(setup, repo, os_source, revision, cache_dir)) - return findings + return [ + _romfolder_pin(_subject(setup, doc, key), setup, doc["sources"][key], platforms_dir, cache_dir, offline) + for setup, doc in romfolder_sources(platforms_dir).items() + for key in watched_sources(doc) + ] + + +def _romfolder_pin( + subject: str, setup: str, source: dict, platforms_dir: Path, cache_dir: str, offline: bool, +) -> Finding: + """One declared source's pin against the platform file, else its forge.""" + pin = str(source.get("tag") or str(source.get("commit") or "")[:12]) + drift = romfolder_pin_drift(setup, source, platforms_dir) + if drift: + return Finding("romfolders", subject, STALE, local=pin, detail=drift) + revision = platform_revision(setup, source, platforms_dir) + repo = upstream.parse_repo(str(source.get("repo") or "")) + commit = str(source.get("commit") or "") + if revision and revision in (source.get("tag"), commit): + return Finding("romfolders", subject, OK, local=pin, detail=f"read where platforms/{setup}.yml is") + if offline: + return Finding("romfolders", subject, SKIPPED, local=pin, detail="offline") + if repo is None or not commit: + return Finding("romfolders", subject, UNKNOWN, local=pin, detail="no forge to ask") + return _romfolder_upstream(setup, subject, repo, source, revision, cache_dir) def _romfolder_upstream( - setup: str, repo: upstream.Repo, os_source: dict, revision: str, cache_dir: str, + setup: str, subject: str, repo: upstream.Repo, source: dict, revision: str, cache_dir: str, ) -> Finding: - commit, tag = str(os_source["commit"]), os_source.get("tag") + commit, tag = str(source["commit"]), source.get("tag") pin = str(tag or commit[:12]) if revision: # The platform names a tag and the setup a commit: the tag's commit decides. tagged = upstream.resolve_tag_commit(repo, revision, cache_dir) if tagged is None: - return Finding("romfolders", setup, UNKNOWN, local=pin, detail=f"tag {revision} not listed") + return Finding("romfolders", subject, UNKNOWN, local=pin, detail=f"tag {revision} not listed") if tagged == commit: - return Finding("romfolders", setup, OK, local=pin, detail=f"commit of {revision}") - return Finding("romfolders", setup, STALE, local=pin, remote=revision, + return Finding("romfolders", subject, OK, local=pin, detail=f"commit of {revision}") + return Finding("romfolders", subject, STALE, local=pin, remote=revision, detail=f"platforms/{setup}.yml was scraped at {revision} ({tagged[:12]})") pinned_at = (upstream.commit_date(repo, commit, cache_dir) or "")[:10] release = upstream.latest_release(repo, cache_dir) if release is not None and release.date: newer = release.tag != tag and release.date > pinned_at - return Finding("romfolders", setup, STALE if newer else OK, local=f"{pin} ({pinned_at})", + return Finding("romfolders", subject, STALE if newer else OK, local=f"{pin} ({pinned_at})", remote=f"{release.tag} ({release.date})", detail="a release is newer than the pin" if newer else "newest release") head = upstream.resolve_head(repo, cache_dir) if head is None: - return Finding("romfolders", setup, UNKNOWN, local=pin, detail="branch head unavailable") + return Finding("romfolders", subject, UNKNOWN, local=pin, detail="branch head unavailable") if head == commit: - return Finding("romfolders", setup, OK, local=pin, detail="branch head") + return Finding("romfolders", subject, OK, local=pin, detail="branch head") counts = upstream.ancestry(repo, commit, head, cache_dir) behind = f"{counts[0]} commits behind the branch head" if counts else "the branch head moved" - return Finding("romfolders", setup, STALE, local=pin, remote=head[:12], detail=behind) + return Finding("romfolders", subject, STALE, local=pin, remote=head[:12], detail=behind) diff --git a/scripts/model/romfolders.py b/scripts/model/romfolders.py index f3e307172..d2aadac8b 100644 --- a/scripts/model/romfolders.py +++ b/scripts/model/romfolders.py @@ -65,6 +65,23 @@ def source_refs(doc: dict) -> Iterator[str]: yield from (item["source_ref"] for item in row.get(key) or () if item.get("source_ref")) +# The source a parser reads its rows from when `params` names none. +DEFAULT_ROW_SOURCE = "os" + + +def row_sources(spec: dict) -> list[str]: + """The declared source keys a setup's rows are read from, in order: + `params.source`, then each `source` nested in `params` (an ES-DE + system list, a custom file, a linked selector), else the default.""" + params = spec.get("params") or {} + keys = [params["source"]] if isinstance(params.get("source"), str) else [] + for value in params.values(): + items = value if isinstance(value, list) else [value] + keys += [item["source"] for item in items + if isinstance(item, dict) and isinstance(item.get("source"), str)] + return list(dict.fromkeys(keys)) or [DEFAULT_ROW_SOURCE] + + def setup_paths(directory: Path = ROMFOLDERS_DIR) -> list[Path]: """The setup files of a directory, without the sources file.""" return sorted(p for p in Path(directory).glob("*.yml") if not p.name.startswith("_")) diff --git a/scripts/scraper/romfolders/es.py b/scripts/scraper/romfolders/es.py index 3503b5f19..da50655db 100644 --- a/scripts/scraper/romfolders/es.py +++ b/scripts/scraper/romfolders/es.py @@ -31,7 +31,7 @@ import shlex from collections import Counter from collections.abc import Iterator -from model.romfolders import format_ref +from model.romfolders import DEFAULT_ROW_SOURCE, format_ref from model.safeparse import parse_untrusted_xml, yaml_load_upstream from ..retropie_modules import PLATFORM_FLAGS, module_enabled, module_fields @@ -135,7 +135,7 @@ def batocera_es(spec: dict, fetcher: Fetcher) -> list[dict]: ConfiggenDefaults.get). """ params = spec["params"] - key = params.get("source", "os") + key = params.get("source", DEFAULT_ROW_SOURCE) es_path = params["es_systems"] es_text = fetcher.text(key, es_path) systems = yaml_load_upstream(es_text) @@ -259,7 +259,7 @@ def es_xml(spec: dict, fetcher: Fetcher) -> list[dict]: installs instead (dArkOS copies es_systems.cfg.): a system whose default differs there gets that board's default.""" params = spec["params"] - key = params.get("source", "os") + key = params.get("source", DEFAULT_ROW_SOURCE) rows = _xml_rows(fetcher.text(key, params["file"]), key, params["file"], params) for board, path in sorted((params.get("boards") or {}).items()): other = {r["name"]: r for r in _xml_rows(fetcher.text(key, path), key, path, params)} @@ -334,7 +334,7 @@ def _xml_rows(text: str, key: str, path: str, params: dict) -> list[dict]: @register("es-json") def es_json(spec: dict, fetcher: Fetcher) -> list[dict]: params = spec["params"] - key = params.get("source", "os") + key = params.get("source", DEFAULT_ROW_SOURCE) path = params["file"] text = fetcher.text(key, path) lines = _all_lines(text, r'^\s{4,6}"name":\s*"([^"]+)"') @@ -476,7 +476,7 @@ def elec_conf(spec: dict, fetcher: Fetcher) -> list[dict]: add_emu_core says true: unconditional, else the `*)` branch of a device case, the branches for named devices going to board_defaults.""" params = spec["params"] - key = params.get("source", "os") + key = params.get("source", DEFAULT_ROW_SOURCE) package = params["package"] cores, systems = _package_calls(fetcher.text(key, package)) if not systems: @@ -797,7 +797,7 @@ def recalbox(spec: dict, fetcher: Fetcher) -> list[dict]: A `.ext!` extension is an archive the frontend hands over unopened, and a `files:` list names whole files instead of extensions.""" params = spec["params"] - key = params.get("source", "os") + key = params.get("source", DEFAULT_ROW_SOURCE) boards = _recalbox_boards(fetcher, key, params["configs_dir"]) rows: list[dict] = [] fetcher.cache_directory(key, params["systems_dir"]) @@ -915,7 +915,7 @@ def retropie(spec: dict, fetcher: Fetcher) -> list[dict]: system no basic-install module serves gets its folder from an optional module, so the first opt, then exp, module stands as its default.""" params = spec["params"] - key = params.get("source", "os") + key = params.get("source", DEFAULT_ROW_SOURCE) cfg_path = params["platforms_cfg"] cfg = fetcher.text(key, cfg_path) exts: dict[str, tuple[str, int]] = {} diff --git a/tests/test_check_freshness.py b/tests/test_check_freshness.py index 7ef92ae38..d7c1911e5 100644 --- a/tests/test_check_freshness.py +++ b/tests/test_check_freshness.py @@ -55,7 +55,7 @@ from freshness.platformdiff import diff_platform, diff_targets from freshness.platforms import _scrapable_platforms, emulatorjs_pin from freshness.profiles import check_profiles, summarize_profile_sync from freshness.report import exit_code, render, unknown_areas_message -from freshness.romfolders import check_romfolders, romfolder_pin_drift, romfolder_sources +from freshness.romfolders import check_romfolders, romfolder_pin_drift, romfolder_sources, watched_sources from freshness.scrapers import rescrape_command, target_scrapers from freshness.targets import ( _declared_cores, @@ -525,9 +525,35 @@ class RomFolderPins(unittest.TestCase): def test_the_committed_setups_agree_with_their_platform_files(self): platforms = REPO_ROOT / "platforms" drift = [d for setup, doc in romfolder_sources(platforms).items() - if (d := romfolder_pin_drift(setup, doc, platforms))] + for key in watched_sources(doc) + if (d := romfolder_pin_drift(setup, doc["sources"][key], platforms))] self.assertEqual(drift, []) + def test_the_source_the_rows_come_from_is_compared(self): + """MiSTer's folders are the games/ tree of Distribution_MiSTer + (`params.source: dist`), and only Main_MiSTer, declared first, was + compared: a new core folder never made the setup stale.""" + with tempfile.TemporaryDirectory() as tmp: + root = self._tree(tmp, "https://github.com/o/r/tree/" + "a" * 40, + " commit: " + "a" * 40 + "\n") + (root / "romfolders" / "_sources" / "card.yml").write_text( + "params:\n source: dist\nsources:\n" + " main:\n repo: https://github.com/m/main\n commit: " + "d" * 40 + "\n" + " dist:\n repo: https://github.com/m/dist\n commit: " + "e" * 40 + "\n") + heads = {"m/main": "d" * 40, "m/dist": "f" * 40} + with mock.patch.object(upstream, "commit_date", return_value="2026-10-01T00:00:00"), \ + mock.patch.object(upstream, "latest_release", return_value=None), \ + mock.patch.object(upstream, "resolve_head", side_effect=lambda repo, _c: heads.get(repo.slug)), \ + mock.patch.object(upstream, "ancestry", return_value=(5, 0)): + rows = {f.subject: f for f in check_romfolders(root, "", False)} + self.assertEqual(rows["card"].status, OK) + self.assertEqual((rows["card:dist"].status, rows["card:dist"].detail), + (STALE, "5 commits behind the branch head")) + + def test_the_committed_mister_setup_watches_its_distribution(self): + doc = romfolder_sources(REPO_ROOT / "platforms")["misterfpga"] + self.assertEqual(watched_sources(doc), ["main", "dist"]) + class CatalogParsingTests(unittest.TestCase): def test_game_catalogs_are_watched(self): diff --git a/tests/test_romfolders_card.py b/tests/test_romfolders_card.py index 44c5769e2..98c903c27 100644 --- a/tests/test_romfolders_card.py +++ b/tests/test_romfolders_card.py @@ -13,7 +13,7 @@ import json import unittest from pathlib import Path -from model.romfolders import format_ref, source_refs, split_refs +from model.romfolders import format_ref, row_sources, source_refs, split_refs from model.safeparse import yaml_load from scraper.romfolders import SourceError, dump_setup, load_sources, scrape_setup from scraper.romfolders.card import ( @@ -289,5 +289,26 @@ class CommittedSetupTests(unittest.TestCase): self.assertGreater(len(doc["folders"]), 0, setup) +class RowSourceTests(unittest.TestCase): + """Which declared source a setup's rows are read from is decided once, + for the parsers and for the freshness check of the pins.""" + + def test_params_source_then_nested_sources(self) -> None: + spec = {"params": {"source": "os", "links": [{"source": "gpsp", "path": "a.lnk"}]}} + self.assertEqual(row_sources(spec), ["os", "gpsp"]) + + def test_nested_sources_only(self) -> None: + spec = {"params": {"systems": {"source": "esde"}, "custom": [{"source": "os"}], + "alternatives": {"source": "os"}}} + self.assertEqual(row_sources(spec), ["esde", "os"]) + + def test_default_without_a_named_source(self) -> None: + self.assertEqual(row_sources({"params": {"file": "es_systems.cfg"}}), ["os"]) + self.assertEqual(row_sources({}), ["os"]) + + def test_mister_rows_come_from_the_distribution(self) -> None: + self.assertEqual(row_sources(load_sources(ROOT / "platforms" / "romfolders" / "_sources")["misterfpga"]), ["dist"]) + + if __name__ == "__main__": unittest.main() diff --git a/wiki/tools.md b/wiki/tools.md index 0b8e54a20..f5a4cacc6 100644 --- a/wiki/tools.md +++ b/wiki/tools.md @@ -738,7 +738,10 @@ The `romfolders` area compares each pin of `platforms/romfolders/_sources/`. A setup that is a registered platform must be read at the revision its platform file was scraped at, which needs no network unless one side names a tag and the other a commit. Any other setup is compared with its project's -newest release, or with its branch head when the project publishes none. A +newest release, or with its branch head when the project publishes none. The +setup's own pin (`os`, else the first source) is compared, and so is every +source its folders are read from (MiSTer's `dist`, the ES-DE list of EmuDeck +and RetroDECK), each as a row `setup:source`. A stale pin is moved by hand, then `python -m scripts.scraper.romfolders --setup NAME` reads the folders again.