8000 Add linux os release info to primary key (#467) · lilyminium/setup-python@592a7a7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 592a7a7

Browse files
authored
Add linux os release info to primary key (actions#467)
1 parent aba6f4b commit 592a7a7

File tree

4 files changed

+82
-9
lines changed

4 files changed

+82
-9
lines changed

__tests__/cache-restore.test.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as core from '@actions/core';
22
import * as cache from '@actions/cache';
33
import * as exec from '@actions/exec';
44
import {getCacheDistributor} from '../src/cache-distributions/cache-factory';
5+
import * as utils from './../src/utils';
56

67
describe('restore-cache', () => {
78
const pipFileLockHash =
@@ -28,6 +29,7 @@ virtualenvs.path = "{cache-dir}/virtualenvs" # /Users/patrick/Library/Caches/py
2829
let saveSatetSpy: jest.SpyInstance;
2930
let getStateSpy: jest.SpyInstance;
3031
let setOutputSpy: jest.SpyInstance;
32+
let getLinuxOSReleaseInfoSpy: jest.SpyInstance;
3133

3234
// cache spy
3335
let restoreCacheSpy: jest.SpyInstance;
@@ -74,6 +76,8 @@ virtualenvs.path = "{cache-dir}/virtualenvs" # /Users/patrick/Library/Caches/py
7476
return primaryKey;
7577
}
7678
);
79+
80+
getLinuxOSReleaseInfoSpy = jest.spyOn(utils, 'getLinuxOSReleaseInfo');
7781
});
7882

7983
describe('Validate provided package manager', () => {
@@ -109,11 +113,24 @@ virtualenvs.path = "{cache-dir}/virtualenvs" # /Users/patrick/Library/Caches/py
109113
pythonVersion,
110114
dependencyFile
111115
);
116+
117+
if (process.platform === 'linux') {
118+
getLinuxOSReleaseInfoSpy.mockImplementation(() =>
119+
Promise.resolve('Ubuntu-20.4')
120+
);
121+
}
122+
112123
await cacheDistributor.restoreCache();
113124

114-
expect(infoSpy).toHaveBeenCalledWith(
115-
`Cache restored from key: setup-python-${process.env['RUNNER_OS']}-python-${pythonVersion}-${packageManager}-${fileHash}`
116-
);
125+
if (process.platform === 'linux' && packageManager === 'pip') {
126+
expect(infoSpy).toHaveBeenCalledWith(
127+
`Cache restored from key: setup-python-${process.env['RUNNER_OS']}-Ubuntu-20.4-python-${pythonVersion}-${packageManager}-${fileHash}`
128+
);
129+
} else {
130+
expect(infoSpy).toHaveBeenCalledWith(
131+
`Cache restored from key: setup-python-${process.env['RUNNER_OS']}-python-${pythonVersion}-${packageManager}-${fileHash}`
132+
);
133+
}
117134
},
118135
30000
119136
);

