resolving comments

This commit is contained in:
Dmitry Shibanov 2021-11-03 13:10:35 +03:00
parent 952fef3565
commit c98dcdec10
8 changed files with 61 additions and 52 deletions

View file

@ -15,7 +15,7 @@ inputs:
description: Used to pull python distributions from actions/python-versions. Since there's a default, this is typically not supplied by the user. description: Used to pull python distributions from actions/python-versions. Since there's a default, this is typically not supplied by the user.
default: ${{ github.token }} default: ${{ github.token }}
cache-dependency-path: cache-dependency-path:
description: 'Used to specify a package manager for caching in the default directory. Supported values: pip, pipenv' description: 'Used to specify the path to a dependency files. Supports wildcards or a list of file names for caching multiple dependencies.'
outputs: outputs:
python-version: python-version:
description: "The installed python version. Useful when given a version range as input." description: "The installed python version. Useful when given a version range as input."

View file

@ -37180,22 +37180,22 @@ var State;
State["CACHE_PATHS"] = "cache-paths"; State["CACHE_PATHS"] = "cache-paths";
})(State = exports.State || (exports.State = {})); })(State = exports.State || (exports.State = {}));
class CacheDistributor { class CacheDistributor {
constructor(toolName, patterns) { constructor(toolName, cacheDependencyPath) {
this.toolName = toolName; this.toolName = toolName;
this.patterns = patterns; this.cacheDependencyPath = cacheDependencyPath;
this.CACHE_KEY_PREFIX = 'setup-python'; this.CACHE_KEY_PREFIX = 'setup-python';
} }
restoreCache() { restoreCache() {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
const { primaryKey, restoreKey } = yield this.computeKeys(); const { primaryKey, restoreKey } = yield this.computeKeys();
const cachePath = yield this.getCacheGlobalDirectories();
core.saveState(State.CACHE_PATHS, cachePath);
core.saveState(State.STATE_CACHE_PRIMARY_KEY, primaryKey);
if (primaryKey.endsWith('-')) { if (primaryKey.endsWith('-')) {
throw new Error(`No file in ${process.cwd()} matched to [${this.patterns throw new Error(`No file in ${process.cwd()} matched to [${this.cacheDependencyPath
.split('\n') .split('\n')
.join(',')}], make sure you have checked out the target repository`); .join(',')}], make sure you have checked out the target repository`);
} }
const cachePath = yield this.getCacheGlobalDirectories();
core.saveState(State.CACHE_PATHS, cachePath);
core.saveState(State.STATE_CACHE_PRIMARY_KEY, primaryKey);
const matchedKey = yield cache.restoreCache(cachePath, primaryKey, restoreKey); const matchedKey = yield cache.restoreCache(cachePath, primaryKey, restoreKey);
if (matchedKey) { if (matchedKey) {
core.saveState(State.CACHE_MATCHED_KEY, matchedKey); core.saveState(State.CACHE_MATCHED_KEY, matchedKey);
@ -45796,7 +45796,7 @@ function run() {
try { try {
const cache = core.getInput('cache'); const cache = core.getInput('cache');
if (cache) { if (cache) {
yield saveCache(); yield saveCache(cache);
} }
} }
catch (error) { catch (error) {
@ -45805,12 +45805,12 @@ function run() {
} }
}); });
} }
function saveCache() { function saveCache(packageManager) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
const cacheDirPaths = JSON.parse(core.getState(cache_distributor_1.State.CACHE_PATHS)); const cachePaths = JSON.parse(core.getState(cache_distributor_1.State.CACHE_PATHS));
core.debug(`paths for caching are ${cacheDirPaths.join(', ')}`); core.debug(`paths for caching are ${cachePaths.join(', ')}`);
if (!isCacheDirectoryExists(cacheDirPaths)) { if (!isCacheDirectoryExists(cachePaths)) {
throw new Error('Cache directories do not exist'); throw new Error(`Cache folder path is retrieved for ${packageManager} but doesn't exist on disk: ${cachePaths.join(', ')}`);
} }
const primaryKey = core.getState(cache_distributor_1.State.STATE_CACHE_PRIMARY_KEY); const primaryKey = core.getState(cache_distributor_1.State.STATE_CACHE_PRIMARY_KEY);
const matchedKey = core.getState(cache_distributor_1.State.CACHE_MATCHED_KEY); const matchedKey = core.getState(cache_distributor_1.State.CACHE_MATCHED_KEY);
@ -45824,7 +45824,7 @@ function saveCache() {
return; return;
} }
try { try {
yield cache.saveCache(cacheDirPaths, primaryKey); yield cache.saveCache(cachePaths, primaryKey);
core.info(`Cache saved with the key: ${primaryKey}`); core.info(`Cache saved with the key: ${primaryKey}`);
} }
catch (error) { catch (error) {

32
dist/setup/index.js vendored
View file

@ -7156,9 +7156,9 @@ class PipenvCache extends cache_distributor_1.default {
} }
getCacheGlobalDirectories() { getCacheGlobalDirectories() {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
const cachePath = path.join(os.homedir(), this.getVirtualenvsPath()); const resolvedPath = path.join(os.homedir(), this.getVirtualenvsPath());
core.debug(`Pipenv virtualenvs path is ${cachePath}`); core.debug(`global cache directory path is ${resolvedPath}`);
return [cachePath]; return [resolvedPath];
}); });
} }
computeKeys() { computeKeys() {
@ -34460,8 +34460,8 @@ const path = __importStar(__webpack_require__(622));
const os_1 = __importDefault(__webpack_require__(87)); const os_1 = __importDefault(__webpack_require__(87));
const cache_distributor_1 = __importDefault(__webpack_require__(435)); const cache_distributor_1 = __importDefault(__webpack_require__(435));
class PipCache extends cache_distributor_1.default { class PipCache extends cache_distributor_1.default {
constructor(patterns = '**/requirements.txt') { constructor(cacheDependencyPath = '**/requirements.txt') {
super('pip', patterns); super('pip', cacheDependencyPath);
} }
getCacheGlobalDirectories() { getCacheGlobalDirectories() {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
@ -34479,9 +34479,9 @@ class PipCache extends cache_distributor_1.default {
} }
computeKeys() { computeKeys() {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
const hash = yield glob.hashFiles(this.patterns); const hash = yield glob.hashFiles(this.cacheDependencyPath);
const primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${this.toolName}-${hash}`; 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 { return {
primaryKey, primaryKey,
restoreKey: [restoreKey] restoreKey: [restoreKey]
@ -35424,22 +35424,22 @@ var State;
State["CACHE_PATHS"] = "cache-paths"; State["CACHE_PATHS"] = "cache-paths";
})(State = exports.State || (exports.State = {})); })(State = exports.State || (exports.State = {}));
class CacheDistributor { class CacheDistributor {
constructor(toolName, patterns) { constructor(toolName, cacheDependencyPath) {
this.toolName = toolName; this.toolName = toolName;
this.patterns = patterns; this.cacheDependencyPath = cacheDependencyPath;
this.CACHE_KEY_PREFIX = 'setup-python'; this.CACHE_KEY_PREFIX = 'setup-python';
} }
restoreCache() { restoreCache() {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
const { primaryKey, restoreKey } = yield this.computeKeys(); const { primaryKey, restoreKey } = yield this.computeKeys();
const cachePath = yield this.getCacheGlobalDirectories();
core.saveState(State.CACHE_PATHS, cachePath);
core.saveState(State.STATE_CACHE_PRIMARY_KEY, primaryKey);
if (primaryKey.endsWith('-')) { if (primaryKey.endsWith('-')) {
throw new Error(`No file in ${process.cwd()} matched to [${this.patterns throw new Error(`No file in ${process.cwd()} matched to [${this.cacheDependencyPath
.split('\n') .split('\n')
.join(',')}], make sure you have checked out the target repository`); .join(',')}], make sure you have checked out the target repository`);
} }
const cachePath = yield this.getCacheGlobalDirectories();
core.saveState(State.CACHE_PATHS, cachePath);
core.saveState(State.STATE_CACHE_PRIMARY_KEY, primaryKey);
const matchedKey = yield cache.restoreCache(cachePath, primaryKey, restoreKey); const matchedKey = yield cache.restoreCache(cachePath, primaryKey, restoreKey);
if (matchedKey) { if (matchedKey) {
core.saveState(State.CACHE_MATCHED_KEY, matchedKey); core.saveState(State.CACHE_MATCHED_KEY, matchedKey);
@ -43887,13 +43887,13 @@ var PackageManagers;
PackageManagers["Pip"] = "pip"; PackageManagers["Pip"] = "pip";
PackageManagers["Pipenv"] = "pipenv"; PackageManagers["Pipenv"] = "pipenv";
})(PackageManagers = exports.PackageManagers || (exports.PackageManagers = {})); })(PackageManagers = exports.PackageManagers || (exports.PackageManagers = {}));
function getCacheDistributor(packageManager, pythonVersion, patterns) { function getCacheDistributor(packageManager, pythonVersion, cacheDependencyPath) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
switch (packageManager) { switch (packageManager) {
case PackageManagers.Pip: case PackageManagers.Pip:
return new pip_cache_1.default(patterns); return new pip_cache_1.default(cacheDependencyPath);
case PackageManagers.Pipenv: case PackageManagers.Pipenv:
return new pipenv_cache_1.default(pythonVersion, patterns); return new pipenv_cache_1.default(pythonVersion, cacheDependencyPath);
default: default:
throw new Error(`Caching for '${packageManager}' is not supported`); throw new Error(`Caching for '${packageManager}' is not supported`);
} }

View file

@ -9,7 +9,7 @@ export enum State {
abstract class CacheDistributor { abstract class CacheDistributor {
protected CACHE_KEY_PREFIX = 'setup-python'; 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 getCacheGlobalDirectories(): Promise<string[]>;
protected abstract computeKeys(): Promise<{ protected abstract computeKeys(): Promise<{
@ -19,22 +19,25 @@ abstract class CacheDistributor {
public async restoreCache() { public async restoreCache() {
const {primaryKey, restoreKey} = await this.computeKeys(); 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('-')) { if (primaryKey.endsWith('-')) {
throw new Error( throw new Error(
`No file in ${process.cwd()} matched to [${this.patterns `No file in ${process.cwd()} matched to [${this.cacheDependencyPath
.split('\n') .split('\n')
.join(',')}], make sure you have checked out the target repository` .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( const matchedKey = await cache.restoreCache(
cachePath, cachePath,
primaryKey, primaryKey,
restoreKey restoreKey
); );
if (matchedKey) { if (matchedKey) {
core.saveState(State.CACHE_MATCHED_KEY, matchedKey); core.saveState(State.CACHE_MATCHED_KEY, matchedKey);
core.info(`Cache restored from key: ${matchedKey}`); core.info(`Cache restored from key: ${matchedKey}`);

View file

@ -9,13 +9,13 @@ export enum PackageManagers {
export async function getCacheDistributor( export async function getCacheDistributor(
packageManager: string, packageManager: string,
pythonVersion: string, pythonVersion: string,
patterns: string | undefined cacheDependencyPath: string | undefined
) { ) {
switch (packageManager) { switch (packageManager) {
case PackageManagers.Pip: case PackageManagers.Pip:
return new PipCache(patterns); return new PipCache(cacheDependencyPath);
case PackageManagers.Pipenv: case PackageManagers.Pipenv:
return new PipenvCache(pythonVersion, patterns); return new PipenvCache(pythonVersion, cacheDependencyPath);
default: default:
throw new Error(`Caching for '${packageManager}' is not supported`); throw new Error(`Caching for '${packageManager}' is not supported`);
} }

View file

@ -8,14 +8,15 @@ import os from 'os';
import CacheDistributor from './cache-distributor'; import CacheDistributor from './cache-distributor';
class PipCache extends CacheDistributor { class PipCache extends CacheDistributor {
constructor(patterns: string = '**/requirements.txt') { constructor(cacheDependencyPath: string = '**/requirements.txt') {
super('pip', patterns); super('pip', cacheDependencyPath);
} }
protected async getCacheGlobalDirectories() { protected async getCacheGlobalDirectories() {
const {stdout, stderr, exitCode} = await exec.getExecOutput( const {stdout, stderr, exitCode} = await exec.getExecOutput(
'pip cache dir' 'pip cache dir'
); );
if (stderr) { if (stderr) {
throw new Error( throw new Error(
`Could not get cache folder path for pip package manager` `Could not get cache folder path for pip package manager`
@ -34,9 +35,10 @@ class PipCache extends CacheDistributor {
} }
protected async computeKeys() { 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 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 { return {
primaryKey, primaryKey,
restoreKey: [restoreKey] restoreKey: [restoreKey]

View file

@ -22,10 +22,10 @@ class PipenvCache extends CacheDistributor {
} }
protected async getCacheGlobalDirectories() { protected async getCacheGlobalDirectories() {
const cachePath = path.join(os.homedir(), this.getVirtualenvsPath()); const resolvedPath = path.join(os.homedir(), this.getVirtualenvsPath());
core.debug(`Pipenv virtualenvs path is ${cachePath}`); core.debug(`global cache directory path is ${resolvedPath}`);
return [cachePath]; return [resolvedPath];
} }
protected async computeKeys() { protected async computeKeys() {

View file

@ -8,7 +8,7 @@ async function run() {
try { try {
const cache = core.getInput('cache'); const cache = core.getInput('cache');
if (cache) { if (cache) {
await saveCache(); await saveCache(cache);
} }
} catch (error) { } catch (error) {
const err = error as Error; const err = error as Error;
@ -16,14 +16,17 @@ async function run() {
} }
} }
async function saveCache() { async function saveCache(packageManager: string) {
const cacheDirPaths = JSON.parse( const cachePaths = JSON.parse(
core.getState(State.CACHE_PATHS) core.getState(State.CACHE_PATHS)
) as string[]; ) as string[];
core.debug(`paths for caching are ${cacheDirPaths.join(', ')}`);
if (!isCacheDirectoryExists(cacheDirPaths)) { core.debug(`paths for caching are ${cachePaths.join(', ')}`);
throw new Error('Cache directories do not exist');
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 primaryKey = core.getState(State.STATE_CACHE_PRIMARY_KEY);
const matchedKey = core.getState(State.CACHE_MATCHED_KEY); const matchedKey = core.getState(State.CACHE_MATCHED_KEY);
@ -37,8 +40,9 @@ async function saveCache() {
); );
return; return;
} }
try { try {
await cache.saveCache(cacheDirPaths, primaryKey); await cache.saveCache(cachePaths, primaryKey);
core.info(`Cache saved with the key: ${primaryKey}`); core.info(`Cache saved with the key: ${primaryKey}`);
} catch (error) { } catch (error) {
const err = error as Error; const err = error as Error;