setup-cpp/src/python/actions_python.ts

89 lines
3.2 KiB
TypeScript
Raw Normal View History

2022-04-25 07:39:43 +08:00
import { useCpythonVersion } from "setup-python/src/find-python"
import { findPyPyVersion } from "setup-python/src/find-pypy"
import { existsSync } from "fs"
2022-04-19 09:29:17 +08:00
import { info, warning } from "../utils/io/io"
2022-04-25 07:39:43 +08:00
import { debug } from "@actions/core"
import path from "path"
2022-08-08 09:48:41 +08:00
import ciDetect from "@npmcli/ci-detect"
2022-07-06 11:56:51 +08:00
import { isCacheFeatureAvailable, IS_LINUX, IS_WINDOWS } from "setup-python/src/utils"
import { getCacheDistributor } from "setup-python/src/cache-distributions/cache-factory"
2021-09-20 20:24:17 +08:00
function isPyPyVersion(versionSpec: string) {
2022-06-26 08:16:32 +08:00
return versionSpec.startsWith("pypy")
2021-09-20 20:24:17 +08:00
}
2022-07-06 11:56:51 +08:00
/*
function resolveVersionInput(version: string): string {
let versionFile = getInput("python-version-file")
2022-06-26 08:16:32 +08:00
2022-07-06 11:56:51 +08:00
if (version && versionFile) {
warning("Both python-version and python-version-file inputs are specified, only python-version will be used")
}
2022-06-26 08:16:32 +08:00
2022-07-06 11:56:51 +08:00
if (version) {
return version
}
2022-06-26 08:16:32 +08:00
2022-07-06 11:56:51 +08:00
versionFile = versionFile || ".python-version"
if (!existsSync(versionFile)) {
throw new Error(`The specified python version file at: ${versionFile} does not exist`)
}
version = readFileSync(versionFile, "utf8")
info(`Resolved ${versionFile} as ${version}`)
2022-06-26 08:16:32 +08:00
2022-07-06 11:56:51 +08:00
return version
}
*/
export async function cacheDependencies(cache: string, pythonVersion: string) {
const cacheDependencyPath = undefined // core.getInput("cache-dependency-path") || undefined
const cacheDistributor = getCacheDistributor(cache, pythonVersion, cacheDependencyPath)
await cacheDistributor.restoreCache()
}
2022-06-26 08:16:32 +08:00
export async function setupActionsPython(version: string, _setupDir: string, arch: string) {
2022-07-06 11:56:51 +08:00
// According to the README windows binaries do not require to be installed
// in the specific location, but Mac and Linux do
if (!IS_WINDOWS && !process.env.AGENT_TOOLSDIRECTORY?.trim()) {
if (IS_LINUX) {
process.env.AGENT_TOOLSDIRECTORY = "/opt/hostedtoolcache"
} else {
process.env.AGENT_TOOLSDIRECTORY = "/Users/runner/hostedtoolcache"
}
2022-06-26 08:30:17 +08:00
process.env.RUNNER_TOOL_CACHE = process.env.AGENT_TOOLSDIRECTORY
2021-09-20 20:24:17 +08:00
}
2022-07-06 11:56:51 +08:00
debug(`Python is expected to be installed into RUNNER_TOOL_CACHE=${process.env.RUNNER_TOOL_CACHE}`)
2022-06-26 08:16:32 +08:00
// const version = resolveVersionInput(versionGiven)
2022-04-25 07:39:43 +08:00
if (version) {
let pythonVersion: string
if (isPyPyVersion(version)) {
2022-07-06 11:56:51 +08:00
const installed = await findPyPyVersion(version, arch, true)
2022-04-25 07:39:43 +08:00
pythonVersion = `${installed.resolvedPyPyVersion}-${installed.resolvedPythonVersion}`
2022-06-26 08:16:32 +08:00
info(`Successfully set up PyPy ${installed.resolvedPyPyVersion} with Python (${installed.resolvedPythonVersion})`)
2022-04-25 07:39:43 +08:00
} else {
2022-07-06 11:56:51 +08:00
const installed = await useCpythonVersion(version, arch, true)
2022-04-25 07:39:43 +08:00
pythonVersion = installed.version
2022-06-26 08:16:32 +08:00
info(`Successfully set up ${installed.impl} (${pythonVersion})`)
2022-04-25 07:39:43 +08:00
}
2022-07-06 11:56:51 +08:00
if (isCacheFeatureAvailable()) {
const cache = "pip" // core.getInput("cache") // package manager used for caching
await cacheDependencies(cache, pythonVersion)
}
2022-04-19 09:29:17 +08:00
}
2022-08-08 09:48:41 +08:00
if (ciDetect() === "github") {
addPythonLoggingMatcher()
}
2021-09-20 20:24:17 +08:00
return undefined
}
function addPythonLoggingMatcher() {
const matcherPath = path.join(__dirname, "python_matcher.json")
if (!existsSync(matcherPath)) {
return warning("the python_matcher.json file does not exist in the same folder as setup_cpp.js")
}
info(`::add-matcher::${matcherPath}`)
}