dist/setup/index.js

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64430,8 +64430,17 @@ class PipCache extends cache_distributor_1.default {
6443064430
computeKeys() {
6443164431
return __awaiter(this, void 0, void 0, function* () {
6443264432
const hash = yield glob.hashFiles(this.cacheDependencyPath);
64433-
const primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-python-${this.pythonVersion}-${this.packageManager}-${hash}`;
64434-
const restoreKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-python-${this.pythonVersion}-${this.packageManager}`;
64433+
let primaryKey = '';
64434+
let restoreKey = '';
64435+
if (utils_1.IS_LINUX) {
64436+
const osRelease = yield utils_1.getLinuxOSReleaseInfo();
64437+
primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${osRelease}-python-${this.pythonVersion}-${this.packageManager}-${hash}`;
64438+
restoreKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${osRelease}-python-${this.pythonVersion}-${this.packageManager}`;
64439+
}
64440+
else {
64441+
primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-python-${this.pythonVersion}-${this.packageManager}-${hash}`;
64442+
restoreKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-python-${this.pythonVersion}-${this.packageManager}`;
64443+
}
6443564444
return {
6443664445
primaryKey,
6443764446
restoreKey: [restoreKey]
@@ -65357,16 +65366,26 @@ var __importStar = (this && this.__importStar) || function (mod) {
6535765366
__setModuleDefault(result, mod);
6535865367
return result;
6535965368
};
65369+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
65370+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
65371+
return new (P || (P = Promise))(function (resolve, reject) {
65372+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
65373+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
65374+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
65375+
step((generator = generator.apply(thisArg, _arguments || [])).next());
65376+
});
65377+
};
6536065378
var __importDefault = (this && this.__importDefault) || function (mod) {
6536165379
return (mod && mod.__esModule) ? mod : { "default": mod };
6536265380
};
6536365381
Object.defineProperty(exports, "__esModule", ({ value: true }));
65364-
exports.isCacheFeatureAvailable = exports.isGhes = exports.validatePythonVersionFormatForPyPy = exports.writeExactPyPyVersionFile = exports.readExactPyPyVersionFile = exports.getPyPyVersionFromPath = exports.isNightlyKeyword = exports.validateVersion = exports.createSymlinkInFolder = exports.WINDOWS_PLATFORMS = exports.WINDOWS_ARCHS = exports.I A3E2 S_LINUX = exports.IS_WINDOWS = void 0;
65382+
exports.getLinuxOSReleaseInfo = exports.isCacheFeatureAvailable = exports.isGhes = exports.validatePythonVersionFormatForPyPy = exports.writeExactPyPyVersionFile = exports.readExactPyPyVersionFile = exports.getPyPyVersionFromPath = exports.isNightlyKeyword = exports.validateVersion = exports.createSymlinkInFolder = exports.WINDOWS_PLATFORMS = exports.WINDOWS_ARCHS = exports.IS_LINUX = exports.IS_WINDOWS = void 0;
6536565383
const cache = __importStar(__nccwpck_require__(7799));
6536665384
const core = __importStar(__nccwpck_require__(2186));
6536765385
const fs_1 = __importDefault(__nccwpck_require__(7147));
6536865386
const path = __importStar(__nccwpck_require__(1017));
6536965387
const semver = __importStar(__nccwpck_require__(1383));
65388+
const exec = __importStar(__nccwpck_require__(1514));
6537065389
exports.IS_WINDOWS = process.platform === 'win32';
6537165390
exports.IS_LINUX = process.platform === 'linux';
6537265391
exports.WINDOWS_ARCHS = ['x86', 'x64'];
@@ -65450,6 +65469,17 @@ function isCacheFeatureAvailable() {
6545065469
return true;
6545165470
}
6545265471
exports.isCacheFeatureAvailable = isCacheFeatureAvailable;
65472+
function getLinuxOSReleaseInfo() {
65473+
return __awaiter(this, void 0, void 0, function* () {
65474+
const { stdout, stderr, exitCode } = yield exec.getExecOutput('lsb_release', ['-i', '-r', '-s'], {
65475+
silent: true
65476+
});
65477+
const [osRelease, osVersion] = stdout.trim().split('\n');
65478+
core.debug(`OS Release: ${osRelease}, Version: ${osVersion}`);
65479+
return `${osVersion}-${osRelease}`;
65480+
});
65481+
}
65482+
exports.getLinuxOSReleaseInfo = getLinuxOSReleaseInfo;
6545365483

6545465484

6545565485
/***/ }),

src/cache-distributions/pip-cache.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import * as path from 'path';
77
import os from 'os';
88

99
import CacheDistributor from './cache-distributor';
10-
import {IS_WINDOWS} from '../utils';
10+
import {getLinuxOSReleaseInfo, IS_LINUX, IS_WINDOWS} from '../utils';
1111

1212
class PipCache extends CacheDistributor {
1313
constructor(
@@ -57,8 +57,17 @@ class PipCache extends CacheDistributor {
5757

5858
protected async computeKeys() {
5959
const hash = await glob.hashFiles(this.cacheDependencyPath);
60-
const primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-python-${this.pythonVersion}-${this.packageManager}-${hash}`;
61-
const restoreKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-python-${this.pythonVersion}-${this.packageManager}`;
60+
let primaryKey = '';
61+
let restoreKey = '';
62+
63+
if (IS_LINUX) {
64+
const osRelease = await getLinuxOSReleaseInfo();
65+
primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${osRelease}-python-${this.pythonVersion}-${this.packageManager}-${hash}`;
66+
restoreKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${osRelease}-python-${this.pythonVersion}-${this.packageManager}`;
67+
} else {
68+
primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-python-${this.pythonVersion}-${this.packageManager}-${hash}`;
69+
restoreKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-python-${this.pythonVersion}-${this.packageManager}`;
70+
}
6271

6372
return {
6473
primaryKey,

src/utils.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as core from '@actions/core';
33
import fs from 'fs';
44
import * as path from 'path';
55
import * as semver from 'semver';
6+
import * as exec from '@actions/exec';
67

78
export const IS_WINDOWS = process.platform === 'win32';
89
export const IS_LINUX = process.platform === 'linux';
@@ -119,3 +120,19 @@ export function isCacheFeatureAvailable(): boolean {
119120

120121
return true;
121122
}
123+
124+
export async function getLinuxOSReleaseInfo() {
125+
const {stdout, stderr, exitCode} = await exec.getExecOutput(
126+
'lsb_release',
127+
['-i', '-r', '-s'],
128+
{
129+
silent: true
130+
}
131+
);
132+
133+
const [osRelease, osVersion] = stdout.trim().split('\n');
134+
135+
core.debug(`OS Release: ${osRelease}, Version: ${osVersion}`);
136+
137+
return `${osVersion}-${osRelease}`;
138+
}

0 commit comments

Comments
 (0)
0