Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend no deploy #339

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -219,6 +219,9 @@ the following packages are omitted as they are already installed on Lambda:
* s3transfer
* setuptools
* six

Alternatively the `extendNoDeploy` option allows you to omit additional packages
as well as those on teh default list.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
as well as those on teh default list.
as well as those on the default list.


This example makes it instead omit pytest:
```yaml
@@ -228,6 +231,14 @@ custom:
- pytest
```

This example makes it omit numpy in addition to the above list:
```yaml
custom:
pythonRequirements:
extendNoDeploy:
- numpy
```

To include the default omitted packages, set the `noDeploy` option to an empty
list:
```yaml
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -64,6 +64,7 @@ class ServerlessPythonRequirements {
'pip',
'setuptools'
],
extendNoDeploy: [],
vendor: ''
},
(this.serverless.service.custom &&
@@ -104,6 +105,9 @@ class ServerlessPythonRequirements {
options.layer = {};
}
}
if (options.extendNoDeploy.length > 0) {
options.noDeploy.push(...options.extendNoDeploy);
}
return options;
}

72 changes: 45 additions & 27 deletions lib/inject.js
Original file line number Diff line number Diff line change
@@ -9,6 +9,45 @@ const { writeZip, zipFile } = require('./zipTree');

BbPromise.promisifyAll(fse);

/**
* Inject requirements into JSZip object
* @param {JSZip} zip js
* @param {string} requirementsPath requirements folder path
* @param {Object} options our options object
* @return {Promise} the JSZip object constructed.
*/
function injectRequirementsToZip(zip, requirementsPath, options) {
const noDeploy = new Set(options.noDeploy || []);
const runtimePath = 'python';

return BbPromise.resolve(
glob.sync([path.join(requirementsPath, '**')], {
mark: true,
dot: true
})
)
.map(file => [file, path.relative(requirementsPath, file)])
.filter(
([file, relativeFile]) =>
!file.endsWith('/') &&
!relativeFile.match(/^__pycache__[\\/]/) &&
!noDeploy.has(relativeFile.split(/([-\\/]|\.py$|\.pyc$)/, 1)[0])
)
.map(([file, relativeFile]) =>
Promise.all([
file,
options.layer ? path.join(runtimePath, relativeFile) : relativeFile,
fse.statAsync(file)
])
)
.map(([file, relativeFile, fileStat]) =>
zipFile(zip, relativeFile, fse.readFileAsync(file), {
unixPermissions: fileStat.mode
})
)
.then(() => zip);
}

/**
* Inject requirements into packaged application.
* @param {string} requirementsPath requirements folder path
@@ -17,35 +56,11 @@ BbPromise.promisifyAll(fse);
* @return {Promise} the JSZip object constructed.
*/
function injectRequirements(requirementsPath, packagePath, options) {
const noDeploy = new Set(options.noDeploy || []);

return fse
.readFileAsync(packagePath)
.then(buffer => JSZip.loadAsync(buffer))
.then(zip =>
BbPromise.resolve(
glob.sync([path.join(requirementsPath, '**')], {
mark: true,
dot: true
})
)
.map(file => [file, path.relative(requirementsPath, file)])
.filter(
([file, relativeFile]) =>
!file.endsWith('/') &&
!relativeFile.match(/^__pycache__[\\/]/) &&
!noDeploy.has(relativeFile.split(/([-\\/]|\.py$|\.pyc$)/, 1)[0])
)
.map(([file, relativeFile]) =>
Promise.all([file, relativeFile, fse.statAsync(file)])
)
.map(([file, relativeFile, fileStat]) =>
zipFile(zip, relativeFile, fse.readFileAsync(file), {
unixPermissions: fileStat.mode
})
)
.then(() => writeZip(zip, packagePath))
);
.then(zip => injectRequirementsToZip(zip, requirementsPath, options))
.then(zip => writeZip(zip, packagePath));
}

/**
@@ -130,4 +145,7 @@ function injectAllRequirements(funcArtifact) {
}
}

module.exports = { injectAllRequirements };
module.exports = {
injectAllRequirements,
injectRequirementsToZip
};
8 changes: 4 additions & 4 deletions lib/layer.js
Original file line number Diff line number Diff line change
@@ -2,7 +2,8 @@ const BbPromise = require('bluebird');
const fse = require('fs-extra');
const path = require('path');
const JSZip = require('jszip');
const { writeZip, addTree } = require('./zipTree');
const { writeZip } = require('./zipTree');
const { injectRequirementsToZip } = require('./inject');

BbPromise.promisifyAll(fse);

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

return addTree(rootZip.folder(runtimepath), src).then(() =>
writeZip(rootZip, path.join('.serverless', 'pythonRequirements.zip'))
return injectRequirementsToZip(rootZip, src, this.options).then(zip =>
writeZip(zip, path.join('.serverless', 'pythonRequirements.zip'))
);
}