Skip to content
This repository was archived by the owner on Oct 24, 2025. It is now read-only.

Commit 6f32335

Browse files
committed
prepend the path in the zip folder depending on the runtime
1 parent 32dd659 commit 6f32335

File tree

1 file changed

+45
-11
lines changed

1 file changed

+45
-11
lines changed

lib/layer.js

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,62 @@ const BbPromise = require('bluebird');
22
const fse = require('fs-extra');
33
const path = require('path');
44
const JSZip = require('jszip');
5-
const { writeZip, addTree } = require('./zipTree');
5+
const {
6+
writeZip,
7+
addTree
8+
} = require('./zipTree');
69

710
BbPromise.promisifyAll(fse);
811

12+
/**
13+
* Get the paths for the compatible runtimes of the layer
14+
* @param {string[]} list of runtime paths
15+
*/
16+
function getRunTimeBuildPaths() {
17+
const runtimepaths = {
18+
'python2.7': 'python',
19+
'python3.6': path.join('python', 'lib', 'python3.6', 'site-packages'),
20+
'python3.7': path.join('python', 'lib', 'python3.7', 'site-packages'),
21+
};
22+
23+
let runtimes = []
24+
25+
// Defer to Layer config first
26+
if (this.options.layer.compatibleRuntimes) {
27+
runtimes = this.options.layer.compatibleRuntimes;
28+
// If none provided, assume the provider runtime
29+
} else if (this.serverless.service.provider.runtime) {
30+
runtimes = [this.serverless.service.provider.runtime];
31+
// If still no runtime found, just assume latest python
32+
} else {
33+
runtimes = ['python3.7'];
34+
}
35+
36+
return BbPromise.resolve(runtimes.map(runtime => runtimepaths[runtime]));
37+
}
38+
939
/**
1040
* Zip up requirements to be used as layer package.
41+
* @param {string[]} list of paths where the requirements should be put in the layer
1142
* @return {Promise} the JSZip object constructed.
1243
*/
13-
function zipRequirements() {
14-
return addTree(new JSZip(), path.join('.serverless', 'requirements')).then(
15-
zip => writeZip(zip, path.join('.serverless', 'pythonRequirements.zip'))
16-
);
44+
function zipRequirements(runtimepaths) {
45+
const rootZip = new JSZip();
46+
const src = path.join('.serverless', 'requirements')
47+
48+
return BbPromise.each(runtimepaths, (runtimepath) => addTree(rootZip.folder(runtimepath), src))
49+
.then(() => writeZip(rootZip, path.join('.serverless', 'pythonRequirementsLayer.zip')))
1750
}
1851

1952
/**
2053
* Creates a layer on the serverless service for the requirements zip.
21-
* @return {Promise}
54+
* @return {Promise} empty promise
2255
*/
2356
function createLayers() {
24-
this.serverless.service.layers['pythonRequirements'] = Object.assign(
25-
{
57+
this.serverless.service.layers['pythonRequirements'] = Object.assign({
2658
artifact: path.join('.serverless', 'pythonRequirements.zip'),
2759
name: `${this.serverless.service.stage}-python-requirements`,
28-
description:
29-
'Python requirements generated by serverless-python-requirements.'
60+
description: 'Python requirements generated by serverless-python-requirements.'
3061
},
3162
this.options.layer
3263
);
@@ -46,8 +77,11 @@ function layerRequirements() {
4677
this.serverless.cli.log('Packaging Python Requirements Lambda Layer...');
4778

4879
return BbPromise.bind(this)
80+
.then(getRunTimeBuildPaths)
4981
.then(zipRequirements)
5082
.then(createLayers);
5183
}
5284

53-
module.exports = { layerRequirements };
85+
module.exports = {
86+
layerRequirements
87+
};

0 commit comments

Comments
 (0)