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

50 lines
1.3 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 {
2021-11-03 13:10:35 +03:00
constructor(cacheDependencyPath: string = '**/requirements.txt') {
super('pip', cacheDependencyPath);
2021-10-27 09:52:29 +03:00
}
protected async getCacheGlobalDirectories() {
const {stdout, stderr, exitCode} = await exec.getExecOutput(
'pip cache dir'
);
2021-11-03 13:10:35 +03:00
2021-10-27 09:52:29 +03:00
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() {
2021-11-03 13:10:35 +03:00
const hash = await glob.hashFiles(this.cacheDependencyPath);
2021-10-27 09:52:29 +03:00
const primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${this.toolName}-${hash}`;
2021-11-03 13:10:35 +03:00
const restoreKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${this.toolName}`;
2021-10-27 09:52:29 +03:00
return {
primaryKey,
restoreKey: [restoreKey]
};
}
}
export default PipCache;