diff --git a/scripts/packs/platformrun.py b/scripts/packs/platformrun.py index 940a652c5..8818dcb58 100644 --- a/scripts/packs/platformrun.py +++ b/scripts/packs/platformrun.py @@ -26,6 +26,7 @@ from packs.naming import pack_narrowings, platform_pack_name, platform_target from packs.outputdir import holds_other_kind, pack_output_lock from packs.packcheck import run_verify_packs from packs.platformpack import generate_pack, generate_split_packs +from packs.runscope import settle_scope from packs.scope import pack_systems from packs.targets import target_list from packs.variants import pack_label, run_variants, skip_empty_truth @@ -287,18 +288,18 @@ def run_platform_mode( emu_profiles = load_emulator_profiles(args.emulators_dir) if system_filter: - platforms = platforms_with_systems( + platforms = settle_scope(platforms_with_systems( platforms, system_filter, emu_profiles, target_cores_cache, args.platforms_dir, args.target, args.all, - ) - platforms = without_profile_blind( + ), sys.stderr) + platforms = settle_scope(without_profile_blind( platforms, _narrowing_flags(args), args.platforms_dir, emu_profiles, target_cores_cache if args.target else None, args.all, - ) + ), sys.stdout) data_registry = load_data_dir_registry(args.platforms_dir) # The output directory is claimed before the data directory refresh: a diff --git a/scripts/packs/runscope.py b/scripts/packs/runscope.py new file mode 100644 index 000000000..9ebed5a0e --- /dev/null +++ b/scripts/packs/runscope.py @@ -0,0 +1,19 @@ +"""The platforms a run keeps, its notices printed and a refusal ending it.""" + +from __future__ import annotations + +import sys +from typing import TextIO + +from resolve.cores import RunScope + + +def settle_scope(scope: RunScope, notices_to: TextIO) -> list[str]: + """Print the scope's notices on `notices_to` and return the platforms it + keeps. A refused scope prints its reason on stderr and exits 1.""" + for line in scope.notices: + print(line, file=notices_to) + if scope.refusal is not None: + print(f"ERROR: {scope.refusal}", file=sys.stderr) + sys.exit(1) + return scope.kept diff --git a/scripts/packs/verifyplatforms.py b/scripts/packs/verifyplatforms.py index c47c0f82f..6561c7d73 100644 --- a/scripts/packs/verifyplatforms.py +++ b/scripts/packs/verifyplatforms.py @@ -2,6 +2,7 @@ from __future__ import annotations +import sys from dataclasses import dataclass, field from model.platforms import load_platform_config @@ -9,6 +10,7 @@ from model.profiles import load_emulator_profiles from model.registry import load_data_dir_registry from model.targets import canonical_target_name, target_scope from packs.coverage import verify_platform +from packs.runscope import settle_scope from resolve.contentlayout import TARGET_KEY from resolve.cores import platforms_with_systems, without_profile_blind from resolve.grouping import group_identical_platforms @@ -26,6 +28,7 @@ class PlatformRun: system_filter: list[str] | None = None no_game_data: bool = False required_only: bool = False + json_output: bool = False @dataclass @@ -48,10 +51,10 @@ def run_target_scope( platforms, run.target, run.platforms_dir, is_all=run.is_all ) if run.system_filter: - platforms = platforms_with_systems( + platforms = settle_scope(platforms_with_systems( platforms, run.system_filter, emu_profiles, target_cores_cache, run.platforms_dir, run.target, run.is_all, - ) + ), sys.stderr) return target_cores_cache, platforms @@ -64,8 +67,9 @@ def verify_platforms(platforms: list[str], db: dict, run: PlatformRun) -> Platfo target_cores_cache, platforms = run_target_scope(run, platforms, emu_profiles) # generate_pack refuses these on a platform whose cores no profile - # describes, and the report answers the same request the same way. - platforms = without_profile_blind( + # describes, and the report answers the same request the same way. Its + # notices go to stderr under --json, which prints the document on stdout. + platforms = settle_scope(without_profile_blind( platforms, [ flag for flag, given in ( @@ -77,7 +81,7 @@ def verify_platforms(platforms: list[str], db: dict, run: PlatformRun) -> Platfo emu_profiles, target_cores_cache if run.target else None, run.is_all, - ) + ), sys.stderr if run.json_output else sys.stdout) # Group identical platforms (same function as generate_pack) groups = group_identical_platforms( diff --git a/scripts/packs/verifyrun.py b/scripts/packs/verifyrun.py index ae388a141..fcdf8dced 100644 --- a/scripts/packs/verifyrun.py +++ b/scripts/packs/verifyrun.py @@ -55,6 +55,7 @@ def _run_platform_mode( system_filter=platform_systems, no_game_data=getattr(args, "no_game_data", False), required_only=args.required_only, + json_output=args.json, )) print_platform_results(args, results) exit_if_strict(args.strict, [r for r, _ in results.group_results]) diff --git a/scripts/resolve/cores.py b/scripts/resolve/cores.py index 4a859c6f6..100955720 100644 --- a/scripts/resolve/cores.py +++ b/scripts/resolve/cores.py @@ -3,8 +3,8 @@ from __future__ import annotations import functools -import sys from collections.abc import Collection +from dataclasses import dataclass from core.cli import suggest_names from core.paths import sanitize_pack_path @@ -16,6 +16,16 @@ from model.targets import list_available_targets, load_target_config from core.paths import REPO_ROOT +@dataclass(frozen=True) +class RunScope: + """The platforms a run keeps, a notice for each one it leaves out, and + why it refuses a platform the command line names.""" + + kept: list[str] + notices: list[str] + refusal: str | None = None + + def without_profile_blind( platforms: list[str], flags: list[str], @@ -23,18 +33,18 @@ def without_profile_blind( profiles: dict[str, dict], target_cores: dict[str, set[str] | None] | None, is_all: bool, -) -> list[str]: +) -> RunScope: """The platforms a narrowing read from profiles can apply to. Regions, search order and game data are read from the profiles of the cores a platform runs. MiSTer and BizHawk declare `cores: []`: a --region, --one-per-slot or --no-game-data pack of theirs withdrew nothing while its name and README said it had. A named platform is - refused (exit 1). Under --all it is left out with a notice. generate_pack - and verify both read this. + refused. Under --all it is left out with a notice. generate_pack and + verify both read this, and print the notices and the refusal. """ if not flags: - return platforms + return RunScope(platforms, []) blind = [ name for name in platforms if not resolve_platform_cores( @@ -43,16 +53,17 @@ def without_profile_blind( target_cores=(target_cores or {}).get(name), ) ] + kept = [name for name in platforms if name not in blind] + notices: list[str] = [] for name in blind: message = ( f"{name} runs no core a profile describes, and {', '.join(flags)} " "reads the profiles" ) if not is_all: - print(f"ERROR: {message}: drop {', '.join(flags)}", file=sys.stderr) - sys.exit(1) - print(f" {message}: left out") - return [name for name in platforms if name not in blind] + return RunScope(kept, notices, f"{message}: drop {', '.join(flags)}") + notices.append(f" {message}: left out") + return RunScope(kept, notices) def resolve_platform_cores( @@ -403,16 +414,18 @@ def platforms_with_systems( platforms_dir: str, target: str | None, is_all: bool, -) -> list[str]: +) -> RunScope: """The platforms a --system list leaves a system to cover, before any lock. Under --all a platform without one is out of scope, as a platform without the target is: `--all --system sony-playstation-2` built every PS2 pack and exited 1 on MiSTer, which has no PS2. Under --platform the - request is refused here, with its reason. verify.py and generate_pack.py - scope a --system list through this one function. + request is refused, with its reason. verify.py and generate_pack.py + scope a --system list through this one function, and print the notices + on stderr: verify --json prints its document on stdout. """ - kept = [] + kept: list[str] = [] + notices: list[str] = [] for name in platforms: config = load_platform_config(name, platforms_dir) tc = target_cores_cache.get(name) if target else None @@ -420,23 +433,22 @@ def platforms_with_systems( kept.append(name) continue if not is_all: - reason = system_scope_error(config, name, system_filter, target) - print(f"ERROR: {reason}", file=sys.stderr) - sys.exit(1) + return RunScope( + kept, notices, system_scope_error(config, name, system_filter, target) + ) declared = select_systems(config.get("systems", {}), system_filter) reason = ( system_scope_error(config, name, system_filter, target) if declared and target else f"no system {', '.join(system_filter)}" ) - # On stderr: verify --json prints its document on stdout. - print(f"INFO: Skipping {name}: {reason}", file=sys.stderr) + notices.append(f"INFO: Skipping {name}: {reason}") if not kept: where = f" on target {target}" if target else "" - print(f"ERROR: no platform here runs {', '.join(system_filter)}{where}", - file=sys.stderr) - sys.exit(1) - return kept + return RunScope( + kept, notices, f"no platform here runs {', '.join(system_filter)}{where}" + ) + return RunScope(kept, notices) def system_scope_error( diff --git a/tests/sources.py b/tests/sources.py index f7304c46c..02e02676a 100644 --- a/tests/sources.py +++ b/tests/sources.py @@ -20,14 +20,14 @@ COMMANDS = { "packs/platformpack.py", "packs/emulatorpack.py", "packs/md5pack.py", "packs/targets.py", "packs/manifestentry.py", "packs/manifestcores.py", "packs/standalone.py", "packs/manifest.py", "packs/finalize.py", "packs/outputdir.py", "packs/variants.py", - "packs/manifestrun.py", "packs/packcheck.py", "packs/platformrun.py", + "packs/manifestrun.py", "packs/packcheck.py", "packs/runscope.py", "packs/platformrun.py", "packs/packoptions.py", "packs/packmodes.py", ), "verify.py": ( "packs/verdicts.py", "packs/gaps.py", "packs/coverage.py", "packs/emulatorcheck.py", "packs/report.py", "packs/verifyplatforms.py", "packs/verifysummary.py", "packs/verifyoutput.py", "packs/strictgate.py", "packs/verifyoptions.py", - "packs/verifyprint.py", "packs/verifyrun.py", + "packs/verifyprint.py", "packs/runscope.py", "packs/verifyrun.py", ), "cross_reference.py": ( "packs/crossref.py", "packs/crossrefscope.py", "packs/crossrefrun.py", diff --git a/tests/test_target_verify.py b/tests/test_target_verify.py index bcfe13b1e..294fbe1a3 100644 --- a/tests/test_target_verify.py +++ b/tests/test_target_verify.py @@ -178,24 +178,54 @@ class APlatformWithoutTheSystemIsOutOfScope(unittest.TestCase): values = {"target": None, "all": True} values.update(args) - out = io.StringIO() - with contextlib.redirect_stdout(out), contextlib.redirect_stderr(out): - kept = platforms_with_systems( - ["retroarch", "misterfpga"], ["sony-playstation-2"], - load_emulator_profiles(str(REPO_ROOT / "emulators")), {}, - str(REPO_ROOT / "platforms"), values["target"], values["all"], - ) - return kept, out.getvalue() + return platforms_with_systems( + ["retroarch", "misterfpga"], ["sony-playstation-2"], + load_emulator_profiles(str(REPO_ROOT / "emulators")), {}, + str(REPO_ROOT / "platforms"), values["target"], values["all"], + ) def test_all_skips_it(self): - kept, out = self._scope() - self.assertEqual(kept, ["retroarch"]) - self.assertIn("INFO: Skipping misterfpga: no system sony-playstation-2", out) + scope = self._scope() + self.assertEqual(scope.kept, ["retroarch"]) + self.assertIsNone(scope.refusal) + self.assertIn("INFO: Skipping misterfpga: no system sony-playstation-2", + scope.notices) def test_a_named_platform_is_refused(self): - with self.assertRaises(SystemExit) as ctx: - self._scope(all=False) + from packs.runscope import settle_scope + + scope = self._scope(all=False) + self.assertIn("misterfpga", scope.refusal or "") + err = io.StringIO() + with contextlib.redirect_stderr(err), self.assertRaises(SystemExit) as ctx: + settle_scope(scope, io.StringIO()) self.assertEqual(ctx.exception.code, 1) + self.assertEqual(err.getvalue(), f"ERROR: {scope.refusal}\n") + + def test_a_profile_blind_platform_prints_nothing_itself(self): + """verify --all --region us --json printed the left-out notice on + stdout, ahead of the JSON document.""" + from model.profiles import load_emulator_profiles + from resolve.cores import without_profile_blind + + out = io.StringIO() + with contextlib.redirect_stdout(out), contextlib.redirect_stderr(out): + scope = without_profile_blind( + ["retroarch", "misterfpga"], ["--region"], str(REPO_ROOT / "platforms"), + load_emulator_profiles(str(REPO_ROOT / "emulators")), None, True, + ) + self.assertEqual(out.getvalue(), "") + self.assertEqual(scope.kept, ["retroarch"]) + self.assertEqual(scope.notices, [ + " misterfpga runs no core a profile describes, and --region reads " + "the profiles: left out", + ]) + named = without_profile_blind( + ["misterfpga"], ["--region"], str(REPO_ROOT / "platforms"), + load_emulator_profiles(str(REPO_ROOT / "emulators")), None, False, + ) + self.assertEqual(named.refusal, "misterfpga runs no core a profile describes, " + "and --region reads the profiles: drop --region") def test_verify_reports_on_the_packs_generate_pack_builds(self): """verify --all --system exited 2 'mutually exclusive' while