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.
This commit is contained in:
Abdessamad Derraz committed 2026-11-21 06:02:35 +01:00
1 parent fb17242a78
commit ccd440f67a
7 files changed
+33 -50

No files matched your search

+2 -6
View File
@@ -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
+2 -6
View File
@@ -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<open><!-- generated: (?P<name>[a-z-]+) -->\n)(?P<body>.*?)"
+2 -6
View File
@@ -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}
+23
View File
@@ -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
+1 -15
View File
@@ -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
+2 -16
View File
@@ -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.",
+1 -1
View File
@@ -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")