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