From 075769806379dbee460c210aef1c67964c8159a1 Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Sun, 22 Nov 2026 03:08:27 +0100 Subject: [PATCH] refactor: one owner for a launcher emulator list The comma-joined emulator field of a launcher record was written in eleven places and read in nine, four of them splitting on ", " with no strip. model.launcherrecords joins and splits it, and every scraper, resolve and packs reader goes through it. --- .github/workflows/deploy-site.yml | 1 + scripts/model/launcherrecords.py | 25 +++++++++++++++++++++++++ scripts/packs/crossref.py | 5 +++-- scripts/packs/crossreflaunchers.py | 3 ++- scripts/resolve/launchers.py | 9 +++++---- scripts/scraper/batocera_configgen.py | 3 ++- scripts/scraper/batocera_launchers.py | 3 ++- scripts/scraper/batocera_scraper.py | 7 ++++--- scripts/scraper/configgen_sites.py | 3 ++- scripts/scraper/emuelec_launchers.py | 3 ++- scripts/scraper/emuelec_scraper.py | 3 ++- scripts/scraper/knulli_launchers.py | 7 ++++--- scripts/scraper/recalbox_launchers.py | 9 +++++---- scripts/scraper/retrobat_launchers.py | 5 +++-- tests/test_launcher_paths.py | 8 ++++++++ tests/test_launchers.py | 3 ++- tests/test_launchers_amberelec.py | 3 ++- tests/test_launchers_knulli.py | 3 ++- tests/test_launchers_recalbox.py | 5 +++-- 19 files changed, 79 insertions(+), 29 deletions(-) create mode 100644 scripts/model/launcherrecords.py diff --git a/.github/workflows/deploy-site.yml b/.github/workflows/deploy-site.yml index 8170d52ab..4e68e073e 100644 --- a/.github/workflows/deploy-site.yml +++ b/.github/workflows/deploy-site.yml @@ -53,6 +53,7 @@ on: - "scripts/model/exportchanges.py" - "scripts/model/guides.py" - "scripts/model/largefiles.py" + - "scripts/model/launcherrecords.py" - "scripts/model/mamerelease.py" - "scripts/model/measurements.py" - "scripts/model/nodedup.py" diff --git a/scripts/model/launcherrecords.py b/scripts/model/launcherrecords.py new file mode 100644 index 000000000..a21bff064 --- /dev/null +++ b/scripts/model/launcherrecords.py @@ -0,0 +1,25 @@ +"""The emulator field of a platform launcher record: one name, or several joined.""" + +from __future__ import annotations + +from collections.abc import Iterable, Mapping +from typing import Any + +# A record written once for several emulators names them all in one field +# (schemas/platform.schema.json, launchers[].emulator). +SEPARATOR = ", " + + +def join_emulators(names: Iterable[str]) -> str: + """The emulator field of a record written for `names`.""" + return SEPARATOR.join(names) + + +def emulator_names(field: str) -> list[str]: + """The names an emulator field joins, in their order.""" + return [name.strip() for name in field.split(",")] + + +def record_emulators(record: Mapping[str, Any]) -> list[str]: + """The emulators a launcher record is written for.""" + return emulator_names(str(record.get("emulator", ""))) diff --git a/scripts/packs/crossref.py b/scripts/packs/crossref.py index 7f05250e7..01d03704d 100644 --- a/scripts/packs/crossref.py +++ b/scripts/packs/crossref.py @@ -19,6 +19,7 @@ from model.alternatives import set_keys, set_requirements, settle_sets from model.database import get_mame_clone_map, sha1_at from model.profiles import lists_files from model.declarations import OUTER_ROOTS +from model.launcherrecords import record_emulators from model.platforms import load_platform_config from model.registry import list_registered_platforms, load_data_dir_registry from model.systems import norm_system_id @@ -1349,8 +1350,8 @@ def launcher_reads_not_held( continue readers = sorted({ key - for part in str(record.get("emulator", "")).split(",") - for key in LauncherLayer._profiles_named(part.strip(), profiles, every_profile) + for part in record_emulators(record) + for key in LauncherLayer._profiles_named(part, profiles, every_profile) }) if not readers: # RetroDECK names its frontend and the core option names the core. diff --git a/scripts/packs/crossreflaunchers.py b/scripts/packs/crossreflaunchers.py index 3b2f84c8e..0d3e73029 100644 --- a/scripts/packs/crossreflaunchers.py +++ b/scripts/packs/crossreflaunchers.py @@ -3,6 +3,7 @@ from __future__ import annotations from model.platforms import load_platform_config +from model.launcherrecords import record_emulators from model.registry import list_registered_platforms from packs.crossref import launcher_reads_not_held from resolve.launchers import LauncherLayer, launcher_unproven @@ -37,7 +38,7 @@ def launcher_rows( row for row in launcher_unproven(configs, all_profiles) if not emulator or any( emulator in LauncherLayer._profiles_named(part, all_profiles, set(all_profiles)) - for part in row["emulator"].split(", ") + for part in record_emulators(row) ) ] return launcher_reads, unproven diff --git a/scripts/resolve/launchers.py b/scripts/resolve/launchers.py index 0f4e14784..8cd13fb15 100644 --- a/scripts/resolve/launchers.py +++ b/scripts/resolve/launchers.py @@ -23,6 +23,7 @@ from dataclasses import dataclass from core.hashing import HASH_FIELDS, declared_hashes from model.database import record_at from model.declarations import CONFIGS_ROOT, ROMS_ROOT, declared_root, declared_slot, outer_layout +from model.launcherrecords import emulator_names, record_emulators from model.profiles import lists_files, platform_builds, profile_names from resolve.content import name_match_size_ok from resolve.localfile import resolve_local_file @@ -429,7 +430,7 @@ class ConfigEvidence: def _named_profiles(record: dict, profiles: Mapping[str, dict], platform: str) -> list[str]: """The profiles a record's emulator names, a platform's own build first.""" found = [] - for part in (e.strip() for e in str(record.get("emulator", "")).split(",")): + for part in record_emulators(record): named = [ key for key, profile in profiles.items() if lists_files(profile) and part in profile_names(key, profile) @@ -857,7 +858,7 @@ def handed_directories( d.startswith(f"{(folder or '').lower()}/") for d in declared ), ) - for emulator in (e.strip() for e in str(record.get("emulator", "")).split(",")) + for emulator in record_emulators(record) ) return found @@ -985,7 +986,7 @@ def executed_files(config: dict, profiles: Mapping[str, dict]) -> list[ExecutedF ExecutedFile( emulator, name.rsplit("/", 1)[-1], place, str(record.get("source_ref", "")) ) - for emulator in (e.strip() for e in str(record.get("emulator", "")).split(",")) + for emulator in record_emulators(record) ) return found @@ -1297,7 +1298,7 @@ class LauncherLayer: standalone = place.build == "standalone" owners = { key - for emulator in (e.strip() for e in place.emulator.split(",")) + for emulator in emulator_names(place.emulator) for key in self._profiles_named(emulator, profiles, relevant) } # RetroDECK names its frontend, retroarch, and the core option it diff --git a/scripts/scraper/batocera_configgen.py b/scripts/scraper/batocera_configgen.py index 4c33417ee..626c219b5 100644 --- a/scripts/scraper/batocera_configgen.py +++ b/scripts/scraper/batocera_configgen.py @@ -9,6 +9,7 @@ import posixpath from collections.abc import Callable from typing import Any +from model.launcherrecords import join_emulators from .configgen_launchers import LauncherShapeError, Source, _assigned_name, assigned_value @@ -159,7 +160,7 @@ class Configgen: def emulator(self, module: str) -> str: """The name configgen loads `module` for (importer.get_generator).""" if module in self._generators: - return ", ".join(self._generators[module]) + return join_emulators(self._generators[module]) package, _, leaf = module.partition(".") if leaf == f"{package}Generator": return package diff --git a/scripts/scraper/batocera_launchers.py b/scripts/scraper/batocera_launchers.py index 057cc17fe..6be6c1b3f 100644 --- a/scripts/scraper/batocera_launchers.py +++ b/scripts/scraper/batocera_launchers.py @@ -19,6 +19,7 @@ import re from collections.abc import Callable, Iterable from typing import Any +from model.launcherrecords import join_emulators from .configgen_launchers import ( Launcher, LauncherShapeError, @@ -939,7 +940,7 @@ def core_file_options(cg: Configgen) -> list[Launcher]: src.fail(f"{setting} has no literal default") depends = option if condition is None else f"{option} (default by {condition})" launchers.append(Launcher( - emulator=", ".join(cores[function_name]), build="libretro", kind="core_option", + emulator=join_emulators(cores[function_name]), build="libretro", kind="core_option", names=names, passes_to=f"core option {setting}", depends_on_option=depends, source_ref=src.ref([src.span(_outermost(function, call))]), )) diff --git a/scripts/scraper/batocera_scraper.py b/scripts/scraper/batocera_scraper.py index 7251dea9e..251db8da3 100644 --- a/scripts/scraper/batocera_scraper.py +++ b/scripts/scraper/batocera_scraper.py @@ -15,6 +15,7 @@ from collections.abc import Callable from model.database import load_database +from model.launcherrecords import record_emulators from core.paths import DB_FILE, REPO_ROOT from model.targets import load_target_config, not_cores from core.hashing import cut_md5_owner @@ -425,11 +426,11 @@ def boards_build(selection: BuildSelection, launchers: list[dict]) -> Callable[[ /userdata/bios/pico-8/pico8) needs no package. """ supplied = { - name.strip() + name for record in launchers if record.get("passes_to") == "exec" and str(record.get("search_dir") or "").startswith("/userdata/bios") - for name in str(record.get("emulator", "")).split(",") + for name in record_emulators(record) } def built(name: str) -> bool: @@ -471,7 +472,7 @@ def records_run(launchers: list[dict], built: Callable[[str], bool]) -> list[dic (KNULLI's citron, pcsx2, shadps4): what they read is never read on the device, so the records do not describe the platform. """ - return [r for r in launchers if any(built(n) for n in str(r["emulator"]).split(", "))] + return [r for r in launchers if any(built(n) for n in record_emulators(r))] class CheckBiosScraper(BaseScraper): diff --git a/scripts/scraper/configgen_sites.py b/scripts/scraper/configgen_sites.py index 48c194910..45bc58901 100644 --- a/scripts/scraper/configgen_sites.py +++ b/scripts/scraper/configgen_sites.py @@ -13,6 +13,7 @@ import re from collections.abc import Iterator from dataclasses import dataclass, field +from model.launcherrecords import join_emulators from .batocera_configgen import Configgen, _path_value from .configgen_launchers import Launcher, Source, _assigned_name, end_line @@ -502,7 +503,7 @@ def bios_sites(cg: Configgen, spec: GenericSites) -> list[Launcher]: site.path = site.path[len(flag) + 1:] emulator = ( spec.emulator - or ", ".join(site.cores or caller_cores) + or join_emulators(site.cores or caller_cores) or (cg.emulator(module) if spec.build == "standalone" else "") ) if not emulator: diff --git a/scripts/scraper/emuelec_launchers.py b/scripts/scraper/emuelec_launchers.py index ee2079009..fb4efd72f 100644 --- a/scripts/scraper/emuelec_launchers.py +++ b/scripts/scraper/emuelec_launchers.py @@ -17,6 +17,7 @@ import re from collections.abc import Callable, Iterable from dataclasses import dataclass, field +from model.launcherrecords import join_emulators from .configgen_launchers import Launcher from .rocknix_launchers import fixed_paths from scraper.shell_launchers import ( @@ -111,7 +112,7 @@ class Runs: return self.by_script[name] def emulator(self, name: str) -> str: - return ", ".join(self.of(name).emulators) + return join_emulators(self.of(name).emulators) def ref(self, name: str) -> tuple[str, set[int]]: return (RUN_EMU, set(self.of(name).lines)) diff --git a/scripts/scraper/emuelec_scraper.py b/scripts/scraper/emuelec_scraper.py index 665f2385e..52dade8bd 100644 --- a/scripts/scraper/emuelec_scraper.py +++ b/scripts/scraper/emuelec_scraper.py @@ -27,6 +27,7 @@ Verification (batocera-systems at the release tag): from __future__ import annotations from model.sourcerevision import required_revision +from model.launcherrecords import record_emulators from model.targets import not_cores from .base_scraper import fetch_github_latest_version, fetch_text from .batocera_scraper import ( @@ -124,7 +125,7 @@ class Scraper(CheckBiosScraper): # no ES core name: only a record of an emulator no image carries goes. launchers = [ r for r in extract_launchers(RepoTree.github(REPO, required_revision(self.url, REPO))) - if not set(str(r["emulator"]).split(", ")) <= unbuilt + if not set(record_emulators(r)) <= unbuilt ] return { "platform": "EmuELEC", diff --git a/scripts/scraper/knulli_launchers.py b/scripts/scraper/knulli_launchers.py index fadfa0dc7..45c31e5d4 100644 --- a/scripts/scraper/knulli_launchers.py +++ b/scripts/scraper/knulli_launchers.py @@ -28,6 +28,7 @@ from typing import Any import yaml +from model.launcherrecords import emulator_names, join_emulators from .batocera_launchers import ( PATH_PATCHES, config_directories, @@ -278,7 +279,7 @@ def core_file_options(cg: Configgen) -> list[Launcher]: src.fail(f"{setting} is not set under a core test") depends = setting if not conditions else f"{setting} (default by {', '.join(conditions)})" launchers.append(Launcher( - emulator=", ".join(_cores_of(holder.test)), build="libretro", kind="core_option", + emulator=join_emulators(_cores_of(holder.test)), build="libretro", kind="core_option", names=names, passes_to=f"core option {setting}", depends_on_option=depends, source_ref=src.ref([src.span(holder.test), _lines(chain)]), )) @@ -476,14 +477,14 @@ def boot_disks( reader = _Reader(cg, src) module = spec.path.removesuffix(".py").replace("/", ".") emulator = ( - ", ".join(_caller_cores(cg, spec.caller)) if spec.caller else cg.emulator(module) + join_emulators(_caller_cores(cg, spec.caller)) if spec.caller else cg.emulator(module) ) if spec.caller and spec.feature[:2] == ("libretro", "cores"): # The option is offered for one core of the system: es_features # lists it under that core, the other cores the generator serves # never see it (KNULLI's macintosh offers mame alone). core = spec.feature[2] - if core not in emulator.split(", "): + if core not in emulator_names(emulator): src.fail(f"es_features offers the option for {core}, which the generator does not serve") emulator = core found = 0 diff --git a/scripts/scraper/recalbox_launchers.py b/scripts/scraper/recalbox_launchers.py index 347a37b84..c6517e282 100644 --- a/scripts/scraper/recalbox_launchers.py +++ b/scripts/scraper/recalbox_launchers.py @@ -21,6 +21,7 @@ from dataclasses import dataclass, field from functools import partial from typing import Any +from model.launcherrecords import emulator_names, join_emulators from .configgen_launchers import ( Launcher, LauncherShapeError, @@ -249,14 +250,14 @@ class Configgen: module = module.replace("/", ".") if module not in self._emulators: raise LauncherShapeError(f"{LAUNCHER}: no emulator loads {module}") - return ", ".join(self._emulators[module]) + return join_emulators(self._emulators[module]) def core(self, function: str) -> str: """The libretro cores fillCoresConfiguration hands to `function`.""" cores = _handler_map(self.source(CORES), "fillCoresConfiguration", "specificCoreHandlers") if function not in cores: self.source(CORES).fail(f"no core is configured by {function}") - return ", ".join(cores[function]) + return join_emulators(cores[function]) def env(self, source: Source, scope: ast.AST | None = None) -> dict[str, Any]: """recalboxFiles, the module's class attributes and the strings @@ -462,7 +463,7 @@ def system_directories(cg: Configgen) -> list[Launcher]: words = ["core with no case of its own"] + ([condition] if condition else []) condition = ", ".join(words) launchers.append(record( - ", ".join(cores) or ALL_CORES, condition, call, + join_emulators(cores) or ALL_CORES, condition, call, [(pattern.lineno, pattern.lineno), (start, end_line(call))], )) @@ -490,7 +491,7 @@ def system_directories(cg: Configgen) -> list[Launcher]: def core_directory(directories: list[Launcher], core: str) -> Launcher: """The unconditional system_directory record that applies to `core`.""" for launcher in directories: - if core in launcher.emulator.split(", ") and launcher.depends_on_option is None: + if core in emulator_names(launcher.emulator) and launcher.depends_on_option is None: return launcher for launcher in directories: if launcher.emulator == ALL_CORES and launcher.depends_on_option == \ diff --git a/scripts/scraper/retrobat_launchers.py b/scripts/scraper/retrobat_launchers.py index 0f96a26ab..12c69cc7f 100644 --- a/scripts/scraper/retrobat_launchers.py +++ b/scripts/scraper/retrobat_launchers.py @@ -13,6 +13,7 @@ from __future__ import annotations import re from collections.abc import Callable +from model.launcherrecords import join_emulators from resolve.launcherrefs import is_feature_ref, split_ref from .csharp_source import ( LauncherPatternMissing, @@ -1423,7 +1424,7 @@ def _cxbx_mount(src: Source, emulators: list[str]) -> list[Record]: ) iso_span = src.lines(iso.start(), iso.end()) when = f'rom extension "{iso.group(1)}"' - emulator = ", ".join(emulators) + emulator = join_emulators(emulators) return [ _record( emulator, @@ -2853,7 +2854,7 @@ def capsimg(sources: dict[str, Source]) -> list[Record]: "capsimg gate", ) block = src.block_at(gate.end()) - [record] = _copies_if_absent(src, ", ".join(names), "libretro", block) + [record] = _copies_if_absent(src, join_emulators(names), "libretro", block) record["source_ref"] = source_ref( (src.path, [(src.line(cores.start()), core_lines[1]), src.statement_span(block)]) ) diff --git a/tests/test_launcher_paths.py b/tests/test_launcher_paths.py index c264a56a5..e1ca4b7b9 100644 --- a/tests/test_launcher_paths.py +++ b/tests/test_launcher_paths.py @@ -297,6 +297,14 @@ class OnlyWhatABoardBuildsIsRun(unittest.TestCase): # Without a selection, every name counts, as before. self.assertIn("suyu", bs.configgen_cores(self.DEFAULTS, self.ES, set())[0]) + def test_a_record_for_several_emulators_runs_when_one_is_built(self): + from scraper import batocera_scraper as bs + + built = bs.boards_build(self.selection(set()), []) + for field in ("citron, mgba", "citron,mgba"): + with self.subTest(field=field): + self.assertEqual(len(bs.records_run([{"emulator": field}], built)), 1) + def test_the_real_lists_name_only_what_a_board_builds(self): import yaml diff --git a/tests/test_launchers.py b/tests/test_launchers.py index 3a4c65b4a..4e11c9bd3 100644 --- a/tests/test_launchers.py +++ b/tests/test_launchers.py @@ -17,6 +17,7 @@ from pathlib import Path import yaml +from model.launcherrecords import record_emulators from scraper.batocera_configgen import CONFIGGEN, GENERATORS, IMPORTER, PATHS_MODULE, Configgen from scraper.batocera_launchers import ( Launcher, @@ -624,7 +625,7 @@ class PlatformRecords(unittest.TestCase): cores = {*self.config["cores"], *self.config.get("standalone_cores", [])} named = { name for record in self.config["launchers"] - for name in record["emulator"].split(", ") + for name in record_emulators(record) } self.assertEqual(sorted(named - cores), []) diff --git a/tests/test_launchers_amberelec.py b/tests/test_launchers_amberelec.py index 112f48a3b..05906c5b5 100644 --- a/tests/test_launchers_amberelec.py +++ b/tests/test_launchers_amberelec.py @@ -16,6 +16,7 @@ from pathlib import Path import yaml +from model.launcherrecords import record_emulators from scraper import amberelec_launchers as al from scraper.configgen_launchers import LauncherShapeError @@ -336,7 +337,7 @@ class PlatformFile(unittest.TestCase): def test_every_emulator_is_one_amberelec_names(self): cores = {*self.config["cores"], *self.config.get("standalone_cores", [])} - named = {n for record in self.config["launchers"] for n in record["emulator"].split(", ")} + named = {n for record in self.config["launchers"] for n in record_emulators(record)} # RetroArch and retrorun themselves, the two runners of every core. self.assertEqual(sorted(named - cores), ["retroarch", "retrorun"]) diff --git a/tests/test_launchers_knulli.py b/tests/test_launchers_knulli.py index c10d49bc0..6458dae69 100644 --- a/tests/test_launchers_knulli.py +++ b/tests/test_launchers_knulli.py @@ -16,6 +16,7 @@ from pathlib import Path import yaml +from model.launcherrecords import record_emulators from scraper import knulli_launchers as kl from scraper.configgen_sites import bios_sites, GenericSites from scraper.batocera_launchers import hatari, melonds @@ -633,7 +634,7 @@ class PlatformFile(unittest.TestCase): def test_every_emulator_is_one_knulli_names(self): cores = {*self.config["cores"], *self.config.get("standalone_cores", [])} - named = {n for record in self.config["launchers"] for n in record["emulator"].split(", ")} + named = {n for record in self.config["launchers"] for n in record_emulators(record)} # RetroArch itself, and the MAME core names configgen tests that # KNULLI's es_systems.yml never offers. self.assertEqual(sorted(named - cores), ["mamevirtual", "mess", "retroarch"]) diff --git a/tests/test_launchers_recalbox.py b/tests/test_launchers_recalbox.py index 95295b4b5..c3990039a 100644 --- a/tests/test_launchers_recalbox.py +++ b/tests/test_launchers_recalbox.py @@ -18,6 +18,7 @@ from unittest import mock import yaml +from model.launcherrecords import emulator_names, record_emulators from scraper import recalbox_scraper from scraper.recalbox_launchers import ( ALL_CORES, @@ -1352,7 +1353,7 @@ class SystemDirectoryPerCore(unittest.TestCase): self.assertEqual(overrides[0].source_ref, ref(RETROARCH, "516-519")) def test_the_cases_cover_the_match(self): - cores = {name for r in self.records for name in r.emulator.split(", ")} + cores = {name for r in self.records for name in emulator_names(r.emulator)} self.assertEqual(len(cores - {ALL_CORES}), 29) def test_dolphin_assets_directory(self): @@ -1641,7 +1642,7 @@ class PlatformRecords(unittest.TestCase): cores = set(self.config["cores"]) | {ALL_CORES} named = { name for record in self.config["launchers"] - for name in record["emulator"].split(", ") + for name in record_emulators(record) } self.assertEqual(sorted(named - cores), [])