From 1631ad0f4d9d9be9f4c7b0301e386f02323392fd Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Tue, 24 Nov 2026 01:37:11 +0100 Subject: [PATCH] refactor: one loader for the rom-folder sources --- scripts/freshness/romfolders.py | 13 ++----------- scripts/model/romfolders.py | 20 ++++++++++++++++++-- scripts/scraper/romfolders/__init__.py | 9 --------- scripts/scraper/romfolders/__main__.py | 11 +++++------ tests/test_check_freshness.py | 7 ++++--- tests/test_romfolders_card.py | 4 ++-- tests/test_romfolders_es.py | 3 +-- 7 files changed, 32 insertions(+), 35 deletions(-) diff --git a/scripts/freshness/romfolders.py b/scripts/freshness/romfolders.py index 4b1970379..32a7d5ea6 100644 --- a/scripts/freshness/romfolders.py +++ b/scripts/freshness/romfolders.py @@ -6,20 +6,11 @@ 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.romfolders import load_sources, row_sources, sources_dir from model.sourcerevision import scraped_revision from freshness.findings import OK, SKIPPED, STALE, UNKNOWN, Finding -def romfolder_sources(platforms_dir: Path) -> dict[str, dict]: - """setup -> its platforms/romfolders/_sources/.yml.""" - out = {} - for path in sorted((platforms_dir / "romfolders" / "_sources").glob("*.yml")): - out[path.stem] = source_yaml_file(path) or {} - return out - - 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 @@ -73,7 +64,7 @@ def check_romfolders(platforms_dir: Path, cache_dir: str, offline: bool) -> list """ 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 setup, doc in load_sources(sources_dir(platforms_dir)).items() for key in watched_sources(doc) ] diff --git a/scripts/model/romfolders.py b/scripts/model/romfolders.py index d2aadac8b..cfe8cfd1f 100644 --- a/scripts/model/romfolders.py +++ b/scripts/model/romfolders.py @@ -24,8 +24,19 @@ from model.profiles import load_emulator_profiles, preferred_profile, upstream_p from model.platforms import load_platform_config, native_layer from model.sources import source_yaml_file -ROMFOLDERS_DIR = Path("platforms") / "romfolders" -SOURCES_DIR = ROMFOLDERS_DIR / "_sources" + +def romfolders_dir(platforms_dir: str | Path = PLATFORMS_DIR) -> Path: + """The rom-folder setups of a platforms directory.""" + return Path(platforms_dir) / "romfolders" + + +def sources_dir(platforms_dir: str | Path = PLATFORMS_DIR) -> Path: + """The setups' declared sources and pins, beside the setup files.""" + return romfolders_dir(platforms_dir) / "_sources" + + +ROMFOLDERS_DIR = romfolders_dir() +SOURCES_DIR = sources_dir() # " :[-]", several joined by "; ". _REF_PART = re.compile(r"^(?P[a-z0-9][a-z0-9-]*) (?P\S+?)(?::(?P\d+(?:-\d+)?(?:,\d+(?:-\d+)?)*))?$") @@ -87,6 +98,11 @@ def setup_paths(directory: Path = ROMFOLDERS_DIR) -> list[Path]: return sorted(p for p in Path(directory).glob("*.yml") if not p.name.startswith("_")) +def load_sources(directory: Path = SOURCES_DIR) -> dict[str, dict]: + """Each setup's declared sources and facts, keyed by its file name.""" + return {path.stem: source_yaml_file(path) or {} for path in sorted(Path(directory).glob("*.yml"))} + + def load_setup(path: Path) -> dict: return source_yaml_file(path) diff --git a/scripts/scraper/romfolders/__init__.py b/scripts/scraper/romfolders/__init__.py index 3c33656d2..5d6686a93 100644 --- a/scripts/scraper/romfolders/__init__.py +++ b/scripts/scraper/romfolders/__init__.py @@ -26,7 +26,6 @@ if (_SCRIPTS_DIR := str(Path(__file__).resolve().parents[2])) not in sys.path: sys.path.insert(0, _SCRIPTS_DIR) from core import upstream -from model.sources import source_yaml_file from ..base_scraper import PlatformDumper @@ -160,14 +159,6 @@ def load_formats() -> dict[str, Parser]: return FORMATS -def load_sources(directory: Path) -> dict[str, dict]: - """Each setup's declared sources and facts, keyed by its file name.""" - out: dict[str, dict] = {} - for path in sorted(Path(directory).glob("*.yml")): - out[path.stem] = source_yaml_file(path) or {} - return out - - def _clean_row(row: dict) -> dict: unknown = set(row) - set(ROW_KEYS) if unknown: diff --git a/scripts/scraper/romfolders/__main__.py b/scripts/scraper/romfolders/__main__.py index db29450f5..a88552318 100644 --- a/scripts/scraper/romfolders/__main__.py +++ b/scripts/scraper/romfolders/__main__.py @@ -11,11 +11,10 @@ import argparse import sys from pathlib import Path -from . import Fetcher, SourceError, dump_setup, load_sources, scrape_setup +from . import Fetcher, SourceError, dump_setup, scrape_setup from core.upstream import UpstreamError from core.artifacts import write_if_changed - -DEFAULT_DIR = Path("platforms") / "romfolders" +from model.romfolders import ROMFOLDERS_DIR, SOURCES_DIR, load_sources def main() -> int: @@ -24,10 +23,10 @@ def main() -> int: which.add_argument("--setup", action="append", help="setup id (repeatable)") which.add_argument("--all", action="store_true", help="every setup of the sources file") which.add_argument("--list", action="store_true", help="list the setups and their format") - parser.add_argument("--sources", type=Path, default=DEFAULT_DIR / "_sources", + parser.add_argument("--sources", type=Path, default=SOURCES_DIR, help="the setups, their repositories and pinned revisions") - parser.add_argument("--output-dir", type=Path, default=DEFAULT_DIR, - help=f"where .yml is written (default {DEFAULT_DIR})") + parser.add_argument("--output-dir", type=Path, default=ROMFOLDERS_DIR, + help=f"where .yml is written (default {ROMFOLDERS_DIR})") parser.add_argument("--offline", action="store_true", help="read the revision cache only") parser.add_argument("--dry-run", action="store_true", help="print counts, write nothing") args = parser.parse_args() diff --git a/tests/test_check_freshness.py b/tests/test_check_freshness.py index d7c1911e5..d67baa331 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, watched_sources +from freshness.romfolders import check_romfolders, romfolder_pin_drift, watched_sources from freshness.scrapers import rescrape_command, target_scrapers from freshness.targets import ( _declared_cores, @@ -66,6 +66,7 @@ from freshness.targets import ( unresolved_target_cores, ) from model.registry import load_platform_registry +from model.romfolders import load_sources, sources_dir from model.safeparse import yaml_load from model.targets import platform_overrides from profiling.reports import ProfileReport, report_to_dict @@ -524,7 +525,7 @@ 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() + drift = [d for setup, doc in load_sources(sources_dir(platforms)).items() for key in watched_sources(doc) if (d := romfolder_pin_drift(setup, doc["sources"][key], platforms))] self.assertEqual(drift, []) @@ -551,7 +552,7 @@ class RomFolderPins(unittest.TestCase): (STALE, "5 commits behind the branch head")) def test_the_committed_mister_setup_watches_its_distribution(self): - doc = romfolder_sources(REPO_ROOT / "platforms")["misterfpga"] + doc = load_sources(sources_dir(REPO_ROOT / "platforms"))["misterfpga"] self.assertEqual(watched_sources(doc), ["main", "dist"]) diff --git a/tests/test_romfolders_card.py b/tests/test_romfolders_card.py index 98c903c27..beaf680d4 100644 --- a/tests/test_romfolders_card.py +++ b/tests/test_romfolders_card.py @@ -13,9 +13,9 @@ import json import unittest from pathlib import Path -from model.romfolders import format_ref, row_sources, source_refs, split_refs +from model.romfolders import format_ref, load_sources, 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 import SourceError, dump_setup, scrape_setup from scraper.romfolders.card import ( libretro_core, parse_allium, diff --git a/tests/test_romfolders_es.py b/tests/test_romfolders_es.py index d03833b25..4e08f43d8 100644 --- a/tests/test_romfolders_es.py +++ b/tests/test_romfolders_es.py @@ -15,13 +15,12 @@ import json import unittest from pathlib import Path -from model.romfolders import setup_paths, source_refs, split_refs +from model.romfolders import load_sources, setup_paths, source_refs, split_refs from scraper.romfolders import ( Fetcher, SourceError, dump_setup, load_formats, - load_sources, scrape_setup, ) from scraper.retropie_modules import module_enabled