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"
|
2023-06-29 06:22:03 +08:00
|
|
|
import { getExecOutput } from "@actions/exec"
|
2024-10-07 11:48:03 +08:00
|
|
|
import ciInfo from "ci-info"
|
|
|
|
|
const { GITHUB_ACTIONS } = ciInfo
|
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-08-30 07:12:54 +08:00
|
|
|
import memoize from "memoizee"
|
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"
|
2024-08-16 16:50:32 +08:00
|
|
|
import { installAptPack } 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"
|
|
|
|
|
import { hasPipx, setupPipPackSystem, setupPipPackWithPython } from "../utils/setup/setupPipPack.js"
|
|
|
|
|
import { isBinUptoDate } from "../utils/setup/version.js"
|
|
|
|
|
import { unique } from "../utils/std/index.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 }> {
|
2023-06-29 06:16:31 +08:00
|
|
|
const installInfo = await findOrSetupPython(version, setupDir, arch)
|
|
|
|
|
assert(installInfo.bin !== undefined)
|
|
|
|
|
const foundPython = installInfo.bin
|
|
|
|
|
|
|
|
|
|
// setup pip
|
|
|
|
|
const foundPip = await findOrSetupPip(foundPython)
|
|
|
|
|
if (foundPip === undefined) {
|
|
|
|
|
throw new Error("pip was not installed correctly")
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-01 17:49:28 +08:00
|
|
|
await setupPipx(foundPython)
|
|
|
|
|
|
|
|
|
|
await setupWheel(foundPython)
|
|
|
|
|
|
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 {
|
2023-09-01 18:10:24 +08:00
|
|
|
if (!(await hasPipx(foundPython))) {
|
2024-02-14 20:16:39 +08:00
|
|
|
try {
|
|
|
|
|
await setupPipPackWithPython(foundPython, "pipx", undefined, { upgrade: true, usePipx: false })
|
|
|
|
|
} catch (err) {
|
|
|
|
|
if (setupPipPackSystem("pipx", false) === null) {
|
|
|
|
|
throw new Error(`pipx was not installed correctly ${err}`)
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-09-01 17:49:28 +08:00
|
|
|
}
|
|
|
|
|
await execa(foundPython, ["-m", "pipx", "ensurepath"], { stdio: "inherit" })
|
2024-09-18 06:32:20 +08:00
|
|
|
await setupVenv(foundPython)
|
2023-09-01 17:49:28 +08:00
|
|
|
} catch (err) {
|
2024-10-31 15:58:11 +08:00
|
|
|
notice(`Failed to install pipx: ${(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) {
|
|
|
|
|
try {
|
|
|
|
|
await setupPipPackWithPython(foundPython, "venv", undefined, { upgrade: false, usePipx: false })
|
|
|
|
|
} 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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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" }])
|
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) {
|
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
|
|
|
|
|
}
|
2023-06-28 08:10:13 +08:00
|
|
|
|
2024-09-18 03:00:18 +08:00
|
|
|
async function addPythonBaseExecPrefix_(python: string) {
|
2022-11-05 10:45:54 +08:00
|
|
|
const dirs: string[] = []
|
2022-11-05 05:53:28 +08:00
|
|
|
|
|
|
|
|
// detection based on the platform
|
|
|
|
|
if (process.platform === "linux") {
|
|
|
|
|
dirs.push("/home/runner/.local/bin/")
|
|
|
|
|
} else if (process.platform === "darwin") {
|
|
|
|
|
dirs.push("/usr/local/bin/")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// detection using python.sys
|
|
|
|
|
const base_exec_prefix = (await getExecOutput(`${python} -c "import sys;print(sys.base_exec_prefix);"`)).stdout.trim()
|
2022-11-05 07:03:42 +08:00
|
|
|
// any of these are possible depending on the operating system!
|
|
|
|
|
dirs.push(join(base_exec_prefix, "Scripts"), join(base_exec_prefix, "Scripts", "bin"), join(base_exec_prefix, "bin"))
|
2022-11-05 05:53:28 +08:00
|
|
|
|
2022-11-05 10:45:54 +08:00
|
|
|
// remove duplicates
|
|
|
|
|
return unique(dirs)
|
2022-11-05 05:53:28 +08:00
|
|
|
}
|
2023-06-29 06:22:03 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add the base exec prefix to the PATH. This is required for Conan, Meson, etc. to work properly.
|
|
|
|
|
*
|
|
|
|
|
* The answer is cached for subsequent calls
|
|
|
|
|
*/
|
2024-09-18 03:00:18 +08:00
|
|
|
export const addPythonBaseExecPrefix = memoize(addPythonBaseExecPrefix_, { promise: true })
|