fix: use input python version if compatible

This commit is contained in:
Frost Ming 2022-07-21 22:12:25 +08:00
parent 06d2d28e10
commit 1912606aa4
No known key found for this signature in database
GPG Key ID: 5BFA9CB4DDA943BF
4 changed files with 988 additions and 75 deletions

View File

@ -11,7 +11,7 @@ jobs:
os: ["windows-latest", "ubuntu-latest"]
name: Test the action
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Setup PDM
uses: ./
with:

957
dist/setup-pdm.js vendored

File diff suppressed because it is too large Load Diff

View File

@ -13,6 +13,7 @@
"dependencies": {
"@actions/core": "^1.2.6",
"@actions/exec": "^1.0.4",
"semver": "^7.3.7",
"setup-python": "actions/setup-python"
},
"devDependencies": {

View File

@ -1,79 +1,60 @@
import * as core from "@actions/core";
import * as exec from "@actions/exec";
import * as setupPython from "setup-python/src/find-python";
import { IS_WINDOWS } from "setup-python/src/utils";
import * as os from "os";
import { exec as execChild } from "child_process";
import { promises as fs } from "fs";
import path from "path";
import * as core from '@actions/core'
import * as exec from '@actions/exec'
import * as setupPython from 'setup-python/src/find-python'
import { IS_WINDOWS } from 'setup-python/src/utils'
import * as os from 'os'
import { promises as fs } from 'fs'
import path from 'path'
import semParse from 'semver/functions/parse'
import semIntersect from 'semver/ranges/intersects'
const INSTALL_VERSION = "3.8";
const GITHUB_REPO = "https://github.com/pdm-project/pdm.git";
const PDM_PYTHON_REQUIRES = '>=3.7'
const FALLBACK_INSTALL_VERSION = '3.10'
const GITHUB_REPO = 'https://github.com/pdm-project/pdm.git'
function getPep582Path(): string {
const installDir = process.env.pythonLocation || "";
function getPep582Path(version: string): string {
const installDir = process.env.pythonLocation || ''
const parsedVersion = semParse(version)!
if (IS_WINDOWS) {
return path.resolve(installDir, "Lib/site-packages/pdm/pep582");
return path.resolve(installDir, 'Lib/site-packages/pdm/pep582')
} else {
return path.resolve(
installDir,
"lib",
`python${INSTALL_VERSION}`,
"site-packages/pdm/pep582"
);
return path.resolve(installDir, 'lib', `python${parsedVersion.major}.${parsedVersion.minor}`, 'site-packages/pdm/pep582')
}
}
async function run(): Promise<void> {
const arch = core.getInput("architecture") || os.arch();
const pdmVersion = core.getInput("version");
const ref = core.getInput("ref");
const pdmPackage = pdmVersion
? `pdm==${pdmVersion}`
: ref
? `pdm @ git+${GITHUB_REPO}@${ref}`
: "pdm";
const cmdArgs = ["-m", "pip", "install", "-U", pdmPackage];
if (core.getInput("prerelease") === "true") {
cmdArgs.push("--pre");
const arch = core.getInput('architecture') || os.arch()
const pdmVersion = core.getInput('version')
const pythonVersion = core.getInput('python-version')
const versionCompatible = semIntersect(PDM_PYTHON_REQUIRES, pythonVersion)
const ref = core.getInput('ref')
const pdmPackage = pdmVersion ? `pdm==${pdmVersion}` : ref ? `pdm @ git+${GITHUB_REPO}@${ref}` : 'pdm'
const cmdArgs = ['-m', 'pip', 'install', '-U', pdmPackage]
if (core.getInput('prerelease') === 'true') {
cmdArgs.push('--pre')
}
try {
let installedPython = await setupPython.findPythonVersion(
INSTALL_VERSION,
arch
);
await exec.exec("python", cmdArgs);
if (core.getInput("enable-pep582") === "true") {
core.exportVariable("PYTHONPATH", getPep582Path());
let installedPython = await setupPython.findPythonVersion(versionCompatible ? pythonVersion : FALLBACK_INSTALL_VERSION, arch)
await exec.exec('python', cmdArgs)
if (core.getInput('enable-pep582') === 'true') {
core.exportVariable('PYTHONPATH', getPep582Path(installedPython.version))
}
if (core.getInput("python-version") !== INSTALL_VERSION) {
installedPython = await setupPython.findPythonVersion(
core.getInput("python-version"),
arch
);
if (!versionCompatible) {
installedPython = await setupPython.findPythonVersion(pythonVersion, arch)
}
const pythonBin = path
.join(
process.env.pythonLocation as string,
IS_WINDOWS ? "python.exe" : "bin/python"
)
.replace(/\\/g, "/");
await fs.writeFile(".pdm.toml", `[python]\npath="${pythonBin}"\n`);
const pdmVersionOutput = (await execChild("pdm --version")).stdout;
if (process.platform === "linux") {
const pythonBin = path.join(process.env.pythonLocation as string, IS_WINDOWS ? 'python.exe' : 'bin/python').replace(/\\/g, '/')
await fs.writeFile('.pdm.toml', `[python]\npath="${pythonBin}"\n`)
const { stdout: pdmVersionOutput } = await exec.getExecOutput('pdm --version')
if (process.platform === 'linux') {
// See https://github.com/actions/virtual-environments/issues/2803
core.exportVariable("LD_PRELOAD", "/lib/x86_64-linux-gnu/libgcc_s.so.1");
core.exportVariable('LD_PRELOAD', '/lib/x86_64-linux-gnu/libgcc_s.so.1')
}
core.info(
`Successfully setup ${
pdmVersionOutput && pdmVersionOutput.read()
} with Python ${installedPython.version}`
);
const matchersPath = path.join(__dirname, "..", ".github");
core.info(`##[add-matcher]${path.join(matchersPath, "python.json")}`);
core.info(`Successfully setup ${pdmVersionOutput} with Python ${installedPython.version}`)
const matchersPath = path.join(__dirname, '..', '.github')
core.info(`##[add-matcher]${path.join(matchersPath, 'python.json')}`)
} catch (error: any) {
core.setFailed(error.message);
core.setFailed(error.message)
}
}
run();
run()