feat: install pip and wheel in all cases

This commit is contained in:
Amin Yahyaabadi 2023-06-28 15:10:33 -07:00
parent 0f6b349a1e
commit 6637fda894
8 changed files with 72 additions and 71 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -15,34 +15,62 @@ import { setupDnfPack } from "../utils/setup/setupDnfPack"
import { isUbuntu } from "../utils/env/isUbuntu" import { isUbuntu } from "../utils/env/isUbuntu"
import { getExecOutput } from "@actions/exec" import { getExecOutput } from "@actions/exec"
import { isBinUptoDate } from "../utils/setup/version" import { isBinUptoDate } from "../utils/setup/version"
import { getVersion } from "../versions/versions"
import assert from "assert"
import { execaSync } from "execa" import { execaSync } from "execa"
import { unique } from "../utils/std" import { unique } from "../utils/std"
import { DefaultVersions } from "../versions/default_versions" import { DefaultVersions } from "../versions/default_versions"
export async function setupPython(version: string, setupDir: string, arch: string) { export async function setupPython(version: string, setupDir: string, arch: string): Promise<InstallationInfo> {
if (!GITHUB_ACTIONS) { let installInfo: InstallationInfo | undefined
// TODO parse version let foundPython = await findPython()
return setupPythonViaSystem(version, setupDir, arch)
} 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 { try {
info("Installing python in GitHub Actions") info("Installing python in GitHub Actions")
const { setupActionsPython } = await import("./actions_python") const { setupActionsPython } = await import("./actions_python")
return setupActionsPython(version, setupDir, arch) await setupActionsPython(version, setupDir, arch)
foundPython = (await findPython())!
const binDir = dirname(foundPython)
installInfo = { bin: foundPython, installDir: binDir, binDir }
} catch (err) { } catch (err) {
warning((err as Error).toString()) warning((err as Error).toString())
return setupPythonViaSystem(version, setupDir, arch) }
}
if (installInfo === undefined) {
// install python via system package manager
installInfo = await setupPythonSystem(setupDir, version)
} }
} }
export async function setupPythonViaSystem( if (foundPython === undefined) {
version: string, foundPython = (await findPython())!
setupDir: string, installInfo.bin = foundPython
// eslint-disable-next-line @typescript-eslint/no-unused-vars }
_arch: string
): Promise<InstallationInfo> { // setup pip
let installInfo: InstallationInfo const foundPip = await findOrSetupPip(foundPython)
if (foundPip === undefined) {
throw new Error("pip was not installed correctly")
}
// setup wheel
try {
setupWheel(foundPython)
} catch (err) {
warning(`Failed to install wheels: ${(err as Error).toString()}. Ignoring...`)
}
return installInfo
}
async function setupPythonSystem(setupDir: string, version: string) {
let installInfo: InstallationInfo | undefined
switch (process.platform) { switch (process.platform) {
case "win32": { case "win32": {
if (setupDir) { if (setupDir) {
@ -81,23 +109,9 @@ export async function setupPythonViaSystem(
throw new Error("Unsupported platform") throw new Error("Unsupported platform")
} }
} }
await findOrSetupPip((await findPython())!)
return installInfo return installInfo
} }
/// setup python and pip if needed
export async function findOrSetupPythonAndPip(): Promise<string> {
const foundPython = await findOrSetupPython()
const foundPip = await findOrSetupPip(foundPython)
if (foundPip === undefined) {
throw new Error("pip was not installed correctly")
}
setupWheel(foundPython)
return foundPython
}
let setupPythonTried = false
async function findPython() { async function findPython() {
if (which.sync("python3", { nothrow: true }) !== null) { if (which.sync("python3", { nothrow: true }) !== null) {
return "python3" return "python3"
@ -107,23 +121,6 @@ async function findPython() {
return undefined return undefined
} }
async function findOrSetupPython() {
const maybeFoundPython = await findPython()
if (maybeFoundPython !== undefined) {
return maybeFoundPython
}
if (setupPythonTried) {
throw new Error("Failed to install python")
}
setupPythonTried = true
// install python
info("python3 was not found. Installing python")
await setupPython(getVersion("python", undefined), "", process.arch)
return findOrSetupPython() // recurse
}
async function findOrSetupPip(foundPython: string) { async function findOrSetupPip(foundPython: string) {
const maybePip = await findPip() const maybePip = await findPip()
@ -161,12 +158,14 @@ function ensurePipUpgrade(foundPython: string) {
try { try {
execaSync(foundPython, ["-m", "ensurepip", "-U", "--upgrade"], { stdio: "inherit" }) execaSync(foundPython, ["-m", "ensurepip", "-U", "--upgrade"], { stdio: "inherit" })
return true return true
} catch { } catch (err1) {
info((err1 as Error)?.toString?.())
try { try {
// ensure pip is disabled on Ubuntu // ensure pip is disabled on Ubuntu
execaSync(foundPython, ["-m", "pip", "install", "--upgrade", "pip"], { stdio: "inherit" }) execaSync(foundPython, ["-m", "pip", "install", "--upgrade", "pip"], { stdio: "inherit" })
return true return true
} catch { } catch (err2) {
info((err2 as Error)?.toString?.())
// pip module not found // pip module not found
} }
} }

View File

@ -34,6 +34,7 @@ export type InstallationInfo = {
/** The top install dir */ /** The top install dir */
installDir?: string installDir?: string
binDir: string binDir: string
bin?: string
} }
let didInit: boolean = false let didInit: boolean = false

View File

@ -3,9 +3,10 @@ import { execaSync } from "execa"
import { pathExists } from "path-exists" import { pathExists } from "path-exists"
import { addExeExt, dirname, join } from "patha" import { addExeExt, dirname, join } from "patha"
import which from "which" import which from "which"
import { addPythonBaseExecPrefix, findOrSetupPythonAndPip } from "../../python/python" import { addPythonBaseExecPrefix, setupPython } from "../../python/python"
import { addPath } from "../env/addEnv" import { addPath } from "../env/addEnv"
import { InstallationInfo } from "./setupBin" import { InstallationInfo } from "./setupBin"
import { getVersion } from "../../versions/versions"
/* eslint-disable require-atomic-updates */ /* eslint-disable require-atomic-updates */
let python: string | undefined let python: string | undefined
@ -16,7 +17,7 @@ export async function setupPipPack(name: string, version?: string): Promise<Inst
info(`Installing ${name} ${version ?? ""} via pip`) info(`Installing ${name} ${version ?? ""} via pip`)
if (python === undefined) { if (python === undefined) {
python = await findOrSetupPythonAndPip() python = (await setupPython(getVersion("python", undefined), "", process.arch)).bin!
} }
execaSync(python, ["-m", "pip", "install", version !== undefined && version !== "" ? `${name}==${version}` : name], { execaSync(python, ["-m", "pip", "install", version !== undefined && version !== "" ? `${name}==${version}` : name], {

View File

@ -28,7 +28,7 @@ export const DefaultVersions: Record<string, string | undefined> = {
task: "3.25.0", // https://github.com/go-task/task/releases task: "3.25.0", // https://github.com/go-task/task/releases
doxygen: isArch() ? "1.9.6-1" : "1.9.7", // https://www.doxygen.nl/download.html // https://packages.ubuntu.com/search?suite=all&arch=any&searchon=names&keywords=doxygen // https://formulae.brew.sh/formula/doxygen // https://archlinux.org/packages/extra/x86_64/doxygen/ doxygen: isArch() ? "1.9.6-1" : "1.9.7", // https://www.doxygen.nl/download.html // https://packages.ubuntu.com/search?suite=all&arch=any&searchon=names&keywords=doxygen // https://formulae.brew.sh/formula/doxygen // https://archlinux.org/packages/extra/x86_64/doxygen/
gcc: isArch() ? "13.1.1-1" : "13", // https://github.com/brechtsanders/winlibs_mingw/releases and // https://packages.ubuntu.com/search?suite=all&arch=any&searchon=names&keywords=gcc gcc: isArch() ? "13.1.1-1" : "13", // https://github.com/brechtsanders/winlibs_mingw/releases and // https://packages.ubuntu.com/search?suite=all&arch=any&searchon=names&keywords=gcc
pip: "23.1.2", pip: "22.3.1",
} }
/// If an ubuntu versions is not in this map: /// If an ubuntu versions is not in this map: