Skip to content

Commit

Permalink
Improve caching for long to install dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
MatteoH2O1999 committed Jan 31, 2023
1 parent 0769741 commit e842c78
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 26 deletions.
24 changes: 22 additions & 2 deletions src/builder/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export default abstract class Builder {
private readonly cacheKey: string;
protected readonly tagZipUri: string;
protected readonly path: string;
protected restored = false;

constructor(specificVersion: PythonTag, arch: string) {
this.specificVersion = specificVersion.version;
Expand Down Expand Up @@ -60,34 +61,53 @@ export default abstract class Builder {

protected abstract CacheKeyOs(): string;

protected abstract additionalCachePaths(): Promise<string[]>;

abstract postInstall(installedPath: string): Promise<void>;

async restoreCache(): Promise<string | null> {
core.startGroup('Trying to use cached built version');
if (!cache.isFeatureAvailable) {
core.info('Cache feature is not available on current runner.');
core.endGroup();
return null;
}
const restoredPath = path.join(this.path, this.buildSuffix());
const restoredPaths = [restoredPath];
const additionalPaths = await this.additionalCachePaths();
if (additionalPaths.length > 0) {
restoredPaths.push(...additionalPaths);
}
core.info(`Restoring cached version with key ${this.cacheKey}...`);
const restoredKey = await cache.restoreCache([restoredPath], this.cacheKey);
core.debug(`Restoring paths ${restoredPaths}`);
const restoredKey = await cache.restoreCache(restoredPaths, this.cacheKey);
if (restoredKey === undefined) {
core.info('Cached version not found.');
core.endGroup();
return null;
}
core.info(`CPython ${this.specificVersion} restored from cache.`);
core.endGroup();
this.restored = true;
return restoredPath;
}

async saveCache(): Promise<void> {
core.startGroup('Caching built files');
if (!cache.isFeatureAvailable) {
core.info('Cache feature is not available on current runner.');
core.endGroup();
return;
}
core.info(`Saving built files with key ${this.cacheKey}...`);
const savePath = path.join(this.path, this.buildSuffix());
await cache.saveCache([savePath], this.cacheKey);
const savePaths = [savePath];
const additionalPaths = await this.additionalCachePaths();
if (additionalPaths.length > 0) {
savePaths.push(...additionalPaths);
}
core.debug(`Saving paths: ${savePaths}`);
await cache.saveCache(savePaths, this.cacheKey);
core.info('Files successfully saved in cache.');
core.endGroup();
}
Expand Down
66 changes: 43 additions & 23 deletions src/builder/darwin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,6 @@ export default class MacOSBuilder extends Builder {
// Running ./configure

core.startGroup('Configuring makefile');
core.info(`Config command: ${configCommand}`);
core.info(`CFLAGS: ${process.env['CFLAGS']}`);
core.info(`LDFLAGS: ${process.env['LDFLAGS']}`);
core.info(`CPPFLAGS: ${process.env['CPPFLAGS']}`);
await exec.exec(configCommand, [], {cwd: this.path});
core.endGroup();

Expand Down Expand Up @@ -199,29 +195,18 @@ export default class MacOSBuilder extends Builder {

await this.installGeneralDependencies();

// Handle ssl installations
// Handle ssl version

if (semver.lt(this.specificVersion, '3.5.0')) {
core.info('Detected version <3.5. OpenSSL version 1.0.2 will be used...');
if (this.sslPath === '') {
await this.installOldSsl(ssl102Url);
} else {
core.info('OpenSSL version 1.0.2 is already installed.');
}
core.info('OpenSSL version 1.0.2 is already installed');
} else if (semver.lt(this.specificVersion, '3.9.0')) {
core.info('Detected version <3.9. OpenSSL version 1.1 will be used...');
if (this.sslPath === '') {
await exec.exec('brew install openssl@1.1');
} else {
core.info('OpenSSL version 1.1 is already installed.');
}
core.info(
'Detected version <3.9. OpenSSL version 1.1 will be installed...'
);
await exec.exec('brew install openssl@1.1');
} else {
core.info('Detected version >=3.9. Default OpenSSL will be used...');
if (this.sslPath === '') {
await exec.exec('brew install openssl');
} else {
core.info('OpenSSL is already installed.');
}
core.info('Detected version >=3.9. Default OpenSSL will be installed...');
await exec.exec('brew install openssl');
}

// Create symlinks
Expand Down Expand Up @@ -297,4 +282,39 @@ export default class MacOSBuilder extends Builder {
await exec.exec('brew install sqlite3', [], {ignoreReturnCode: true});
await exec.exec('brew install readline', [], {ignoreReturnCode: true});
}

protected override async additionalCachePaths(): Promise<string[]> {
const additionalPaths: string[] = [];
if (semver.lt(this.specificVersion, '3.5.0')) {
const tempPath = process.env['RUNNER_TEMP'] || os.tmpdir();
const ssl = await tc.downloadTool(
ssl102Url.url,
path.join(tempPath, `${ssl102Url.name}.rb`)
);
let openssl102Path = '';
await exec.exec(`brew --prefix ${ssl102Url.name}.rb`, [], {
cwd: tempPath,
listeners: {
stdout: (buffer: Buffer) => {
openssl102Path = openssl102Path.concat(buffer.toString());
}
},
silent: true
});
let openssl102Cellar = '';
await exec.exec(`brew --cellar ${ssl102Url.name}.rb`, [], {
cwd: tempPath,
listeners: {
stdout: (buffer: Buffer) => {
openssl102Cellar = openssl102Cellar.concat(buffer.toString());
}
},
silent: true
});
await io.rmRF(ssl);
additionalPaths.push(openssl102Path.trim());
additionalPaths.push(openssl102Cellar.trim());
}
return additionalPaths;
}
}
6 changes: 5 additions & 1 deletion src/builder/linux.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default class LinuxBuilder extends Builder {

// Configuring flags

const flags: string[] = ['--enable-shared'];
const flags: string[] = [];
if (semver.lt(this.specificVersion, '3.0.0')) {
flags.push('--enable-unicode=ucs4');
}
Expand Down Expand Up @@ -206,4 +206,8 @@ export default class LinuxBuilder extends Builder {
);
}
}

protected override async additionalCachePaths(): Promise<string[]> {
return [];
}
}
4 changes: 4 additions & 0 deletions src/builder/windows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,8 @@ export default class WindowsBuilder extends Builder {

core.endGroup();
}

protected override async additionalCachePaths(): Promise<string[]> {
return [];
}
}

0 comments on commit e842c78

Please sign in to comment.