mirror of
https://github.com/actions/setup-python.git
synced 2025-06-12 15:27:13 +02:00
Implementation of python's caching (#266)
This commit is contained in:
53
src/cache-distributions/cache-distributor.ts
Normal file
53
src/cache-distributions/cache-distributor.ts
Normal file
@ -0,0 +1,53 @@
|
||||
import * as cache from '@actions/cache';
|
||||
import * as core from '@actions/core';
|
||||
|
||||
export enum State {
|
||||
STATE_CACHE_PRIMARY_KEY = 'cache-primary-key',
|
||||
CACHE_MATCHED_KEY = 'cache-matched-key',
|
||||
CACHE_PATHS = 'cache-paths'
|
||||
}
|
||||
|
||||
abstract class CacheDistributor {
|
||||
protected CACHE_KEY_PREFIX = 'setup-python';
|
||||
constructor(
|
||||
protected packageManager: string,
|
||||
protected cacheDependencyPath: string
|
||||
) {}
|
||||
|
||||
protected abstract getCacheGlobalDirectories(): Promise<string[]>;
|
||||
protected abstract computeKeys(): Promise<{
|
||||
primaryKey: string;
|
||||
restoreKey: string[] | undefined;
|
||||
}>;
|
||||
|
||||
public async restoreCache() {
|
||||
const {primaryKey, restoreKey} = await this.computeKeys();
|
||||
if (primaryKey.endsWith('-')) {
|
||||
throw new Error(
|
||||
`No file in ${process.cwd()} matched to [${this.cacheDependencyPath
|
||||
.split('\n')
|
||||
.join(',')}], make sure you have checked out the target repository`
|
||||
);
|
||||
}
|
||||
|
||||
const cachePath = await this.getCacheGlobalDirectories();
|
||||
|
||||
core.saveState(State.CACHE_PATHS, cachePath);
|
||||
core.saveState(State.STATE_CACHE_PRIMARY_KEY, primaryKey);
|
||||
|
||||
const matchedKey = await cache.restoreCache(
|
||||
cachePath,
|
||||
primaryKey,
|
||||
restoreKey
|
||||
);
|
||||
|
||||
if (matchedKey) {
|
||||
core.saveState(State.CACHE_MATCHED_KEY, matchedKey);
|
||||
core.info(`Cache restored from key: ${matchedKey}`);
|
||||
} else {
|
||||
core.info(`${this.packageManager} cache is not found`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default CacheDistributor;
|
22
src/cache-distributions/cache-factory.ts
Normal file
22
src/cache-distributions/cache-factory.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import PipCache from './pip-cache';
|
||||
import PipenvCache from './pipenv-cache';
|
||||
|
||||
export enum PackageManagers {
|
||||
Pip = 'pip',
|
||||
Pipenv = 'pipenv'
|
||||
}
|
||||
|
||||
export function getCacheDistributor(
|
||||
packageManager: string,
|
||||
pythonVersion: string,
|
||||
cacheDependencyPath: string | undefined
|
||||
) {
|
||||
switch (packageManager) {
|
||||
case PackageManagers.Pip:
|
||||
return new PipCache(cacheDependencyPath);
|
||||
case PackageManagers.Pipenv:
|
||||
return new PipenvCache(pythonVersion, cacheDependencyPath);
|
||||
default:
|
||||
throw new Error(`Caching for '${packageManager}' is not supported`);
|
||||
}
|
||||
}
|
49
src/cache-distributions/pip-cache.ts
Normal file
49
src/cache-distributions/pip-cache.ts
Normal file
@ -0,0 +1,49 @@
|
||||
import * as glob from '@actions/glob';
|
||||
import * as core from '@actions/core';
|
||||
import * as exec from '@actions/exec';
|
||||
|
||||
import * as path from 'path';
|
||||
import os from 'os';
|
||||
|
||||
import CacheDistributor from './cache-distributor';
|
||||
|
||||
class PipCache extends CacheDistributor {
|
||||
constructor(cacheDependencyPath: string = '**/requirements.txt') {
|
||||
super('pip', cacheDependencyPath);
|
||||
}
|
||||
|
||||
protected async getCacheGlobalDirectories() {
|
||||
const {stdout, stderr, exitCode} = await exec.getExecOutput(
|
||||
'pip cache dir'
|
||||
);
|
||||
|
||||
if (exitCode && stderr) {
|
||||
throw new Error(
|
||||
`Could not get cache folder path for pip package manager`
|
||||
);
|
||||
}
|
||||
|
||||
let resolvedPath = stdout.trim();
|
||||
|
||||
if (resolvedPath.includes('~')) {
|
||||
resolvedPath = path.join(os.homedir(), resolvedPath.slice(1));
|
||||
}
|
||||
|
||||
core.debug(`global cache directory path is ${resolvedPath}`);
|
||||
|
||||
return [resolvedPath];
|
||||
}
|
||||
|
||||
protected async computeKeys() {
|
||||
const hash = await glob.hashFiles(this.cacheDependencyPath);
|
||||
const primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${this.packageManager}-${hash}`;
|
||||
const restoreKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${this.packageManager}`;
|
||||
|
||||
return {
|
||||
primaryKey,
|
||||
restoreKey: [restoreKey]
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export default PipCache;
|
44
src/cache-distributions/pipenv-cache.ts
Normal file
44
src/cache-distributions/pipenv-cache.ts
Normal file
@ -0,0 +1,44 @@
|
||||
import * as glob from '@actions/glob';
|
||||
import * as os from 'os';
|
||||
import * as path from 'path';
|
||||
import * as core from '@actions/core';
|
||||
|
||||
import CacheDistributor from './cache-distributor';
|
||||
|
||||
class PipenvCache extends CacheDistributor {
|
||||
constructor(
|
||||
private pythonVersion: string,
|
||||
protected patterns: string = '**/Pipfile.lock'
|
||||
) {
|
||||
super('pipenv', patterns);
|
||||
}
|
||||
|
||||
protected async getCacheGlobalDirectories() {
|
||||
let virtualEnvRelativePath;
|
||||
|
||||
// Default virtualenv directories are hardcoded,
|
||||
// because pipenv is not preinstalled on hosted images and virtualenv is not created:
|
||||
// https://github.com/pypa/pipenv/blob/1daaa0de9a0b00d386c6baeb809d8d4ee6795cfd/pipenv/utils.py#L1990-L2002
|
||||
if (process.platform === 'win32') {
|
||||
virtualEnvRelativePath = '.virtualenvs';
|
||||
} else {
|
||||
virtualEnvRelativePath = '.local/share/virtualenvs';
|
||||
}
|
||||
const resolvedPath = path.join(os.homedir(), virtualEnvRelativePath);
|
||||
core.debug(`global cache directory path is ${resolvedPath}`);
|
||||
|
||||
return [resolvedPath];
|
||||
}
|
||||
|
||||
protected async computeKeys() {
|
||||
const hash = await glob.hashFiles(this.patterns);
|
||||
const primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-python-${this.pythonVersion}-${this.packageManager}-${hash}`;
|
||||
const restoreKey = undefined;
|
||||
return {
|
||||
primaryKey,
|
||||
restoreKey
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export default PipenvCache;
|
Reference in New Issue
Block a user