setup-python/src/install-python.ts

170 lines
5.1 KiB
TypeScript
Raw Normal View History

import * as path from 'path';
import * as core from '@actions/core';
import * as tc from '@actions/tool-cache';
import * as exec from '@actions/exec';
import * as httpm from '@actions/http-client';
import {ExecOptions} from '@actions/exec/lib/interfaces';
import {IS_WINDOWS, IS_LINUX, getDownloadFileName} from './utils';
2025-04-04 03:11:43 +00:00
import {IToolRelease} from '@actions/tool-cache';
2020-05-20 13:35:09 +00:00
const TOKEN = core.getInput('token');
const AUTH = !TOKEN ? undefined : `token ${TOKEN}`;
const MANIFEST_REPO_OWNER = 'actions';
const MANIFEST_REPO_NAME = 'python-versions';
2020-07-15 09:26:28 +00:00
const MANIFEST_REPO_BRANCH = 'main';
export const MANIFEST_URL = `https://raw.githubusercontent.com/${MANIFEST_REPO_OWNER}/${MANIFEST_REPO_NAME}/${MANIFEST_REPO_BRANCH}/versions-manifest.json`;
export async function findReleaseFromManifest(
semanticVersionSpec: string,
2022-07-25 14:54:04 +00:00
architecture: string,
manifest: tc.IToolRelease[] | null
): Promise<tc.IToolRelease | undefined> {
2022-07-25 14:54:04 +00:00
if (!manifest) {
manifest = await getManifest();
}
const foundRelease = await tc.findFromManifest(
semanticVersionSpec,
false,
manifest,
architecture
);
2022-07-25 14:54:04 +00:00
return foundRelease;
}
2025-04-04 03:11:43 +00:00
function isIToolRelease(obj: any): obj is IToolRelease {
return (
typeof obj === 'object' &&
obj !== null &&
typeof obj.version === 'string' &&
typeof obj.stable === 'boolean' &&
Array.isArray(obj.files) &&
obj.files.every(
(file: any) =>
typeof file.filename === 'string' &&
typeof file.platform === 'string' &&
typeof file.arch === 'string' &&
typeof file.download_url === 'string'
)
);
}
export async function getManifest(): Promise<tc.IToolRelease[]> {
try {
2025-04-04 03:11:43 +00:00
const repoManifest = await getManifestFromRepo();
if (
Array.isArray(repoManifest) &&
repoManifest.length &&
repoManifest.every(isIToolRelease)
) {
return repoManifest;
}
2025-04-09 09:42:48 +00:00
throw new Error(
'The repository manifest is invalid or does not include any valid tool release (IToolRelease) entries.'
);
} catch (err) {
2025-04-14 02:42:58 +00:00
core.debug('Failed to fetch the manifest from the repository API.');
if (err instanceof Error) {
2025-04-14 02:42:58 +00:00
core.debug(`Error message: ${err.message}`);
2025-04-04 03:11:43 +00:00
core.debug(`Error stack: ${err.stack}`);
} else {
2025-04-09 09:42:48 +00:00
core.error('An unexpected error occurred while fetching the manifest.');
}
}
return await getManifestFromURL();
}
export function getManifestFromRepo(): Promise<tc.IToolRelease[]> {
2025-04-04 03:25:02 +00:00
core.info(
2022-07-25 14:54:04 +00:00
`Getting manifest from ${MANIFEST_REPO_OWNER}/${MANIFEST_REPO_NAME}@${MANIFEST_REPO_BRANCH}`
);
return tc.getManifestFromRepo(
MANIFEST_REPO_OWNER,
MANIFEST_REPO_NAME,
AUTH,
MANIFEST_REPO_BRANCH
);
}
export async function getManifestFromURL(): Promise<tc.IToolRelease[]> {
2025-04-04 03:25:02 +00:00
core.info('Falling back to fetching the manifest using raw URL.');
const http: httpm.HttpClient = new httpm.HttpClient('tool-cache');
const response = await http.getJson<tc.IToolRelease[]>(MANIFEST_URL);
if (!response.result) {
2025-04-04 03:25:02 +00:00
throw new Error(
`Unable to get manifest from ${MANIFEST_URL}. HTTP status: ${response.statusCode}`
);
}
return response.result;
}
async function installPython(workingDirectory: string) {
const options: ExecOptions = {
cwd: workingDirectory,
env: {
...process.env,
...(IS_LINUX && {LD_LIBRARY_PATH: path.join(workingDirectory, 'lib')})
},
silent: true,
listeners: {
stdout: (data: Buffer) => {
core.info(data.toString().trim());
},
stderr: (data: Buffer) => {
core.error(data.toString().trim());
}
}
};
if (IS_WINDOWS) {
await exec.exec('powershell', ['./setup.ps1'], options);
} else {
await exec.exec('bash', ['./setup.sh'], options);
}
}
export async function installCpythonFromRelease(release: tc.IToolRelease) {
2025-04-04 03:25:02 +00:00
if (!release.files || release.files.length === 0) {
throw new Error('No files found in the release to download.');
}
const downloadUrl = release.files[0].download_url;
core.info(`Download from "${downloadUrl}"`);
2022-10-24 09:10:18 +00:00
let pythonPath = '';
try {
const fileName = getDownloadFileName(downloadUrl);
pythonPath = await tc.downloadTool(downloadUrl, fileName, AUTH);
2022-10-24 09:10:18 +00:00
core.info('Extract downloaded archive');
let pythonExtractedFolder;
if (IS_WINDOWS) {
pythonExtractedFolder = await tc.extractZip(pythonPath);
} else {
pythonExtractedFolder = await tc.extractTar(pythonPath);
}
2022-10-24 09:10:18 +00:00
core.info('Execute installation script');
await installPython(pythonExtractedFolder);
} catch (err) {
if (err instanceof tc.HTTPError) {
// Rate limit?
2025-04-09 09:42:48 +00:00
if (err.httpStatusCode === 403) {
core.error(
`Received HTTP status code 403 (Forbidden). This usually indicates that the request is not authorized. Please check your credentials or permissions.`
);
} else if (err.httpStatusCode === 429) {
2025-04-10 03:08:23 +00:00
core.info(
2025-04-09 09:42:48 +00:00
`Received HTTP status code 429 (Too Many Requests). This usually indicates that the rate limit has been exceeded. Please wait and try again later.`
2022-10-24 09:10:18 +00:00
);
} else {
2025-04-04 03:25:02 +00:00
core.error(err.message);
2022-10-24 09:10:18 +00:00
}
if (err.stack) {
core.debug(err.stack);
}
}
throw err;
}
}