fix: refactor checking for python binary

This commit is contained in:
Amin Yahyaabadi 2023-06-29 13:21:36 -07:00
parent 130062b173
commit 5ab4d6a34d
5 changed files with 55 additions and 48 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -122,32 +122,34 @@ async function setupPythonSystem(setupDir: string, version: string) {
async function findPython(binDir?: string) {
const foundBins = (
await Promise.all(
["python3", "python"].map(async (pythonBin) => {
await Promise.all(["python3", "python"].map((pythonBin) => isPythonUpToDate(pythonBin, binDir)))
).filter((bin) => bin !== undefined) as string[]
if (foundBins.length === 0) {
return undefined
}
return foundBins[0]
}
async function isPythonUpToDate(candidate: string, binDir?: string) {
try {
if (binDir !== undefined) {
if (
(await pathExists(join(binDir, addExeExt(pythonBin)))) &&
(await isBinUptoDate(pythonBin, MinVersions.python!))
) {
return pythonBin
const pythonBinPath = join(binDir, addExeExt(candidate))
if (await pathExists(pythonBinPath)) {
if (await isBinUptoDate(pythonBinPath, MinVersions.python!)) {
return pythonBinPath
}
}
if (
(await which(pythonBin, { nothrow: true })) !== null &&
(await isBinUptoDate(pythonBin, MinVersions.python!))
) {
return pythonBin
}
const pythonBinPath: string | null = await which(candidate, { nothrow: true })
if (pythonBinPath !== null && (await isBinUptoDate(pythonBinPath, MinVersions.python!))) {
return pythonBinPath
}
} catch {
// ignore
// fall through
}
return undefined
})
)
).filter((bin) => bin !== undefined)
return foundBins?.[0]
}
async function findOrSetupPip(foundPython: string) {
@ -164,22 +166,27 @@ async function findOrSetupPip(foundPython: string) {
}
async function findPip() {
const foundBins = (
await Promise.all(
["pip3", "pip"].map(async (pip) => {
const foundBins = (await Promise.all(["pip3", "pip"].map(isPipUptoDate))).filter(
(bin) => bin !== undefined
) as string[]
if (foundBins.length === 0) {
return undefined
}
return foundBins[0]
}
async function isPipUptoDate(pip: string) {
try {
if ((await which(pip, { nothrow: true })) !== null && (await isBinUptoDate(pip, MinVersions.pip!))) {
return pip
const pipPath: string | null = await which(pip, { nothrow: true })
if (pipPath !== null && (await isBinUptoDate(pipPath, MinVersions.pip!))) {
return pipPath
}
} catch {
// ignore
// fall through
}
return undefined
})
)
).filter((bin) => bin !== undefined)
return foundBins?.[0]
}
async function setupPip(foundPython: string) {