1
0
Fork 0
mirror of https://code.forgejo.org/actions/setup-python synced 2025-03-14 22:26:58 +01:00
This commit is contained in:
sgpinkus 2025-03-13 18:57:51 +01:00 committed by GitHub
commit 76c716709d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 32 additions and 30 deletions

View file

@ -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',
@ -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`
);

View file

@ -1 +0,0 @@
export const CACHE_DEPENDENCY_BACKUP_PATH = '**/pyproject.toml';

View file

@ -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() {
@ -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 = '';
@ -76,7 +79,8 @@ class PipCache extends CacheDistributor {
return {
primaryKey,
restoreKey: [restoreKey]
restoreKey: [restoreKey],
cacheDependencyPath,
};
}
}

View file

@ -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() {
@ -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,
};
}
}

View file

@ -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);
@ -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,
};
}