setup-python/src/cache-distributions/pip-cache.ts

48 lines
1.2 KiB
TypeScript
Raw Normal View History

2021-10-27 09:52:29 +03:00
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(patterns: string = '**/requirements.txt') {
super('pip', patterns);
}
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`
);
}
let resolvedPath = stdout.trim();
if (resolvedPath.includes('~')) {
resolvedPath = path.join(os.homedir(), resolvedPath.slice(1));
}
core.info(`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']}-${this.toolName}-${hash}`;
const restoreKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${this.toolName}-`;
return {
primaryKey,
restoreKey: [restoreKey]
};
}
}
export default PipCache;