({ cwd, spawnSyncImpl, useCache } = {})
| 10 | } |
| 11 | |
| 12 | function resolvePythonSpec({ cwd, spawnSyncImpl, useCache } = {}) { |
| 13 | const runner = typeof spawnSyncImpl === "function" ? spawnSyncImpl : spawnSync; |
| 14 | const canUseCache = useCache !== false; |
| 15 | if (canUseCache && cachedPythonSpec && typeof cachedPythonSpec.cmd === "string") { |
| 16 | return { cmd: cachedPythonSpec.cmd, argsPrefix: [...cachedPythonSpec.argsPrefix] }; |
| 17 | } |
| 18 | |
| 19 | const candidates = [{ cmd: "python3", argsPrefix: [] }, { cmd: "py", argsPrefix: ["-3"] }, { cmd: "python", argsPrefix: [] }]; |
| 20 | for (const spec of candidates) { |
| 21 | const probe = runner(spec.cmd, [...spec.argsPrefix, "--version"], { cwd, stdio: "ignore" }); |
| 22 | if (probe?.error) { |
| 23 | if (probe.error.code === "ENOENT") continue; |
| 24 | continue; |
| 25 | } |
| 26 | if (probe?.status === 0) { |
| 27 | const resolved = { cmd: spec.cmd, argsPrefix: [...spec.argsPrefix] }; |
| 28 | if (canUseCache) cachedPythonSpec = resolved; |
| 29 | return resolved; |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | throw new Error("python runtime not found (tried: python3, py -3, python)"); |
| 34 | } |
| 35 | |
| 36 | function runPython(args, { cwd } = {}) { |
| 37 | const spec = resolvePythonSpec({ cwd }); |
no outgoing calls
no test coverage detected