Skip to content

Commit

Permalink
Merge pull request #178 from aminya/python-is-python3 [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya authored Jul 4, 2023
2 parents c362004 + 70bf67f commit 0ef456c
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 64 deletions.
38 changes: 19 additions & 19 deletions dist/node12/setup-cpp.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/node12/setup-cpp.js.map

Large diffs are not rendered by default.

38 changes: 19 additions & 19 deletions dist/node16/setup-cpp.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/node16/setup-cpp.js.map

Large diffs are not rendered by default.

32 changes: 17 additions & 15 deletions src/python/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getExecOutput } from "@actions/exec"
import assert from "assert"
import { GITHUB_ACTIONS } from "ci-info"
import { info, warning } from "ci-log"
import { execaSync } from "execa"
import { execa } from "execa"
import memoize from "micro-memoize"
import { addExeExt, dirname, join } from "patha"
import which from "which"
Expand All @@ -21,6 +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"

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

// setup wheel
// setup wheel and setuptools
try {
setupWheel(foundPython)
await setupPipPackWithPython(foundPython, "setuptools", undefined, true)
await setupPipPackWithPython(foundPython, "wheel", undefined, true)
} catch (err) {
warning(`Failed to install wheels: ${(err as Error).toString()}. Ignoring...`)
warning(`Failed to install setuptools or wheel: ${(err as Error).toString()}. Ignoring...`)
}

return installInfo
Expand Down Expand Up @@ -99,6 +101,11 @@ async function setupPythonSystem(setupDir: string, version: string) {
}
case "darwin": {
installInfo = await setupBrewPack("python3", version)
// add the python and pip binaries to the path
const brewPythonPrefix = await execa("brew", ["--prefix", "python"], { stdio: "pipe" })
const brewPythonBin = join(brewPythonPrefix.stdout, "libexec", "bin")
await addPath(brewPythonBin)

break
}
case "linux": {
Expand All @@ -107,7 +114,7 @@ async function setupPythonSystem(setupDir: string, version: string) {
} else if (hasDnf()) {
installInfo = setupDnfPack("python3", version)
} else if (isUbuntu()) {
installInfo = await setupAptPack([{ name: "python3", version }])
installInfo = await setupAptPack([{ name: "python3", version }, { name: "python-is-python3" }])
} else {
throw new Error("Unsupported linux distributions")
}
Expand Down Expand Up @@ -194,23 +201,23 @@ async function isPipUptoDate(pip: string) {
}

async function setupPip(foundPython: string) {
const upgraded = ensurePipUpgrade(foundPython)
const upgraded = await ensurePipUpgrade(foundPython)
if (!upgraded) {
await setupPipSystem()
// upgrade pip
ensurePipUpgrade(foundPython)
await ensurePipUpgrade(foundPython)
}
}

function ensurePipUpgrade(foundPython: string) {
async function ensurePipUpgrade(foundPython: string) {
try {
execaSync(foundPython, ["-m", "ensurepip", "-U", "--upgrade"], { stdio: "inherit" })
await execa(foundPython, ["-m", "ensurepip", "-U", "--upgrade"], { stdio: "inherit" })
return true
} catch (err1) {
info((err1 as Error)?.toString?.())
try {
// ensure pip is disabled on Ubuntu
execaSync(foundPython, ["-m", "pip", "install", "--upgrade", "pip"], { stdio: "inherit" })
await execa(foundPython, ["-m", "pip", "install", "--upgrade", "pip"], { stdio: "inherit" })
return true
} catch (err2) {
info((err2 as Error)?.toString?.())
Expand All @@ -235,11 +242,6 @@ function setupPipSystem() {
throw new Error(`Could not install pip on ${process.platform}`)
}

/** Install wheel (required for Conan, Meson, etc.) */
function setupWheel(foundPython: string) {
execaSync(foundPython, ["-m", "pip", "install", "-U", "wheel"], { stdio: "inherit" })
}

async function addPythonBaseExecPrefix_raw(python: string) {
const dirs: string[] = []

Expand Down
33 changes: 24 additions & 9 deletions src/utils/setup/setupPipPack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,45 @@ import { addPath } from "../env/addEnv"
import { InstallationInfo } from "./setupBin"
import { getVersion } from "../../versions/versions"
import { ubuntuVersion } from "../env/ubuntu_version"

/* eslint-disable require-atomic-updates */
let python: string | undefined
import memoize from "micro-memoize"

/** A function that installs a package using pip */
export async function setupPipPack(name: string, version?: string): Promise<InstallationInfo> {
export async function setupPipPack(name: string, version?: string, upgrade = false): Promise<InstallationInfo> {
return setupPipPackWithPython(await getPython(), name, version, upgrade)
}

export async function setupPipPackWithPython(
givenPython: string,
name: string,
version?: string,
upgrade = false
): Promise<InstallationInfo> {
info(`Installing ${name} ${version ?? ""} via pip`)

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

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

const execPaths = await addPythonBaseExecPrefix(python)
const execPaths = await addPythonBaseExecPrefix(givenPython)
const binDir = await findBinDir(execPaths, name)

await addPath(binDir)

return { binDir }
}

async function getPython_raw(): Promise<string> {
const pythonBin = (await setupPython(getVersion("python", undefined, await ubuntuVersion()), "", process.arch)).bin
if (pythonBin === undefined) {
throw new Error("Python binary was not found")
}
return pythonBin
}
const getPython = memoize(getPython_raw)

async function findBinDir(dirs: string[], name: string) {
const exists = await Promise.all(dirs.map((dir) => pathExists(join(dir, addExeExt(name)))))
const dirIndex = exists.findIndex((exist) => exist)
Expand Down

0 comments on commit 0ef456c

Please sign in to comment.