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

52 lines
1.4 KiB
TypeScript
Raw Normal View History

2021-09-17 10:17:17 +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 * as os from 'os';
import CacheDistributor, {IPackageManager} from './cache-distributor';
2021-09-14 17:44:36 +03:00
class PipCache extends CacheDistributor {
2021-09-17 10:17:17 +03:00
constructor(info: IPackageManager) {
2021-09-14 17:44:36 +03:00
super({
2021-09-17 10:17:17 +03:00
patterns:
info.patterns.length == 0 ? ['**/requirements.txt'] : info.patterns,
toolName: info.toolName
2021-09-14 17:44:36 +03:00
});
}
2021-09-17 10:17:17 +03:00
protected async getCacheGlobalDirectories() {
const {stdout, stderr, exitCode} = await exec.getExecOutput(
'pip cache dir'
);
if (stderr) {
throw new Error(
`failed to procceed with caching with error: ${exitCode}`
);
}
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.packageManager.patterns.join('\n'));
const primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${this.packageManager.toolName}-${hash}`;
const restoreKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${this.packageManager.toolName}-`;
return {
primaryKey,
restoreKey
};
}
2021-09-14 17:44:36 +03:00
}
export default PipCache;