From 2a49bf237b246b6ed642fbf6151e5498abd16798 Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Sat, 21 Nov 2026 14:30:08 +0100 Subject: [PATCH] refactor: move the export run into exporter --- scripts/export_native.py | 90 ++++++-------------------------------- scripts/exporter/runner.py | 68 ++++++++++++++++++++++++++++ tests/sources.py | 4 ++ tests/test_exporters.py | 6 +-- 4 files changed, 88 insertions(+), 80 deletions(-) create mode 100644 scripts/exporter/runner.py diff --git a/scripts/export_native.py b/scripts/export_native.py index 122fac395..4d443df5f 100644 --- a/scripts/export_native.py +++ b/scripts/export_native.py @@ -15,77 +15,20 @@ Usage: from __future__ import annotations -from core.paths import TRUTH_DIR import argparse +import contextlib import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent)) from core.cli import add_repo_inputs -from model.cliargs import platform_arg, selected_platforms -from model.registry import load_data_dir_registry, load_platform_registry -from exporter import discover_exporters -from exporter.native import export_platform, refresh_cache +from core.paths import TRUTH_DIR from exporter.originals import DEFAULT_CACHE - - -def run( - platforms: list[str], - truth_dir: str, - output_dir: str, - platforms_dir: str, - upstream_dir: str, - allow_fetch: bool, - refresh_only: bool = False, -) -> int: - exporters = discover_exporters() - registry = load_platform_registry(platforms_dir) - failures = 0 - skipped: list[str] = [] - - for platform in sorted(platforms): - exporter_class = exporters.get(platform) - if not exporter_class and (registry.get(platform) or {}).get("native_list") == "none": - # The platform's code names no BIOS file: there is no list of its - # own to hand back corrected. - print(f" SKIP {platform}: names no BIOS file of its own (native_list: none)") - continue - if not exporter_class: - skipped.append(platform) - print(f" SKIP {platform}: no exporter") - continue - - if refresh_only: - ok, messages = refresh_cache( - platform, - exporter_class, - Path(truth_dir), - platforms_dir, - Path(upstream_dir), - ) - else: - ok, messages = export_platform( - platform, - exporter_class, - Path(truth_dir), - Path(output_dir), - platforms_dir, - Path(upstream_dir), - allow_fetch, - ) - label = "OK " if ok else "FAIL" - print(f" {label} {platform}") - for message in messages: - print(f" {message}") - if not ok: - failures += 1 - - if skipped: - print(f"\n{len(skipped)} platform(s) without an exporter: {', '.join(skipped)}") - failures += len(skipped) - - return 1 if failures else 0 +from exporter.runner import run +from model.cliargs import platform_arg, selected_platforms +from model.registry import load_data_dir_registry +from packs.datadirs import hold_data_dirs def main() -> None: @@ -126,19 +69,12 @@ def main() -> None: platforms = selected_platforms(args) - if not args.refresh_cache: - import contextlib - - from packs.datadirs import hold_data_dirs - - # Removal candidates are proved against data/: a refresh swapping a - # tree mid-walk listed entries only that tree holds as removable. - # The process exit releases the hold. - held = contextlib.ExitStack() - hold_data_dirs(held, load_data_dir_registry(args.platforms_dir)) - - sys.exit( - run( + with contextlib.ExitStack() as held: + if not args.refresh_cache: + # Removal candidates are proved against data/: a refresh swapping + # a tree mid-walk listed entries only that tree holds as removable. + hold_data_dirs(held, load_data_dir_registry(args.platforms_dir)) + code = run( platforms, args.truth_dir, args.output_dir, @@ -147,7 +83,7 @@ def main() -> None: args.fetch, args.refresh_cache, ) - ) + sys.exit(code) if __name__ == "__main__": diff --git a/scripts/exporter/runner.py b/scripts/exporter/runner.py new file mode 100644 index 000000000..82511e031 --- /dev/null +++ b/scripts/exporter/runner.py @@ -0,0 +1,68 @@ +"""Exporting, or refreshing the cached originals of, each selected platform in turn.""" + +from __future__ import annotations + +from pathlib import Path + +from exporter import discover_exporters +from exporter.native import export_platform, refresh_cache +from model.registry import load_platform_registry + + +def run( + platforms: list[str], + truth_dir: str, + output_dir: str, + platforms_dir: str, + upstream_dir: str, + allow_fetch: bool, + refresh_only: bool = False, +) -> int: + """Export each platform, or refresh its cached original: 1 when any failed or has no exporter.""" + exporters = discover_exporters() + registry = load_platform_registry(platforms_dir) + failures = 0 + skipped: list[str] = [] + + for platform in sorted(platforms): + exporter_class = exporters.get(platform) + if not exporter_class and (registry.get(platform) or {}).get("native_list") == "none": + # The platform's code names no BIOS file: there is no list of its + # own to hand back corrected. + print(f" SKIP {platform}: names no BIOS file of its own (native_list: none)") + continue + if not exporter_class: + skipped.append(platform) + print(f" SKIP {platform}: no exporter") + continue + + if refresh_only: + ok, messages = refresh_cache( + platform, + exporter_class, + Path(truth_dir), + platforms_dir, + Path(upstream_dir), + ) + else: + ok, messages = export_platform( + platform, + exporter_class, + Path(truth_dir), + Path(output_dir), + platforms_dir, + Path(upstream_dir), + allow_fetch, + ) + label = "OK " if ok else "FAIL" + print(f" {label} {platform}") + for message in messages: + print(f" {message}") + if not ok: + failures += 1 + + if skipped: + print(f"\n{len(skipped)} platform(s) without an exporter: {', '.join(skipped)}") + failures += len(skipped) + + return 1 if failures else 0 diff --git a/tests/sources.py b/tests/sources.py index 1b285585c..ea6eda1d7 100644 --- a/tests/sources.py +++ b/tests/sources.py @@ -38,6 +38,10 @@ COMMANDS = { "mame_ref_audit.py": ("profiling/mamerefs.py",), "fileless_audit.py": ("profiling/fileless.py",), "bundled_only.py": ("packs/bundledonly.py",), + "export_native.py": ("exporter/runner.py", "exporter/native.py", "exporter/originals.py", "exporter/landed.py"), + "mame_ref_audit.py": ("profiling/mamerefs.py",), + "fileless_audit.py": ("profiling/fileless.py",), + "bundled_only.py": ("packs/bundledonly.py",), "mame_ref_audit.py": ("profiling/mamerefs.py",), "fileless_audit.py": ("profiling/fileless.py",), "mame_ref_audit.py": ("profiling/mamerefs.py",), diff --git a/tests/test_exporters.py b/tests/test_exporters.py index 0022d050f..d69a37757 100644 --- a/tests/test_exporters.py +++ b/tests/test_exporters.py @@ -616,7 +616,7 @@ class ExporterRegistry(unittest.TestCase): to export. A platform with no exporter and no such mark still fails.""" import contextlib - import export_native + from exporter import runner with tempfile.TemporaryDirectory() as directory: platforms = Path(directory) @@ -624,9 +624,9 @@ class ExporterRegistry(unittest.TestCase): "platforms:\n cardonly:\n native_list: none\n unwritten: {}\n", encoding="utf-8") with contextlib.redirect_stdout(io.StringIO()) as out: - self.assertEqual(export_native.run( + self.assertEqual(runner.run( ["cardonly"], directory, directory, str(platforms), directory, False), 0) - self.assertEqual(export_native.run( + self.assertEqual(runner.run( ["unwritten"], directory, directory, str(platforms), directory, False), 1) self.assertIn("SKIP cardonly: names no BIOS file of its own", out.getvalue())