From 19bcc76c8fde75766958da77315b57bd4a137fad Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Sun, 22 Nov 2026 00:34:16 +0100 Subject: [PATCH] refactor: hand the native model its profiles --- scripts/exporter/native.py | 18 ++++++++++-------- scripts/freshness/native.py | 5 +++-- scripts/resolve/reconcile.py | 18 +++++++++++------- tests/test_check_freshness.py | 2 +- tests/test_exporters.py | 11 +++++++++-- 5 files changed, 34 insertions(+), 20 deletions(-) diff --git a/scripts/exporter/native.py b/scripts/exporter/native.py index a80c24737..3b0a02644 100644 --- a/scripts/exporter/native.py +++ b/scripts/exporter/native.py @@ -13,6 +13,7 @@ from pathlib import Path import yaml from core.artifacts import write_bytes_atomic, write_if_changed +from core.paths import EMULATORS_DIR from core.text import count_text from core.ziptools import build_zip_contents_index from .base_exporter import BaseExporter @@ -60,12 +61,12 @@ def _truth_sources(platforms_dir: str) -> tuple[dict, dict, dict | None]: """Registry, profiles and database, as generate_truth reads them.""" registry = load_platform_registry(platforms_dir) db = load_database(TRUTH_DB) if Path(TRUTH_DB).exists() else None - return registry, _profiles(), db + return registry, export_profiles(), db -def _profiles() -> dict[str, dict]: +def export_profiles() -> dict[str, dict]: """The emulator profiles the truth and the exporters read.""" - return load_emulator_profiles("emulators") + return load_emulator_profiles(EMULATORS_DIR) def current_truth(platform: str, config: dict, platforms_dir: str) -> dict: @@ -118,8 +119,9 @@ def refresh_cache( truth, scraped = load_inputs(platform, truth_dir, platforms_dir) except StaleTruth as exc: return False, [str(exc)] - systems, _ = build_native_model(truth or {}, scraped) - exporter: BaseExporter = exporter_class(_profiles()) + profiles = export_profiles() + systems, _ = build_native_model(truth or {}, scraped, profiles) + exporter: BaseExporter = exporter_class(profiles) wanted = wanted_sources(exporter, systems, scraped) off_pin = unpinned(wanted, scraped) if off_pin: @@ -145,7 +147,7 @@ def doubled_in(scraped: dict | None, platforms_dir: str) -> list[dict]: return [] db, zip_contents = _collection(TRUTH_DB) return doubled_destinations( - scraped, _profiles(), db, zip_contents, + scraped, export_profiles(), db, zip_contents, load_data_dir_registry(platforms_dir), ) @@ -223,14 +225,14 @@ def build_export( f"python scripts/generate_truth.py --platform {platform}" ]) - systems, report = build_native_model(truth, scraped) + profiles = export_profiles() + systems, report = build_native_model(truth, scraped, profiles) if not systems: return ExportResult( platform, False, ["nothing to write: neither the platform nor the truth has data"], ) - profiles = _profiles() exporter: BaseExporter = exporter_class(profiles) produced, issues, stopped = _produce( exporter, systems, report, scraped, upstream_dir, allow_fetch, diff --git a/scripts/freshness/native.py b/scripts/freshness/native.py index 50685adc0..8990c51f2 100644 --- a/scripts/freshness/native.py +++ b/scripts/freshness/native.py @@ -8,7 +8,7 @@ from collections.abc import Iterator from pathlib import Path from exporter import discover_exporters -from exporter.native import StaleTruth, build_export, load_inputs +from exporter.native import StaleTruth, build_export, export_profiles, load_inputs from exporter.originals import SOURCES_INDEX, SourcesIndexUnreadable, load_sources, source_key, wanted_sources from model.platforms import inheritance_chain from model.registry import list_registered_platforms @@ -119,6 +119,7 @@ def check_native( except SourcesIndexUnreadable as exc: return [Finding("native", index.name, ERROR, detail=str(exc))] findings: list[Finding] = [] + profiles = export_profiles() for name in list_registered_platforms(str(platforms_dir)): exporter_class = exporters.get(name) if not exporter_class: @@ -129,7 +130,7 @@ def check_native( except StaleTruth as exc: findings.append(Finding("native", name, STALE, detail=str(exc))) continue - systems, _ = build_native_model(truth or {}, scraped) + systems, _ = build_native_model(truth or {}, scraped, profiles) wanted = wanted_sources(exporter_class(), systems, scraped) reason = native_cache_state( wanted, cache_dir / name, recorded, index, _transcribed_at(platforms_dir, name) diff --git a/scripts/resolve/reconcile.py b/scripts/resolve/reconcile.py index 5e561ae8d..a53fab3f9 100644 --- a/scripts/resolve/reconcile.py +++ b/scripts/resolve/reconcile.py @@ -13,14 +13,15 @@ grouping is rebuilt from the per-file native_system the scrapers record. from __future__ import annotations -from collections.abc import Callable +from collections.abc import Callable, Mapping from dataclasses import dataclass, field - +from types import MappingProxyType from core.hashing import HASH_FIELDS, declared_hashes, parse_hash_list from model.profiles import profile_names from model.systems import norm_system_id -from core.paths import REPO_ROOT + +_NO_PROFILES: Mapping[str, dict] = MappingProxyType({}) def checked_hashes(entry: dict | None) -> dict | None: @@ -715,9 +716,12 @@ class _TruthPass: def build_native_model( truth: dict, scraped: dict | None, + profiles: Mapping[str, dict] = _NO_PROFILES, ) -> tuple[dict[str, NativeSystem], Report]: """Rebuild the platform's systems, corrected by the truth. + `profiles` are the emulator profiles the truth was built from: they + name the platform's runner of each addition, and none means no runner. Returns the systems keyed by native id, in the platform's own order first and truth-only additions after, plus what changed. """ @@ -740,11 +744,13 @@ def build_native_model( tail = [fe for fe in system.files if fe.platform is None] system.files = head + search_order(tail) - _attach_runners(systems, scraped) + _attach_runners(systems, scraped, profiles) return systems, report -def _attach_runners(systems: dict[str, NativeSystem], scraped: dict | None) -> None: +def _attach_runners( + systems: dict[str, NativeSystem], scraped: dict | None, profiles: Mapping[str, dict] +) -> None: """Name the cores behind each addition the way the platform names them. The platform lists its cores by its own names (Batocera's @@ -756,7 +762,6 @@ def _attach_runners(systems: dict[str, NativeSystem], scraped: dict | None) -> N """ if not scraped: return - from model.profiles import load_emulator_profiles from resolve.layout import runs_standalone from resolve.standalone import standalone_set @@ -765,7 +770,6 @@ def _attach_runners(systems: dict[str, NativeSystem], scraped: dict | None) -> N standalone = {str(c) for c in scraped.get("standalone_cores") or []} # The build decision reads the tagged set (own builds, twins), as the # pack does. The runner's name is looked up among the plain names. - profiles = load_emulator_profiles(str(REPO_ROOT / "emulators")) layout_set = standalone_set(scraped, profiles) for system in systems.values(): for fe in system.files: diff --git a/tests/test_check_freshness.py b/tests/test_check_freshness.py index 0ff83a678..ca0bb51e0 100644 --- a/tests/test_check_freshness.py +++ b/tests/test_check_freshness.py @@ -169,7 +169,7 @@ class NativeCacheTests(unittest.TestCase): from resolve.truth import log as truth_log - def noisy(truth, scraped): + def noisy(truth, scraped, profiles): truth_log.warning("WARNING: hash conflict for GameIndex.yaml (sha1: a vs b, core yaps2)") print("other diagnostic", file=sys.stderr) return {}, None diff --git a/tests/test_exporters.py b/tests/test_exporters.py index 25d78659b..9e349992e 100644 --- a/tests/test_exporters.py +++ b/tests/test_exporters.py @@ -91,6 +91,13 @@ TRUTH = { }, } +# The profiles that name the runners: beetle_psx answers to the buildbot's +# mednafen_psx, duckstation has a standalone build. +PROFILES = { + "beetle_psx": {"type": "libretro", "cores": ["mednafen_psx"]}, + "duckstation": {"type": "standalone + libretro"}, +} + SCRAPED = { # The cores the platform runs, by its own names: an addition is written # with the runner that wants it. @@ -2443,7 +2450,7 @@ class BatoceraExport(unittest.TestCase): }, } } - systems, report = build_native_model(truth, SCRAPED) + systems, report = build_native_model(truth, SCRAPED, PROFILES) (foreign,) = systems["nintendo-snes"].files self.assertTrue(foreign.foreign_system) own = next(fe for fe in systems["psx"].files if fe.name == "scph7001.bin") @@ -2956,7 +2963,7 @@ class AnAdditionCarriesItsRunner(unittest.TestCase): {"name": "b.bin", "md5": "2" * 32, "_cores": ["duckstation"]}, {"name": "c.bin", "md5": "3" * 32, "_cores": ["no-such-core"]}, ]}}} - systems, _ = build_native_model(truth, scraped) + systems, _ = build_native_model(truth, scraped, PROFILES) runners = {fe.name: fe.runners for fe in systems["psx"].files} self.assertEqual(runners["a.bin"], [(False, "mednafen_psx")]) self.assertEqual(runners["b.bin"], [(True, "duckstation")])