From 7bc02025dc20e209996b17bcbdad5978427e39a0 Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Sat, 21 Nov 2026 08:19:26 +0100 Subject: [PATCH] refactor: share the cli mode conflict table model.cliargs.mode_conflicts holds the mode, --standalone, --target and --no-game-data rules verify and generate_pack read, each adding its own flags. verify --all --system --standalone is now refused instead of dropping --standalone. Summary, listing and pack counts go through count_text (no more "1 platforms" or "0 data directory"). --- scripts/generate_pack.py | 23 +++----------- scripts/model/cliargs.py | 41 ++++++++++++++++++++++-- scripts/verify.py | 31 ++++-------------- tests/test_audit_regressions.py | 5 +++ tests/test_e2e.py | 56 +++++++++++++++------------------ 5 files changed, 81 insertions(+), 75 deletions(-) diff --git a/scripts/generate_pack.py b/scripts/generate_pack.py index 3d9d82daf..eb13e0299 100644 --- a/scripts/generate_pack.py +++ b/scripts/generate_pack.py @@ -36,6 +36,7 @@ from model.profiles import ( from model.cliargs import ( LISTING_FLAGS, comma_list, + mode_conflicts, platform_arg, refuse_beside_mode, requested_regions, @@ -54,6 +55,7 @@ from core.artifacts import ( ArtifactLockBusy, ) from core.ziptools import build_zip_contents_index +from core.text import count_text from resolve.cores import ( platforms_with_systems, without_profile_blind, @@ -190,8 +192,6 @@ def _argument_conflicts(args: argparse.Namespace) -> list[tuple[bool, str]]: # seen: compared with "full", `--emulator X --source full` went through. source_given = getattr(args, "source", None) is not None with_platform = has_system and (has_platform or has_all) - # Game data is categorized against a platform's list and the cores it - # runs: a pack of one emulator or of a system's emulators has neither. no_game_data = getattr(args, "no_game_data", False) one_per_slot = getattr(args, "one_per_slot", False) return [ @@ -199,21 +199,10 @@ def _argument_conflicts(args: argparse.Namespace) -> list[tuple[bool, str]]: (has_from_md5 and has_all, "--from-md5 requires --platform or --emulator, not --all"), (has_from_md5 and has_system, "--from-md5 and --system are mutually exclusive"), (has_from_md5 and args.split, "--split and --from-md5 are mutually exclusive"), - # --platform/--all and --system can combine (system filters within platform) - # --emulator is exclusive with everything else - (has_emulator and (has_platform or has_all or has_system), - "--emulator is mutually exclusive with --platform, --all, and --system"), - (has_platform and has_all, "--platform and --all are mutually exclusive"), - (not (has_platform or has_all or has_emulator or has_system or has_from_md5), - "Specify --platform, --all, --emulator, --system, or --from-md5"), - (args.standalone and not ( - has_emulator or (has_system and not has_platform and not has_all) - ), "--standalone requires --emulator or --system (without --platform)"), + *mode_conflicts(args, (("--from-md5", has_from_md5),)), (args.split and not (has_platform or has_all), "--split requires --platform or --all"), (args.split and has_emulator, "--split is incompatible with --emulator"), (args.group_by is not None and not args.split, "--group-by requires --split"), - (args.target and not (has_platform or has_all), "--target requires --platform or --all"), - (args.target and has_emulator, "--target is incompatible with --emulator"), (args.manifest and not (has_platform or has_all), "--manifest requires --platform or --all"), (args.manifest and has_emulator, "--manifest is incompatible with --emulator"), @@ -228,8 +217,6 @@ def _argument_conflicts(args: argparse.Namespace) -> list[tuple[bool, str]]: (has_from_md5 and source_given, "--source is incompatible with --from-md5"), (with_platform and args.manifest, "--system is incompatible with --manifest"), (with_platform and args.split, "--system is incompatible with --split"), - (no_game_data and not (has_platform or has_all), - "--no-game-data requires --platform or --all"), (no_game_data and has_from_md5, "--no-game-data is incompatible with --from-md5"), (one_per_slot and has_from_md5, "--one-per-slot and --from-md5 are mutually exclusive"), (one_per_slot and args.manifest, "--one-per-slot is incompatible with --manifest"), @@ -516,7 +503,7 @@ def _verification_summary(failing: list[str], verified: int, output_dir: str) -> file=sys.stderr, ) elif verified: - print(f"Verified {verified} pack{'s' if verified != 1 else ''}: all OK") + print(f"Verified {count_text(verified, 'pack')}: all OK") if not verified: # Every platform skipped is no check at all: a mistyped or purged # output directory read as a passed integrity check. @@ -1175,7 +1162,7 @@ def _refresh_data_directories(args: argparse.Namespace, data_registry: dict) -> results = refresh_all(data_registry, force=args.refresh_data) updated = sum(1 for v in results.values() if v) if updated: - print(f"Refreshed {updated} data director{'ies' if updated > 1 else 'y'}") + print(f"Refreshed {count_text(updated, 'data directory', 'data directories')}") failed = sorted(k for k, v in results.items() if v is None) if failed: print(f"WARNING: data directory refresh failed: {', '.join(failed)}") diff --git a/scripts/model/cliargs.py b/scripts/model/cliargs.py index 35e716545..45914122a 100644 --- a/scripts/model/cliargs.py +++ b/scripts/model/cliargs.py @@ -20,6 +20,7 @@ from model.registry import ( ) from model.systems import platform_system_names, resolve_system_arg, system_index from core.paths import PLATFORMS_DIR, REPO_ROOT +from core.text import count_text, series def work_from_repo_root( @@ -172,6 +173,42 @@ LISTING_FLAGS: dict[str, set[str]] = { } +def mode_conflicts( + args: argparse.Namespace, other_modes: Sequence[tuple[str, bool]] = (), +) -> list[tuple[bool, str]]: + """The mode rules verify.py and generate_pack.py share, each refused + combination with its message, in the order they are checked. + + --platform or --all with --system narrows each platform to the system. + *other_modes* are the modes a CLI adds (generate_pack's --from-md5), + named in the message asking for a mode. Each CLI checks its own flags + after these. + """ + has_platform = bool(args.platform) + has_all = bool(args.all) + has_emulator = bool(args.emulator) + has_system = bool(args.system) + scoped = has_platform or has_all + names = ["--platform", "--all", "--emulator", "--system", *(name for name, _ in other_modes)] + return [ + (has_emulator and (scoped or has_system), + "--emulator is mutually exclusive with --platform, --all, and --system"), + (has_platform and has_all, "--platform and --all are mutually exclusive"), + (not (scoped or has_emulator or has_system or any(on for _, on in other_modes)), + f"Specify {series(names, 'or')}"), + (bool(args.standalone) and scoped, + "--standalone is incompatible with --platform and --all"), + (bool(args.standalone) and not (has_emulator or has_system), + "--standalone requires --emulator or --system"), + (bool(getattr(args, "target", None)) and not scoped, + "--target requires --platform or --all"), + # Game data is categorized against a platform's list and the cores it + # runs: a run over one emulator or a system's emulators has neither. + (bool(getattr(args, "no_game_data", False)) and not scoped, + "--no-game-data requires --platform or --all"), + ] + + def refuse_beside_mode( modes: Sequence[tuple[str, object]], given: set[str], @@ -236,7 +273,7 @@ def list_system_ids(emulators_dir: str) -> None: index = system_index(load_emulator_profiles(emulators_dir)) for _key, (spelling, emulators) in sorted(index.items(), key=lambda kv: kv[1][0]): count = len(emulators) - print(f" {spelling:35s} ({count} emulator{'s' if count > 1 else ''})") + print(f" {spelling:35s} ({count_text(count, 'emulator')})") def print_platform_listing(platforms_dir: str) -> None: @@ -262,6 +299,6 @@ def list_platform_system_ids(platform_name: str, platforms_dir: str) -> None: file_count = len(systems[sys_id].get("files", [])) mfr = systems[sys_id].get("manufacturer", "") mfr_display = f" [{mfr.split('|')[0]}]" if mfr else "" - count = f"({file_count} file{'s' if file_count != 1 else ''})" + count = f"({count_text(file_count, 'file')})" also = f" also: {', '.join(names[sys_id])}" if sys_id in names else "" print(f" {sys_id:35s} {count:12s}{mfr_display}{also}".rstrip()) diff --git a/scripts/verify.py b/scripts/verify.py index 7d63f5787..2dc1e552c 100644 --- a/scripts/verify.py +++ b/scripts/verify.py @@ -33,6 +33,7 @@ from pathlib import Path sys.path.insert(0, os.path.dirname(__file__)) from core.cli import add_emulator_selection, add_repo_inputs +from core.text import count_text from resolve.contentlayout import TARGET_KEY from model.targets import ( canonical_target_name, @@ -41,6 +42,7 @@ from model.targets import ( from model.cliargs import ( LISTING_FLAGS, comma_list, + mode_conflicts, platform_arg, refuse_beside_mode, requested_regions, @@ -109,21 +111,11 @@ def _refuse_listing_narrowings( def _refuse_mode_flags(args: argparse.Namespace, parser: argparse.ArgumentParser) -> None: """A flag the chosen mode does not read is refused, never ignored.""" for refused, message in ( - (args.standalone and not (args.emulator or args.system), - "--standalone requires --emulator or --system"), - (args.standalone and args.platform, - "--standalone is incompatible with --platform"), - (args.target and not (args.platform or args.all), - "--target requires --platform or --all"), - (args.target and (args.emulator or (args.system and not (args.platform or args.all))), - "--target is incompatible with --emulator and --system"), + *mode_conflicts(args), (args.destinations and args.system, "--destinations describes the full pack and is incompatible with --system"), (args.destinations and not (args.platform or args.all), "--destinations requires --platform or --all"), - # Game data is categorized against a platform's list and its cores. - (getattr(args, "no_game_data", False) and not (args.platform or args.all), - "--no-game-data requires --platform or --all"), (args.destinations and getattr(args, "region", None), "--destinations describes the full pack and is incompatible with --region"), (args.destinations and args.required_only, @@ -197,19 +189,8 @@ def _build_parser() -> argparse.ArgumentParser: def _check_mode(args: argparse.Namespace, parser: argparse.ArgumentParser) -> bool: """Refuse a request naming no mode or several, and say if it is the system mode.""" - # --system beside --platform or --all narrows each platform, as in - # generate_pack: refused under --all, verify could not report on the - # packs generate_pack --all --system builds. - system_mode = bool(args.system) and not (args.platform or args.all) - modes = sum(1 for x in (args.platform, args.all, args.emulator, system_mode) if x) - if modes == 0: - parser.error("Specify --platform, --all, --emulator, or --system") - if modes > 1: - parser.error( - "--platform, --all, --emulator, and --system are mutually exclusive" - ) _refuse_mode_flags(args, parser) - return system_mode + return bool(args.system) and not (args.platform or args.all) def _run_emulator_mode(args: argparse.Namespace, db: dict, requested_regions: list[str]) -> None: @@ -480,8 +461,8 @@ def _overall_summary(group_results: list[tuple[dict, list[str]]]) -> str: for r, group in group_results } total = sum(counts.values()) - parts.append(f"{total} {'discrepancy' if total == 1 else 'discrepancies'}{where(counts)}") - lines = [f"{platforms} platforms: {', '.join(parts)}"] + parts.append(f"{count_text(total, 'discrepancy', 'discrepancies')}{where(counts)}") + lines = [f"{count_text(platforms, 'platform')}: {', '.join(parts)}"] # Grouped by file, not by platform: sfiii2a.zip missing everywhere was # counted and printed once per platform, 24 files for 10 distinct ones. diff --git a/tests/test_audit_regressions.py b/tests/test_audit_regressions.py index 63551c5a1..d9eab78b8 100644 --- a/tests/test_audit_regressions.py +++ b/tests/test_audit_regressions.py @@ -4631,6 +4631,11 @@ class EveryToolRefusesAFlagItWouldDrop(unittest.TestCase): ["--list-systems", "--region", "us"], ["--emulator", "handy", "--target", "switch"], ["--platform", "retroarch", "--standalone"], + ["--all", "--system", "sony-playstation", "--standalone"], + ], + "scripts/cross_reference.py": [ + ["--all", "--platform", "retroarch"], + ["--list-emulators", "--all"], ], "scripts/pipeline.py": [ ["--offline", "--skip-packs", "--source", "truth"], diff --git a/tests/test_e2e.py b/tests/test_e2e.py index b296f5a85..0713d3020 100644 --- a/tests/test_e2e.py +++ b/tests/test_e2e.py @@ -2959,38 +2959,34 @@ class TestE2E(unittest.TestCase): """--platform + --system is accepted (not rejected at validation stage).""" import argparse - # Build the same parser as generate_pack.main() - parser = argparse.ArgumentParser() - parser.add_argument("--platform", "-p") - parser.add_argument("--all", action="store_true") - parser.add_argument("--emulator", "-e") - parser.add_argument("--system", "-s") - parser.add_argument("--standalone", action="store_true") - parser.add_argument("--split", action="store_true") - parser.add_argument( - "--group-by", choices=["system", "manufacturer"], default="system" - ) - parser.add_argument("--target", "-t") - parser.add_argument("--from-md5") - parser.add_argument("--from-md5-file") - parser.add_argument("--required-only", action="store_true") - args = parser.parse_args(["--platform", "retroarch", "--system", "psx"]) + from model.cliargs import mode_conflicts - # Replicate validation logic from main() - has_platform = bool(args.platform) - has_all = args.all - has_emulator = bool(args.emulator) - has_system = bool(args.system) - has_from_md5 = bool(args.from_md5 or args.from_md5_file) + def refused(**given: object) -> list[str]: + args = argparse.Namespace(**{ + "platform": None, "all": False, "emulator": None, "system": None, + "standalone": False, "target": None, "no_game_data": False, **given, + }) + return [message for on, message in mode_conflicts(args) if on] - # These should NOT raise - self.assertFalse(has_emulator and (has_platform or has_all or has_system)) - self.assertFalse(has_platform and has_all) - self.assertTrue( - has_platform or has_all or has_emulator or has_system or has_from_md5 - ) - # --platform + --system is a valid combination - self.assertTrue(has_platform and has_system) + self.assertEqual(refused(platform="retroarch", system="psx"), []) + self.assertEqual(refused(all=True, system="psx"), []) + self.assertEqual(refused(system="psx", standalone=True), []) + # One table for verify and generate_pack: verify took --all --system + # --standalone and dropped --standalone. + self.assertTrue(refused(all=True, system="psx", standalone=True)) + self.assertTrue(refused(emulator="handy", system="psx")) + self.assertTrue(refused(platform="retroarch", all=True)) + self.assertTrue(refused(system="psx", target="switch")) + self.assertTrue(refused(emulator="handy", no_game_data=True)) + self.assertIn("Specify --platform, --all, --emulator, --system or --from-md5", [ + message for on, message in mode_conflicts( + argparse.Namespace(platform=None, all=False, emulator=None, system=None, + standalone=False), + (("--from-md5", False),), + ) if on + ]) + for script in ("verify.py", "generate_pack.py"): + self.assertIn("mode_conflicts(args", script_source(script), script) # BizHawk scraper tests