2023-01-27 21:19:31 +00:00
|
|
|
import * as os from 'os';
|
2020-12-17 15:03:54 +00:00
|
|
|
import * as path from 'path';
|
|
|
|
import * as core from '@actions/core';
|
|
|
|
import * as tc from '@actions/tool-cache';
|
|
|
|
import * as semver from 'semver';
|
|
|
|
import * as httpm from '@actions/http-client';
|
|
|
|
import * as exec from '@actions/exec';
|
|
|
|
import fs from 'fs';
|
|
|
|
|
|
|
|
import {
|
|
|
|
IS_WINDOWS,
|
2021-04-12 17:59:38 +00:00
|
|
|
WINDOWS_PLATFORMS,
|
2020-12-17 15:03:54 +00:00
|
|
|
IPyPyManifestRelease,
|
|
|
|
createSymlinkInFolder,
|
|
|
|
isNightlyKeyword,
|
2023-10-10 12:59:54 +00:00
|
|
|
writeExactPyPyVersionFile,
|
|
|
|
getBinaryDirectory
|
2020-12-17 15:03:54 +00:00
|
|
|
} from './utils';
|
|
|
|
|
|
|
|
export async function installPyPy(
|
|
|
|
pypyVersion: string,
|
|
|
|
pythonVersion: string,
|
2022-07-25 14:54:04 +00:00
|
|
|
architecture: string,
|
2023-01-27 21:19:31 +00:00
|
|
|
allowPreReleases: boolean,
|
2022-07-25 14:54:04 +00:00
|
|
|
releases: IPyPyManifestRelease[] | undefined
|
2020-12-17 15:03:54 +00:00
|
|
|
) {
|
|
|
|
let downloadDir;
|
|
|
|
|
2022-07-25 14:54:04 +00:00
|
|
|
releases = releases ?? (await getAvailablePyPyVersions());
|
|
|
|
|
2020-12-17 15:03:54 +00:00
|
|
|
if (!releases || releases.length === 0) {
|
|
|
|
throw new Error('No release was found in PyPy version.json');
|
|
|
|
}
|
|
|
|
|
2023-01-27 21:19:31 +00:00
|
|
|
let releaseData = findRelease(
|
2020-12-17 15:03:54 +00:00
|
|
|
releases,
|
|
|
|
pythonVersion,
|
|
|
|
pypyVersion,
|
2023-01-27 21:19:31 +00:00
|
|
|
architecture,
|
|
|
|
false
|
2020-12-17 15:03:54 +00:00
|
|
|
);
|
|
|
|
|
2023-01-27 21:19:31 +00:00
|
|
|
if (allowPreReleases && (!releaseData || !releaseData.foundAsset)) {
|
|
|
|
// check for pre-release
|
|
|
|
core.info(
|
|
|
|
[
|
|
|
|
`Stable PyPy version ${pythonVersion} (${pypyVersion}) with arch ${architecture} not found`,
|
|
|
|
`Trying pre-release versions`
|
|
|
|
].join(os.EOL)
|
|
|
|
);
|
|
|
|
releaseData = findRelease(
|
|
|
|
releases,
|
|
|
|
pythonVersion,
|
|
|
|
pypyVersion,
|
|
|
|
architecture,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-12-17 15:03:54 +00:00
|
|
|
if (!releaseData || !releaseData.foundAsset) {
|
|
|
|
throw new Error(
|
|
|
|
`PyPy version ${pythonVersion} (${pypyVersion}) with arch ${architecture} not found`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const {foundAsset, resolvedPythonVersion, resolvedPyPyVersion} = releaseData;
|
2023-03-09 10:44:56 +00:00
|
|
|
const downloadUrl = `${foundAsset.download_url}`;
|
2020-12-17 15:03:54 +00:00
|
|
|
|
|
|
|
core.info(`Downloading PyPy from "${downloadUrl}" ...`);
|
|
|
|
|
2022-10-24 09:10:18 +00:00
|
|
|
try {
|
|
|
|
const pypyPath = await tc.downloadTool(downloadUrl);
|
|
|
|
|
|
|
|
core.info('Extracting downloaded archive...');
|
|
|
|
if (IS_WINDOWS) {
|
|
|
|
downloadDir = await tc.extractZip(pypyPath);
|
|
|
|
} else {
|
|
|
|
downloadDir = await tc.extractTar(pypyPath, undefined, 'x');
|
|
|
|
}
|
|
|
|
|
|
|
|
// root folder in archive can have unpredictable name so just take the first folder
|
|
|
|
// downloadDir is unique folder under TEMP and can't contain any other folders
|
|
|
|
const archiveName = fs.readdirSync(downloadDir)[0];
|
|
|
|
|
|
|
|
const toolDir = path.join(downloadDir, archiveName);
|
|
|
|
let installDir = toolDir;
|
|
|
|
if (!isNightlyKeyword(resolvedPyPyVersion)) {
|
|
|
|
installDir = await tc.cacheDir(
|
|
|
|
toolDir,
|
|
|
|
'PyPy',
|
|
|
|
resolvedPythonVersion,
|
|
|
|
architecture
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
writeExactPyPyVersionFile(installDir, resolvedPyPyVersion);
|
|
|
|
|
2023-10-10 12:59:54 +00:00
|
|
|
const binaryPath = getBinaryDirectory(installDir);
|
2022-10-24 09:10:18 +00:00
|
|
|
await createPyPySymlink(binaryPath, resolvedPythonVersion);
|
|
|
|
await installPip(binaryPath);
|
|
|
|
|
|
|
|
return {installDir, resolvedPythonVersion, resolvedPyPyVersion};
|
|
|
|
} catch (err) {
|
|
|
|
if (err instanceof Error) {
|
|
|
|
// Rate limit?
|
|
|
|
if (
|
|
|
|
err instanceof tc.HTTPError &&
|
|
|
|
(err.httpStatusCode === 403 || err.httpStatusCode === 429)
|
|
|
|
) {
|
|
|
|
core.info(
|
|
|
|
`Received HTTP status code ${err.httpStatusCode}. This usually indicates the rate limit has been exceeded`
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
core.info(err.message);
|
|
|
|
}
|
|
|
|
if (err.stack !== undefined) {
|
|
|
|
core.debug(err.stack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw err;
|
2020-12-17 15:03:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-25 14:54:04 +00:00
|
|
|
export async function getAvailablePyPyVersions() {
|
2020-12-17 15:03:54 +00:00
|
|
|
const url = 'https://downloads.python.org/pypy/versions.json';
|
|
|
|
const http: httpm.HttpClient = new httpm.HttpClient('tool-cache');
|
|
|
|
|
|
|
|
const response = await http.getJson<IPyPyManifestRelease[]>(url);
|
|
|
|
if (!response.result) {
|
|
|
|
throw new Error(
|
|
|
|
`Unable to retrieve the list of available PyPy versions from '${url}'`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.result;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function createPyPySymlink(
|
|
|
|
pypyBinaryPath: string,
|
|
|
|
pythonVersion: string
|
|
|
|
) {
|
|
|
|
const version = semver.coerce(pythonVersion)!;
|
|
|
|
const pythonBinaryPostfix = semver.major(version);
|
2022-04-28 13:26:17 +00:00
|
|
|
const pythonMinor = semver.minor(version);
|
2020-12-17 15:03:54 +00:00
|
|
|
const pypyBinaryPostfix = pythonBinaryPostfix === 2 ? '' : '3';
|
2022-04-28 13:26:17 +00:00
|
|
|
const pypyMajorMinorBinaryPostfix = `${pythonBinaryPostfix}.${pythonMinor}`;
|
2023-03-09 10:44:56 +00:00
|
|
|
const binaryExtension = IS_WINDOWS ? '.exe' : '';
|
2020-12-17 15:03:54 +00:00
|
|
|
|
|
|
|
core.info('Creating symlinks...');
|
|
|
|
createSymlinkInFolder(
|
|
|
|
pypyBinaryPath,
|
|
|
|
`pypy${pypyBinaryPostfix}${binaryExtension}`,
|
|
|
|
`python${pythonBinaryPostfix}${binaryExtension}`,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
createSymlinkInFolder(
|
|
|
|
pypyBinaryPath,
|
|
|
|
`pypy${pypyBinaryPostfix}${binaryExtension}`,
|
|
|
|
`python${binaryExtension}`,
|
|
|
|
true
|
|
|
|
);
|
2022-04-28 13:26:17 +00:00
|
|
|
|
|
|
|
createSymlinkInFolder(
|
|
|
|
pypyBinaryPath,
|
|
|
|
`pypy${pypyBinaryPostfix}${binaryExtension}`,
|
|
|
|
`pypy${pypyMajorMinorBinaryPostfix}${binaryExtension}`,
|
|
|
|
true
|
|
|
|
);
|
2020-12-17 15:03:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function installPip(pythonLocation: string) {
|
|
|
|
core.info('Installing and updating pip');
|
|
|
|
const pythonBinary = path.join(pythonLocation, 'python');
|
|
|
|
await exec.exec(`${pythonBinary} -m ensurepip`);
|
|
|
|
|
|
|
|
await exec.exec(
|
|
|
|
`${pythonLocation}/python -m pip install --ignore-installed pip`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function findRelease(
|
|
|
|
releases: IPyPyManifestRelease[],
|
|
|
|
pythonVersion: string,
|
|
|
|
pypyVersion: string,
|
2023-01-27 21:19:31 +00:00
|
|
|
architecture: string,
|
|
|
|
includePrerelease: boolean
|
2020-12-17 15:03:54 +00:00
|
|
|
) {
|
2023-01-27 21:19:31 +00:00
|
|
|
const options = {includePrerelease: includePrerelease};
|
2020-12-17 15:03:54 +00:00
|
|
|
const filterReleases = releases.filter(item => {
|
|
|
|
const isPythonVersionSatisfied = semver.satisfies(
|
|
|
|
semver.coerce(item.python_version)!,
|
|
|
|
pythonVersion
|
|
|
|
);
|
|
|
|
const isPyPyNightly =
|
|
|
|
isNightlyKeyword(pypyVersion) && isNightlyKeyword(item.pypy_version);
|
|
|
|
const isPyPyVersionSatisfied =
|
|
|
|
isPyPyNightly ||
|
2023-01-27 21:19:31 +00:00
|
|
|
semver.satisfies(
|
|
|
|
pypyVersionToSemantic(item.pypy_version),
|
|
|
|
pypyVersion,
|
|
|
|
options
|
|
|
|
);
|
2020-12-17 15:03:54 +00:00
|
|
|
const isArchPresent =
|
|
|
|
item.files &&
|
2021-04-12 17:59:38 +00:00
|
|
|
(IS_WINDOWS
|
2022-10-18 09:01:15 +00:00
|
|
|
? isArchPresentForWindows(item, architecture)
|
2021-04-12 17:59:38 +00:00
|
|
|
: isArchPresentForMacOrLinux(item, architecture, process.platform));
|
2020-12-17 15:03:54 +00:00
|
|
|
return isPythonVersionSatisfied && isPyPyVersionSatisfied && isArchPresent;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (filterReleases.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const sortedReleases = filterReleases.sort((previous, current) => {
|
|
|
|
return (
|
|
|
|
semver.compare(
|
|
|
|
semver.coerce(pypyVersionToSemantic(current.pypy_version))!,
|
|
|
|
semver.coerce(pypyVersionToSemantic(previous.pypy_version))!
|
|
|
|
) ||
|
|
|
|
semver.compare(
|
|
|
|
semver.coerce(current.python_version)!,
|
|
|
|
semver.coerce(previous.python_version)!
|
|
|
|
)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
const foundRelease = sortedReleases[0];
|
2021-04-12 17:59:38 +00:00
|
|
|
const foundAsset = IS_WINDOWS
|
2022-10-18 09:01:15 +00:00
|
|
|
? findAssetForWindows(foundRelease, architecture)
|
2021-04-12 17:59:38 +00:00
|
|
|
: findAssetForMacOrLinux(foundRelease, architecture, process.platform);
|
2020-12-17 15:03:54 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
foundAsset,
|
|
|
|
resolvedPythonVersion: foundRelease.python_version,
|
2023-02-20 10:28:16 +00:00
|
|
|
resolvedPyPyVersion: foundRelease.pypy_version.trim()
|
2020-12-17 15:03:54 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function pypyVersionToSemantic(versionSpec: string) {
|
|
|
|
const prereleaseVersion = /(\d+\.\d+\.\d+)((?:a|b|rc))(\d*)/g;
|
|
|
|
return versionSpec.replace(prereleaseVersion, '$1-$2.$3');
|
|
|
|
}
|
2021-04-12 17:59:38 +00:00
|
|
|
|
2022-10-18 09:01:15 +00:00
|
|
|
export function isArchPresentForWindows(item: any, architecture: string) {
|
|
|
|
architecture = replaceX32toX86(architecture);
|
2021-04-12 17:59:38 +00:00
|
|
|
return item.files.some(
|
|
|
|
(file: any) =>
|
2022-10-18 09:01:15 +00:00
|
|
|
WINDOWS_PLATFORMS.includes(file.platform) && file.arch === architecture
|
2021-04-12 17:59:38 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isArchPresentForMacOrLinux(
|
|
|
|
item: any,
|
|
|
|
architecture: string,
|
|
|
|
platform: string
|
|
|
|
) {
|
|
|
|
return item.files.some(
|
|
|
|
(file: any) => file.arch === architecture && file.platform === platform
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-10-18 09:01:15 +00:00
|
|
|
export function findAssetForWindows(releases: any, architecture: string) {
|
|
|
|
architecture = replaceX32toX86(architecture);
|
2021-04-12 17:59:38 +00:00
|
|
|
return releases.files.find(
|
|
|
|
(item: any) =>
|
2022-10-18 09:01:15 +00:00
|
|
|
WINDOWS_PLATFORMS.includes(item.platform) && item.arch === architecture
|
2021-04-12 17:59:38 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function findAssetForMacOrLinux(
|
|
|
|
releases: any,
|
|
|
|
architecture: string,
|
|
|
|
platform: string
|
|
|
|
) {
|
|
|
|
return releases.files.find(
|
|
|
|
(item: any) => item.arch === architecture && item.platform === platform
|
|
|
|
);
|
|
|
|
}
|
2022-10-18 09:01:15 +00:00
|
|
|
|
|
|
|
function replaceX32toX86(architecture: string): string {
|
|
|
|
// convert x32 to x86 because os.arch() returns x32 for 32-bit systems but PyPy releases json has x86 arch value.
|
|
|
|
if (architecture === 'x32') {
|
|
|
|
architecture = 'x86';
|
|
|
|
}
|
|
|
|
return architecture;
|
|
|
|
}
|