Skip to content

Commit a11a459

Browse files
committed
refactor(build): isolate Python command cache
1 parent b2fe55b commit a11a459

1 file changed

Lines changed: 32 additions & 12 deletions

File tree

packages/build-infra/lib/python-installer.mts

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -279,21 +279,15 @@ export async function getPythonCommand() {
279279
// likely to be PEP 668 locked and missing packages we installed in
280280
// the venv earlier).
281281
if (!venvInitialized) {
282-
const probeVenvPath = venvPath || getDefaultVenvPath()
283-
const probePython = path.join(probeVenvPath, 'bin', 'python3')
284-
const probePip = path.join(probeVenvPath, 'bin', 'pip3')
285-
if (existsSync(probePython) && existsSync(probePip)) {
286-
venvPath = probeVenvPath
287-
venvPythonPath = probePython
288-
venvPipPath = probePip
289-
venvAvailable = true
290-
venvInitialized = true
291-
return probePython
282+
const existingVenvPython = probeExistingVenvPython()
283+
if (existingVenvPython) {
284+
return existingVenvPython
292285
}
293286
}
294287

295-
if (cachedPythonCommand !== undefined) {
296-
return cachedPythonCommand || undefined
288+
const cached = readCachedPythonCommand()
289+
if (cached.hit) {
290+
return cached.value
297291
}
298292

299293
const pip = getPipCommand()
@@ -359,3 +353,29 @@ export async function getPythonCommand() {
359353
cachedPythonCommand = pythonPath || ''
360354
return cachedPythonCommand || undefined
361355
}
356+
357+
export function probeExistingVenvPython() {
358+
const probeVenvPath = venvPath || getDefaultVenvPath()
359+
const probePython = path.join(probeVenvPath, 'bin', 'python3')
360+
const probePip = path.join(probeVenvPath, 'bin', 'pip3')
361+
if (!existsSync(probePython) || !existsSync(probePip)) {
362+
return undefined
363+
}
364+
venvPath = probeVenvPath
365+
venvPythonPath = probePython
366+
venvPipPath = probePip
367+
venvAvailable = true
368+
venvInitialized = true
369+
return probePython
370+
}
371+
372+
export function readCachedPythonCommand() {
373+
if (cachedPythonCommand === undefined) {
374+
return { __proto__: null, hit: false, value: undefined }
375+
}
376+
return {
377+
__proto__: null,
378+
hit: true,
379+
value: cachedPythonCommand === '' ? undefined : cachedPythonCommand,
380+
}
381+
}

0 commit comments

Comments
 (0)