Skip to content

Commit 7df3a04

Browse files
author
Yossi Synett
committed
Properly filter requirements for lambda layer
1 parent 3233c60 commit 7df3a04

File tree

2 files changed

+50
-32
lines changed

2 files changed

+50
-32
lines changed

lib/inject.js

+46-28
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,46 @@ const { writeZip, zipFile } = require('./zipTree');
99

1010
BbPromise.promisifyAll(fse);
1111

12+
/**
13+
* Inject requirements into JSZip object
14+
* @param {JSZip} zip js
15+
* @param {string} requirementsPath requirements folder path
16+
* @param {Object} options our options object
17+
* @return {Promise} the JSZip object constructed.
18+
*/
19+
function injectRequirementsToZip(zip, requirementsPath, options) {
20+
const noDeploy = new Set(options.noDeploy || []);
21+
const runtimePath = 'python';
22+
23+
return BbPromise.resolve(
24+
glob.sync([path.join(requirementsPath, '**')], {
25+
mark: true,
26+
dot: true
27+
})
28+
)
29+
.map(file => [file, path.relative(requirementsPath, file)])
30+
.filter(
31+
([file, relativeFile]) =>
32+
!file.endsWith('/') &&
33+
!relativeFile.match(/^__pycache__[\\/]/) &&
34+
!noDeploy.has(relativeFile.split(/([-\\/]|\.py$|\.pyc$)/, 1)[0])
35+
)
36+
.map(([file, relativeFile]) =>
37+
Promise.all([
38+
file,
39+
options.layer ? path.join(runtimePath, relativeFile) : relativeFile,
40+
fse.statAsync(file)
41+
])
42+
)
43+
.mapSeries(([file, relativeFile, fileStat]) =>
44+
zipFile(zip, relativeFile, fse.readFileAsync(file), {
45+
unixPermissions: fileStat.mode,
46+
createFolders: false
47+
})
48+
)
49+
.then(() => zip);
50+
}
51+
1252
/**
1353
* Inject requirements into packaged application.
1454
* @param {string} requirementsPath requirements folder path
@@ -17,36 +57,11 @@ BbPromise.promisifyAll(fse);
1757
* @return {Promise} the JSZip object constructed.
1858
*/
1959
function injectRequirements(requirementsPath, packagePath, options) {
20-
const noDeploy = new Set(options.noDeploy || []);
21-
2260
return fse
2361
.readFileAsync(packagePath)
2462
.then(buffer => JSZip.loadAsync(buffer))
25-
.then(zip =>
26-
BbPromise.resolve(
27-
glob.sync([path.join(requirementsPath, '**')], {
28-
mark: true,
29-
dot: true
30-
})
31-
)
32-
.map(file => [file, path.relative(requirementsPath, file)])
33-
.filter(
34-
([file, relativeFile]) =>
35-
!file.endsWith('/') &&
36-
!relativeFile.match(/^__pycache__[\\/]/) &&
37-
!noDeploy.has(relativeFile.split(/([-\\/]|\.py$|\.pyc$)/, 1)[0])
38-
)
39-
.map(([file, relativeFile]) =>
40-
Promise.all([file, relativeFile, fse.statAsync(file)])
41-
)
42-
.mapSeries(([file, relativeFile, fileStat]) =>
43-
zipFile(zip, relativeFile, fse.readFileAsync(file), {
44-
unixPermissions: fileStat.mode,
45-
createFolders: false
46-
})
47-
)
48-
.then(() => writeZip(zip, packagePath))
49-
);
63+
.then(zip => injectRequirementsToZip(zip, requirementsPath, options))
64+
.then(zip => writeZip(zip, packagePath));
5065
}
5166

5267
/**
@@ -140,4 +155,7 @@ function injectAllRequirements(funcArtifact) {
140155
}
141156
}
142157

143-
module.exports = { injectAllRequirements };
158+
module.exports = {
159+
injectAllRequirements,
160+
injectRequirementsToZip
161+
};

lib/layer.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ 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 { writeZip } = require('./zipTree');
6+
const { injectRequirementsToZip } = require('./inject');
67

78
BbPromise.promisifyAll(fse);
89

@@ -13,10 +14,9 @@ BbPromise.promisifyAll(fse);
1314
function zipRequirements() {
1415
const rootZip = new JSZip();
1516
const src = path.join('.serverless', 'requirements');
16-
const runtimepath = 'python';
1717

18-
return addTree(rootZip.folder(runtimepath), src).then(() =>
19-
writeZip(rootZip, path.join('.serverless', 'pythonRequirements.zip'))
18+
return injectRequirementsToZip(rootZip, src, this.options).then(zip =>
19+
writeZip(zip, path.join('.serverless', 'pythonRequirements.zip'))
2020
);
2121
}
2222

0 commit comments

Comments
 (0)