From ccd440f67a083820365109fa88cec21dd59fe557 Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Sat, 21 Nov 2026 04:38:57 +0100 Subject: [PATCH] refactor: load install.py as a module in one place The event schema, the installer doc, the launcher index, the site and the README each executed install.py with their own importlib block. installer_build.module.installer_module is cached and read by all five. --- scripts/installer_build/eventschema.py | 8 ++------ scripts/installer_build/installerdoc.py | 8 ++------ scripts/installer_build/launcherindex.py | 8 ++------ scripts/installer_build/module.py | 23 +++++++++++++++++++++++ scripts/sitegen/install.py | 16 +--------------- scripts/sitegen/readme.py | 18 ++---------------- tests/test_site_entry.py | 2 +- 7 files changed, 33 insertions(+), 50 deletions(-) create mode 100644 scripts/installer_build/module.py diff --git a/scripts/installer_build/eventschema.py b/scripts/installer_build/eventschema.py index b4a02a2f6..893a335e8 100644 --- a/scripts/installer_build/eventschema.py +++ b/scripts/installer_build/eventschema.py @@ -2,19 +2,15 @@ from __future__ import annotations -import importlib.util import json from core.paths import REPO_ROOT +from installer_build.module import installer_module OUTPUT = REPO_ROOT / "schemas" / "install-events.schema.json" -_spec = importlib.util.spec_from_file_location("install", REPO_ROOT / "install.py") -if _spec is None or _spec.loader is None: - raise ImportError(f"cannot load {REPO_ROOT / 'install.py'}") -_install = importlib.util.module_from_spec(_spec) -_spec.loader.exec_module(_install) +_install = installer_module() EVENT_KEYS: dict[str, tuple[str, ...]] = _install.EVENT_KEYS EVENT_SCHEMA: int = _install.EVENT_SCHEMA diff --git a/scripts/installer_build/installerdoc.py b/scripts/installer_build/installerdoc.py index 22417cf68..c41de3f4f 100644 --- a/scripts/installer_build/installerdoc.py +++ b/scripts/installer_build/installerdoc.py @@ -6,7 +6,6 @@ import argparse import ast import contextlib import http.server -import importlib.util import json import os import re @@ -23,6 +22,7 @@ from typing import Any, Callable, TYPE_CHECKING from core.paths import REPO_ROOT from installer_build import handhelds +from installer_build.module import installer_module if TYPE_CHECKING: from install import LocalCopy, Progress, _OsRule @@ -31,11 +31,7 @@ if TYPE_CHECKING: PAGE = REPO_ROOT / "wiki" / "installer.md" INSTALLER = REPO_ROOT / "install.py" -_spec = importlib.util.spec_from_file_location("install", INSTALLER) -if _spec is None or _spec.loader is None: - raise ImportError(f"cannot load {INSTALLER}") -_install = importlib.util.module_from_spec(_spec) -_spec.loader.exec_module(_install) +_install = installer_module() BLOCK = re.compile( r"(?P\n)(?P.*?)" diff --git a/scripts/installer_build/launcherindex.py b/scripts/installer_build/launcherindex.py index 5ceffae3b..b8855025c 100644 --- a/scripts/installer_build/launcherindex.py +++ b/scripts/installer_build/launcherindex.py @@ -3,11 +3,11 @@ from __future__ import annotations import hashlib -import importlib.util import json from pathlib import Path from core.paths import REPO_ROOT +from installer_build.module import installer_module from installer_build.pins import installer_sha256, pinned_sha256 from installer_build.port import ( BATOCERA_ZIP, @@ -20,11 +20,7 @@ from installer_build.port import ( OUTPUT = REPO_ROOT / "install" / "launchers" / "index.json" -_spec = importlib.util.spec_from_file_location("install", REPO_ROOT / "install.py") -if _spec is None or _spec.loader is None: - raise ImportError(f"cannot load {REPO_ROOT / 'install.py'}") -_install = importlib.util.module_from_spec(_spec) -_spec.loader.exec_module(_install) +_install = installer_module() # The archive whose members each system gets, by its key in APP_PLACES. LAUNCHER_ARCHIVES = {"muos": MUOS_APP, "knulli": BATOCERA_ZIP, "rocknix": TOOLS_ZIP} diff --git a/scripts/installer_build/module.py b/scripts/installer_build/module.py new file mode 100644 index 000000000..648f623de --- /dev/null +++ b/scripts/installer_build/module.py @@ -0,0 +1,23 @@ +"""install.py read as a module, once per run.""" + +from __future__ import annotations + +import functools +import importlib.util +import types + +from core.paths import REPO_ROOT + + +@functools.cache +def installer_module() -> types.ModuleType: + """The generated install.py, the artifact served, for its constants and + words: the schema, the doc tables, the launcher index and the site read + the same module.""" + path = REPO_ROOT / "install.py" + spec = importlib.util.spec_from_file_location("install", path) + if spec is None or spec.loader is None: + raise ImportError(f"cannot load {path}") + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module diff --git a/scripts/sitegen/install.py b/scripts/sitegen/install.py index c61916fca..0d7248895 100644 --- a/scripts/sitegen/install.py +++ b/scripts/sitegen/install.py @@ -7,7 +7,6 @@ import html import json import re import shlex -import types from collections import Counter from model import siteinputs @@ -19,6 +18,7 @@ from resolve.layout import runs_standalone, standalone_layout_path from resolve.standalone import standalone_set from sitegen import markup from core.paths import REPO_ROOT +from installer_build.module import installer_module from sitegen.fields import search_excluded from sitegen.guides import fence, platform_args, terminal_tabs from sitegen.render import code_span, escape_tags @@ -88,20 +88,6 @@ def install_commands(args: str = "", indent: str = "", return lines -@functools.cache -def installer_module() -> types.ModuleType: - """install.py, read as a module: the names `--emulator` accepts are its - own (`emulator_names`).""" - import importlib.util - - spec = importlib.util.spec_from_file_location("install", REPO_ROOT / "install.py") - if spec is None or spec.loader is None: - raise ImportError(f"cannot load {REPO_ROOT / 'install.py'}") - module = importlib.util.module_from_spec(spec) - spec.loader.exec_module(module) - return module - - @functools.cache def _published_emulator_names() -> dict[str, dict[str, str]]: """Per published manifest, every name `--emulator` accepts there and the diff --git a/scripts/sitegen/readme.py b/scripts/sitegen/readme.py index 015dc3fbd..f6f8c1a84 100644 --- a/scripts/sitegen/readme.py +++ b/scripts/sitegen/readme.py @@ -9,7 +9,6 @@ import sys from collections.abc import Callable from datetime import datetime, timezone from pathlib import Path -from types import ModuleType from romrecipes import ledger from devtools import prchecks @@ -19,6 +18,7 @@ from core.cli import InputMissing from core.paths import DB_FILE, PLATFORMS_DIR, REPO_ROOT from core.sizes import size_text from core.upstream import forge_headers +from installer_build.module import installer_module from model.composition import compute_composition, count_catalog_matched from model.declarations import declared_root from model.guides import PACKAGE_COMMAND, install_command @@ -32,20 +32,6 @@ from resolve.cores import resolve_platform_cores from resolve.review import LARGE_FILE_ISSUE_URL -def _installer_words() -> ModuleType: - """install.py read as a module, for the words its screen shows: the - README named a line menu the one-liner never opens.""" - import importlib.util - - path = REPO_ROOT / "install.py" - spec = importlib.util.spec_from_file_location("install", path) - if spec is None or spec.loader is None: - raise ImportError(f"cannot load {path}") - module = importlib.util.module_from_spec(spec) - spec.loader.exec_module(module) - return module - - # The repository's profiles wherever the script is run from: a bare # "emulators" read the working directory, and a run elsewhere counted 0 cores. _EMULATORS_DIR = str(REPO_ROOT / "emulators") @@ -663,7 +649,7 @@ def generate_readme(db: dict, platforms_dir: str, db_path: str = DB_FILE) -> str "", "The installer finds the setup and its BIOS folder, installs the files it needs (only" " what the games need when it finds them on the card) and checks each one. Its screen" - " installs everything, or fewer under **" + _installer_words().TUI_SYSTEMS_ROW + "**." + " installs everything, or fewer under **" + installer_module().TUI_SYSTEMS_ROW + "**." " Arrow keys and Enter drive it, or a controller on a handheld. It sends no report of" f" its own ([what is counted]({SITE_URL}wiki/privacy/)). Options go after the line in the" f" form the [installer page]({SITE_URL}wiki/installer/#passing-options) gives.", diff --git a/tests/test_site_entry.py b/tests/test_site_entry.py index 9dbbe966f..8d81ecbad 100644 --- a/tests/test_site_entry.py +++ b/tests/test_site_entry.py @@ -1112,7 +1112,7 @@ class TheSiteSaysWhatTheInstallerShows(unittest.TestCase): self.assertIn(f"fewer under **{installer.TUI_SYSTEMS_ROW}**", readme) self.assertNotIn("offers to install everything or to choose systems", readme) source = script_source("generate_readme.py") - self.assertIn("_installer_words().TUI_SYSTEMS_ROW", source) + self.assertIn("installer_module().TUI_SYSTEMS_ROW", source) from installer_build import installerdoc page = (REPO_ROOT / "wiki" / "installer.md").read_text(encoding="utf-8")