2021-09-15 04:23:45 +08:00
|
|
|
import * as core from "@actions/core"
|
|
|
|
|
import * as finder from "./setup-python/src/find-python"
|
|
|
|
|
import * as finderPyPy from "./setup-python/src/find-pypy"
|
|
|
|
|
import * as path from "path"
|
2021-09-18 20:39:59 +08:00
|
|
|
import { addPath } from "../utils/path/addPath"
|
|
|
|
|
import { setupAptPack } from "../utils/setup/setupAptPack"
|
|
|
|
|
import { setupBrewPack } from "../utils/setup/setupBrewPack"
|
|
|
|
|
import { setupChocoPack } from "../utils/setup/setupChocoPack"
|
|
|
|
|
import hasha from "hasha"
|
|
|
|
|
import { join } from "path"
|
2021-09-18 21:27:06 +08:00
|
|
|
import { isCI } from "../utils/env/isci"
|
2021-09-15 04:23:45 +08:00
|
|
|
|
|
|
|
|
function isPyPyVersion(versionSpec: string) {
|
|
|
|
|
return versionSpec.startsWith("pypy-")
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-18 20:39:59 +08:00
|
|
|
export async function setupPython(version: string, setupCppDir: string, arch: string) {
|
2021-09-18 21:27:06 +08:00
|
|
|
if (!isCI()) {
|
2021-09-18 21:35:41 +08:00
|
|
|
// TODO parse versoin
|
|
|
|
|
return setupPythonViaSystem("", setupCppDir, arch)
|
2021-09-18 20:50:31 +08:00
|
|
|
}
|
|
|
|
|
|
2021-09-15 04:23:45 +08:00
|
|
|
try {
|
2021-09-16 19:57:37 +08:00
|
|
|
if (isPyPyVersion(version)) {
|
|
|
|
|
const installed = await finderPyPy.findPyPyVersion(version, arch)
|
|
|
|
|
core.info(
|
|
|
|
|
`Successfully setup PyPy ${installed.resolvedPyPyVersion} with Python (${installed.resolvedPythonVersion})`
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
const installed = await finder.findPythonVersion(version, arch)
|
|
|
|
|
core.info(`Successfully setup ${installed.impl} (${installed.version})`)
|
2021-09-15 04:23:45 +08:00
|
|
|
}
|
2021-09-18 21:27:17 +08:00
|
|
|
const matchersPath = path.join("setup-pthon", ".github")
|
2021-09-15 04:23:45 +08:00
|
|
|
core.info(`##[add-matcher]${path.join(matchersPath, "python.json")}`)
|
2021-09-18 20:39:59 +08:00
|
|
|
return undefined
|
2021-09-15 04:23:45 +08:00
|
|
|
} catch (err) {
|
2021-09-18 20:50:31 +08:00
|
|
|
return setupPythonViaSystem(version, setupCppDir, arch)
|
2021-09-18 20:39:59 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-18 20:50:31 +08:00
|
|
|
export async function setupPythonViaSystem(version: string, setupCppDir: string, arch: string) {
|
2021-09-18 20:39:59 +08:00
|
|
|
switch (process.platform) {
|
|
|
|
|
case "win32": {
|
|
|
|
|
// Get an unique output directory name from the URL.
|
|
|
|
|
const key: string = await hasha.async(version + arch, { algorithm: "md5" })
|
|
|
|
|
const installDir = join(setupCppDir, key, "python")
|
|
|
|
|
const binDir = installDir
|
|
|
|
|
await setupChocoPack("python3", version, [`/InstallDir:${installDir}`])
|
|
|
|
|
|
|
|
|
|
// Adding the bin dir to the path
|
|
|
|
|
/** The directory which the tool is installed to */
|
|
|
|
|
core.info(`Add ${binDir} to PATH`)
|
2021-09-19 16:49:42 +08:00
|
|
|
addPath(binDir)
|
2021-09-18 20:39:59 +08:00
|
|
|
|
|
|
|
|
return { installDir, binDir }
|
|
|
|
|
}
|
|
|
|
|
case "darwin": {
|
|
|
|
|
return setupBrewPack("python3", version)
|
|
|
|
|
}
|
|
|
|
|
case "linux": {
|
|
|
|
|
const installInfo = await setupAptPack("python3", version)
|
|
|
|
|
await setupAptPack("python3-pip")
|
|
|
|
|
return installInfo
|
|
|
|
|
}
|
|
|
|
|
default: {
|
|
|
|
|
throw new Error(`Unsupported platform`)
|
|
|
|
|
}
|
2021-09-15 04:23:45 +08:00
|
|
|
}
|
|
|
|
|
}
|