mirror of
https://github.com/Abdess/retroarch_system.git
synced 2026-11-26 11:28:51 -06:00
refactor: share CLI options and default paths
core.paths holds the default database, platforms, emulators and bios paths, core.cli declares the options built on them (add_repo_inputs) and the --emulator/--system modes verify and generate_pack share. model.targets.target_scope answers --target for a command line, model.profiles.no_profile_for_systems the system with no emulator. tests/sources.declared_options reads the options a script declares through either form.
This commit is contained in:
1 parent
348d5b51c3
commit
cf03dcb2ef
50 files changed
+260
-405
No files matched your search
@@ -7,6 +7,8 @@ command keeps its options, the packages keep the rest.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import ast
|
||||
from functools import cache
|
||||
from pathlib import Path
|
||||
|
||||
SCRIPTS = Path(__file__).resolve().parents[1] / "scripts"
|
||||
@@ -107,3 +109,40 @@ def script_paths(name: str) -> tuple[Path, ...]:
|
||||
def script_source(name: str) -> str:
|
||||
"""The text of a script and of the package modules that hold its code."""
|
||||
return "\n".join(path.read_text(encoding="utf-8") for path in script_paths(name))
|
||||
|
||||
|
||||
def _helper_options(function: str) -> frozenset[str]:
|
||||
"""The long options a core.cli helper declares through add_argument."""
|
||||
tree = ast.parse((SCRIPTS / "core" / "cli.py").read_text(encoding="utf-8"))
|
||||
body = next(node for node in tree.body if isinstance(node, ast.FunctionDef) and node.name == function)
|
||||
return frozenset(_add_argument_flags(body))
|
||||
|
||||
|
||||
def _add_argument_flags(tree: ast.AST) -> set[str]:
|
||||
return {
|
||||
arg.value
|
||||
for node in ast.walk(tree)
|
||||
if isinstance(node, ast.Call) and isinstance(node.func, ast.Attribute)
|
||||
and node.func.attr == "add_argument"
|
||||
for arg in node.args
|
||||
if isinstance(arg, ast.Constant) and isinstance(arg.value, str) and arg.value.startswith("--")
|
||||
}
|
||||
|
||||
|
||||
@cache
|
||||
def declared_options(source: str) -> frozenset[str]:
|
||||
"""The long options a script's source declares: its add_argument calls and
|
||||
the shared ones core.cli's add_repo_inputs and add_emulator_selection add."""
|
||||
from core.cli import REPO_INPUTS
|
||||
|
||||
tree = ast.parse(source)
|
||||
found = _add_argument_flags(tree)
|
||||
for node in ast.walk(tree):
|
||||
if not isinstance(node, ast.Call) or not isinstance(node.func, ast.Name):
|
||||
continue
|
||||
if node.func.id == "add_repo_inputs":
|
||||
found |= {REPO_INPUTS[arg.value][0] for arg in node.args[1:]
|
||||
if isinstance(arg, ast.Constant) and arg.value in REPO_INPUTS}
|
||||
elif node.func.id == "add_emulator_selection":
|
||||
found |= _helper_options("add_emulator_selection")
|
||||
return frozenset(found)
|
||||
@@ -10,9 +10,8 @@ tool prints, and a size is the one the release record holds.
|
||||
from __future__ import annotations
|
||||
|
||||
import tests
|
||||
from tests.sources import script_source
|
||||
from tests.sources import declared_options, script_source
|
||||
|
||||
import ast
|
||||
import contextlib
|
||||
import re
|
||||
import unittest
|
||||
@@ -529,14 +528,7 @@ def parser_options(script: str) -> frozenset[str]:
|
||||
"""The long options *script* declares through add_argument, with the
|
||||
shared ones of scraper_cli for a scraper that hands its parser over."""
|
||||
source = (REPO_ROOT / script).read_text(encoding="utf-8")
|
||||
tree = ast.parse(source)
|
||||
found = set()
|
||||
for node in ast.walk(tree):
|
||||
if (isinstance(node, ast.Call) and isinstance(node.func, ast.Attribute)
|
||||
and node.func.attr == "add_argument"):
|
||||
found.update(arg.value for arg in node.args
|
||||
if isinstance(arg, ast.Constant) and isinstance(arg.value, str)
|
||||
and arg.value.startswith("--"))
|
||||
found = set(declared_options(source))
|
||||
if "scraper_cli(" in source and script != "scripts/scraper/base_scraper.py":
|
||||
found |= parser_options("scripts/scraper/base_scraper.py")
|
||||
return frozenset(found)
|
||||
|
||||
@@ -29,6 +29,7 @@ from packs import archive
|
||||
from packs import emulatorpack
|
||||
from packs import md5pack
|
||||
from tests.scratchtree import scratch_checkout, snapshot
|
||||
from tests.sources import declared_options
|
||||
|
||||
REPO_ROOT = Path(__file__).resolve().parent.parent
|
||||
|
||||
@@ -215,7 +216,7 @@ class AMissingDatabaseIsNamed(unittest.TestCase):
|
||||
def _taking_db(self) -> list[str]:
|
||||
return sorted(
|
||||
path.name for path in (REPO_ROOT / "scripts").glob("*.py")
|
||||
if re.search(r'add_argument\(\s*"--db"', path.read_text(encoding="utf-8"))
|
||||
if "--db" in declared_options(path.read_text(encoding="utf-8"))
|
||||
)
|
||||
|
||||
def test_every_script_names_the_missing_file(self):
|
||||
@@ -253,7 +254,7 @@ class AMissingBiosDirIsRefused(unittest.TestCase):
|
||||
out = str(Path(scratch.name) / "out")
|
||||
taking = sorted(
|
||||
path.name for path in (REPO_ROOT / "scripts").glob("*.py")
|
||||
if re.search(r'add_argument\(\s*"--bios-dir"', path.read_text(encoding="utf-8"))
|
||||
if "--bios-dir" in declared_options(path.read_text(encoding="utf-8"))
|
||||
and path.name != "generate_db.py"
|
||||
)
|
||||
self.assertGreater(len(taking), 4)
|
||||
@@ -271,11 +272,10 @@ class AMissingBiosDirIsRefused(unittest.TestCase):
|
||||
|
||||
|
||||
def _scripts_taking(flag: str) -> list[Path]:
|
||||
pattern = re.compile(rf'add_argument\(\s*"{re.escape(flag)}"')
|
||||
return [
|
||||
path for path in sorted((REPO_ROOT / "scripts").glob("*.py"))
|
||||
+ sorted((REPO_ROOT / "scripts" / "scraper").glob("*.py"))
|
||||
if pattern.search(path.read_text(encoding="utf-8"))
|
||||
if flag in declared_options(path.read_text(encoding="utf-8"))
|
||||
]
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user