Skip to content

Play your own Switch games on iPhone/iPad — one-click macOS app, no terminal. | 把 Switch 游戏装进 iPhone:图形界面一键构建,不用命令行。Bring your own game.

Python detection: a venv below 3.12 is no longer ready, and mise/asdf Pythons are found

v0lumehi45badd85f16e
diff --git a/frontend-qt/nspb_qt/model.py b/frontend-qt/nspb_qt/model.py
index e67baef1..5450f317 100644
--- a/frontend-qt/nspb_qt/model.py
+++ b/frontend-qt/nspb_qt/model.py
@@ -842,6 +842,25 @@ class Model(QObject):
def _make_venv(self, base: str) -> bool:
"""Create or reuse the app's venv and install the build packages into it."""
vpy = pu.venv_python()
+ if Path(vpy).is_file():
+ ver = pu._python_version(vpy)
+ if not ver or pu._ver_tuple(ver) < pu.MIN_PYTHON:
+ # A venv an older app made from Apple's 3.9 (issue #75): pip into it would
+ # "succeed" and the build would still fail. Moved aside, never deleted, because
+ # the folder is the user's. Only when a base can build a new one.
+ new_base = pu.venv_base(base)
+ if new_base is None or new_base == vpy:
+ self.log(f'{pu.venv_dir()} is older than Python {pu.MIN_PYTHON[0]}.'
+ f'{pu.MIN_PYTHON[1]} and no newer Python was found to rebuild it from\n')
+ return False
+ vdir = pu.venv_dir()
+ aside = vdir.with_name(f'{vdir.name}.old-{time.strftime("%Y%m%d-%H%M%S")}')
+ try:
+ vdir.rename(aside)
+ except OSError as e:
+ self.log(f'could not move {vdir} aside: {e}\n')
+ return False
+ self.log(f'moved the old venv to {aside}\n')
if not Path(vpy).is_file():
# The panel's last resort is /usr/bin/python3 (3.9) or the venv path itself, and
# neither can run `-m venv`: the row used to stay red however often Install ran.
diff --git a/frontend-qt/nspb_qt/platform_util.py b/frontend-qt/nspb_qt/platform_util.py
index 35cde9bf..bd121477 100644
--- a/frontend-qt/nspb_qt/platform_util.py
+++ b/frontend-qt/nspb_qt/platform_util.py
@@ -68,12 +68,44 @@ def uv_pythons() -> list[str]:
return out
+def version_manager_pythons() -> list[str]:
+ """The interpreters mise and asdf manage (issue #113).
+
+ mise keeps its installs in `~/.local/share/mise` (`MISE_DATA_DIR`) with shims in `shims/`,
+ asdf in `~/.asdf` (`ASDF_DATA_DIR`). A GUI process's PATH has neither. asdf's shims are
+ scripts that need `asdf` on the PATH, so only its installs are probed. Not on Windows.
+ """
+ if IS_WIN:
+ return []
+ # The interpreter itself, not `python3-config` and friends that share the folder.
+ name_re = re.compile(r'^python3(\.\d+)?$')
+
+ def newest_first(paths) -> list:
+ return sorted(paths, key=lambda n: [int(x) if x.isdigit() else 0
+ for x in re.split(r'(\d+)', Path(n).name)], reverse=True)
+
+ def named(d: Path) -> list[str]:
+ if not d.is_dir():
+ return []
+ return newest_first(str(p) for p in d.iterdir() if name_re.match(p.name))
+
+ home = Path.home()
+ mise = Path(os.environ.get('MISE_DATA_DIR') or home / '.local' / 'share' / 'mise')
+ asdf = Path(os.environ.get('ASDF_DATA_DIR') or home / '.asdf')
+ out: list[str] = []
+ for root in (mise / 'installs' / 'python', asdf / 'installs' / 'python'):
+ if root.is_dir():
+ for install in newest_first(root.iterdir()):
+ out += named(install / 'bin')
+ return out + named(mise / 'shims')
+
+
def python_candidates(extra_candidates: list[str] | None = None) -> list[str]:
"""Every interpreter a build could run under, best first.
The app's own venv, then whatever the caller passes (the interpreter the app installed for
itself, which a machine with no system Python is exactly the one that has to be found again),
- then the platform's usual installs, uv's store, and last what `which` can see — which, in a
+ then the platform's usual installs, uv's store, mise and asdf, and last what `which` can see — which, in a
GUI launch, is next to nothing.
"""
cands: list[str] = [venv_python()] + [c for c in (extra_candidates or []) if c]
@@ -90,6 +122,7 @@ def python_candidates(extra_candidates: list[str] | None = None) -> list[str]:
cands.append(f'{fw}/{v}/bin/python3')
cands += ['/opt/homebrew/bin/python3', '/usr/local/bin/python3', '/usr/bin/python3']
cands += uv_pythons()
+ cands += version_manager_pythons()
if w := _which('python3' if not IS_WIN else 'python'):
cands.append(w)
return cands
@@ -112,12 +145,15 @@ def find_python(extra_candidates: list[str] | None = None) -> tuple[str, bool, s
ready: tuple[str, str] | None = None # newest that already has the packages
fallback: tuple[str, str] | None = None # newest that runs at all: 安装 finishes it
+ old_venv: tuple[str, str] | None = None # the app's venv when it is below the floor
seen: set[str] = set()
for c in python_candidates(extra_candidates):
if not c or c in seen or not Path(c).is_file():
continue
seen.add(c)
ver = _python_version(c)
+ if ver and _ver_tuple(ver) < MIN_PYTHON and c == venv_python():
+ old_venv = (c, ver)
if not ver or _ver_tuple(ver) < MIN_PYTHON:
continue
if _runs(c, ['-c', probe]):
@@ -129,6 +165,8 @@ def find_python(extra_candidates: list[str] | None = None) -> tuple[str, bool, s
return (ready[0], True, ready[1])
if fallback:
return (fallback[0], False, fallback[1])
+ if old_venv:
+ return (old_venv[0], False, old_venv[1]) # the row says which version is too old
return ('python' if IS_WIN else '/usr/bin/python3', False, '')
diff --git a/frontend-qt/tests/test_python_venv.py b/frontend-qt/tests/test_python_venv.py
new file mode 100644
index 00000000..b7b71abe
--- /dev/null
+++ b/frontend-qt/tests/test_python_venv.py
@@ -0,0 +1,114 @@
+"""Issues #75 and #113: a venv below the script's floor, and Pythons that mise or asdf manage.
+
+ python3 -m unittest discover -s frontend-qt/tests
+"""
+from __future__ import annotations
+
+import os
+import sys
+import tempfile
+import types
+import unittest
+from pathlib import Path
+from unittest import mock
+
+ROOT = Path(__file__).resolve().parents[2]
+sys.path.insert(0, str(ROOT / 'frontend-qt'))
+
+from nspb_qt import platform_util as pu # noqa: E402
+
+
+def _exe(path: Path) -> str:
+ path.parent.mkdir(parents=True, exist_ok=True)
+ path.write_text('#!/bin/sh\n')
+ path.chmod(0o755)
+ return str(path)
+
+
+class StaleVenvTest(unittest.TestCase):
+ def test_a_3_9_venv_is_not_returned_as_ready(self) -> None:
+ with tempfile.TemporaryDirectory() as td:
+ vpy = _exe(Path(td) / 'venv' / 'bin' / 'python3')
+ with mock.patch.object(pu, 'IS_WIN', False), \
+ mock.patch.object(pu, 'venv_python', return_value=vpy), \
+ mock.patch.object(pu, '_which', return_value=None), \
+ mock.patch.object(pu, 'version_manager_pythons', return_value=[]), \
+ mock.patch.object(pu, '_python_version', return_value='3.9'), \
+ mock.patch.object(pu, '_runs', return_value=True):
+ py, ok, ver = pu.find_python()
+ self.assertFalse(ok, 'a 3.9 venv with every package is still below the floor')
+ self.assertEqual((py, ver), (vpy, '3.9'), 'the row can say which version is too old')
+
+ def test_a_newer_interpreter_wins_over_a_stale_venv(self) -> None:
+ with tempfile.TemporaryDirectory() as td:
+ vpy = _exe(Path(td) / 'venv' / 'bin' / 'python3')
+ new = _exe(Path(td) / 'mise' / 'python3.12')
+ versions = {vpy: '3.9', new: '3.12'}
+ with mock.patch.object(pu, 'IS_WIN', False), \
+ mock.patch.object(pu, 'venv_python', return_value=vpy), \
+ mock.patch.object(pu, '_which', return_value=None), \
+ mock.patch.object(pu, 'version_manager_pythons', return_value=[new]), \
+ mock.patch.object(pu, '_python_version', side_effect=lambda p: versions.get(p, '')), \
+ mock.patch.object(pu, '_runs', return_value=True):
+ py, ok, ver = pu.find_python()
+ self.assertEqual((py, ok, ver), (new, True, '3.12'))
+
+
+class VersionManagerPythonsTest(unittest.TestCase):
+ def test_mise_installs_shims_and_asdf_installs_are_candidates(self) -> None:
+ with tempfile.TemporaryDirectory() as td:
+ root = Path(td)
+ install = _exe(root / 'mise' / 'installs' / 'python' / '3.12.8' / 'bin' / 'python3')
+ minor = _exe(root / 'mise' / 'installs' / 'python' / '3.12.8' / 'bin' / 'python3.12')
+ config = _exe(root / 'mise' / 'installs' / 'python' / '3.12.8' / 'bin' / 'python3-config')
+ shim = _exe(root / 'mise' / 'shims' / 'python3')
+ asdf = _exe(root / 'asdf' / 'installs' / 'python' / '3.13.1' / 'bin' / 'python3')
+ env = {'MISE_DATA_DIR': str(root / 'mise'), 'ASDF_DATA_DIR': str(root / 'asdf')}
+ with mock.patch.object(pu, 'IS_WIN', False), mock.patch.dict(os.environ, env):
+ cands = pu.version_manager_pythons()
+ for p in (install, minor, shim, asdf):
+ self.assertIn(p, cands)
+ self.assertNotIn(config, cands, 'python3-config is not an interpreter')
+
+
+try:
+ from nspb_qt.model import Model
+except ImportError: # PySide6 is not installed
+ Model = None
+
+
[email protected](Model is None, 'PySide6 is not installed')
+class MakeVenvTest(unittest.TestCase):
+ def _run(self, venv_ver: str, base: str | None):
+ logs: list[str] = []
+ fake = types.SimpleNamespace(log=logs.append)
+ with tempfile.TemporaryDirectory() as td:
+ vdir = Path(td) / 'venv'
+ vpy = _exe(vdir / 'bin' / 'python3')
+ base_py = base if base is not None else vpy
+ if base is not None:
+ base_py = _exe(Path(td) / 'base' / 'python3.12')
+ versions = {base_py: '3.12'} if base is not None else {}
+ versions[vpy] = venv_ver
+ with mock.patch.object(pu, 'venv_dir', return_value=vdir), \
+ mock.patch.object(pu, 'venv_python', return_value=vpy), \
+ mock.patch.object(pu, '_python_version', side_effect=lambda p: versions.get(p, '')), \
+ mock.patch.object(pu, 'python_candidates', return_value=[]), \
+ mock.patch.object(pu, '_runs', return_value=False):
+ ok = Model._make_venv(fake, base_py)
+ return ok, sorted(p.name for p in Path(td).iterdir()), logs
+
+ def test_a_too_old_venv_is_renamed_not_deleted(self) -> None:
+ ok, names, _ = self._run('3.9', 'new')
+ self.assertFalse(ok) # venv creation is mocked to fail: the move is what is tested
+ self.assertEqual(len([n for n in names if n.startswith('venv.old-')]), 1, names)
+
+ def test_a_too_old_venv_stays_when_there_is_no_newer_base(self) -> None:
+ ok, names, logs = self._run('3.9', None)
+ self.assertFalse(ok)
+ self.assertEqual(names, ['venv'])
+ self.assertTrue(any('no newer Python' in line for line in logs))
+
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/frontend/Sources/NSPortBuilder/ContentView.swift b/frontend/Sources/NSPortBuilder/ContentView.swift
index 966032b4..f12fb137 100644
--- a/frontend/Sources/NSPortBuilder/ContentView.swift
+++ b/frontend/Sources/NSPortBuilder/ContentView.swift
@@ -2184,6 +2184,18 @@ final class BuildModel: ObservableObject {
return dir.isEmpty ? NSHomeDirectory() + "/.local/share/uv/python" : dir
}()
+ /// Where mise and asdf keep the Pythons they install, and mise's shims. `MISE_DATA_DIR` and
+ /// `ASDF_DATA_DIR` move them. A GUI launch's PATH does not include the shims directory, so a
+ /// Python managed by either was not found at all (issue #113). Injectable for tests.
+ nonisolated static var miseDataDir: String = {
+ let dir = ProcessInfo.processInfo.environment["MISE_DATA_DIR"] ?? ""
+ return dir.isEmpty ? NSHomeDirectory() + "/.local/share/mise" : dir
+ }()
+ nonisolated static var asdfDataDir: String = {
+ let dir = ProcessInfo.processInfo.environment["ASDF_DATA_DIR"] ?? ""
+ return dir.isEmpty ? NSHomeDirectory() + "/.asdf" : dir
+ }()
+
nonisolated static func importsOK(_ py: String) -> Bool {
runOK(py, ["-c", importProbe], env: ProcessInfo.processInfo.environment.merging(BundledTools.environment(for: py)) { $1 })
}
@@ -2217,6 +2229,21 @@ final class BuildModel: ObservableObject {
return p.terminationStatus == 0
}
let vpy = venvPython()
+ // An existing venv below minPython (built from Apple's 3.9 by an older app, issue #75) is
+ // moved aside and rebuilt: pip into it would "succeed" and the build would still fail.
+ // Moved, never deleted, because the folder is the user's. Only when a usable base exists,
+ // so a Mac with no new Python keeps its old venv instead of losing it for nothing.
+ if FileManager.default.isExecutableFile(atPath: vpy), pythonVersion(vpy).map({ $0 < minPython }) ?? true {
+ guard let interp = venvBase(preferred: base), interp != vpy else {
+ return (false, log + "\(venvDir) is older than Python \(minPython.0).\(minPython.1) and no newer Python was found to rebuild it from\n")
+ }
+ let stamp = ISO8601DateFormatter().string(from: Date()).replacingOccurrences(of: ":", with: "-")
+ let aside = venvDir + ".old-" + stamp
+ do { try FileManager.default.moveItem(atPath: venvDir, toPath: aside) } catch {
+ return (false, log + "could not move \(venvDir) aside: \(error.localizedDescription)\n")
+ }
+ log += "moved the old venv (Python below \(minPython.0).\(minPython.1)) to \(aside)\n"
+ }
if !FileManager.default.isExecutableFile(atPath: vpy) {
// findPython() returns the venv path it hopes to create when no interpreter was
// found, and the panel hands that to this function. It cannot be a base: `-m venv`
@@ -2289,8 +2316,26 @@ final class BuildModel: ObservableObject {
return out
}
+ /// Pythons managed by mise (installs, then its shims) and asdf (installs only: an asdf shim is
+ /// a script that needs `asdf` on the PATH, which a GUI launch lacks). Only the interpreter
+ /// names are taken, never `python3-config` and the other files that share the folder.
+ nonisolated static func versionManagerPythonCandidates() -> [String] {
+ var out: [String] = []
+ let strict = #"^python3(\.\d+)?$"#
+ for root in [miseDataDir + "/installs/python", asdfDataDir + "/installs/python"] {
+ let installs = (try? FileManager.default.contentsOfDirectory(atPath: root)) ?? []
+ for d in installs.sorted(by: { $0.compare($1, options: .numeric) == .orderedDescending }) {
+ let bin = root + "/" + d + "/bin"
+ out += pythonNames(in: bin, matching: strict).map { bin + "/" + $0 }
+ }
+ }
+ let shims = miseDataDir + "/shims"
+ out += pythonNames(in: shims, matching: strict).map { shims + "/" + $0 }
+ return out
+ }
+
/// Every interpreter a build could run under, best first: python.org's framework, Homebrew,
- /// /usr/local, uv's store, then whatever `which` can see. Probed by hand because a GUI app
+ /// /usr/local, uv's store, mise and asdf, then whatever `which` can see. Probed by hand because a GUI app
/// does not inherit the shell's PATH — the same reason .NET (in ~/.dotnet) used to show as
/// missing while a terminal saw it.
nonisolated static func pythonCandidates() -> [String] {
@@ -2308,6 +2353,7 @@ final class BuildModel: ObservableObject {
cands += pythonNames(in: dir, matching: #"^python3\.\d+$"#).map { dir + "/" + $0 }
}
cands += uvPythonCandidates()
+ cands += versionManagerPythonCandidates()
let p = Process()
p.executableURL = URL(fileURLWithPath: "/usr/bin/which")
p.arguments = ["-a", "python3"]
@@ -2341,7 +2387,10 @@ final class BuildModel: ObservableObject {
}
// No system interpreter has the packages: the venv (existing, or created by 安装) is next.
let vpy = venvPython()
- if FileManager.default.isExecutableFile(atPath: vpy) {
+ // A venv below minPython is not ready however complete its packages are: an older app
+ // version made it from Apple's 3.9, and the build script refuses that (issue #75). It
+ // falls through to the candidates above, or to the row saying the Python is too old.
+ if FileManager.default.isExecutableFile(atPath: vpy), let v = pythonVersion(vpy), v >= minPython {
if importsOK(vpy) { return (vpy, true) }
return (vpy, false) // exists but incomplete: 安装 finishes it
}
@@ -2566,21 +2615,25 @@ final class BuildModel: ObservableObject {
"curl -sSL https://dot.net/v1/dotnet-install.sh -o /tmp/di.sh && chmod +x /tmp/di.sh && /tmp/di.sh --channel 10.0 --install-dir $HOME/.dotnet"]))
let (py, pyOK) = findPython()
- let pyVer = pythonVersion(py).map { "\($0.0).\($0.1)" } ?? "?"
// A too-old interpreter is a different problem from a missing package: say which one it is,
// because the fix differs (install a newer Python vs pip install into this one).
// A version we cannot read means no interpreter ran at all (py is then the venv
// that does not exist yet), which is not the same as one that is too old.
+ let pyDetail = pythonDetail(py: py, ok: pyOK)
+ out.append(Dep(id: "python", name: "dep.python", hint: "dep.python.hint",
+ present: pyOK, detail: pyDetail,
+ installCmd: [py, "-m", "pip", "install", "--user", "lz4", "capstone", "fonttools", "backports.zstd"]))
+ return out
+ }
+
+ nonisolated static func pythonDetail(py: String, ok: Bool) -> String {
+ let pyVer = pythonVersion(py).map { "\($0.0).\($0.1)" } ?? "?"
let minVer = "\(minPython.0).\(minPython.1)"
- let pyDetail = pyOK ? (py == BundledTools.python() ? "dep.bundled" : py)
+ return ok ? (py == BundledTools.python() ? "dep.bundled" : py)
: pythonVersion(py).map { $0 >= minPython
? "dep.python.missing:\(py) (\(pyVer))"
: "dep.python.old:\(pyVer):\(minVer)" }
?? "dep.python.notFound:\(minVer):\(minVer)"
- out.append(Dep(id: "python", name: "dep.python", hint: "dep.python.hint",
- present: pyOK, detail: pyDetail,
- installCmd: [py, "-m", "pip", "install", "--user", "lz4", "capstone", "fonttools", "backports.zstd"]))
- return out
}
func checkDeps() {
diff --git a/frontend/Tests/NSPortBuilderTests/PythonVenvTests.swift b/frontend/Tests/NSPortBuilderTests/PythonVenvTests.swift
new file mode 100644
index 00000000..9c7fbdf2
--- /dev/null
+++ b/frontend/Tests/NSPortBuilderTests/PythonVenvTests.swift
@@ -0,0 +1,81 @@
+import XCTest
+@testable import NSPortBuilder
+
+/// A stale venv below minPython (issue #75) and Pythons managed by mise or asdf (issue #113).
+final class PythonVenvTests: XCTestCase {
+ private var tmp: URL!
+ private var oldVenv = "", oldMise = "", oldAsdf = ""
+
+ override func setUpWithError() throws {
+ tmp = FileManager.default.temporaryDirectory.appendingPathComponent("pyvenv-" + UUID().uuidString)
+ try FileManager.default.createDirectory(at: tmp, withIntermediateDirectories: true)
+ oldVenv = BuildModel.venvDir; oldMise = BuildModel.miseDataDir; oldAsdf = BuildModel.asdfDataDir
+ }
+
+ override func tearDownWithError() throws {
+ BuildModel.venvDir = oldVenv; BuildModel.miseDataDir = oldMise; BuildModel.asdfDataDir = oldAsdf
+ try? FileManager.default.removeItem(at: tmp)
+ }
+
+ /// A stand-in interpreter that reports `version` and accepts every import probe.
+ @discardableResult
+ private func stub(_ rel: String, version: String) throws -> String {
+ let url = tmp.appendingPathComponent(rel)
+ try FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true)
+ try Data("#!/bin/sh\ncase \"$2\" in *sys.version_info*) echo \(version);; esac\nexit 0\n".utf8).write(to: url)
+ try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: url.path)
+ return url.path
+ }
+
+ func testAVenvBelowTheFloorIsNotReturnedAsReady() throws {
+ BuildModel.venvDir = tmp.appendingPathComponent("venv").path
+ let vpy = try stub("venv/bin/python3", version: "3.9")
+ let found = BuildModel.findPython(resources: tmp)
+ XCTAssertFalse(found.path == vpy && found.ok, "a 3.9 venv with every package is still too old")
+ // With nothing else on the Mac the row reports the old state, not ready.
+ let detail = BuildModel.pythonDetail(py: vpy, ok: false)
+ XCTAssertTrue(detail.hasPrefix("dep.python.old:3.9:"), detail)
+ }
+
+ func testAVenvAtTheFloorIsStillReady() throws {
+ BuildModel.venvDir = tmp.appendingPathComponent("venv").path
+ let vpy = try stub("venv/bin/python3", version: "3.12")
+ let found = BuildModel.findPython(resources: tmp)
+ // A system Python with the packages may win on a developer Mac, the venv must not be rejected.
+ XCTAssertTrue(found.ok)
+ XCTAssertTrue(found.path == vpy || FileManager.default.isExecutableFile(atPath: found.path))
+ }
+
+ func testMakeVenvMovesATooOldVenvAside() throws {
+ BuildModel.venvDir = tmp.appendingPathComponent("venv").path
+ try stub("venv/bin/python3", version: "3.9")
+ let base = try stub("base/python3", version: "3.12")
+ _ = BuildModel.makeVenv(base: base)
+ let names = try FileManager.default.contentsOfDirectory(atPath: tmp.path)
+ let aside = names.filter { $0.hasPrefix("venv.old-") }
+ XCTAssertEqual(aside.count, 1, "the old venv is renamed, not deleted: \(names)")
+ XCTAssertTrue(FileManager.default.isExecutableFile(
+ atPath: tmp.appendingPathComponent(aside[0] + "/bin/python3").path))
+ }
+
+ func testMakeVenvLeavesAVenvAtTheFloorInPlace() throws {
+ BuildModel.venvDir = tmp.appendingPathComponent("venv").path
+ let vpy = try stub("venv/bin/python3", version: "3.12")
+ _ = BuildModel.makeVenv(base: vpy)
+ XCTAssertTrue(FileManager.default.isExecutableFile(atPath: vpy))
+ XCTAssertFalse(try FileManager.default.contentsOfDirectory(atPath: tmp.path).contains { $0.hasPrefix("venv.old-") })
+ }
+
+ func testMiseAndAsdfPythonsAreCandidates() throws {
+ BuildModel.miseDataDir = tmp.appendingPathComponent("mise").path
+ BuildModel.asdfDataDir = tmp.appendingPathComponent("asdf").path
+ let install = try stub("mise/installs/python/3.12.8/bin/python3", version: "3.12")
+ let minor = try stub("mise/installs/python/3.12.8/bin/python3.12", version: "3.12")
+ let config = try stub("mise/installs/python/3.12.8/bin/python3-config", version: "3.12")
+ let shim = try stub("mise/shims/python3", version: "3.12")
+ let asdf = try stub("asdf/installs/python/3.13.1/bin/python3", version: "3.13")
+ let cands = BuildModel.pythonCandidates()
+ for p in [install, minor, shim, asdf] { XCTAssertTrue(cands.contains(p), p) }
+ XCTAssertFalse(cands.contains(config), "python3-config is not an interpreter")
+ }
+}