Skip to content

override slimPatterns instead of appending #276

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

Merged
merged 19 commits into from
Nov 17, 2018
Merged
Show file tree
Hide file tree
Changes from 6 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
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ except ImportError:
_Works on non 'win32' environments: Docker, WSL are included_
To remove the tests, information and caches from the installed packages,
enable the `slim` option. This will: `strip` the `.so` files, remove `__pycache__`
directories and `dist-info` directories.
and `dist-info` directories as well as `.pyc` and `.pyo` files.
```yaml
custom:
pythonRequirements:
Expand All @@ -116,7 +116,8 @@ custom:
#### Custom Removal Patterns
To specify additional directories to remove from the installed packages,
define a list of patterns in the serverless config using the `slimPatterns`
option and glob syntax. Note, it matches against whole paths, so to match a file in any
option and glob syntax. These paterns will be added to the default ones (`**/*.py[c|o]`, `**/__pycache__*`, `**/*.dist-info*`).
Note, the glob syntax matches against whole paths, so to match a file in any
directory, start your pattern with `**/`.
```yaml
custom:
Expand All @@ -125,6 +126,15 @@ custom:
slimPatterns:
- "**/*.egg-info*"
```
To overwrite the default patterns set the option `slimPatternsAppendDefaults` to `false` (`true` by default).
```yaml
custom:
pythonRequirements:
slim: true
slimPatternsAppendDefaults: false
slimPatterns:
- "**/*.egg-info*"
```
This will remove all folders within the installed requirements that match
the names in `slimPatterns`
## Omitting Packages
Expand Down
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class ServerlessPythonRequirements {
{
slim: false,
slimPatterns: false,
slimPatternsAppendDefaults: true,
zip: false,
cleanupZipHelper: true,
invalidateCaches: false,
Expand Down Expand Up @@ -62,7 +63,7 @@ class ServerlessPythonRequirements {
},
(this.serverless.service.custom &&
this.serverless.service.custom.pythonRequirements) ||
{}
{}
);
if (options.dockerizePip === 'non-linux') {
options.dockerizePip = process.platform !== 'linux';
Expand All @@ -86,7 +87,7 @@ class ServerlessPythonRequirements {
// If no dockerFile is provided, use default image
const defaultImage = `lambci/lambda:build-${
this.serverless.service.provider.runtime
}`;
}`;
options.dockerImage = options.dockerImage || defaultImage;
}
return options;
Expand Down Expand Up @@ -165,7 +166,7 @@ class ServerlessPythonRequirements {
.then(() =>
injectAllRequirements.bind(this)(
arguments[1].functionObj &&
arguments[1].functionObj.package.artifact
arguments[1].functionObj.package.artifact
)
);
};
Expand Down
6 changes: 5 additions & 1 deletion lib/slim.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ const getStripCommand = (options, folderPath) =>
const deleteFiles = (options, folderPath) => {
let patterns = ['**/*.py[c|o]', '**/__pycache__*', '**/*.dist-info*'];
if (options.slimPatterns) {
patterns = patterns.concat(options.slimPatterns);
if (options.slimPatternsAppendDefaults === false || options.slimPatternsAppendDefaults == 'false') {
patterns = options.slimPatterns;
} else {
patterns = patterns.concat(options.slimPatterns);
}
}
for (const pattern of patterns) {
for (const file of glob.sync(`${folderPath}/${pattern}`)) {
Expand Down
61 changes: 60 additions & 1 deletion test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,17 @@ teardown() {
test $(find puck -type d -name "*.egg-info*" | wc -l) -eq 0
}

@test "py3.6 can package flask with slim, slimPatternsAppendDefaults=false & slimPatterns options" {
cd tests/base
cat _slimPatterns.yml > slimPatterns.yml
npm i $(npm pack ../..)
sls --slim=true --slimPatternsAppendDefaults false package
unzip .serverless/sls-py-req-test.zip -d puck
ls puck/flask
test $(find puck -name "*.pyc" | wc -l) -ge 1
test $(find puck -type d -name "*.egg-info*" | wc -l) -eq 0
}

@test "py3.6 doesn't package boto3 by default" {
cd tests/base
npm i $(npm pack ../..)
Expand Down Expand Up @@ -146,6 +157,19 @@ teardown() {
test $(find puck -type d -name "*.egg-info*" | wc -l) -eq 0
}

@test "py3.6 can package flask with slim & dockerizePip & slimPatterns & slimPatternsAppendDefaults=false options" {
cd tests/base
cat _slimPatterns.yml > slimPatterns.yml
npm i $(npm pack ../..)
docker &> /dev/null || skip "docker not present"
! uname -sm|grep Linux || groups|grep docker || id -u|egrep '^0$' || skip "can't dockerize on linux if not root & not in docker group"
sls --dockerizePip=true --slim=true --slimPatternsAppendDefaults false package
unzip .serverless/sls-py-req-test.zip -d puck
ls puck/flask
test $(find puck -name "*.pyc" | wc -l) -ge 1
test $(find puck -type d -name "*.egg-info*" | wc -l) -eq 0
}

@test "py3.6 uses download cache with useDownloadCache option" {
cd tests/base
npm i $(npm pack ../..)
Expand Down Expand Up @@ -297,13 +321,24 @@ teardown() {
cd tests/base
cat _slimPatterns.yml > slimPatterns.yml
npm i $(npm pack ../..)
sls --runtime=python2.7 --slim=true packag
sls --runtime=python2.7 --slim=true package
unzip .serverless/sls-py-req-test.zip -d puck
ls puck/flask
test $(find puck -name "*.pyc" | wc -l) -eq 0
test $(find puck -type d -name "*.egg-info*" | wc -l) -eq 0
}

@test "py2.7 can package flask with slim & dockerizePip & slimPatterns & slimPatternsAppendDefaults=false options" {
cd tests/base
cat _slimPatterns.yml > slimPatterns.yml
npm i $(npm pack ../..)
sls --runtime=python2.7 --slim=true --slimPatternsAppendDefaults false package
unzip .serverless/sls-py-req-test.zip -d puck
ls puck/flask
test $(find puck -name "*.pyc" | wc -l) -ge 1
test $(find puck -type d -name "*.egg-info*" | wc -l) -eq 0
}

@test "py2.7 doesn't package boto3 by default" {
cd tests/base
npm i $(npm pack ../..)
Expand Down Expand Up @@ -375,6 +410,19 @@ teardown() {
test $(find puck -type d -name "*.egg-info*" | wc -l) -eq 0
}

@test "py2.7 can package flask with slim & dockerizePip & slimPatterns & slimPatternsAppendDefaults=false options" {
cd tests/base
cat _slimPatterns.yml > slimPatterns.yml
npm i $(npm pack ../..)
docker &> /dev/null || skip "docker not present"
! uname -sm|grep Linux || groups|grep docker || id -u|egrep '^0$' || skip "can't dockerize on linux if not root & not in docker group"
sls --dockerizePip=true --slim=true --slimPatternsAppendDefaults false --runtime=python2.7 package
unzip .serverless/sls-py-req-test.zip -d puck
ls puck/flask
test $(find puck -name "*.pyc" | wc -l) -ge 1
test $(find puck -type d -name "*.egg-info*" | wc -l) -eq 0
}

@test "pipenv py3.6 can package flask with default options" {
cd tests/pipenv
npm i $(npm pack ../..)
Expand Down Expand Up @@ -403,6 +451,17 @@ teardown() {
test $(find puck -type d -name "*.egg-info*" | wc -l) -eq 0
}

@test "pipenv py3.6 can package flask with slim & slimPatterns & slimPatternsAppendDefaults false option" {
cd tests/pipenv
npm i $(npm pack ../..)
cat _slimPatterns.yml > slimPatterns.yml
sls --slim=true --slimPatternsAppendDefaults false package
unzip .serverless/sls-py-req-test.zip -d puck
ls puck/flask
test $(find puck -name "*.pyc" | wc -l) -ge 1
test $(find puck -type d -name "*.egg-info*" | wc -l) -eq 0
}

@test "pipenv py3.6 can package flask with zip option" {
cd tests/pipenv
npm i $(npm pack ../..)
Expand Down
2 changes: 2 additions & 0 deletions tests/base/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ custom:
dockerizePip: ${opt:dockerizePip, self:custom.defaults.dockerizePip}
slim: ${opt:slim, self:custom.defaults.slim}
slimPatterns: ${file(./slimPatterns.yml):slimPatterns, self:custom.defaults.slimPatterns}
slimPatternsAppendDefaults: ${opt:slimPatternsAppendDefaults, self:custom.defaults.slimPatternsAppendDefaults}
vendor: ${opt:vendor, ''}
fileName: ${opt:fileName, 'requirements.txt'}
defaults:
slim: false
slimPatterns: false
slimPatternsAppendDefaults: true
zip: false
dockerizePip: false
individually: false
Expand Down