Skip to content

Commit 42d3ea4

Browse files
committed
fix: add system package manager fallback for pip packages
1 parent 0916dc5 commit 42d3ea4

File tree

8 files changed

+48
-47
lines changed

8 files changed

+48
-47
lines changed

dist/actions/setup-cpp.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/actions/setup-cpp.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/legacy/setup-cpp.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/legacy/setup-cpp.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/modern/setup-cpp.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/modern/setup-cpp.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/python/python.ts

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { isBinUptoDate } from "../utils/setup/version"
2121
import { unique } from "../utils/std"
2222
import { MinVersions } from "../versions/default_versions"
2323
import { pathExists } from "path-exists"
24-
import { hasPipx, setupPipPackWithPython } from "../utils/setup/setupPipPack"
24+
import { hasPipx, setupPipPackSystem, setupPipPackWithPython } from "../utils/setup/setupPipPack"
2525

2626
export async function setupPython(version: string, setupDir: string, arch: string): Promise<InstallationInfo> {
2727
const installInfo = await findOrSetupPython(version, setupDir, arch)
@@ -44,19 +44,7 @@ export async function setupPython(version: string, setupDir: string, arch: strin
4444
async function setupPipx(foundPython: string) {
4545
try {
4646
if (!(await hasPipx(foundPython))) {
47-
try {
48-
await setupPipPackWithPython(foundPython, "pipx", undefined, { upgrade: true, usePipx: false })
49-
} catch (err) {
50-
if (isUbuntu()) {
51-
await setupAptPack([{ name: "python3-pipx" }])
52-
} else if (isArch()) {
53-
await setupPacmanPack("python-pipx")
54-
} else if (hasDnf()) {
55-
await setupDnfPack([{ name: "python3-pipx" }])
56-
} else {
57-
throw err
58-
}
59-
}
47+
await setupPipPackWithPython(foundPython, "pipx", undefined, { upgrade: true, usePipx: false })
6048
}
6149
await execa(foundPython, ["-m", "pipx", "ensurepath"], { stdio: "inherit" })
6250
} catch (err) {
@@ -236,7 +224,9 @@ async function isPipUptoDate(pip: string) {
236224
async function setupPip(foundPython: string) {
237225
const upgraded = await ensurePipUpgrade(foundPython)
238226
if (!upgraded) {
239-
await setupPipSystem()
227+
// ensure that pip is installed on Linux (happens when python is found but pip not installed)
228+
await setupPipPackSystem("pip")
229+
240230
// upgrade pip
241231
await ensurePipUpgrade(foundPython)
242232
}
@@ -261,20 +251,6 @@ async function ensurePipUpgrade(foundPython: string) {
261251
return false
262252
}
263253

264-
function setupPipSystem() {
265-
if (process.platform === "linux") {
266-
// ensure that pip is installed on Linux (happens when python is found but pip not installed)
267-
if (isArch()) {
268-
return setupPacmanPack("python-pip")
269-
} else if (hasDnf()) {
270-
return setupDnfPack([{ name: "python3-pip" }])
271-
} else if (isUbuntu()) {
272-
return setupAptPack([{ name: "python3-pip" }])
273-
}
274-
}
275-
throw new Error(`Could not install pip on ${process.platform}`)
276-
}
277-
278254
async function addPythonBaseExecPrefix_raw(python: string) {
279255
const dirs: string[] = []
280256

src/utils/setup/setupPipPack.ts

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ import { InstallationInfo } from "./setupBin"
99
import { getVersion } from "../../versions/versions"
1010
import { ubuntuVersion } from "../env/ubuntu_version"
1111
import memoize from "micro-memoize"
12+
import { isArch } from "../env/isArch"
13+
import { setupPacmanPack } from "./setupPacmanPack"
14+
import { hasDnf } from "../env/hasDnf"
15+
import { setupDnfPack } from "./setupDnfPack"
16+
import { isUbuntu } from "../env/isUbuntu"
17+
import { setupAptPack } from "./setupAptPack"
1218

1319
export type SetupPipPackOptions = {
1420
/** Whether to use pipx instead of pip */
@@ -43,13 +49,19 @@ export async function setupPipPackWithPython(
4349

4450
info(`Installing ${name} ${version ?? ""} via ${pip}`)
4551

46-
const nameAndVersion = version !== undefined && version !== "" ? `${name}==${version}` : name
47-
const upgradeFlag = upgrade ? (isPipx ? ["upgrade"] : ["install", "--upgrade"]) : ["install"]
48-
const userFlag = !isPipx && user ? ["--user"] : []
52+
try {
53+
const nameAndVersion = version !== undefined && version !== "" ? `${name}==${version}` : name
54+
const upgradeFlag = upgrade ? (isPipx ? ["upgrade"] : ["install", "--upgrade"]) : ["install"]
55+
const userFlag = !isPipx && user ? ["--user"] : []
4956

50-
execaSync(givenPython, ["-m", pip, ...upgradeFlag, ...userFlag, nameAndVersion], {
51-
stdio: "inherit",
52-
})
57+
execaSync(givenPython, ["-m", pip, ...upgradeFlag, ...userFlag, nameAndVersion], {
58+
stdio: "inherit",
59+
})
60+
} catch (err) {
61+
if ((await setupPipPackSystem(name)) === null) {
62+
throw new Error(`Failed to install ${name} via ${pip} ${err}`)
63+
}
64+
}
5365

5466
const execPaths = await addPythonBaseExecPrefix(givenPython)
5567
const binDir = await findBinDir(execPaths, name)
@@ -87,3 +99,16 @@ async function findBinDir(dirs: string[], name: string) {
8799

88100
return dirs[dirs.length - 1]
89101
}
102+
103+
export function setupPipPackSystem(name: string) {
104+
if (process.platform === "linux") {
105+
if (isArch()) {
106+
return setupPacmanPack(`python-${name}`)
107+
} else if (hasDnf()) {
108+
return setupDnfPack([{ name: `python3-${name}` }])
109+
} else if (isUbuntu()) {
110+
return setupAptPack([{ name: `python3-${name}` }])
111+
}
112+
}
113+
return null
114+
}

0 commit comments

Comments
 (0)