setup-cpp/src/python/python.ts

110 lines
3.7 KiB
TypeScript
Raw Normal View History

2022-03-01 13:54:49 +08:00
import { addPath } from "../utils/env/addEnv"
import { setupAptPack } from "../utils/setup/setupAptPack"
import { setupPacmanPack } from "../utils/setup/setupPacmanPack"
import { setupBrewPack } from "../utils/setup/setupBrewPack"
import { setupChocoPack } from "../utils/setup/setupChocoPack"
2022-08-08 09:48:41 +08:00
import ciDetect from "@npmcli/ci-detect"
2022-08-08 16:22:28 +08:00
import { warning, info } from "ci-log"
2022-06-30 10:06:33 +08:00
import { isArch } from "../utils/env/isArch"
import which from "which"
import { InstallationInfo } from "../utils/setup/setupBin"
import { dirname, join } from "patha"
import { hasDnf } from "../utils/env/hasDnf"
import { setupDnfPack } from "../utils/setup/setupDnfPack"
import { isUbuntu } from "../utils/env/isUbuntu"
import { getExecOutput } from "@actions/exec"
import { existsSync } from "fs"
2022-04-25 07:47:52 +08:00
export async function setupPython(version: string, setupDir: string, arch: string) {
if (ciDetect() !== "github-actions") {
// TODO parse version
return setupPythonViaSystem(version, setupDir, arch)
}
try {
2022-07-06 12:56:30 +08:00
info("Installing python in GitHub Actions")
2022-04-25 07:47:52 +08:00
const { setupActionsPython } = await import("./actions_python")
return setupActionsPython(version, setupDir, arch)
} catch (err) {
warning((err as Error).toString())
return setupPythonViaSystem(version, setupDir, arch)
}
}
export async function setupPythonViaSystem(
version: string,
setupDir: string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_arch: string
): Promise<InstallationInfo> {
switch (process.platform) {
case "win32": {
if (setupDir) {
2022-05-13 03:55:00 +08:00
await setupChocoPack("python3", version, [`--params=/InstallDir:${setupDir}`])
} else {
2022-05-13 03:55:00 +08:00
await setupChocoPack("python3", version)
}
// Adding the bin dir to the path
const pythonBinPath =
which.sync("python3.exe", { nothrow: true }) ??
which.sync("python.exe", { nothrow: true }) ??
join(setupDir, "python.exe")
const pythonSetupDir = dirname(pythonBinPath)
/** The directory which the tool is installed to */
await addPath(pythonSetupDir)
return { installDir: pythonSetupDir, binDir: pythonSetupDir }
}
case "darwin": {
return setupBrewPack("python3", version)
}
case "linux": {
let installInfo: InstallationInfo
2022-06-30 10:06:33 +08:00
if (isArch()) {
installInfo = setupPacmanPack("python", version)
setupPacmanPack("python-pip")
} else if (hasDnf()) {
installInfo = setupDnfPack("python3", version)
2022-07-11 09:12:19 +08:00
setupDnfPack("python3-pip")
} else if (isUbuntu()) {
installInfo = await setupAptPack("python3", version)
await setupAptPack("python3-pip")
} else {
throw new Error(`Unsupported linux distributions`)
}
return installInfo
}
default: {
throw new Error(`Unsupported platform`)
}
}
}
export async function addPythonBaseExecPrefix(python: string) {
let dirs: string[] = []
// 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()
dirs.push(join(base_exec_prefix, "Scripts"), join(base_exec_prefix, "Scripts", "bin"))
// exclude the non existing ones
dirs = dirs.filter((dir) => existsSync(dir))
// add the directories to the path
await Promise.all(dirs.map((dir) => addPath(dir)))
// the last directory is the bin directory (not empty)
const foundBinDir = dirs.pop()
if (foundBinDir === undefined) {
warning("The binary directory for pip dependencies could not be found")
}
return foundBinDir!
}