* Ensure the Python `uncommon-route` package is installed. * Tries: pipx → uv → pip (with --user fallback). * Returns the python executable to use.
(logger)
| 64 | * Returns the python executable to use. |
| 65 | */ |
| 66 | function ensurePythonDeps(logger) { |
| 67 | const pythonCandidates = ["python3", "python"]; |
| 68 | let pythonPath = null; |
| 69 | |
| 70 | for (const candidate of pythonCandidates) { |
| 71 | const path = which(candidate); |
| 72 | if (path) { |
| 73 | try { |
| 74 | const ver = execSync(`${path} --version`, { encoding: "utf-8" }).trim(); |
| 75 | const match = ver.match(/(\d+)\.(\d+)/); |
| 76 | if (match && (parseInt(match[1]) > 3 || (parseInt(match[1]) === 3 && parseInt(match[2]) >= 11))) { |
| 77 | pythonPath = path; |
| 78 | break; |
| 79 | } |
| 80 | } catch { /* skip */ } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | if (!pythonPath) { |
| 85 | logger.error("Python 3.11+ not found. Install Python first: https://python.org"); |
| 86 | logger.error(" macOS: brew install python@3.12"); |
| 87 | logger.error(" Ubuntu: sudo apt install python3.12"); |
| 88 | return null; |
| 89 | } |
| 90 | |
| 91 | if (isPythonPackageInstalled(pythonPath)) { |
| 92 | logger.info(`Python package '${PY_PACKAGE}' already installed`); |
| 93 | return pythonPath; |
| 94 | } |
| 95 | |
| 96 | logger.info(`Installing Python package '${PY_PACKAGE}'...`); |
| 97 | |
| 98 | // Strategy 1: pipx (isolated, clean) |
| 99 | if (which("pipx")) { |
| 100 | try { |
| 101 | execSync(`pipx install ${PY_PACKAGE}`, { stdio: "pipe" }); |
| 102 | logger.info(`Installed via pipx`); |
| 103 | return pythonPath; |
| 104 | } catch { /* fallthrough */ } |
| 105 | } |
| 106 | |
| 107 | // Strategy 2: uv (fast, modern) |
| 108 | if (which("uv")) { |
| 109 | try { |
| 110 | execSync(`uv pip install ${PY_PACKAGE}`, { stdio: "pipe" }); |
| 111 | logger.info(`Installed via uv`); |
| 112 | return pythonPath; |
| 113 | } catch { /* fallthrough */ } |
| 114 | } |
| 115 | |
| 116 | // Strategy 3: pip install --user |
| 117 | try { |
| 118 | execSync(`${pythonPath} -m pip install ${PY_PACKAGE} --user --quiet`, { stdio: "pipe" }); |
| 119 | logger.info(`Installed via pip --user`); |
| 120 | return pythonPath; |
| 121 | } catch { /* fallthrough */ } |
| 122 | |
| 123 | // Strategy 4: pip install --break-system-packages (last resort on managed envs) |
no test coverage detected