2024-08-07 14:44:32 +08:00
|
|
|
import assert from "assert"
|
2024-02-01 15:43:59 +08:00
|
|
|
import { homedir } from "os"
|
2024-09-04 16:49:14 +08:00
|
|
|
import { dirname, join, parse as pathParse } from "path"
|
2024-10-07 11:48:03 +08:00
|
|
|
import ciInfo from "ci-info"
|
|
|
|
|
const { GITHUB_ACTIONS } = ciInfo
|
2025-03-23 08:52:31 +08:00
|
|
|
import { endGroup, startGroup } from "@actions/core"
|
2024-10-31 15:58:11 +08:00
|
|
|
import { info, notice, warning } from "ci-log"
|
2024-08-16 17:52:11 +08:00
|
|
|
import { addPath } from "envosman"
|
2023-07-05 03:29:30 +08:00
|
|
|
import { execa } from "execa"
|
2024-02-01 15:43:59 +08:00
|
|
|
import { readdir } from "fs/promises"
|
2024-04-03 15:15:43 +08:00
|
|
|
import { pathExists } from "path-exists"
|
2024-09-04 16:49:14 +08:00
|
|
|
import { addExeExt } from "patha"
|
2025-03-23 16:18:33 +08:00
|
|
|
import { hasApk, installApkPackage } from "setup-alpine"
|
2025-03-23 08:56:47 +08:00
|
|
|
import { installAptPack, isAptPackInstalled } from "setup-apt"
|
2024-08-24 06:20:37 +08:00
|
|
|
import { installBrewPack } from "setup-brew"
|
2023-06-29 06:22:03 +08:00
|
|
|
import which from "which"
|
2024-08-16 06:22:07 +08:00
|
|
|
import { rcOptions } from "../cli-options.js"
|
|
|
|
|
import { hasDnf } from "../utils/env/hasDnf.js"
|
|
|
|
|
import { isArch } from "../utils/env/isArch.js"
|
|
|
|
|
import { isUbuntu } from "../utils/env/isUbuntu.js"
|
|
|
|
|
import type { InstallationInfo } from "../utils/setup/setupBin.js"
|
|
|
|
|
import { setupChocoPack } from "../utils/setup/setupChocoPack.js"
|
|
|
|
|
import { setupDnfPack } from "../utils/setup/setupDnfPack.js"
|
|
|
|
|
import { setupPacmanPack } from "../utils/setup/setupPacmanPack.js"
|
2025-01-12 19:41:12 +08:00
|
|
|
import {
|
|
|
|
|
hasPipxBinary,
|
|
|
|
|
hasPipxModule,
|
2025-03-23 17:40:21 +08:00
|
|
|
isExternallyManaged,
|
2025-01-12 19:41:12 +08:00
|
|
|
setupPipPackSystem,
|
|
|
|
|
setupPipPackWithPython,
|
|
|
|
|
} from "../utils/setup/setupPipPack.js"
|
2024-08-16 06:22:07 +08:00
|
|
|
import { isBinUptoDate } from "../utils/setup/version.js"
|
2024-10-25 18:03:21 +08:00
|
|
|
import { getVersionDefault, isMinVersion } from "../versions/versions.js"
|
2021-09-15 04:23:45 +08:00
|
|
|
|
2024-09-18 06:49:19 +08:00
|
|
|
export async function setupPython(
|
2024-10-31 14:37:26 +08:00
|
|
|
version: string,
|
2024-09-18 06:49:19 +08:00
|
|
|
setupDir: string,
|
|
|
|
|
arch: string,
|
|
|
|
|
): Promise<InstallationInfo & { bin: string }> {
|
2025-03-23 08:52:31 +08:00
|
|
|
startGroup("Setup Python")
|
2023-06-29 06:16:31 +08:00
|
|
|
const installInfo = await findOrSetupPython(version, setupDir, arch)
|
|
|
|
|
assert(installInfo.bin !== undefined)
|
|
|
|
|
const foundPython = installInfo.bin
|
2025-03-23 08:52:31 +08:00
|
|
|
endGroup()
|
2023-06-29 06:16:31 +08:00
|
|
|
|
2025-01-12 18:55:14 +08:00
|
|
|
// setup venv
|
2025-03-23 08:52:31 +08:00
|
|
|
startGroup("Setup venv")
|
2025-01-12 18:55:14 +08:00
|
|
|
await setupVenv(foundPython)
|
2025-03-23 08:52:31 +08:00
|
|
|
endGroup()
|
2025-01-12 18:55:14 +08:00
|
|
|
|
2023-06-29 06:16:31 +08:00
|
|
|
// setup pip
|
2025-03-23 08:52:31 +08:00
|
|
|
startGroup("Setup pip")
|
2023-06-29 06:16:31 +08:00
|
|
|
const foundPip = await findOrSetupPip(foundPython)
|
2025-03-23 08:52:31 +08:00
|
|
|
endGroup()
|
2023-06-29 06:16:31 +08:00
|
|
|
if (foundPip === undefined) {
|
|
|
|
|
throw new Error("pip was not installed correctly")
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-23 08:52:31 +08:00
|
|
|
// setup pipx
|
|
|
|
|
startGroup("Setup pipx")
|
2023-09-01 17:49:28 +08:00
|
|
|
await setupPipx(foundPython)
|
2025-03-23 08:52:31 +08:00
|
|
|
endGroup()
|
|
|
|
|
|
|
|
|
|
// setup wheel
|
|
|
|
|
startGroup("Setup wheel")
|
2023-09-01 17:49:28 +08:00
|
|
|
await setupWheel(foundPython)
|
2025-03-23 08:52:31 +08:00
|
|
|
endGroup()
|
2023-09-01 17:49:28 +08:00
|
|
|
|
2024-09-18 06:49:19 +08:00
|
|
|
return installInfo as InstallationInfo & { bin: string }
|
2023-09-01 17:49:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function setupPipx(foundPython: string) {
|
|
|
|
|
try {
|
2025-01-12 19:41:12 +08:00
|
|
|
if (!(await hasPipxModule(foundPython))) {
|
2025-03-23 08:33:51 +08:00
|
|
|
// install pipx for the system-wide python
|
2024-02-14 20:16:39 +08:00
|
|
|
try {
|
2025-03-23 16:53:17 +08:00
|
|
|
await setupPipPackSystem("pipx", false)
|
2025-01-12 19:20:44 +08:00
|
|
|
} catch (err) {
|
2025-03-23 08:33:51 +08:00
|
|
|
notice(`pipx was not installed completely for the system-wide python: ${err}`)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// install pipx for the given python if the system-wide pipx is different for the given python
|
|
|
|
|
try {
|
|
|
|
|
if (!(await hasPipxModule(foundPython))) {
|
|
|
|
|
await setupPipPackWithPython(foundPython, "pipx", undefined, { upgrade: true, usePipx: false })
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
notice(`pipx was not installed completely for ${foundPython}: ${err}`)
|
2024-02-14 20:16:39 +08:00
|
|
|
}
|
2023-09-01 17:49:28 +08:00
|
|
|
}
|
2025-03-23 08:33:51 +08:00
|
|
|
|
|
|
|
|
// install ensurepath for the given python
|
2025-01-12 19:41:12 +08:00
|
|
|
if (await hasPipxModule(foundPython)) {
|
|
|
|
|
await execa(foundPython, ["-m", "pipx", "ensurepath"], { stdio: "inherit" })
|
|
|
|
|
} else if (await hasPipxBinary()) {
|
2025-03-23 08:33:51 +08:00
|
|
|
// install ensurepath with the pipx binary
|
|
|
|
|
notice(`pipx module not found for ${foundPython}. Trying to install with pipx binary...`)
|
2025-01-12 19:41:12 +08:00
|
|
|
await execa("pipx", ["ensurepath"], { stdio: "inherit" })
|
|
|
|
|
} else {
|
2025-01-12 20:10:52 +08:00
|
|
|
throw new Error("pipx module or pipx binary not found. Corrput pipx installation.")
|
2025-01-12 19:41:12 +08:00
|
|
|
}
|
2023-09-01 17:49:28 +08:00
|
|
|
} catch (err) {
|
2025-03-23 08:33:51 +08:00
|
|
|
notice(`Failed to install pipx completely for ${foundPython}: ${(err as Error).toString()}. Ignoring...`)
|
2023-09-01 17:49:28 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-18 06:32:20 +08:00
|
|
|
async function setupVenv(foundPython: string) {
|
2025-01-12 18:32:41 +08:00
|
|
|
if (await hasVenv(foundPython)) {
|
2025-01-12 18:55:14 +08:00
|
|
|
info("venv module already installed.")
|
2025-01-12 18:32:41 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-18 06:32:20 +08:00
|
|
|
try {
|
2025-01-12 18:32:41 +08:00
|
|
|
await setupPipPackSystem("venv")
|
2024-09-18 06:32:20 +08:00
|
|
|
} catch (err) {
|
2024-11-01 20:35:48 +08:00
|
|
|
info(`Failed to install venv: ${(err as Error).toString()}. Ignoring...`)
|
2024-09-18 06:32:20 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-12 18:32:41 +08:00
|
|
|
async function hasVenv(foundPython: string): Promise<boolean> {
|
|
|
|
|
try {
|
|
|
|
|
// check if venv module exits
|
2025-01-12 19:20:44 +08:00
|
|
|
await execa(foundPython, ["-m", "venv", "-h"], { stdio: "ignore" })
|
2025-03-23 08:56:47 +08:00
|
|
|
|
|
|
|
|
// checking venv module is not enough on Ubuntu 20.04
|
|
|
|
|
if (isUbuntu()) {
|
|
|
|
|
return isAptPackInstalled("python3-venv")
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-12 18:32:41 +08:00
|
|
|
return true
|
|
|
|
|
} catch {
|
|
|
|
|
// if module not found, continue
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-01 17:49:28 +08:00
|
|
|
/** Setup wheel and setuptools */
|
|
|
|
|
async function setupWheel(foundPython: string) {
|
2023-06-29 06:16:31 +08:00
|
|
|
try {
|
2023-09-01 18:28:05 +08:00
|
|
|
await setupPipPackWithPython(foundPython, "setuptools", undefined, {
|
|
|
|
|
upgrade: true,
|
|
|
|
|
isLibrary: true,
|
|
|
|
|
usePipx: false,
|
|
|
|
|
})
|
2024-09-18 06:32:20 +08:00
|
|
|
await setupPipPackWithPython(foundPython, "wheel", undefined, { upgrade: false, isLibrary: true, usePipx: false })
|
2023-06-29 06:16:31 +08:00
|
|
|
} catch (err) {
|
2024-11-01 20:35:48 +08:00
|
|
|
info(`Failed to install setuptools/wheel: ${(err as Error).toString()}. Ignoring...`)
|
2023-06-29 06:16:31 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-31 14:37:26 +08:00
|
|
|
async function findOrSetupPython(givenVersion: string, setupDir: string, arch: string): Promise<InstallationInfo> {
|
|
|
|
|
// if a version range specified, use the default version, and later check the range
|
|
|
|
|
const version = isMinVersion(givenVersion) ? "" : givenVersion
|
|
|
|
|
|
2023-06-29 06:10:33 +08:00
|
|
|
let installInfo: InstallationInfo | undefined
|
2023-06-29 06:44:25 +08:00
|
|
|
let foundPython = await findPython(setupDir)
|
2023-06-29 06:10:33 +08:00
|
|
|
|
|
|
|
|
if (foundPython !== undefined) {
|
|
|
|
|
const binDir = dirname(foundPython)
|
|
|
|
|
installInfo = { bin: foundPython, installDir: binDir, binDir }
|
|
|
|
|
} else {
|
|
|
|
|
// if python is not found, try to install it
|
|
|
|
|
if (GITHUB_ACTIONS) {
|
|
|
|
|
// install python in GitHub Actions
|
|
|
|
|
try {
|
|
|
|
|
info("Installing python in GitHub Actions")
|
2024-08-16 06:22:07 +08:00
|
|
|
const { setupActionsPython } = await import("./actions_python.js")
|
2023-06-29 06:10:33 +08:00
|
|
|
await setupActionsPython(version, setupDir, arch)
|
|
|
|
|
|
2024-02-01 15:43:42 +08:00
|
|
|
foundPython = await findPython(setupDir)
|
|
|
|
|
if (foundPython === undefined) {
|
|
|
|
|
throw new Error("Python binary could not be found")
|
|
|
|
|
}
|
2023-06-29 06:10:33 +08:00
|
|
|
const binDir = dirname(foundPython)
|
|
|
|
|
installInfo = { bin: foundPython, installDir: binDir, binDir }
|
|
|
|
|
} catch (err) {
|
|
|
|
|
warning((err as Error).toString())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (installInfo === undefined) {
|
|
|
|
|
// install python via system package manager
|
|
|
|
|
installInfo = await setupPythonSystem(setupDir, version)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-30 07:12:54 +08:00
|
|
|
if (foundPython === undefined || installInfo.bin === undefined) {
|
2024-02-01 15:43:42 +08:00
|
|
|
foundPython = await findPython(setupDir)
|
|
|
|
|
if (foundPython === undefined) {
|
|
|
|
|
throw new Error("Python binary could not be found")
|
|
|
|
|
}
|
2024-08-29 04:54:48 +08:00
|
|
|
installInfo = { bin: foundPython, installDir: dirname(foundPython), binDir: dirname(foundPython) }
|
2021-09-18 20:50:31 +08:00
|
|
|
}
|
2023-06-29 06:10:33 +08:00
|
|
|
|
|
|
|
|
return installInfo
|
2021-09-18 20:39:59 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-29 06:10:33 +08:00
|
|
|
async function setupPythonSystem(setupDir: string, version: string) {
|
|
|
|
|
let installInfo: InstallationInfo | undefined
|
2021-09-18 20:39:59 +08:00
|
|
|
switch (process.platform) {
|
|
|
|
|
case "win32": {
|
2022-02-05 08:02:04 +08:00
|
|
|
if (setupDir) {
|
2022-05-13 03:55:00 +08:00
|
|
|
await setupChocoPack("python3", version, [`--params=/InstallDir:${setupDir}`])
|
2022-02-05 08:02:04 +08:00
|
|
|
} else {
|
2022-05-13 03:55:00 +08:00
|
|
|
await setupChocoPack("python3", version)
|
2022-02-05 08:02:04 +08:00
|
|
|
}
|
2021-09-18 20:39:59 +08:00
|
|
|
// Adding the bin dir to the path
|
2024-02-01 15:43:42 +08:00
|
|
|
const bin = await findPython(setupDir)
|
|
|
|
|
if (bin === undefined) {
|
|
|
|
|
throw new Error("Python binary could not be found")
|
|
|
|
|
}
|
2023-06-29 06:44:25 +08:00
|
|
|
const binDir = dirname(bin)
|
2021-09-18 20:39:59 +08:00
|
|
|
/** The directory which the tool is installed to */
|
2024-08-15 09:43:53 +08:00
|
|
|
await addPath(binDir, rcOptions)
|
2023-06-29 06:44:25 +08:00
|
|
|
installInfo = { installDir: binDir, binDir, bin }
|
2023-06-29 05:45:15 +08:00
|
|
|
break
|
2021-09-18 20:39:59 +08:00
|
|
|
}
|
|
|
|
|
case "darwin": {
|
2024-08-24 06:20:37 +08:00
|
|
|
installInfo = await installBrewPack("python3", version)
|
2023-07-05 03:30:06 +08:00
|
|
|
// add the python and pip binaries to the path
|
2024-04-03 16:20:06 +08:00
|
|
|
const brewPythonPrefix: {
|
|
|
|
|
stdout: string
|
|
|
|
|
stderr: string
|
|
|
|
|
} = await execa("brew", ["--prefix", "python"], { stdio: "pipe" })
|
2023-07-05 03:30:06 +08:00
|
|
|
const brewPythonBin = join(brewPythonPrefix.stdout, "libexec", "bin")
|
2024-08-15 09:43:53 +08:00
|
|
|
await addPath(brewPythonBin, rcOptions)
|
2023-07-05 03:30:06 +08:00
|
|
|
|
2023-06-29 05:45:15 +08:00
|
|
|
break
|
2021-09-18 20:39:59 +08:00
|
|
|
}
|
|
|
|
|
case "linux": {
|
2022-06-30 10:06:33 +08:00
|
|
|
if (isArch()) {
|
2023-05-25 05:12:42 +08:00
|
|
|
installInfo = await setupPacmanPack("python", version)
|
2022-07-11 07:34:56 +08:00
|
|
|
} else if (hasDnf()) {
|
2023-07-16 18:12:24 +08:00
|
|
|
installInfo = await setupDnfPack([{ name: "python3", version }])
|
2022-07-11 08:53:17 +08:00
|
|
|
} else if (isUbuntu()) {
|
2024-08-16 16:50:32 +08:00
|
|
|
installInfo = await installAptPack([{ name: "python3", version }, { name: "python-is-python3" }])
|
2025-03-23 16:18:33 +08:00
|
|
|
} else if (await hasApk()) {
|
|
|
|
|
installInfo = await installApkPackage([{ name: "python3", version }])
|
2022-07-11 08:53:17 +08:00
|
|
|
} else {
|
2023-04-22 17:19:33 +08:00
|
|
|
throw new Error("Unsupported linux distributions")
|
2022-06-30 00:06:35 +08:00
|
|
|
}
|
2023-06-29 05:45:15 +08:00
|
|
|
break
|
2021-09-18 20:39:59 +08:00
|
|
|
}
|
|
|
|
|
default: {
|
2023-04-22 17:19:33 +08:00
|
|
|
throw new Error("Unsupported platform")
|
2021-09-18 20:39:59 +08:00
|
|
|
}
|
2021-09-15 04:23:45 +08:00
|
|
|
}
|
2023-06-29 05:45:15 +08:00
|
|
|
return installInfo
|
2021-09-15 04:23:45 +08:00
|
|
|
}
|
2022-11-05 05:53:28 +08:00
|
|
|
|
2023-06-29 06:44:25 +08:00
|
|
|
async function findPython(binDir?: string) {
|
2024-09-18 08:17:23 +08:00
|
|
|
for (const pythonBin of ["python", "python3"]) {
|
2023-06-30 04:58:48 +08:00
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
|
const foundPython = await isPythonUpToDate(pythonBin, binDir)
|
|
|
|
|
if (foundPython !== undefined) {
|
|
|
|
|
return foundPython
|
|
|
|
|
}
|
2023-06-30 04:21:36 +08:00
|
|
|
}
|
2024-02-01 15:43:59 +08:00
|
|
|
|
|
|
|
|
// On Windows, search in C:\PythonXX
|
|
|
|
|
if (process.platform === "win32") {
|
|
|
|
|
const rootDir = pathParse(homedir()).root
|
|
|
|
|
// find all directories in rootDir using readdir
|
|
|
|
|
const pythonDirs = (await readdir(rootDir)).filter((dir) => dir.startsWith("Python"))
|
|
|
|
|
|
|
|
|
|
for (const pythonDir of pythonDirs) {
|
|
|
|
|
for (const pythonBin of ["python3", "python"]) {
|
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
|
const foundPython = await isPythonUpToDate(pythonBin, join(rootDir, pythonDir))
|
|
|
|
|
if (foundPython !== undefined) {
|
|
|
|
|
return foundPython
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-30 04:58:48 +08:00
|
|
|
return undefined
|
2023-06-30 04:21:36 +08:00
|
|
|
}
|
2023-06-29 07:19:00 +08:00
|
|
|
|
2023-06-30 04:21:36 +08:00
|
|
|
async function isPythonUpToDate(candidate: string, binDir?: string) {
|
|
|
|
|
try {
|
2024-10-25 16:49:11 +08:00
|
|
|
const targetVersion = getVersionDefault("python")
|
|
|
|
|
|
2023-06-30 04:21:36 +08:00
|
|
|
if (binDir !== undefined) {
|
|
|
|
|
const pythonBinPath = join(binDir, addExeExt(candidate))
|
2024-10-25 16:49:11 +08:00
|
|
|
if (await pathExists(pythonBinPath) && await isBinUptoDate(pythonBinPath, targetVersion!)) {
|
2024-09-18 08:17:23 +08:00
|
|
|
return pythonBinPath
|
2023-06-30 04:21:36 +08:00
|
|
|
}
|
|
|
|
|
}
|
2024-04-03 16:20:06 +08:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
2023-06-30 04:58:48 +08:00
|
|
|
const pythonBinPaths = (await which(candidate, { nothrow: true, all: true })) ?? []
|
|
|
|
|
for (const pythonBinPath of pythonBinPaths) {
|
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
2024-10-25 16:49:11 +08:00
|
|
|
if (await isBinUptoDate(pythonBinPath, targetVersion!)) {
|
2023-06-30 04:58:48 +08:00
|
|
|
return pythonBinPath
|
|
|
|
|
}
|
2023-06-30 04:21:36 +08:00
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
|
|
|
|
return undefined
|
2023-06-29 05:45:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function findOrSetupPip(foundPython: string) {
|
|
|
|
|
const maybePip = await findPip()
|
2023-06-28 08:10:13 +08:00
|
|
|
|
2023-06-29 05:45:15 +08:00
|
|
|
if (maybePip === undefined) {
|
|
|
|
|
// install pip if not installed
|
|
|
|
|
info("pip was not found. Installing pip")
|
|
|
|
|
await setupPip(foundPython)
|
|
|
|
|
return findPip() // recurse to check if pip is on PATH and up-to-date
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return maybePip
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function findPip() {
|
2023-06-30 04:58:48 +08:00
|
|
|
for (const pipCandidate of ["pip3", "pip"]) {
|
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
|
const maybePip = await isPipUptoDate(pipCandidate)
|
|
|
|
|
if (maybePip !== undefined) {
|
|
|
|
|
return maybePip
|
|
|
|
|
}
|
2023-06-30 04:21:36 +08:00
|
|
|
}
|
2023-06-30 04:58:48 +08:00
|
|
|
return undefined
|
2023-06-30 04:21:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function isPipUptoDate(pip: string) {
|
|
|
|
|
try {
|
2024-10-25 16:49:11 +08:00
|
|
|
const targetVersion = getVersionDefault("pip")
|
|
|
|
|
|
2024-04-03 16:20:06 +08:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
2023-06-30 04:58:48 +08:00
|
|
|
const pipPaths = (await which(pip, { nothrow: true, all: true })) ?? []
|
|
|
|
|
for (const pipPath of pipPaths) {
|
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
2024-10-25 16:49:11 +08:00
|
|
|
if (await isBinUptoDate(pipPath, targetVersion!)) {
|
2023-06-30 04:58:48 +08:00
|
|
|
return pipPath
|
|
|
|
|
}
|
2023-06-30 04:21:36 +08:00
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
|
|
|
|
return undefined
|
2023-06-28 08:10:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function setupPip(foundPython: string) {
|
2023-07-05 03:29:30 +08:00
|
|
|
const upgraded = await ensurePipUpgrade(foundPython)
|
2023-06-29 05:45:15 +08:00
|
|
|
if (!upgraded) {
|
2023-09-01 18:44:04 +08:00
|
|
|
// ensure that pip is installed on Linux (happens when python is found but pip not installed)
|
|
|
|
|
await setupPipPackSystem("pip")
|
|
|
|
|
|
2023-06-29 07:43:01 +08:00
|
|
|
// upgrade pip
|
2023-07-05 03:29:30 +08:00
|
|
|
await ensurePipUpgrade(foundPython)
|
2023-06-29 05:45:15 +08:00
|
|
|
}
|
|
|
|
|
}
|
2023-06-28 08:10:13 +08:00
|
|
|
|
2023-07-05 03:29:30 +08:00
|
|
|
async function ensurePipUpgrade(foundPython: string) {
|
2025-03-23 17:40:21 +08:00
|
|
|
if (await isExternallyManaged(foundPython)) {
|
|
|
|
|
// let system tools handle pip
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-29 05:45:15 +08:00
|
|
|
try {
|
2023-07-05 03:29:30 +08:00
|
|
|
await execa(foundPython, ["-m", "ensurepip", "-U", "--upgrade"], { stdio: "inherit" })
|
2023-06-29 05:45:15 +08:00
|
|
|
return true
|
2023-06-29 06:10:33 +08:00
|
|
|
} catch (err1) {
|
2024-01-22 18:28:55 +08:00
|
|
|
info((err1 as Error).toString())
|
2023-06-29 05:45:15 +08:00
|
|
|
try {
|
|
|
|
|
// ensure pip is disabled on Ubuntu
|
2023-07-05 03:29:30 +08:00
|
|
|
await execa(foundPython, ["-m", "pip", "install", "--upgrade", "pip"], { stdio: "inherit" })
|
2023-06-29 05:45:15 +08:00
|
|
|
return true
|
2023-06-29 06:10:33 +08:00
|
|
|
} catch (err2) {
|
2024-01-22 18:28:55 +08:00
|
|
|
info((err2 as Error).toString())
|
2023-06-29 05:45:15 +08:00
|
|
|
// pip module not found
|
2023-06-28 08:10:13 +08:00
|
|
|
}
|
|
|
|
|
}
|
2023-06-29 05:45:15 +08:00
|
|
|
// all methods failed
|
|
|
|
|
return false
|
|
|
|
|
}
|