Skip to content

Commit 0ef456c

Browse files
authored
Merge pull request #178 from aminya/python-is-python3 [skip ci]
2 parents c362004 + 70bf67f commit 0ef456c

File tree

6 files changed

+81
-64
lines changed

6 files changed

+81
-64
lines changed

dist/node12/setup-cpp.js

+19-19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/node12/setup-cpp.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/node16/setup-cpp.js

+19-19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/node16/setup-cpp.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/python/python.ts

+17-15
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { getExecOutput } from "@actions/exec"
33
import assert from "assert"
44
import { GITHUB_ACTIONS } from "ci-info"
55
import { info, warning } from "ci-log"
6-
import { execaSync } from "execa"
6+
import { execa } from "execa"
77
import memoize from "micro-memoize"
88
import { addExeExt, dirname, join } from "patha"
99
import which from "which"
@@ -21,6 +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 { setupPipPackWithPython } from "../utils/setup/setupPipPack"
2425

2526
export async function setupPython(version: string, setupDir: string, arch: string): Promise<InstallationInfo> {
2627
const installInfo = await findOrSetupPython(version, setupDir, arch)
@@ -33,11 +34,12 @@ export async function setupPython(version: string, setupDir: string, arch: strin
3334
throw new Error("pip was not installed correctly")
3435
}
3536

36-
// setup wheel
37+
// setup wheel and setuptools
3738
try {
38-
setupWheel(foundPython)
39+
await setupPipPackWithPython(foundPython, "setuptools", undefined, true)
40+
await setupPipPackWithPython(foundPython, "wheel", undefined, true)
3941
} catch (err) {
40-
warning(`Failed to install wheels: ${(err as Error).toString()}. Ignoring...`)
42+
warning(`Failed to install setuptools or wheel: ${(err as Error).toString()}. Ignoring...`)
4143
}
4244

4345
return installInfo
@@ -99,6 +101,11 @@ async function setupPythonSystem(setupDir: string, version: string) {
99101
}
100102
case "darwin": {
101103
installInfo = await setupBrewPack("python3", version)
104+
// add the python and pip binaries to the path
105+
const brewPythonPrefix = await execa("brew", ["--prefix", "python"], { stdio: "pipe" })
106+
const brewPythonBin = join(brewPythonPrefix.stdout, "libexec", "bin")
107+
await addPath(brewPythonBin)
108+
102109
break
103110
}
104111
case "linux": {
@@ -107,7 +114,7 @@ async function setupPythonSystem(setupDir: string, version: string) {
107114
} else if (hasDnf()) {
108115
installInfo = setupDnfPack("python3", version)
109116
} else if (isUbuntu()) {
110-
installInfo = await setupAptPack([{ name: "python3", version }])
117+
installInfo = await setupAptPack([{ name: "python3", version }, { name: "python-is-python3" }])
111118
} else {
112119
throw new Error("Unsupported linux distributions")
113120
}
@@ -194,23 +201,23 @@ async function isPipUptoDate(pip: string) {
194201
}
195202

196203
async function setupPip(foundPython: string) {
197-
const upgraded = ensurePipUpgrade(foundPython)
204+
const upgraded = await ensurePipUpgrade(foundPython)
198205
if (!upgraded) {
199206
await setupPipSystem()
200207
// upgrade pip
201-
ensurePipUpgrade(foundPython)
208+
await ensurePipUpgrade(foundPython)
202209
}
203210
}
204211

205-
function ensurePipUpgrade(foundPython: string) {
212+
async function ensurePipUpgrade(foundPython: string) {
206213
try {
207-
execaSync(foundPython, ["-m", "ensurepip", "-U", "--upgrade"], { stdio: "inherit" })
214+
await execa(foundPython, ["-m", "ensurepip", "-U", "--upgrade"], { stdio: "inherit" })
208215
return true
209216
} catch (err1) {
210217
info((err1 as Error)?.toString?.())
211218
try {
212219
// ensure pip is disabled on Ubuntu
213-
execaSync(foundPython, ["-m", "pip", "install", "--upgrade", "pip"], { stdio: "inherit" })
220+
await execa(foundPython, ["-m", "pip", "install", "--upgrade", "pip"], { stdio: "inherit" })
214221
return true
215222
} catch (err2) {
216223
info((err2 as Error)?.toString?.())
@@ -235,11 +242,6 @@ function setupPipSystem() {
235242
throw new Error(`Could not install pip on ${process.platform}`)
236243
}
237244

238-
/** Install wheel (required for Conan, Meson, etc.) */
239-
function setupWheel(foundPython: string) {
240-
execaSync(foundPython, ["-m", "pip", "install", "-U", "wheel"], { stdio: "inherit" })
241-
}
242-
243245
async function addPythonBaseExecPrefix_raw(python: string) {
244246
const dirs: string[] = []
245247

src/utils/setup/setupPipPack.ts

+24-9
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,45 @@ import { addPath } from "../env/addEnv"
88
import { InstallationInfo } from "./setupBin"
99
import { getVersion } from "../../versions/versions"
1010
import { ubuntuVersion } from "../env/ubuntu_version"
11-
12-
/* eslint-disable require-atomic-updates */
13-
let python: string | undefined
11+
import memoize from "micro-memoize"
1412

1513
/** A function that installs a package using pip */
16-
export async function setupPipPack(name: string, version?: string): Promise<InstallationInfo> {
14+
export async function setupPipPack(name: string, version?: string, upgrade = false): Promise<InstallationInfo> {
15+
return setupPipPackWithPython(await getPython(), name, version, upgrade)
16+
}
17+
18+
export async function setupPipPackWithPython(
19+
givenPython: string,
20+
name: string,
21+
version?: string,
22+
upgrade = false
23+
): Promise<InstallationInfo> {
1724
info(`Installing ${name} ${version ?? ""} via pip`)
1825

19-
if (python === undefined) {
20-
python = (await setupPython(getVersion("python", undefined, await ubuntuVersion()), "", process.arch)).bin!
21-
}
26+
const nameAndVersion = version !== undefined && version !== "" ? `${name}==${version}` : name
27+
const upgradeFlag = upgrade === true ? ["--upgrade"] : []
2228

23-
execaSync(python, ["-m", "pip", "install", version !== undefined && version !== "" ? `${name}==${version}` : name], {
29+
execaSync(givenPython, ["-m", "pip", "install", ...upgradeFlag, nameAndVersion], {
2430
stdio: "inherit",
2531
})
2632

27-
const execPaths = await addPythonBaseExecPrefix(python)
33+
const execPaths = await addPythonBaseExecPrefix(givenPython)
2834
const binDir = await findBinDir(execPaths, name)
2935

3036
await addPath(binDir)
3137

3238
return { binDir }
3339
}
3440

41+
async function getPython_raw(): Promise<string> {
42+
const pythonBin = (await setupPython(getVersion("python", undefined, await ubuntuVersion()), "", process.arch)).bin
43+
if (pythonBin === undefined) {
44+
throw new Error("Python binary was not found")
45+
}
46+
return pythonBin
47+
}
48+
const getPython = memoize(getPython_raw)
49+
3550
async function findBinDir(dirs: string[], name: string) {
3651
const exists = await Promise.all(dirs.map((dir) => pathExists(join(dir, addExeExt(name)))))
3752
const dirIndex = exists.findIndex((exist) => exist)

0 commit comments

Comments
 (0)