fix: add system package manager fallback for pip packages

This commit is contained in:
Amin Yahyaabadi 2023-09-01 03:44:04 -07:00
parent 0916dc5cfb
commit 42d3ea447e
8 changed files with 48 additions and 47 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

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -21,7 +21,7 @@ import { isBinUptoDate } from "../utils/setup/version"
import { unique } from "../utils/std" import { unique } from "../utils/std"
import { MinVersions } from "../versions/default_versions" import { MinVersions } from "../versions/default_versions"
import { pathExists } from "path-exists" import { pathExists } from "path-exists"
import { hasPipx, setupPipPackWithPython } from "../utils/setup/setupPipPack" import { hasPipx, setupPipPackSystem, setupPipPackWithPython } from "../utils/setup/setupPipPack"
export async function setupPython(version: string, setupDir: string, arch: string): Promise<InstallationInfo> { export async function setupPython(version: string, setupDir: string, arch: string): Promise<InstallationInfo> {
const installInfo = await findOrSetupPython(version, setupDir, arch) const installInfo = await findOrSetupPython(version, setupDir, arch)
@ -44,19 +44,7 @@ export async function setupPython(version: string, setupDir: string, arch: strin
async function setupPipx(foundPython: string) { async function setupPipx(foundPython: string) {
try { try {
if (!(await hasPipx(foundPython))) { if (!(await hasPipx(foundPython))) {
try { await setupPipPackWithPython(foundPython, "pipx", undefined, { upgrade: true, usePipx: false })
await setupPipPackWithPython(foundPython, "pipx", undefined, { upgrade: true, usePipx: false })
} catch (err) {
if (isUbuntu()) {
await setupAptPack([{ name: "python3-pipx" }])
} else if (isArch()) {
await setupPacmanPack("python-pipx")
} else if (hasDnf()) {
await setupDnfPack([{ name: "python3-pipx" }])
} else {
throw err
}
}
} }
await execa(foundPython, ["-m", "pipx", "ensurepath"], { stdio: "inherit" }) await execa(foundPython, ["-m", "pipx", "ensurepath"], { stdio: "inherit" })
} catch (err) { } catch (err) {
@ -236,7 +224,9 @@ async function isPipUptoDate(pip: string) {
async function setupPip(foundPython: string) { async function setupPip(foundPython: string) {
const upgraded = await ensurePipUpgrade(foundPython) const upgraded = await ensurePipUpgrade(foundPython)
if (!upgraded) { if (!upgraded) {
await setupPipSystem() // ensure that pip is installed on Linux (happens when python is found but pip not installed)
await setupPipPackSystem("pip")
// upgrade pip // upgrade pip
await ensurePipUpgrade(foundPython) await ensurePipUpgrade(foundPython)
} }
@ -261,20 +251,6 @@ async function ensurePipUpgrade(foundPython: string) {
return false return false
} }
function setupPipSystem() {
if (process.platform === "linux") {
// ensure that pip is installed on Linux (happens when python is found but pip not installed)
if (isArch()) {
return setupPacmanPack("python-pip")
} else if (hasDnf()) {
return setupDnfPack([{ name: "python3-pip" }])
} else if (isUbuntu()) {
return setupAptPack([{ name: "python3-pip" }])
}
}
throw new Error(`Could not install pip on ${process.platform}`)
}
async function addPythonBaseExecPrefix_raw(python: string) { async function addPythonBaseExecPrefix_raw(python: string) {
const dirs: string[] = [] const dirs: string[] = []

View File

@ -9,6 +9,12 @@ import { InstallationInfo } from "./setupBin"
import { getVersion } from "../../versions/versions" import { getVersion } from "../../versions/versions"
import { ubuntuVersion } from "../env/ubuntu_version" import { ubuntuVersion } from "../env/ubuntu_version"
import memoize from "micro-memoize" import memoize from "micro-memoize"
import { isArch } from "../env/isArch"
import { setupPacmanPack } from "./setupPacmanPack"
import { hasDnf } from "../env/hasDnf"
import { setupDnfPack } from "./setupDnfPack"
import { isUbuntu } from "../env/isUbuntu"
import { setupAptPack } from "./setupAptPack"
export type SetupPipPackOptions = { export type SetupPipPackOptions = {
/** Whether to use pipx instead of pip */ /** Whether to use pipx instead of pip */
@ -43,13 +49,19 @@ export async function setupPipPackWithPython(
info(`Installing ${name} ${version ?? ""} via ${pip}`) info(`Installing ${name} ${version ?? ""} via ${pip}`)
const nameAndVersion = version !== undefined && version !== "" ? `${name}==${version}` : name try {
const upgradeFlag = upgrade ? (isPipx ? ["upgrade"] : ["install", "--upgrade"]) : ["install"] const nameAndVersion = version !== undefined && version !== "" ? `${name}==${version}` : name
const userFlag = !isPipx && user ? ["--user"] : [] const upgradeFlag = upgrade ? (isPipx ? ["upgrade"] : ["install", "--upgrade"]) : ["install"]
const userFlag = !isPipx && user ? ["--user"] : []
execaSync(givenPython, ["-m", pip, ...upgradeFlag, ...userFlag, nameAndVersion], { execaSync(givenPython, ["-m", pip, ...upgradeFlag, ...userFlag, nameAndVersion], {
stdio: "inherit", stdio: "inherit",
}) })
} catch (err) {
if ((await setupPipPackSystem(name)) === null) {
throw new Error(`Failed to install ${name} via ${pip} ${err}`)
}
}
const execPaths = await addPythonBaseExecPrefix(givenPython) const execPaths = await addPythonBaseExecPrefix(givenPython)
const binDir = await findBinDir(execPaths, name) const binDir = await findBinDir(execPaths, name)
@ -87,3 +99,16 @@ async function findBinDir(dirs: string[], name: string) {
return dirs[dirs.length - 1] return dirs[dirs.length - 1]
} }
export function setupPipPackSystem(name: string) {
if (process.platform === "linux") {
if (isArch()) {
return setupPacmanPack(`python-${name}`)
} else if (hasDnf()) {
return setupDnfPack([{ name: `python3-${name}` }])
} else if (isUbuntu()) {
return setupAptPack([{ name: `python3-${name}` }])
}
}
return null
}