From 0548d3d82342711630980a5565d500a347b126da Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Sat, 21 Nov 2026 04:29:48 +0100 Subject: [PATCH] refactor: group every scraper's systems one way --- scripts/scraper/base_scraper.py | 24 +++------------------ scripts/scraper/coreinfo_scraper.py | 13 ++--------- scripts/scraper/misterfpga_scraper.py | 31 +++++++++------------------ scripts/scraper/retrodeck_scraper.py | 7 ++---- tests/test_scraper_contract.py | 14 ++++++++++++ 5 files changed, 31 insertions(+), 58 deletions(-) diff --git a/scripts/scraper/base_scraper.py b/scripts/scraper/base_scraper.py index 8201b3f16..d6d629364 100644 --- a/scripts/scraper/base_scraper.py +++ b/scripts/scraper/base_scraper.py @@ -562,27 +562,9 @@ def _print_json(reqs: list[BiosRequirement]) -> None: def _generic_config(reqs: list[BiosRequirement]) -> dict: """Generic fallback: just systems from requirements.""" - config: dict = {"systems": {}} - for req in reqs: - sys_id = req.system - if sys_id not in config["systems"]: - sys_entry: dict = {"files": []} - if req.native_id: - sys_entry["native_id"] = req.native_id - config["systems"][sys_id] = sys_entry - entry = { - "name": req.name, - "destination": req.destination or req.name, - "required": req.required, - } - if req.sha1: - entry["sha1"] = req.sha1 - if req.md5: - entry["md5"] = req.md5 - if req.zipped_file: - entry["zipped_file"] = req.zipped_file - config["systems"][sys_id]["files"].append(entry) - return config + return {"systems": systems_from_requirements( + reqs, lambda req: {"native_id": req.native_id} if req.native_id else {}, + )} def _keep_existing(config: dict, output_path: Path) -> None: diff --git a/scripts/scraper/coreinfo_scraper.py b/scripts/scraper/coreinfo_scraper.py index e2d0a3f39..5d7bcabf9 100644 --- a/scripts/scraper/coreinfo_scraper.py +++ b/scripts/scraper/coreinfo_scraper.py @@ -31,6 +31,7 @@ from .base_scraper import ( BiosRequirement, fetch_github_latest_version, fetch_text, + _print_dry_run, github_tree, ) @@ -331,17 +332,7 @@ def main() -> None: print(f" {r.system}: {r.destination} {opt}") return - from collections import defaultdict - - by_system = defaultdict(list) - for r in reqs: - by_system[r.system].append(r) - - print(f"Total: {len(reqs)} unique firmware paths across {len(by_system)} systems") - for sys_name, files in sorted(by_system.items()): - req_count = sum(1 for f in files if f.required) - opt_count = sum(1 for f in files if not f.required) - print(f" {sys_name}: {req_count} required, {opt_count} optional") + _print_dry_run(reqs) if __name__ == "__main__": diff --git a/scripts/scraper/misterfpga_scraper.py b/scripts/scraper/misterfpga_scraper.py index 0b5e0055a..86c8b822c 100644 --- a/scripts/scraper/misterfpga_scraper.py +++ b/scripts/scraper/misterfpga_scraper.py @@ -32,7 +32,7 @@ from .base_scraper import ( fetch_branch_head, fetch_bytes, json_object, - requirement_entry, + systems_from_requirements, ) PLATFORM_NAME = "misterfpga" @@ -117,6 +117,14 @@ def system_of(path: str) -> str: return remainder.split("/", 1)[0] +def _system_fields(req: BiosRequirement) -> dict: + _, display, repo = SYSTEM_MAP.get(req.native_id or "", (req.system, req.system, "")) + fields: dict = {"native_id": req.native_id, "name": display} + if repo: + fields["docs"] = f"https://github.com/MiSTer-devel/{repo}" + return fields + + class Scraper(BaseScraper): """Scraper for the MiSTer BiosDB database.""" @@ -174,26 +182,7 @@ class Scraper(BaseScraper): def generate_platform_yaml(self) -> dict: """Build the MiSTer FPGA platform configuration.""" data = json.loads(self._fetch_raw()) - systems: dict[str, dict] = {} - - for req in self.fetch_requirements(): - _, display, repo = SYSTEM_MAP.get( - req.native_id or "", (req.system, req.system, "") - ) - entry = systems.setdefault( - req.system, - { - "files": [], - "native_id": req.native_id, - "name": display, - "docs": f"https://github.com/MiSTer-devel/{repo}" if repo else "", - }, - ) - entry["files"].append(requirement_entry(req)) - - for entry in systems.values(): - if not entry["docs"]: - del entry["docs"] + systems = systems_from_requirements(self.fetch_requirements(), _system_fields) # The BiosDB has no release: the date it was built is the only # version the list carries, and the manifest needs one. diff --git a/scripts/scraper/retrodeck_scraper.py b/scripts/scraper/retrodeck_scraper.py index 04765c4ba..f5f11bfe2 100644 --- a/scripts/scraper/retrodeck_scraper.py +++ b/scripts/scraper/retrodeck_scraper.py @@ -51,7 +51,7 @@ from .base_scraper import ( github_tree, json_object, md5_field, - requirement_entry, + systems_from_requirements, ) from .retrodeck_launchers import extract_launchers @@ -526,10 +526,7 @@ class Scraper(BaseScraper): } ) - systems: dict[str, dict] = {} - for req in reqs: - sys_entry = systems.setdefault(req.system, {"files": []}) - sys_entry["files"].append(requirement_entry(req)) + systems = systems_from_requirements(reqs) version = fetch_github_latest_version("RetroDECK/RetroDECK") # The ROM folders each libretro core runs for: its save folders. diff --git a/tests/test_scraper_contract.py b/tests/test_scraper_contract.py index 56e22fad0..6ee605b62 100644 --- a/tests/test_scraper_contract.py +++ b/tests/test_scraper_contract.py @@ -112,6 +112,20 @@ class CompareWithConfig(unittest.TestCase): self.assertEqual([r.name for r in changes.removed], ["a.bin"]) +class GenericConfigWritesFullEntries(unittest.TestCase): + def test_fallback_serializes_like_every_scraper(self): + from scraper.base_scraper import _generic_config, requirement_entry + + req = BiosRequirement( + name="a.bin", system="sys", md5="ABCDEF", size=4, native_id="nat", + ) + self.assertEqual( + _generic_config([req]), + {"systems": {"sys": {"files": [requirement_entry(req)], "native_id": "nat"}}}, + ) + self.assertEqual(requirement_entry(req)["md5"], "abcdef") + + class TestConnection(unittest.TestCase): def test_reachable_source_reports_true(self): self.assertTrue(_StubScraper([]).test_connection())