fix: handle upgrade/user flags for pipx

This commit is contained in:
Amin Yahyaabadi 2023-09-01 03:02:09 -07:00
parent 4e60284097
commit 512202e7f4
8 changed files with 35 additions and 28 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 { MinVersions } from "../versions/default_versions"
import { pathExists } from "path-exists"
import { setupPipPackWithPython } from "../utils/setup/setupPipPack"
import { hasPipx, setupPipPackWithPython } from "../utils/setup/setupPipPack"
export async function setupPython(version: string, setupDir: string, arch: string): Promise<InstallationInfo> {
const installInfo = await findOrSetupPython(version, setupDir, arch)
@ -43,6 +43,7 @@ export async function setupPython(version: string, setupDir: string, arch: strin
async function setupPipx(foundPython: string) {
try {
if (!(await hasPipx())) {
try {
await setupPipPackWithPython(foundPython, "pipx", undefined, true)
} catch (err) {
@ -56,6 +57,7 @@ async function setupPipx(foundPython: string) {
throw err
}
}
}
await execa(foundPython, ["-m", "pipx", "ensurepath"], { stdio: "inherit" })
} catch (err) {
warning(`Failed to install pipx: ${(err as Error).toString()}. Ignoring...`)

View File

@ -22,15 +22,16 @@ export async function setupPipPackWithPython(
upgrade = false,
user = true,
): Promise<InstallationInfo> {
const pip = (await which("pipx", { nothrow: true })) !== null ? "pipx" : "pip"
const isPipx = await hasPipx()
const pip = isPipx ? "pipx" : "pip"
info(`Installing ${name} ${version ?? ""} via ${pip}`)
const nameAndVersion = version !== undefined && version !== "" ? `${name}==${version}` : name
const upgradeFlag = upgrade === true ? ["--upgrade"] : []
const userFlag = user === true ? ["--user"] : []
const upgradeFlag = upgrade ? (isPipx ? ["upgrade"] : ["install", "--upgrade"]) : ["install"]
const userFlag = !isPipx && user ? ["--user"] : []
execaSync(givenPython, ["-m", pip, "install", ...upgradeFlag, ...userFlag, nameAndVersion], {
execaSync(givenPython, ["-m", pip, ...upgradeFlag, ...userFlag, nameAndVersion], {
stdio: "inherit",
})
@ -42,6 +43,10 @@ export async function setupPipPackWithPython(
return { binDir }
}
export async function hasPipx() {
return (await which("pipx", { nothrow: true })) !== null
}
async function getPython_raw(): Promise<string> {
const pythonBin = (await setupPython(getVersion("python", undefined, await ubuntuVersion()), "", process.arch)).bin
if (pythonBin === undefined) {