Skip to content

Commit e9be714

Browse files
committed
fix: refactor setupPythonAndPip function
1 parent 623216a commit e9be714

File tree

6 files changed

+56
-48
lines changed

6 files changed

+56
-48
lines changed

dist/node12/setup_cpp.js

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/node12/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/node16/setup_cpp.js

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/node16/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: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ import { setupDnfPack } from "../utils/setup/setupDnfPack"
1414
import { isUbuntu } from "../utils/env/isUbuntu"
1515
import { getExecOutput } from "@actions/exec"
1616
import { existsSync } from "fs"
17+
import { isBinUptoDate } from "../utils/setup/version"
18+
import { getVersion } from "../versions/versions"
19+
import assert from "assert"
20+
import execa from "execa"
1721

1822
export async function setupPython(version: string, setupDir: string, arch: string) {
1923
if (ciDetect() !== "github-actions") {
@@ -78,6 +82,52 @@ export async function setupPythonViaSystem(
7882
}
7983
}
8084

85+
let setupPythonAndPipTried = false
86+
87+
/// setup python and pip if needed
88+
export async function setupPythonAndPip(): Promise<string> {
89+
let foundPython: string
90+
91+
// install python
92+
if (which.sync("python3", { nothrow: true }) !== null) {
93+
foundPython = "python3"
94+
} else if (which.sync("python", { nothrow: true }) !== null && (await isBinUptoDate("python", "3.0.0"))) {
95+
foundPython = "python"
96+
} else {
97+
info("python3 was not found. Installing python")
98+
await setupPython(getVersion("python", undefined), "", process.arch)
99+
// try again
100+
if (setupPythonAndPipTried) {
101+
throw new Error("Failed to install python")
102+
}
103+
setupPythonAndPipTried = true
104+
return setupPythonAndPip() // recurse
105+
}
106+
107+
assert(typeof foundPython === "string")
108+
109+
// install pip
110+
if (process.platform === "win32") {
111+
// downgrade pip on Windows
112+
// https://github.com/pypa/pip/issues/10875#issuecomment-1030293005
113+
execa.sync(foundPython, ["-m", "pip", "install", "-U", "pip==21.3.1"], { stdio: "inherit" })
114+
} else if (process.platform === "linux") {
115+
// ensure that pip is installed on Linux (happens when python is found but pip not installed)
116+
if (isArch()) {
117+
setupPacmanPack("python-pip")
118+
} else if (hasDnf()) {
119+
setupDnfPack("python3-pip")
120+
} else if (isUbuntu()) {
121+
await setupAptPack("python3-pip")
122+
}
123+
}
124+
125+
// install wheel (required for Conan, Meson, etc.)
126+
execa.sync(foundPython, ["-m", "pip", "install", "-U", "wheel"], { stdio: "inherit" })
127+
128+
return foundPython
129+
}
130+
81131
export async function addPythonBaseExecPrefix(python: string) {
82132
let dirs: string[] = []
83133

src/utils/setup/setupPipPack.ts

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,18 @@
11
/* eslint-disable require-atomic-updates */
22
import execa from "execa"
3-
import which from "which"
43
import { info } from "@actions/core"
5-
import { addPythonBaseExecPrefix, setupPython } from "../../python/python"
6-
import { isBinUptoDate } from "./version"
7-
import { getVersion } from "../../versions/versions"
4+
import { addPythonBaseExecPrefix, setupPythonAndPip } from "../../python/python"
85
import { InstallationInfo } from "./setupBin"
9-
import { setupAptPack } from "./setupAptPack"
10-
import { setupPacmanPack } from "./setupPacmanPack"
11-
import { isArch } from "../env/isArch"
12-
import { isUbuntu } from "../env/isUbuntu"
13-
import { hasDnf } from "../env/hasDnf"
14-
import { setupDnfPack } from "./setupDnfPack"
156

167
let python: string | undefined
178
let binDir: string | undefined
189

19-
let tried = false
20-
2110
/** A function that installs a package using pip */
2211
export async function setupPipPack(name: string, version?: string): Promise<InstallationInfo> {
2312
info(`Installing ${name} ${version ?? ""} via pip`)
2413

25-
// setup python and pip if needed
2614
if (python === undefined) {
27-
if (which.sync("python3", { nothrow: true }) !== null) {
28-
python = "python3"
29-
} else if (which.sync("python", { nothrow: true }) !== null && (await isBinUptoDate("python", "3.0.0"))) {
30-
python = "python"
31-
} else {
32-
info("python3 was not found. Installing python")
33-
await setupPython(getVersion("python", undefined), "", process.arch)
34-
// try again
35-
if (tried) {
36-
throw new Error("Failed to install python")
37-
}
38-
tried = true
39-
return setupPipPack(name, version)
40-
}
41-
if (process.platform === "win32") {
42-
// downgrade pip on Windows
43-
// https://github.com/pypa/pip/issues/10875#issuecomment-1030293005
44-
execa.sync(python, ["-m", "pip", "install", "-U", "pip==21.3.1"], { stdio: "inherit" })
45-
} else if (process.platform === "linux") {
46-
// ensure that pip is installed on Linux (happens when python is found but pip not installed)
47-
if (isArch()) {
48-
setupPacmanPack("python-pip")
49-
} else if (hasDnf()) {
50-
setupDnfPack("python3-pip")
51-
} else if (isUbuntu()) {
52-
await setupAptPack("python3-pip")
53-
}
54-
}
55-
56-
// install wheel (required for Conan, Meson, etc.)
57-
execa.sync(python, ["-m", "pip", "install", "-U", "wheel"], { stdio: "inherit" })
15+
python = await setupPythonAndPip()
5816
}
5917

6018
execa.sync(python, ["-m", "pip", "install", version !== undefined && version !== "" ? `${name}==${version}` : name], {

0 commit comments

Comments
 (0)