Skip to content

Commit 5ab4d6a

Browse files
committed
fix: refactor checking for python binary
1 parent 130062b commit 5ab4d6a

File tree

5 files changed

+55
-48
lines changed

5 files changed

+55
-48
lines changed

dist/node12/setup-cpp.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/node12/setup-cpp.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/node16/setup-cpp.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/node16/setup-cpp.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/python/python.ts

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -122,32 +122,34 @@ async function setupPythonSystem(setupDir: string, version: string) {
122122

123123
async function findPython(binDir?: string) {
124124
const foundBins = (
125-
await Promise.all(
126-
["python3", "python"].map(async (pythonBin) => {
127-
try {
128-
if (binDir !== undefined) {
129-
if (
130-
(await pathExists(join(binDir, addExeExt(pythonBin)))) &&
131-
(await isBinUptoDate(pythonBin, MinVersions.python!))
132-
) {
133-
return pythonBin
134-
}
135-
}
136-
if (
137-
(await which(pythonBin, { nothrow: true })) !== null &&
138-
(await isBinUptoDate(pythonBin, MinVersions.python!))
139-
) {
140-
return pythonBin
141-
}
142-
} catch {
143-
// ignore
144-
}
145-
return undefined
146-
})
147-
)
148-
).filter((bin) => bin !== undefined)
125+
await Promise.all(["python3", "python"].map((pythonBin) => isPythonUpToDate(pythonBin, binDir)))
126+
).filter((bin) => bin !== undefined) as string[]
127+
128+
if (foundBins.length === 0) {
129+
return undefined
130+
}
131+
132+
return foundBins[0]
133+
}
149134

150-
return foundBins?.[0]
135+
async function isPythonUpToDate(candidate: string, binDir?: string) {
136+
try {
137+
if (binDir !== undefined) {
138+
const pythonBinPath = join(binDir, addExeExt(candidate))
139+
if (await pathExists(pythonBinPath)) {
140+
if (await isBinUptoDate(pythonBinPath, MinVersions.python!)) {
141+
return pythonBinPath
142+
}
143+
}
144+
}
145+
const pythonBinPath: string | null = await which(candidate, { nothrow: true })
146+
if (pythonBinPath !== null && (await isBinUptoDate(pythonBinPath, MinVersions.python!))) {
147+
return pythonBinPath
148+
}
149+
} catch {
150+
// fall through
151+
}
152+
return undefined
151153
}
152154

153155
async function findOrSetupPip(foundPython: string) {
@@ -164,22 +166,27 @@ async function findOrSetupPip(foundPython: string) {
164166
}
165167

166168
async function findPip() {
167-
const foundBins = (
168-
await Promise.all(
169-
["pip3", "pip"].map(async (pip) => {
170-
try {
171-
if ((await which(pip, { nothrow: true })) !== null && (await isBinUptoDate(pip, MinVersions.pip!))) {
172-
return pip
173-
}
174-
} catch {
175-
// ignore
176-
}
177-
return undefined
178-
})
179-
)
180-
).filter((bin) => bin !== undefined)
169+
const foundBins = (await Promise.all(["pip3", "pip"].map(isPipUptoDate))).filter(
170+
(bin) => bin !== undefined
171+
) as string[]
172+
173+
if (foundBins.length === 0) {
174+
return undefined
175+
}
181176

182-
return foundBins?.[0]
177+
return foundBins[0]
178+
}
179+
180+
async function isPipUptoDate(pip: string) {
181+
try {
182+
const pipPath: string | null = await which(pip, { nothrow: true })
183+
if (pipPath !== null && (await isBinUptoDate(pipPath, MinVersions.pip!))) {
184+
return pipPath
185+
}
186+
} catch {
187+
// fall through
188+
}
189+
return undefined
183190
}
184191

185192
async function setupPip(foundPython: string) {

0 commit comments

Comments
 (0)