Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor code readability improvements discovered on code review. #1003

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 4 additions & 12 deletions src/cache-distributions/cache-distributor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as cache from '@actions/cache';
import * as core from '@actions/core';
import {CACHE_DEPENDENCY_BACKUP_PATH} from './constants';

export enum State {
STATE_CACHE_PRIMARY_KEY = 'cache-primary-key',
Expand All @@ -10,27 +9,20 @@ export enum State {

abstract class CacheDistributor {
protected CACHE_KEY_PREFIX = 'setup-python';
constructor(
protected packageManager: string,
protected cacheDependencyPath: string
) {}
protected abstract readonly packageManager: string;

protected abstract getCacheGlobalDirectories(): Promise<string[]>;
protected abstract computeKeys(): Promise<{
primaryKey: string;
restoreKey: string[] | undefined;
cacheDependencyPath: string;
}>;
protected async handleLoadedCache() {}

public async restoreCache() {
const {primaryKey, restoreKey} = await this.computeKeys();
const {primaryKey, restoreKey, cacheDependencyPath} = await this.computeKeys();
if (primaryKey.endsWith('-')) {
const file =
this.packageManager === 'pip'
? `${this.cacheDependencyPath
.split('\n')
.join(',')} or ${CACHE_DEPENDENCY_BACKUP_PATH}`
: this.cacheDependencyPath.split('\n').join(',');
const file = cacheDependencyPath.split('\n').join(',');
throw new Error(
`No file in ${process.cwd()} matched to [${file}], make sure you have checked out the target repository`
);
Expand Down
1 change: 0 additions & 1 deletion src/cache-distributions/constants.ts

This file was deleted.

20 changes: 12 additions & 8 deletions src/cache-distributions/pip-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ import os from 'os';

import CacheDistributor from './cache-distributor';
import {getLinuxInfo, IS_LINUX, IS_WINDOWS} from '../utils';
import {CACHE_DEPENDENCY_BACKUP_PATH} from './constants';

class PipCache extends CacheDistributor {
private cacheDependencyBackupPath: string = CACHE_DEPENDENCY_BACKUP_PATH;
private cacheDependencyBackupPath = '**/pyproject.toml';
protected readonly packageManager = 'pip';

constructor(
private pythonVersion: string,
cacheDependencyPath = '**/requirements.txt'
protected readonly cacheDependencyPath = '**/requirements.txt'
) {
super('pip', cacheDependencyPath);
super();
}

protected async getCacheGlobalDirectories() {
Expand Down Expand Up @@ -59,9 +59,12 @@ class PipCache extends CacheDistributor {
}

protected async computeKeys() {
const hash =
(await glob.hashFiles(this.cacheDependencyPath)) ||
(await glob.hashFiles(this.cacheDependencyBackupPath));
let cacheDependencyPath = this.cacheDependencyPath;
let hash = await glob.hashFiles(this.cacheDependencyPath);
if(!hash) {
hash = await glob.hashFiles(this.cacheDependencyBackupPath);
cacheDependencyPath = this.cacheDependencyBackupPath;
}
let primaryKey = '';
let restoreKey = '';

Expand All @@ -76,7 +79,8 @@ class PipCache extends CacheDistributor {

return {
primaryKey,
restoreKey: [restoreKey]
restoreKey: [restoreKey],
cacheDependencyPath,
};
}
}
Expand Down
11 changes: 7 additions & 4 deletions src/cache-distributions/pipenv-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import * as core from '@actions/core';
import CacheDistributor from './cache-distributor';

class PipenvCache extends CacheDistributor {
protected readonly packageManager = 'pipenv';

constructor(
private pythonVersion: string,
protected patterns: string = '**/Pipfile.lock'
protected readonly cacheDependencyPath: string = '**/Pipfile.lock'
) {
super('pipenv', patterns);
super();
}

protected async getCacheGlobalDirectories() {
Expand All @@ -31,12 +33,13 @@ class PipenvCache 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']}-${process.arch}-python-${this.pythonVersion}-${this.packageManager}-${hash}`;
const restoreKey = undefined;
return {
primaryKey,
restoreKey
restoreKey,
cacheDependencyPath: this.cacheDependencyPath,
};
}
}
Expand Down
14 changes: 9 additions & 5 deletions src/cache-distributions/poetry-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@ import CacheDistributor from './cache-distributor';
import {logWarning} from '../utils';

class PoetryCache extends CacheDistributor {
protected readonly packageManager = 'poetry';


constructor(
private pythonVersion: string,
protected patterns: string = '**/poetry.lock',
protected readonly cacheDependencyPath: string = '**/poetry.lock',
protected poetryProjects: Set<string> = new Set<string>()
) {
super('poetry', patterns);
super();
}

protected async getCacheGlobalDirectories() {
// Same virtualenvs path may appear for different projects, hence we use a Set
const paths = new Set<string>();
const globber = await glob.create(this.patterns);
const globber = await glob.create(this.cacheDependencyPath);

for await (const file of globber.globGenerator()) {
const basedir = path.dirname(file);
Expand All @@ -45,13 +48,14 @@ class PoetryCache extends CacheDistributor {
}

protected async computeKeys() {
const hash = await glob.hashFiles(this.patterns);
const hash = await glob.hashFiles(this.cacheDependencyPath);
// "v2" is here to invalidate old caches of this cache distributor, which were created broken:
const primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${process.arch}-python-${this.pythonVersion}-${this.packageManager}-v2-${hash}`;
const restoreKey = undefined;
return {
primaryKey,
restoreKey
restoreKey,
cacheDependencyPath: this.cacheDependencyPath,
};
}

Expand Down