diff --git a/scripts/cross_reference.py b/scripts/cross_reference.py index 62f907b60..5a998bd65 100644 --- a/scripts/cross_reference.py +++ b/scripts/cross_reference.py @@ -206,22 +206,15 @@ class _PlatformScope: run_anywhere: set[str] -def _platform_scope( - args: argparse.Namespace, parser: argparse.ArgumentParser, profiles: dict[str, dict], -) -> _PlatformScope: - from model.targets import load_target_config +def _platform_scope(args: argparse.Namespace, profiles: dict[str, dict]) -> _PlatformScope: + from model.targets import target_scope from resolve.cores import resolve_platform_cores - try: - target_cores = ( - load_target_config(args.platform, args.target, args.platforms_dir) - if args.target - else None - ) - except (FileNotFoundError, ValueError) as exc: - # A typo ended in a traceback where verify and slots name the - # closest target. - parser.error(str(exc)) + # A typo ended in a traceback where verify and slots name the closest + # target, and is refused the way they refuse it. + target_cores = target_scope( + [args.platform], args.target, args.platforms_dir + )[0].get(args.platform) config = load_platform_config(args.platform, args.platforms_dir) relevant = resolve_platform_cores(config, profiles, target_cores=target_cores) run_anywhere = ( @@ -321,7 +314,7 @@ def main() -> None: platform_scope: _PlatformScope | None = None standalone_cores: set[str] = set() if args.platform: - platform_scope = _platform_scope(args, parser, profiles) + platform_scope = _platform_scope(args, profiles) profiles = platform_scope.profiles standalone_cores = platform_scope.standalone_cores else: diff --git a/scripts/model/targets.py b/scripts/model/targets.py index 901334860..7201c40f5 100644 --- a/scripts/model/targets.py +++ b/scripts/model/targets.py @@ -272,8 +272,9 @@ def build_target_cores_cache( # The seventeen per-platform lines buried the one suggestion that # mattered: the refusal names the closest target across them all. raise ValueError(_unknown_target_message(target, platforms, platforms_dir)) + # On stderr: slots --json printed them ahead of its document. for note in notes: - print(note) + print(note, file=sys.stderr) return cache, kept diff --git a/scripts/slots.py b/scripts/slots.py index 291a6282a..fa2b29f61 100644 --- a/scripts/slots.py +++ b/scripts/slots.py @@ -28,8 +28,9 @@ from model.platforms import load_platform_config from core.ziptools import build_zip_contents_index from model.profiles import load_emulator_profiles from model.registry import list_registered_platforms, load_data_dir_registry -from model.targets import build_target_cores_cache +from model.targets import target_scope from resolve.localfile import resolution_is_hash_exact +from resolve.nativemode import platform_mode from resolve.standalone import standalone_set as platform_standalone_set from packs.slots import ADDRESSEE, Claim, Conflict, Decision, arbitrate, find_conflicts @@ -120,7 +121,8 @@ def format_conflict(conflict: Conflict) -> str: def scan_platform( platform: str, profiles: dict, db: dict, platforms_dir: str = PLATFORMS_DIR, - target_cores: set[str] | None = None, + target_cores: set[str] | None = None, zip_index: dict | None = None, + data_registry: dict | None = None, ) -> list[Conflict]: """Conflicts on one platform, using the cores that platform actually runs.""" from model.platforms import load_platform_config @@ -138,8 +140,8 @@ def scan_platform( db, config.get("base_destination", ""), platform_standalone_set(config), - build_zip_contents_index(db), - load_data_dir_registry(platforms_dir), + build_zip_contents_index(db) if zip_index is None else zip_index, + load_data_dir_registry(platforms_dir) if data_registry is None else data_registry, ) @@ -174,13 +176,13 @@ def main() -> int: import contextlib - from model.registry import load_data_dir_registry from packs.datadirs import hold_data_dirs # Claims resolve against data/: the trees stay put until the process # ends, which releases the hold. held = contextlib.ExitStack() - hold_data_dirs(held, load_data_dir_registry(args.platforms_dir)) + data_registry = load_data_dir_registry(args.platforms_dir) + hold_data_dirs(held, data_registry) db = load_database(args.db) profiles = load_emulator_profiles(args.emulators_dir) @@ -191,28 +193,25 @@ def main() -> int: if args.platform else list_registered_platforms(args.platforms_dir) ) - target_cores: dict[str, set[str] | None] = {} - if args.target: - try: - target_cores, names = build_target_cores_cache( - names, args.target, args.platforms_dir, is_all=not args.platform - ) - except (FileNotFoundError, ValueError) as exc: - parser.error(str(exc)) + target_cores, names = target_scope( + names, args.target, args.platforms_dir, is_all=not args.platform + ) + zip_index = build_zip_contents_index(db) found: dict[str, list[Conflict]] = {} collided: dict[str, list[Collision]] = {} for name in names: conflicts = scan_platform( - name, profiles, db, args.platforms_dir, target_cores.get(name) + name, profiles, db, args.platforms_dir, target_cores.get(name), + zip_index, data_registry, ) if conflicts: found[name] = conflicts collisions = find_collisions( load_platform_config(name, args.platforms_dir), db, - build_zip_contents_index(db), - load_data_dir_registry(args.platforms_dir), + zip_index, + data_registry, ) if collisions: collided[name] = collisions @@ -247,7 +246,7 @@ def main() -> int: fixable = 0 for platform, conflicts in found.items(): config = load_platform_config(platform, args.platforms_dir) - mode = config.get("verification_mode", "existence") + mode = platform_mode(config) print(f"{platform}: {len(conflicts)} contradicted [{mode}]") for conflict in conflicts: decision = arbitrate(conflict, mode, rule=config.get("hash_rule", "")) diff --git a/tests/test_tool_messages.py b/tests/test_tool_messages.py index d8c4d6f69..689bea2d4 100644 --- a/tests/test_tool_messages.py +++ b/tests/test_tool_messages.py @@ -292,6 +292,29 @@ class AMistypedTargetAcrossAllPlatforms(unittest.TestCase): self.assertEqual(out.getvalue(), "") +class AMistypedTargetEndsEveryToolAlike(unittest.TestCase): + """--target swtich: verify exited 1, slots and cross_reference 2.""" + + def test_one_exit_status(self): + for script in ("verify.py", "slots.py", "cross_reference.py"): + with self.subTest(script=script): + proc = subprocess.run( + [sys.executable, f"scripts/{script}", "--platform", "retroarch", + "--target", "swtich"], + cwd=REPO_ROOT, capture_output=True, text=True, check=False, timeout=300, + ) + self.assertEqual(proc.returncode, 1, proc.stderr) + self.assertIn("ERROR:", proc.stderr) + + def test_skipped_platforms_are_noted_on_stderr(self): + platforms = list_registered_platforms(str(REPO_ROOT / "platforms")) + out, err = io.StringIO(), io.StringIO() + with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err): + build_target_cores_cache(platforms, "switch", str(REPO_ROOT / "platforms"), is_all=True) + self.assertEqual(out.getvalue(), "") + self.assertIn("INFO: Skipping", err.getvalue()) + + class AMistypedDataKeyGetsTheClosestName(unittest.TestCase): """`--key dolphin-sis` printed all 27 keys on one line."""