diff --git a/Build.sh b/Build.sh index 567f3661e..69461918c 100755 --- a/Build.sh +++ b/Build.sh @@ -4,13 +4,8 @@ # ./Build.sh --out /mnt/games/Leonida --work /mnt/scratch/leonida-work # Nothing is installed or downloaded here; make_game.py lists anything missing (README.md, "How to build"). here=$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd) || exit 1 -# python3.N first: a distribution's python3 can be older than 3.12 while a newer one is installed beside it. -for py in python3.14 python3.13 python3.12 python3 python; do - command -v "$py" >/dev/null 2>&1 || continue - if "$py" -c 'import sys; sys.exit(0 if sys.version_info >= (3, 12) else 1)' >/dev/null 2>&1; then - exec "$py" -I "$here/make_game.py" "$@" - fi -done +. "$here/scripts/launcher_python.sh" +exec_leonida_python "$here/make_game.py" "$@" echo "Python 3.12 or newer is needed. Install it with your package manager (Debian and Ubuntu:" >&2 echo "sudo apt install python3; macOS: brew install python), then run ./Build.sh again." >&2 exit 1 diff --git a/Mods.sh b/Mods.sh index 7bb6985b4..b80cc7296 100755 --- a/Mods.sh +++ b/Mods.sh @@ -2,12 +2,8 @@ # Linux and macOS counterpart of Mods.bat: opens the mod manager window (tools/leonida_mods.py gui). It manages # /mods and never touches the game package. The window needs Tkinter (Debian and Ubuntu: python3-tk). here=$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd) || exit 1 -for py in python3.14 python3.13 python3.12 python3 python; do - command -v "$py" >/dev/null 2>&1 || continue - if "$py" -c 'import sys; sys.exit(0 if sys.version_info >= (3, 12) else 1)' >/dev/null 2>&1; then - exec "$py" -I "$here/tools/leonida_mods.py" gui "$@" - fi -done +. "$here/scripts/launcher_python.sh" +exec_leonida_python "$here/tools/leonida_mods.py" gui "$@" echo "Python 3.12 or newer is needed. Install it with your package manager (Debian and Ubuntu:" >&2 echo "sudo apt install python3 python3-tk; macOS: brew install python), then run ./Mods.sh again." >&2 exit 1 diff --git a/scripts/launcher_python.sh b/scripts/launcher_python.sh new file mode 100644 index 000000000..a469f34ec --- /dev/null +++ b/scripts/launcher_python.sh @@ -0,0 +1,40 @@ +# Sourced by the POSIX launchers. Search for versioned Python names on PATH so a new Python minor +# release works even if the system's unversioned python3 still points to an older interpreter. +exec_leonida_python() { + remaining=${PATH-} + best_minor=11 + best_python= + while :; do + directory=${remaining%%:*} + for candidate in "${directory:-.}"/python3.*; do + [ -f "$candidate" ] && [ -x "$candidate" ] || continue + name=${candidate##*/} + minor=${name#python3.} + case $minor in + 0*|''|*[!0-9]*) continue ;; + esac + # Invoke by name to keep PATH precedence if that version appears in more than one directory. + if [ "$minor" -gt "$best_minor" ] 2>/dev/null && leonida_python_is_supported "$name"; then + best_minor=$minor + best_python=$name + fi + done + case $remaining in + *:*) remaining=${remaining#*:} ;; + *) break ;; + esac + done + + if [ -n "$best_python" ]; then + exec "$best_python" -I "$@" + fi + for name in python3 python; do + if leonida_python_is_supported "$name"; then + exec "$name" -I "$@" + fi + done +} + +leonida_python_is_supported() { + "$1" -I -c 'import sys; sys.exit(0 if sys.version_info >= (3, 12) else 1)' >/dev/null 2>&1 +} diff --git a/tests/test_shell_launchers.py b/tests/test_shell_launchers.py index d8cad6ae5..e8dbe942a 100644 --- a/tests/test_shell_launchers.py +++ b/tests/test_shell_launchers.py @@ -3,8 +3,8 @@ python -I tests/test_shell_launchers.py Each launcher is run with a PATH that holds only stub interpreters: shell scripts that answer the version check with -the version they are given and otherwise record their arguments. The launcher must pick the first one that is 3.12 -or newer, run make_game.py (or the mod manager) through it with -I and every argument unchanged, from any folder, +the version they are given and otherwise record their arguments. The launcher must pick the newest usable versioned +name or fall back to python3 or python, run the build or mod manager with -I and every argument unchanged, pass the exit code back, and stop with a message when there is none. POSIX only (/bin/sh); no Python is started for real, no compiler and no game files. """ @@ -19,7 +19,7 @@ ROOT = Path(__file__).resolve().parents[1] LAUNCHERS = {"Build.sh": ["make_game.py"], "Mods.sh": ["tools/leonida_mods.py", "gui"]} STUB = """#!/bin/sh -if [ "$1" = -c ]; then +if [ "$1" = -I ] && [ "$2" = -c ]; then exit {ok} fi for a in "$@"; do printf '%s\\n' "$a"; done > "$LEONIDA_STUB_LOG.{name}" @@ -33,7 +33,7 @@ class ShellLauncherTest(unittest.TestCase): tmp = tempfile.TemporaryDirectory() self.addCleanup(tmp.cleanup) self.tmp = Path(tmp.name) - self.bin = self.tmp / "bin" + self.bin = self.tmp / "python bin" self.bin.mkdir() self.log = self.tmp / "log" # PATH holds the stubs and the one external command the launchers run, dirname (cd, pwd, command -v and @@ -46,8 +46,8 @@ class ShellLauncherTest(unittest.TestCase): (tools / "dirname").symlink_to(dirname) self.path = os.pathsep.join([str(self.bin), str(tools)]) - def stub(self, name, ok=True, status=0): - path = self.bin / name + def stub(self, name, ok=True, status=0, directory=None): + path = (directory or self.bin) / name path.write_text(STUB.format(ok=0 if ok else 1, name=name, status=status), encoding="utf-8", newline="\n") path.chmod(0o755) @@ -64,7 +64,7 @@ class ShellLauncherTest(unittest.TestCase): with self.subTest(launcher=launcher): for name in ("python3.14", "python3.13", "python3.12", "python3", "python"): (self.bin / name).unlink(missing_ok=True) - self.stub("python3.13", ok=False) # too old: skipped + self.stub("python3.13", ok=False) # unusable: skipped self.stub("python3.12") self.stub("python3") # also new enough, but later in the order args = ["--out", "/mnt/a b/Leonida", "", "it's", "$HOME", "*"] @@ -74,6 +74,22 @@ class ShellLauncherTest(unittest.TestCase): self.assertEqual(self.recorded("python3.12"), want) self.assertFalse((self.tmp / "log.python3").exists()) + def test_discovers_newer_versioned_names_and_preserves_path_precedence(self): + other = self.tmp / "second bin" + other.mkdir() + self.path += os.pathsep + str(other) + self.stub("python3", ok=False) + self.stub("python3.15") + self.stub("python3.20") + self.stub("python3.20", status=9, directory=other) + self.stub("python3.99-config", status=8) + for launcher, tool in LAUNCHERS.items(): + with self.subTest(launcher=launcher): + p = self.run_launcher(launcher, "--help") + self.assertEqual(p.returncode, 0, p.stderr) + self.assertEqual(self.recorded("python3.20"), ["-I", str(ROOT / tool[0]), *tool[1:], "--help"]) + self.assertFalse((self.tmp / "log.python3.15").exists()) + def test_python3_is_used_when_there_is_no_versioned_name(self): self.stub("python3") p = self.run_launcher("Build.sh", "--check")