diff --git a/scripts/check_freshness.py b/scripts/check_freshness.py index fba0e6299..cb5de1a18 100644 --- a/scripts/check_freshness.py +++ b/scripts/check_freshness.py @@ -63,7 +63,7 @@ from installer_build import pins as installer_build_pins from packs import datadirs from core import upstream from core.cli import InputMissing, add_repo_inputs, require_directory, suggest_names -from model.targets import apply_target_overrides +from model.targets import apply_target_overrides, platform_overrides, read_target_overrides from model.registry import list_registered_platforms, load_platform_registry from model.profiles import load_emulator_profiles, upstream_profile_index from core.artifacts import write_text_atomic @@ -580,11 +580,6 @@ def profile_name_index(profiles: dict[str, dict]) -> dict[str, set[str]]: return upstream_profile_index(profiles, include_aliases=True) -def _platform_overrides(overrides: dict, platform: str) -> dict: - """The per-target entries of _overrides.yml for one platform.""" - return ((overrides.get(platform) or {}).get("targets")) or {} - - def unresolved_target_cores( targets: dict, index: Container[str], overrides: dict ) -> dict[str, list[str]]: @@ -618,11 +613,7 @@ def check_targets( offline: bool, ) -> list[Finding]: targets_dir = platforms_dir / "targets" - overrides_path = targets_dir / "_overrides.yml" - overrides: dict = {} - if overrides_path.is_file(): - with overrides_path.open(encoding="utf-8") as fh: - overrides = yaml_load(fh) or {} + overrides = read_target_overrides(targets_dir) index = profile_name_index(profiles) scrapers = _target_scrapers() findings: list[Finding] = [] @@ -642,7 +633,7 @@ def check_targets( # read as current. stamp = str(old.get("scraped_at", ""))[:10] out = [Finding("targets", platform, SKIPPED, local=stamp, detail="offline")] - missing = unresolved_target_cores(old, index, _platform_overrides(overrides, platform)) + missing = unresolved_target_cores(old, index, platform_overrides(overrides, platform)) out.extend(_unresolved_findings(platform, missing)) return out if module is None and targets and all( @@ -657,7 +648,7 @@ def check_targets( declared = _declared_cores(platform) missing = unresolved_target_cores( {"targets": {name: {"cores": declared} for name in targets}}, - index, _platform_overrides(overrides, platform), + index, platform_overrides(overrides, platform), ) out.extend(_unresolved_findings(platform, missing)) return out @@ -675,7 +666,7 @@ def check_targets( detail=f"{age} days old" if age is not None else "no scrape stamp", ) ] - missing = unresolved_target_cores(old, index, _platform_overrides(overrides, platform)) + missing = unresolved_target_cores(old, index, platform_overrides(overrides, platform)) out.extend(_unresolved_findings(platform, missing)) return out try: @@ -697,7 +688,7 @@ def check_targets( detail=diff.summary(), ) ] - missing = unresolved_target_cores(new, index, _platform_overrides(overrides, platform)) + missing = unresolved_target_cores(new, index, platform_overrides(overrides, platform)) out.extend(_unresolved_findings(platform, missing)) return out diff --git a/scripts/model/targets.py b/scripts/model/targets.py index a35028182..901334860 100644 --- a/scripts/model/targets.py +++ b/scripts/model/targets.py @@ -12,12 +12,33 @@ from core.cli import SourceUnreadable, suggest_names from model.sources import source_yaml_file -def _target_overrides(platform_name: str, targets_dir: str) -> dict: +def read_target_overrides(targets_dir: str | os.PathLike[str]) -> dict: + """The whole _overrides.yml of a targets directory, {} without one.""" overrides_file = os.path.join(targets_dir, "_overrides.yml") if not os.path.exists(overrides_file): return {} - all_overrides = source_yaml_file(overrides_file) or {} - return all_overrides.get(platform_name, {}).get("targets", {}) + return source_yaml_file(overrides_file) or {} + + +def platform_overrides(all_overrides: dict, platform: str) -> dict: + """The per-target entries of _overrides.yml for one platform, an empty + value read as no entry.""" + return ((all_overrides.get(platform) or {}).get("targets")) or {} + + +def target_overrides(platform: str, targets_dir: str | os.PathLike[str]) -> dict: + """One platform's per-target entries, read from its targets directory.""" + return platform_overrides(read_target_overrides(targets_dir), platform) + + +def target_aliases(overrides: dict, target: str) -> list[str]: + """The other names a target answers to: the pack builder and the + installer read this one list, or the installer refuses a word the pack + accepts.""" + return [ + alias for alias in (overrides.get(target) or {}).get("aliases") or [] + if isinstance(alias, str) + ] def _target_alias_index(targets: dict, overrides: dict) -> dict[str, str]: @@ -25,19 +46,26 @@ def _target_alias_index(targets: dict, overrides: dict) -> dict[str, str]: alias_index: dict[str, str] = {} for tname in targets: alias_index[tname] = tname - for alias in overrides.get(tname, {}).get("aliases", []): + for alias in target_aliases(overrides, tname): alias_index[alias] = tname return alias_index +def _platform_targets(platform: str, targets_dir: str) -> dict | None: + """The `targets:` mapping of a platform's targets file, None without one.""" + target_file = os.path.join(targets_dir, f"{platform}.yml") + if not os.path.exists(target_file): + return None + return (source_yaml_file(target_file) or {}).get("targets", {}) + + def platform_files_target(platform: str, target: str, platforms_dir: str = PLATFORMS_DIR) -> bool: """Whether the platform's targets file knows this target, by any alias.""" targets_dir = os.path.join(platforms_dir, "targets") - target_file = os.path.join(targets_dir, f"{platform}.yml") - if not os.path.exists(target_file): + targets = _platform_targets(platform, targets_dir) + if targets is None: return False - targets = (source_yaml_file(target_file) or {}).get("targets", {}) - return target in _target_alias_index(targets, _target_overrides(platform, targets_dir)) + return target in _target_alias_index(targets, target_overrides(platform, targets_dir)) def canonical_target_name( @@ -50,14 +78,14 @@ def canonical_target_name( kept when no platform knows it or when platforms file it differently. """ targets_dir = os.path.join(platforms_dir, "targets") + all_overrides = read_target_overrides(targets_dir) found: set[str] = set() for platform_name in platforms: - target_file = os.path.join(targets_dir, f"{platform_name}.yml") - if not os.path.exists(target_file): + targets = _platform_targets(platform_name, targets_dir) + if targets is None: continue - targets = (source_yaml_file(target_file) or {}).get("targets", {}) canonical = _target_alias_index( - targets, _target_overrides(platform_name, targets_dir) + targets, platform_overrides(all_overrides, platform_name) ).get(target) if canonical: found.add(canonical) @@ -96,8 +124,8 @@ def load_target_config( Raises FileNotFoundError if no target file exists for the platform. """ targets_dir = os.path.join(platforms_dir, "targets") - target_file = os.path.join(targets_dir, f"{platform_name}.yml") - if not os.path.exists(target_file): + targets = _platform_targets(platform_name, targets_dir) + if targets is None: # The expected state for a platform that ships one build, said as # such: the file path read like a file missing from the repository. with_targets = sorted( @@ -108,10 +136,7 @@ def load_target_config( f"{platform_name} has no hardware targets: drop --target " f"(platforms with targets: {', '.join(with_targets)})" ) - data = source_yaml_file(target_file) or {} - - targets = data.get("targets", {}) - overrides = _target_overrides(platform_name, targets_dir) + overrides = target_overrides(platform_name, targets_dir) alias_index = _target_alias_index(targets, overrides) canonical = alias_index.get(target) @@ -175,19 +200,18 @@ def list_available_targets( Returns empty list if no target file exists. """ targets_dir = os.path.join(platforms_dir, "targets") - target_file = os.path.join(targets_dir, f"{platform_name}.yml") - if not os.path.exists(target_file): + targets = _platform_targets(platform_name, targets_dir) + if targets is None: return [] - data = source_yaml_file(target_file) or {} # The count --target filters on: overrides applied, as load_target_config # applies them. The scraped list counted Batocera's machine variants and # launchers, 198 cores shown for 187 used. - overrides = _target_overrides(platform_name, targets_dir) + overrides = target_overrides(platform_name, targets_dir) result = [] - for tname, tdata in sorted(data.get("targets", {}).items()): - aliases = (overrides.get(tname) or {}).get("aliases", []) + for tname, tdata in sorted(targets.items()): + aliases = target_aliases(overrides, tname) # None: the target runs every core the platform declares. found = _target_core_set(tdata or {}, overrides, tname, platforms_dir) count = None if found is None else len(found) @@ -275,13 +299,13 @@ def _unknown_target_message(target: str, platforms: list[str], platforms_dir: st import difflib targets_dir = os.path.join(platforms_dir, "targets") + all_overrides = read_target_overrides(targets_dir) owners: dict[str, list[str]] = {} for platform in platforms: - target_file = os.path.join(targets_dir, f"{platform}.yml") - if not os.path.exists(target_file): + targets = _platform_targets(platform, targets_dir) + if targets is None: continue - targets = (source_yaml_file(target_file) or {}).get("targets", {}) - overrides = _target_overrides(platform, targets_dir) + overrides = platform_overrides(all_overrides, platform) for name in _target_alias_index(targets, overrides): owners.setdefault(name, []).append(platform) message = f"unknown target '{target}': no platform has it." diff --git a/scripts/packs/targets.py b/scripts/packs/targets.py index 4742c895c..46ec358eb 100644 --- a/scripts/packs/targets.py +++ b/scripts/packs/targets.py @@ -8,8 +8,8 @@ import sys from pathlib import Path from core.artifacts import write_text_atomic -from model.safeparse import yaml_load -from model.targets import load_target_config +from model.sources import source_yaml_file +from model.targets import load_target_config, target_aliases, target_overrides def generate_target_manifests(targets_dir: str, output_dir: str) -> None: @@ -35,8 +35,7 @@ def target_list(yml_file: Path) -> dict[str, list[str] | dict | None]: """A platform's cores per hardware target, as install/targets/ publishes them and a pack carries them: aliases included, overrides applied.""" targets_path = yml_file.parent - with open(yml_file) as f: - data = yaml_load(f) or {} + data = source_yaml_file(yml_file) or {} targets = data.get("targets", {}) if not isinstance(targets, dict): raise ValueError(f"{yml_file}: targets must be a mapping") @@ -45,19 +44,7 @@ def target_list(yml_file: Path) -> dict[str, list[str] | dict | None]: # --target switch works there. Emitting only canonical names left the # installer rejecting the same word and, worse, carrying on with every # file instead: the user asked for a filter and got the full pack. - overrides_path = targets_path / "_overrides.yml" - alias_map: dict[str, list[str]] = {} - platform_overrides: dict = {} - if overrides_path.is_file(): - with open(overrides_path) as f: - all_overrides = yaml_load(f) or {} - platform_overrides = ( - all_overrides.get(yml_file.stem, {}).get("targets", {}) or {} - ) - for tname, ovr in platform_overrides.items(): - names = [a for a in (ovr or {}).get("aliases", []) if isinstance(a, str)] - if names: - alias_map[tname] = names + overrides = target_overrides(yml_file.stem, targets_path) result: dict[str, list[str] | dict | None] = {} for target_name, target_info in targets.items(): @@ -94,10 +81,10 @@ def target_list(yml_file: Path) -> dict[str, list[str] | dict | None]: if images and listed is not None else listed ) - for target_name, names in alias_map.items(): + for target_name in overrides: if target_name not in result: continue - for alias in names: + for alias in target_aliases(overrides, target_name): if alias in result: raise ValueError( f"{yml_file}: alias {alias!r} collides with a target name" diff --git a/scripts/scraper/batocera_scraper.py b/scripts/scraper/batocera_scraper.py index 1b126f644..e01d2929a 100644 --- a/scripts/scraper/batocera_scraper.py +++ b/scripts/scraper/batocera_scraper.py @@ -16,8 +16,8 @@ from pathlib import Path from model.database import load_database -from model.targets import load_target_config -from model.safeparse import yaml_load +from core.paths import PLATFORMS_DIR, REPO_ROOT +from model.targets import load_target_config, target_overrides from core.hashing import cut_md5_owner from model.safeparse import yaml_load_upstream @@ -87,13 +87,12 @@ ES_SYSTEMS_URL = ( # Names es_systems.yml files under an emulator that are not emulators: # machine selectors, video plugins, launchers. Read from the overrides the # target scraper applies, so one list says what is not a core. -OVERRIDES_PATH = Path(__file__).resolve().parents[2] / "platforms" / "targets" / "_overrides.yml" +TARGETS_DIR = REPO_ROOT / PLATFORMS_DIR / "targets" def _not_cores(platform: str = "batocera") -> set[str]: - data = yaml_load(OVERRIDES_PATH.read_text(encoding="utf-8")) or {} - default = (data.get(platform, {}).get("targets", {}) or {}).get("_default", {}) or {} - return {str(name) for name in default.get("remove_cores", [])} + default = target_overrides(platform, TARGETS_DIR).get("_default") or {} + return {str(name) for name in default.get("remove_cores") or []} def _es_systems_data(text: str) -> dict: diff --git a/tests/test_check_freshness.py b/tests/test_check_freshness.py index c5ef38ace..73aa8dc7a 100644 --- a/tests/test_check_freshness.py +++ b/tests/test_check_freshness.py @@ -272,7 +272,7 @@ class CoreResolutionTests(unittest.TestCase): } } self.assertEqual( - cf.unresolved_target_cores(targets, index, cf._platform_overrides(overrides, "plat")), + cf.unresolved_target_cores(targets, index, cf.platform_overrides(overrides, "plat")), {"ghost": ["x86_64"]}, ) diff --git a/tests/test_scraper_contract.py b/tests/test_scraper_contract.py index a14fb36e6..b5a97c416 100644 --- a/tests/test_scraper_contract.py +++ b/tests/test_scraper_contract.py @@ -625,7 +625,7 @@ class EveryTargetCoreReachesAProfile(unittest.TestCase): continue unresolved = check_freshness.unresolved_target_cores( yaml.safe_load(Path(path).read_text()), index, - check_freshness._platform_overrides(overrides, platform), + check_freshness.platform_overrides(overrides, platform), ) with self.subTest(platform=platform): if platform == "retropie": diff --git a/tests/test_target_names.py b/tests/test_target_names.py index 92d12fdeb..ef2f5d220 100644 --- a/tests/test_target_names.py +++ b/tests/test_target_names.py @@ -241,5 +241,31 @@ class PublishedListsAreNotNarrowed(unittest.TestCase): self.assertEqual(data["regions"], []) +class AnEmptyOverrideIsNoOverride(unittest.TestCase): + """`batocera:` or `board:` with no value raised AttributeError in the + builder and the installer lists, where check_freshness read it as none.""" + + def test_every_reader_takes_an_empty_value(self): + from model.targets import list_available_targets, load_target_config + from packs.targets import target_list + + with tempfile.TemporaryDirectory(dir=REPO_ROOT / "tmp") as tmp: + targets = Path(tmp) / "targets" + targets.mkdir() + for platform in ("p", "q"): + (targets / f"{platform}.yml").write_text( + "targets:\n board:\n cores: [a]\n", encoding="utf-8" + ) + (targets / "_overrides.yml").write_text( + "p:\nq:\n targets:\n board:\n", encoding="utf-8" + ) + for platform in ("p", "q"): + with self.subTest(platform=platform): + self.assertEqual(load_target_config(platform, "board", tmp), {"a"}) + self.assertEqual(list_available_targets(platform, tmp)[0]["aliases"], []) + self.assertEqual(target_list(targets / f"{platform}.yml"), {"board": ["a"]}) + self.assertEqual(canonical_target_name(["p", "q"], "board", tmp), "board") + + if __name__ == "__main__": unittest.main()