mirror of
https://github.com/actions/setup-python.git
synced 2026-01-19 19:38:56 +08:00
resolving comments
This commit is contained in:
parent
952fef3565
commit
c98dcdec10
8 changed files with 61 additions and 52 deletions
|
|
@ -9,7 +9,7 @@ export enum State {
|
|||
|
||||
abstract class CacheDistributor {
|
||||
protected CACHE_KEY_PREFIX = 'setup-python';
|
||||
constructor(protected toolName: string, protected patterns: string) {}
|
||||
constructor(protected toolName: string, protected cacheDependencyPath: string) {}
|
||||
|
||||
protected abstract getCacheGlobalDirectories(): Promise<string[]>;
|
||||
protected abstract computeKeys(): Promise<{
|
||||
|
|
@ -19,22 +19,25 @@ abstract class CacheDistributor {
|
|||
|
||||
public async restoreCache() {
|
||||
const {primaryKey, restoreKey} = await this.computeKeys();
|
||||
const cachePath = await this.getCacheGlobalDirectories();
|
||||
core.saveState(State.CACHE_PATHS, cachePath);
|
||||
core.saveState(State.STATE_CACHE_PRIMARY_KEY, primaryKey);
|
||||
if (primaryKey.endsWith('-')) {
|
||||
throw new Error(
|
||||
`No file in ${process.cwd()} matched to [${this.patterns
|
||||
`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}`);
|
||||
|
|
|
|||
|
|
@ -9,13 +9,13 @@ export enum PackageManagers {
|
|||
export async function getCacheDistributor(
|
||||
packageManager: string,
|
||||
pythonVersion: string,
|
||||
patterns: string | undefined
|
||||
cacheDependencyPath: string | undefined
|
||||
) {
|
||||
switch (packageManager) {
|
||||
case PackageManagers.Pip:
|
||||
return new PipCache(patterns);
|
||||
return new PipCache(cacheDependencyPath);
|
||||
case PackageManagers.Pipenv:
|
||||
return new PipenvCache(pythonVersion, patterns);
|
||||
return new PipenvCache(pythonVersion, cacheDependencyPath);
|
||||
default:
|
||||
throw new Error(`Caching for '${packageManager}' is not supported`);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,14 +8,15 @@ import os from 'os';
|
|||
import CacheDistributor from './cache-distributor';
|
||||
|
||||
class PipCache extends CacheDistributor {
|
||||
constructor(patterns: string = '**/requirements.txt') {
|
||||
super('pip', patterns);
|
||||
constructor(cacheDependencyPath: string = '**/requirements.txt') {
|
||||
super('pip', cacheDependencyPath);
|
||||
}
|
||||
|
||||
protected async getCacheGlobalDirectories() {
|
||||
const {stdout, stderr, exitCode} = await exec.getExecOutput(
|
||||
'pip cache dir'
|
||||
);
|
||||
|
||||
if (stderr) {
|
||||
throw new Error(
|
||||
`Could not get cache folder path for pip package manager`
|
||||
|
|
@ -34,9 +35,10 @@ class PipCache extends CacheDistributor {
|
|||
}
|
||||
|
||||
protected async computeKeys() {
|
||||
const hash = await glob.hashFiles(this.patterns);
|
||||
const hash = await glob.hashFiles(this.cacheDependencyPath);
|
||||
const primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${this.toolName}-${hash}`;
|
||||
const restoreKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${this.toolName}-`;
|
||||
const restoreKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${this.toolName}`;
|
||||
|
||||
return {
|
||||
primaryKey,
|
||||
restoreKey: [restoreKey]
|
||||
|
|
|
|||
|
|
@ -22,10 +22,10 @@ class PipenvCache extends CacheDistributor {
|
|||
}
|
||||
|
||||
protected async getCacheGlobalDirectories() {
|
||||
const cachePath = path.join(os.homedir(), this.getVirtualenvsPath());
|
||||
core.debug(`Pipenv virtualenvs path is ${cachePath}`);
|
||||
const resolvedPath = path.join(os.homedir(), this.getVirtualenvsPath());
|
||||
core.debug(`global cache directory path is ${resolvedPath}`);
|
||||
|
||||
return [cachePath];
|
||||
return [resolvedPath];
|
||||
}
|
||||
|
||||
protected async computeKeys() {
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ async function run() {
|
|||
try {
|
||||
const cache = core.getInput('cache');
|
||||
if (cache) {
|
||||
await saveCache();
|
||||
await saveCache(cache);
|
||||
}
|
||||
} catch (error) {
|
||||
const err = error as Error;
|
||||
|
|
@ -16,14 +16,17 @@ async function run() {
|
|||
}
|
||||
}
|
||||
|
||||
async function saveCache() {
|
||||
const cacheDirPaths = JSON.parse(
|
||||
async function saveCache(packageManager: string) {
|
||||
const cachePaths = JSON.parse(
|
||||
core.getState(State.CACHE_PATHS)
|
||||
) as string[];
|
||||
core.debug(`paths for caching are ${cacheDirPaths.join(', ')}`);
|
||||
if (!isCacheDirectoryExists(cacheDirPaths)) {
|
||||
throw new Error('Cache directories do not exist');
|
||||
|
||||
core.debug(`paths for caching are ${cachePaths.join(', ')}`);
|
||||
|
||||
if (!isCacheDirectoryExists(cachePaths)) {
|
||||
throw new Error(`Cache folder path is retrieved for ${packageManager} but doesn't exist on disk: ${cachePaths.join(', ')}`);
|
||||
}
|
||||
|
||||
const primaryKey = core.getState(State.STATE_CACHE_PRIMARY_KEY);
|
||||
const matchedKey = core.getState(State.CACHE_MATCHED_KEY);
|
||||
|
||||
|
|
@ -37,8 +40,9 @@ async function saveCache() {
|
|||
);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await cache.saveCache(cacheDirPaths, primaryKey);
|
||||
await cache.saveCache(cachePaths, primaryKey);
|
||||
core.info(`Cache saved with the key: ${primaryKey}`);
|
||||
} catch (error) {
|
||||
const err = error as Error;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue