mirror of
https://github.com/Abdess/retroarch_system.git
synced 2026-11-23 10:07:48 -06:00
fix: share listing and region parsing in CLIs
verify and generate_pack read --region, comma lists and the listing modes through model.cliargs (requested_regions, comma_list, LISTING_FLAGS, refuse_beside_mode, run_listing). verify --list-emulators --list-systems printed the first listing and exited 0; it now refuses the second as generate_pack does.
This commit is contained in:
1 parent
ee66aa4e66
commit
b52c7cfafb
4 files changed
+116
-130
No files matched your search
+20
-60
@@ -34,16 +34,16 @@ from model.profiles import (
|
||||
load_emulator_profiles,
|
||||
)
|
||||
from model.cliargs import (
|
||||
LISTING_FLAGS,
|
||||
comma_list,
|
||||
platform_arg,
|
||||
refuse_beside_mode,
|
||||
requested_regions,
|
||||
require_platform,
|
||||
list_emulator_profiles,
|
||||
list_platform_system_ids,
|
||||
print_platform_listing,
|
||||
list_system_ids,
|
||||
run_listing,
|
||||
)
|
||||
from core.cli import add_emulator_selection, add_repo_inputs, require_directory
|
||||
from model.targets import (
|
||||
target_listing_line,
|
||||
target_scope,
|
||||
)
|
||||
from model.systems import (
|
||||
@@ -74,7 +74,6 @@ from model.platforms import load_platform_config
|
||||
from model.safeparse import require_yaml
|
||||
from resolve import packfiles as packresolve
|
||||
from packs.datadirs import hold_data_dirs
|
||||
from model import region
|
||||
|
||||
from packs.integrity import verify_pack_against_platform
|
||||
from resolve.packfiles import (
|
||||
@@ -909,37 +908,25 @@ def _refuse_unapplied_flags(args: argparse.Namespace, parser: argparse.ArgumentP
|
||||
"""
|
||||
# Parsed before the quick-exit modes: --verify-packs returns early and
|
||||
# still needs the region priority list to narrow its expectation.
|
||||
args.regions = []
|
||||
if args.region:
|
||||
try:
|
||||
args.regions = region.parse_requested(args.region)
|
||||
except ValueError as exc:
|
||||
parser.error(str(exc))
|
||||
if args.manifest_targets:
|
||||
parser.error("--region is incompatible with --manifest-targets")
|
||||
args.regions = requested_regions(args.region, parser)
|
||||
if args.region and args.manifest_targets:
|
||||
parser.error("--region is incompatible with --manifest-targets")
|
||||
if args.one_per_slot and args.manifest_targets:
|
||||
parser.error("--one-per-slot is incompatible with --manifest-targets")
|
||||
_refuse_refresh_data(args, parser)
|
||||
|
||||
_refuse_for_all_variants(args, parser)
|
||||
mode = next(
|
||||
refuse_beside_mode(
|
||||
(
|
||||
name for name, on in (
|
||||
("--manifest-targets", args.manifest_targets),
|
||||
("--verify-packs", args.verify_packs and not args.all_variants),
|
||||
("--list-platforms", args.list),
|
||||
("--list-emulators", args.list_emulators),
|
||||
("--list-systems", args.list_systems),
|
||||
("--list-targets", args.list_targets),
|
||||
) if on
|
||||
("--manifest-targets", args.manifest_targets),
|
||||
("--verify-packs", args.verify_packs and not args.all_variants),
|
||||
("--list-platforms", args.list),
|
||||
("--list-emulators", args.list_emulators),
|
||||
("--list-systems", args.list_systems),
|
||||
("--list-targets", args.list_targets),
|
||||
),
|
||||
None,
|
||||
_given_flags(args), QUICK_MODE_FLAGS, parser,
|
||||
)
|
||||
if mode is None:
|
||||
return
|
||||
given = _given_flags(args)
|
||||
for flag in sorted(given - QUICK_MODE_FLAGS[mode] - {mode}):
|
||||
parser.error(f"{flag} is incompatible with {mode}")
|
||||
|
||||
|
||||
# What each mode that returns early reads. Anything else given beside it is
|
||||
@@ -952,10 +939,7 @@ QUICK_MODE_FLAGS: dict[str, set[str]] = {
|
||||
},
|
||||
# One manifest per hardware target, from the target files themselves.
|
||||
"--manifest-targets": set(),
|
||||
"--list-platforms": set(),
|
||||
"--list-emulators": set(),
|
||||
"--list-systems": {"--platform"},
|
||||
"--list-targets": {"--platform"},
|
||||
**LISTING_FLAGS,
|
||||
}
|
||||
|
||||
|
||||
@@ -1147,31 +1131,7 @@ def _run_quick_mode(
|
||||
os.path.join(args.platforms_dir, "targets"), args.output_dir
|
||||
)
|
||||
return True
|
||||
if args.list:
|
||||
print_platform_listing(args.platforms_dir)
|
||||
return True
|
||||
if args.list_emulators:
|
||||
list_emulator_profiles(args.emulators_dir)
|
||||
return True
|
||||
if args.list_systems:
|
||||
if args.platform:
|
||||
list_platform_system_ids(args.platform, args.platforms_dir)
|
||||
else:
|
||||
list_system_ids(args.emulators_dir)
|
||||
return True
|
||||
if args.list_targets:
|
||||
if not args.platform:
|
||||
parser.error("--list-targets requires --platform")
|
||||
from model.targets import list_available_targets
|
||||
|
||||
targets = list_available_targets(args.platform, args.platforms_dir)
|
||||
if not targets:
|
||||
print(f"No targets configured for platform '{args.platform}'")
|
||||
return True
|
||||
for t in targets:
|
||||
print(target_listing_line(t))
|
||||
return True
|
||||
return False
|
||||
return run_listing(args, parser, args.list)
|
||||
|
||||
|
||||
def _run_hash_mode(args: argparse.Namespace) -> None:
|
||||
@@ -1224,7 +1184,7 @@ def _run_profile_mode(args: argparse.Namespace, db: dict, zip_contents: dict) ->
|
||||
"""--emulator, or --system without a platform: the emulators' own layout."""
|
||||
# Emulator mode
|
||||
if args.emulator:
|
||||
names = [n.strip() for n in args.emulator.split(",") if n.strip()]
|
||||
names = comma_list(args.emulator)
|
||||
with _pack_output_lock(args.output_dir):
|
||||
built = generate_emulator_pack(
|
||||
names,
|
||||
@@ -1244,7 +1204,7 @@ def _run_profile_mode(args: argparse.Namespace, db: dict, zip_contents: dict) ->
|
||||
return
|
||||
|
||||
# System mode (standalone, without platform context)
|
||||
system_ids = [s.strip() for s in args.system.split(",") if s.strip()]
|
||||
system_ids = comma_list(args.system)
|
||||
with _pack_output_lock(args.output_dir):
|
||||
built = generate_system_pack(
|
||||
system_ids,
|
||||
|
||||
@@ -5,6 +5,7 @@ from __future__ import annotations
|
||||
import argparse
|
||||
import os
|
||||
import re
|
||||
from collections.abc import Mapping, Sequence
|
||||
from pathlib import Path
|
||||
|
||||
try:
|
||||
@@ -108,6 +109,76 @@ def selected_platforms(
|
||||
raise SystemExit("ERROR: specify --platform or --all")
|
||||
|
||||
|
||||
def comma_list(value: str) -> list[str]:
|
||||
"""The names of a comma-separated option, blanks dropped."""
|
||||
return [name.strip() for name in value.split(",") if name.strip()]
|
||||
|
||||
|
||||
def requested_regions(value: str | None, parser: argparse.ArgumentParser) -> list[str]:
|
||||
"""--region as a priority list, best first; a bad value ends the run."""
|
||||
if not value:
|
||||
return []
|
||||
from model import region
|
||||
|
||||
try:
|
||||
return region.parse_requested(value)
|
||||
except ValueError as exc:
|
||||
parser.error(str(exc))
|
||||
|
||||
|
||||
# What each listing reads. Anything else given beside it, another listing
|
||||
# included, is refused: printing past a narrowing flag lets the user believe
|
||||
# it applied.
|
||||
LISTING_FLAGS: dict[str, set[str]] = {
|
||||
"--list-platforms": set(),
|
||||
"--list-emulators": set(),
|
||||
"--list-systems": {"--platform"},
|
||||
"--list-targets": {"--platform"},
|
||||
}
|
||||
|
||||
|
||||
def refuse_beside_mode(
|
||||
modes: Sequence[tuple[str, object]],
|
||||
given: set[str],
|
||||
allowed: Mapping[str, set[str]],
|
||||
parser: argparse.ArgumentParser,
|
||||
) -> str | None:
|
||||
"""The first mode requested, after refusing every other given flag it
|
||||
does not read. A flag is applied or refused, never ignored."""
|
||||
mode = next((name for name, on in modes if on), None)
|
||||
if mode is not None:
|
||||
for flag in sorted(given - allowed[mode] - {mode}):
|
||||
parser.error(f"{flag} is incompatible with {mode}")
|
||||
return mode
|
||||
|
||||
|
||||
def run_listing(
|
||||
args: argparse.Namespace, parser: argparse.ArgumentParser, list_platforms: bool
|
||||
) -> bool:
|
||||
"""Print the listing the command line asks for. True when one ran."""
|
||||
if list_platforms:
|
||||
print_platform_listing(args.platforms_dir)
|
||||
elif args.list_emulators:
|
||||
list_emulator_profiles(args.emulators_dir)
|
||||
elif args.list_systems and args.platform:
|
||||
list_platform_system_ids(args.platform, args.platforms_dir)
|
||||
elif args.list_systems:
|
||||
list_system_ids(args.emulators_dir)
|
||||
elif args.list_targets:
|
||||
if not args.platform:
|
||||
parser.error("--list-targets requires --platform")
|
||||
from model.targets import list_available_targets, target_listing_line
|
||||
|
||||
targets = list_available_targets(args.platform, args.platforms_dir)
|
||||
if not targets:
|
||||
print(f"No targets configured for platform '{args.platform}'")
|
||||
for t in targets:
|
||||
print(target_listing_line(t))
|
||||
else:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def list_emulator_profiles(emulators_dir: str, skip_aliases: bool = True) -> None:
|
||||
"""Print available emulator profiles."""
|
||||
profiles = load_emulator_profiles(emulators_dir, skip_aliases=False)
|
||||
|
||||
+23
-70
@@ -36,19 +36,18 @@ from core.cli import add_emulator_selection, add_repo_inputs
|
||||
from resolve.contentlayout import TARGET_KEY
|
||||
from model.targets import (
|
||||
canonical_target_name,
|
||||
target_listing_line,
|
||||
list_available_targets,
|
||||
target_scope,
|
||||
)
|
||||
from model.cliargs import (
|
||||
LISTING_FLAGS,
|
||||
comma_list,
|
||||
platform_arg,
|
||||
refuse_beside_mode,
|
||||
requested_regions,
|
||||
run_listing,
|
||||
work_from_repo_root,
|
||||
require_platform,
|
||||
selected_platforms,
|
||||
list_platform_system_ids,
|
||||
list_emulator_profiles,
|
||||
list_system_ids,
|
||||
print_platform_listing,
|
||||
)
|
||||
from model.systems import resolve_system_arg
|
||||
from model.profiles import (
|
||||
@@ -83,26 +82,14 @@ yaml = require_yaml()
|
||||
|
||||
def _refuse_listing_narrowings(
|
||||
args: argparse.Namespace, parser: argparse.ArgumentParser
|
||||
) -> None:
|
||||
"""A listing mode reads --platform at most, anything else is refused.
|
||||
|
||||
Printing past a narrowing flag lets the user believe it applied
|
||||
(generate_pack applies the same table).
|
||||
"""
|
||||
listing = next(
|
||||
(flag for flag, on in (
|
||||
("--list-platforms", args.list_platforms),
|
||||
("--list-emulators", args.list_emulators),
|
||||
("--list-systems", args.list_systems),
|
||||
("--list-targets", args.list_targets),
|
||||
) if on),
|
||||
None,
|
||||
)
|
||||
if not listing:
|
||||
return
|
||||
reads_platform = listing not in ("--list-emulators", "--list-platforms")
|
||||
for flag, on in (
|
||||
("--platform", args.platform and not reads_platform),
|
||||
) -> str | None:
|
||||
"""A listing reads --platform at most: anything else given beside it,
|
||||
another listing included, is refused (generate_pack applies the same
|
||||
table). The listing asked for, if any."""
|
||||
listings = [(flag, getattr(args, flag[2:].replace("-", "_"))) for flag in LISTING_FLAGS]
|
||||
given = {flag for flag, on in (
|
||||
*listings,
|
||||
("--platform", args.platform),
|
||||
("--all", args.all),
|
||||
("--emulator", args.emulator),
|
||||
("--system", args.system),
|
||||
@@ -116,9 +103,8 @@ def _refuse_listing_narrowings(
|
||||
("--offline", args.offline),
|
||||
("--no-game-data", getattr(args, "no_game_data", False)),
|
||||
("--required-only", args.required_only),
|
||||
):
|
||||
if on:
|
||||
parser.error(f"{flag} is incompatible with {listing}")
|
||||
) if on}
|
||||
return refuse_beside_mode(listings, given, LISTING_FLAGS, parser)
|
||||
|
||||
|
||||
def _refuse_mode_flags(args: argparse.Namespace, parser: argparse.ArgumentParser) -> None:
|
||||
@@ -148,26 +134,6 @@ def _refuse_mode_flags(args: argparse.Namespace, parser: argparse.ArgumentParser
|
||||
parser.error(message)
|
||||
|
||||
|
||||
def _run_listing(args: argparse.Namespace, parser: argparse.ArgumentParser) -> None:
|
||||
if args.list_platforms:
|
||||
print_platform_listing(args.platforms_dir)
|
||||
elif args.list_emulators:
|
||||
list_emulator_profiles(args.emulators_dir)
|
||||
elif args.list_systems and args.platform:
|
||||
list_platform_system_ids(args.platform, args.platforms_dir)
|
||||
elif args.list_systems:
|
||||
list_system_ids(args.emulators_dir)
|
||||
else:
|
||||
if not args.platform:
|
||||
parser.error("--list-targets requires --platform")
|
||||
targets = list_available_targets(args.platform, args.platforms_dir)
|
||||
if not targets:
|
||||
print(f"No targets configured for platform '{args.platform}'")
|
||||
return
|
||||
for t in targets:
|
||||
print(target_listing_line(t))
|
||||
|
||||
|
||||
def main() -> None:
|
||||
with contextlib.ExitStack() as held:
|
||||
_main(held)
|
||||
@@ -230,18 +196,6 @@ def _build_parser() -> argparse.ArgumentParser:
|
||||
return parser
|
||||
|
||||
|
||||
def _requested_regions(args: argparse.Namespace, parser: argparse.ArgumentParser) -> list[str]:
|
||||
requested_regions: list[str] = []
|
||||
if getattr(args, "region", None):
|
||||
from model import region
|
||||
|
||||
try:
|
||||
requested_regions = region.parse_requested(args.region)
|
||||
except ValueError as exc:
|
||||
parser.error(str(exc))
|
||||
return requested_regions
|
||||
|
||||
|
||||
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 narrows that platform, as in generate_pack.
|
||||
@@ -274,7 +228,7 @@ def _platform_system_filter(
|
||||
|
||||
|
||||
def _run_emulator_mode(args: argparse.Namespace, db: dict, requested_regions: list[str]) -> None:
|
||||
names = [n.strip() for n in args.emulator.split(",") if n.strip()]
|
||||
names = comma_list(args.emulator)
|
||||
result = verify_emulator(
|
||||
names, args.emulators_dir, db, args.standalone,
|
||||
regions=requested_regions, platforms_dir=args.platforms_dir,
|
||||
@@ -289,7 +243,7 @@ def _run_emulator_mode(args: argparse.Namespace, db: dict, requested_regions: li
|
||||
|
||||
|
||||
def _run_system_mode(args: argparse.Namespace, db: dict, requested_regions: list[str]) -> None:
|
||||
system_ids = [s.strip() for s in args.system.split(",") if s.strip()]
|
||||
system_ids = comma_list(args.system)
|
||||
result = verify_system(
|
||||
system_ids, args.emulators_dir, db, args.standalone,
|
||||
regions=requested_regions, platforms_dir=args.platforms_dir,
|
||||
@@ -441,11 +395,10 @@ def _main(held: contextlib.ExitStack) -> None:
|
||||
|
||||
packresolve.set_offline(True)
|
||||
|
||||
requested_regions = _requested_regions(args, parser)
|
||||
regions = requested_regions(args.region, parser)
|
||||
|
||||
_refuse_listing_narrowings(args, parser)
|
||||
if args.list_platforms or args.list_emulators or args.list_systems or args.list_targets:
|
||||
_run_listing(args, parser)
|
||||
if _refuse_listing_narrowings(args, parser):
|
||||
run_listing(args, parser, args.list_platforms)
|
||||
return
|
||||
|
||||
system_mode = _check_mode(args, parser)
|
||||
@@ -458,15 +411,15 @@ def _main(held: contextlib.ExitStack) -> None:
|
||||
db = load_database(args.db)
|
||||
|
||||
if args.emulator:
|
||||
_run_emulator_mode(args, db, requested_regions)
|
||||
_run_emulator_mode(args, db, regions)
|
||||
return
|
||||
|
||||
if system_mode:
|
||||
_run_system_mode(args, db, requested_regions)
|
||||
_run_system_mode(args, db, regions)
|
||||
return
|
||||
|
||||
# Platform mode (existing)
|
||||
_run_platform_mode(args, parser, db, requested_regions, platform_systems)
|
||||
_run_platform_mode(args, parser, db, regions, platform_systems)
|
||||
|
||||
|
||||
def _json_details(details: list[dict], verbose: bool) -> list[dict]:
|
||||
|
||||
Reference in new issue
Block a user