Skip to content

Only pack requirements for Python runtimes #204

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
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
15 changes: 15 additions & 0 deletions lib/zip.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ function addVendorHelper() {
if (this.options.zip) {
if (this.serverless.service.package.individually) {
return BbPromise.resolve(values(this.serverless.service.functions))
.filter(func =>
(func.runtime || this.serverless.service.provider.runtime).match(
/^python.*/
)
)
.map(f => {
if (!get(f, 'package.include')) {
set(f, ['package', 'include'], []);
Expand Down Expand Up @@ -64,6 +69,11 @@ function removeVendorHelper() {
if (this.options.zip && this.options.cleanupZipHelper) {
if (this.serverless.service.package.individually) {
return BbPromise.resolve(values(this.serverless.service.functions))
.filter(func =>
(func.runtime || this.serverless.service.provider.runtime).match(
/^python.*/
)
)
.map(f => {
if (!get(f, 'module')) {
set(f, ['module'], '.');
Expand Down Expand Up @@ -96,6 +106,11 @@ function packRequirements() {
if (this.options.zip) {
if (this.serverless.service.package.individually) {
return BbPromise.resolve(values(this.serverless.service.functions))
.filter(f =>
(f.runtime || this.serverless.service.provider.runtime).match(
/^python.*/
)
)
.map(f => {
if (!get(f, 'module')) {
set(f, ['module'], '.');
Expand Down
50 changes: 50 additions & 0 deletions test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,56 @@ teardown() {
! ls puck2/pyaml
}

@test "py2.7 can package module requirements with zip option" {
Copy link
Author

Choose a reason for hiding this comment

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

@dschep Is it expected functionality for the zip flag not to move up the root module?

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think so. That seems to be the cause of #203

Copy link

@felipe-augusto felipe-augusto Jun 18, 2018

Choose a reason for hiding this comment

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

I managed to fix this problem, by doing this: felipe-augusto@2a14d92

But I don't know if this is the desired behavior.

Copy link
Author

Choose a reason for hiding this comment

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

@dschep I believe so as well.

To anyone: Feel free to cherry-pick or modify the regression tests from 6f7b229 and a20a209 to validate the fix. I believe a fix to that issue precedes my addition.

cd tests/individually
npm i $(npm pack ../..)
sls --zip=true --runtime=python2.7 package
unzip .serverless/hello1.zip -d puck
unzip .serverless/hello2.zip -d puck2
ls puck/.requirements.zip puck/unzip_requirements.py
ls puck2/.requirements.zip puck2/unzip_requirements.py
}

@test "py3.6 can package module requirements with zip option" {
cd tests/individually
npm i $(npm pack ../..)
sls --zip=true package
unzip .serverless/hello1.zip -d puck
unzip .serverless/hello2.zip -d puck2
ls puck/.requirements.zip puck/unzip_requirements.py
ls puck2/.requirements.zip puck2/unzip_requirements.py
}

@test "py2.7 can package only python runtimes" {
cd tests/individually
npm i $(npm pack ../..)
sls --runtime=python2.7 package
unzip .serverless/module1-sls-py-req-test-indiv-dev-hello1.zip -d puck
unzip .serverless/module2-sls-py-req-test-indiv-dev-hello2.zip -d puck2
unzip .serverless/hello3.zip -d puck3
ls puck3/module3/handler3.js
! ls puck/handler3.js
! ls puck2/handler3.js
! ls puck3/flask
! ls puck3/pyaml
}

@test "py2.7 can package only python runtimes with zip option" {
cd tests/individually
npm i $(npm pack ../..)
sls --zip=true --runtime=python2.7 package
unzip .serverless/hello3.zip -d puck3
! ls puck3/.requirements.zip puck3/unzip_requirements.py
}

@test "py3.6 can package only python runtimes with zip option" {
cd tests/individually
npm i $(npm pack ../..)
sls --zip=true package
unzip .serverless/hello3.zip -d puck3
! ls puck3/.requirements.zip puck3/unzip_requirements.py
}

@test "py3.6 can package lambda-decorators using vendor option" {
cd tests/base
npm i $(npm pack ../..)
Expand Down
3 changes: 3 additions & 0 deletions tests/individually/module3/handler3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
exports.hello = function(event, context, callback) {
callback(null, { status: 200 })
};
16 changes: 15 additions & 1 deletion tests/individually/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,32 @@ service: sls-py-req-test-indiv

provider:
name: aws
runtime: python3.6
runtime: ${opt:runtime, 'python3.6'}

package:
individually: true

custom:
pythonRequirements:
zip: ${opt:zip, self:custom.defaults.zip}
defaults:
zip: false

functions:
hello1:
handler: handler1.hello
module: module1
hello2:
handler: handler2.hello
module: module2
hello3:
handler: module3/handler3.hello
runtime: node6.10
package:
exclude:
- '**/*'
include:
- 'module3/**/*'

plugins:
- serverless-python-requirements