Skip to content

Commit a83f907

Browse files
authored
Merge branch 'master' into package_individually_like_base_sls
2 parents 99a4411 + 8551233 commit a83f907

File tree

8 files changed

+174
-13
lines changed

8 files changed

+174
-13
lines changed

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ except ImportError:
116116
_Works on non 'win32' environments: Docker, WSL are included_
117117
To remove the tests, information and caches from the installed packages,
118118
enable the `slim` option. This will: `strip` the `.so` files, remove `__pycache__`
119-
directories and `dist-info` directories.
119+
and `dist-info` directories as well as `.pyc` and `.pyo` files.
120120
```yaml
121121
custom:
122122
pythonRequirements:
@@ -125,7 +125,8 @@ custom:
125125
#### Custom Removal Patterns
126126
To specify additional directories to remove from the installed packages,
127127
define a list of patterns in the serverless config using the `slimPatterns`
128-
option and glob syntax. Note, it matches against whole paths, so to match a file in any
128+
option and glob syntax. These paterns will be added to the default ones (`**/*.py[c|o]`, `**/__pycache__*`, `**/*.dist-info*`).
129+
Note, the glob syntax matches against whole paths, so to match a file in any
129130
directory, start your pattern with `**/`.
130131
```yaml
131132
custom:
@@ -134,6 +135,15 @@ custom:
134135
slimPatterns:
135136
- "**/*.egg-info*"
136137
```
138+
To overwrite the default patterns set the option `slimPatternsAppendDefaults` to `false` (`true` by default).
139+
```yaml
140+
custom:
141+
pythonRequirements:
142+
slim: true
143+
slimPatternsAppendDefaults: false
144+
slimPatterns:
145+
- "**/*.egg-info*"
146+
```
137147
This will remove all folders within the installed requirements that match
138148
the names in `slimPatterns`
139149
## Omitting Packages

appveyor.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
version: '{build}'
22
init:
3-
- ps: npm i -g serverless
3+
- cmd: pip install pipenv
4+
- ps: npm i -g serverless
45
build: off
56
test_script:
6-
- cmd: >-
7-
npm i
7+
- cmd: >-
8+
npm i
89
9-
node test.js
10+
node test.js

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class ServerlessPythonRequirements {
2929
{
3030
slim: false,
3131
slimPatterns: false,
32+
slimPatternsAppendDefaults: true,
3233
zip: false,
3334
cleanupZipHelper: true,
3435
invalidateCaches: false,

lib/slim.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@ const getStripCommand = (options, folderPath) =>
1010
const deleteFiles = (options, folderPath) => {
1111
let patterns = ['**/*.py[c|o]', '**/__pycache__*', '**/*.dist-info*'];
1212
if (options.slimPatterns) {
13-
patterns = patterns.concat(options.slimPatterns);
13+
if (
14+
options.slimPatternsAppendDefaults === false ||
15+
options.slimPatternsAppendDefaults == 'false'
16+
) {
17+
patterns = options.slimPatterns;
18+
} else {
19+
patterns = patterns.concat(options.slimPatterns);
20+
}
1421
}
1522
for (const pattern of patterns) {
1623
for (const file of glob.sync(`${folderPath}/${pattern}`)) {

test.bats

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ teardown() {
303303
cd tests/base
304304
cat _slimPatterns.yml > slimPatterns.yml
305305
npm i $(npm pack ../..)
306-
sls --runtime=python2.7 --slim=true packag
306+
sls --runtime=python2.7 --slim=true package
307307
unzip .serverless/sls-py-req-test.zip -d puck
308308
ls puck/flask
309309
test $(find puck -name "*.pyc" | wc -l) -eq 0
@@ -468,7 +468,6 @@ teardown() {
468468
test $(find "puck*" -name "*.pyc" | wc -l) -eq 0
469469
}
470470

471-
472471
@test "py2.7 can package flask with package individually option" {
473472
cd tests/base
474473
npm i $(npm pack ../..)
@@ -494,7 +493,6 @@ teardown() {
494493
test $(find puck* -name "*.pyc" | wc -l) -eq 0
495494
}
496495

497-
498496
@test "py3.6 can package only requirements of module" {
499497
cd tests/individually
500498
npm i $(npm pack ../..)

test.js

Lines changed: 143 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ const deasync = require('deasync-promise');
33
const glob = require('glob-all');
44
const JSZip = require('jszip');
55
const tape = require('tape');
6-
const { removeSync, readFileSync, pathExistsSync } = require('fs-extra');
6+
7+
const { removeSync, readFileSync, pathExistsSync, copySync } = require('fs-extra');
78
const { sep } = require('path');
89

910
const { getUserCachePath } = require('./lib/shared');
@@ -63,8 +64,8 @@ const teardown = () => {
6364
removeSync('tests/base with a space');
6465
};
6566

66-
const test = (desc, func) =>
67-
tape.test(desc, t => {
67+
const test = (desc, func, opts = {}) =>
68+
tape.test(desc, opts, t => {
6869
setup();
6970
try {
7071
func(t);
@@ -89,6 +90,16 @@ const listRequirementsZipFiles = filename => {
8990
return Object.keys(reqsZip.files);
9091
};
9192

93+
const canUseDocker = () => {
94+
let result;
95+
try {
96+
result = crossSpawn.sync('docker', ['ps']);
97+
} catch (e) {
98+
return false;
99+
}
100+
return result.status === 0;
101+
};
102+
92103
test('default pythonBin can package flask with default options', t => {
93104
process.chdir('tests/base');
94105
const path = npm(['pack', '../..']);
@@ -530,6 +541,7 @@ test('py3.6 uses download cache with cacheLocation option', t => {
530541
t.end();
531542
});
532543

544+
533545
/*
534546
* News tests not in test.bats
535547
*/
@@ -569,3 +581,131 @@ test("py3.6 doesn't package bottle with zip option", t => {
569581
);
570582
t.end();
571583
});
584+
585+
test('py3.6 can package flask with slim, slimPatterns & slimPatternsAppendDefaults=false options', t => {
586+
process.chdir('tests/base');
587+
copySync('_slimPatterns.yml', 'slimPatterns.yml');
588+
const path = npm(['pack', '../..']);
589+
npm(['i', path]);
590+
sls(['--slim=true', '--slimPatternsAppendDefaults=false', 'package']);
591+
592+
const zipfiles = listZipFiles('.serverless/sls-py-req-test.zip');
593+
t.true(zipfiles.includes(`flask${sep}__init__.py`), 'flask is packaged');
594+
t.true(
595+
zipfiles.filter(filename => filename.endsWith('.pyc')).length >= 1,
596+
'pyc files are packaged'
597+
);
598+
t.deepEqual(
599+
zipfiles.filter(filename => filename.includes('.egg-info')),
600+
[],
601+
'.egg-info folders are not packaged'
602+
);
603+
t.end();
604+
});
605+
606+
test(
607+
'py3.6 can package flask with slim & dockerizePip & slimPatterns & slimPatternsAppendDefaults=false options',
608+
t => {
609+
process.chdir('tests/base');
610+
copySync('_slimPatterns.yml', 'slimPatterns.yml');
611+
const path = npm(['pack', '../..']);
612+
npm(['i', path]);
613+
sls([
614+
'--dockerizePip=true',
615+
'--slim=true',
616+
'--slimPatternsAppendDefaults=false',
617+
'package'
618+
]);
619+
620+
const zipfiles = listZipFiles('.serverless/sls-py-req-test.zip');
621+
t.true(zipfiles.includes(`flask${sep}__init__.py`), 'flask is packaged');
622+
t.true(
623+
zipfiles.filter(filename => filename.endsWith('.pyc')).length >= 1,
624+
'pyc files are packaged'
625+
);
626+
t.deepEqual(
627+
zipfiles.filter(filename => filename.includes('.egg-infooo')),
628+
[],
629+
'.egg-info folders are not packaged'
630+
);
631+
t.end();
632+
},
633+
{ skip: !canUseDocker() }
634+
);
635+
636+
test('py2.7 can package flask with slim & slimPatterns & slimPatternsAppendDefaults=false options', t => {
637+
process.chdir('tests/base');
638+
copySync('_slimPatterns.yml', 'slimPatterns.yml');
639+
const path = npm(['pack', '../..']);
640+
npm(['i', path]);
641+
sls([
642+
'--runtime=python2.7',
643+
'--slim=true',
644+
'--slimPatternsAppendDefaults=false',
645+
'package'
646+
]);
647+
648+
const zipfiles = listZipFiles('.serverless/sls-py-req-test.zip');
649+
t.true(zipfiles.includes(`flask${sep}__init__.py`), 'flask is packaged');
650+
t.true(
651+
zipfiles.filter(filename => filename.endsWith('.pyc')).length >= 1,
652+
'pyc files are packaged'
653+
);
654+
t.deepEqual(
655+
zipfiles.filter(filename => filename.includes('.egg-info')),
656+
[],
657+
'.egg-info folders are not packaged'
658+
);
659+
t.end();
660+
});
661+
662+
test(
663+
'py2.7 can package flask with slim & dockerizePip & slimPatterns & slimPatternsAppendDefaults=false options',
664+
t => {
665+
process.chdir('tests/base');
666+
copySync('_slimPatterns.yml', 'slimPatterns.yml');
667+
const path = npm(['pack', '../..']);
668+
npm(['i', path]);
669+
sls([
670+
'--dockerizePip=true',
671+
'--runtime=python2.7',
672+
'--slim=true',
673+
'--slimPatternsAppendDefaults=false',
674+
'package'
675+
]);
676+
const zipfiles = listZipFiles('.serverless/sls-py-req-test.zip');
677+
t.true(zipfiles.includes(`flask${sep}__init__.py`), 'flask is packaged');
678+
t.true(
679+
zipfiles.filter(filename => filename.endsWith('.pyc')).length >= 1,
680+
'pyc files are packaged'
681+
);
682+
t.deepEqual(
683+
zipfiles.filter(filename => filename.includes('.egg-info')),
684+
[],
685+
'.egg-info folders are not packaged'
686+
);
687+
t.end();
688+
},
689+
{ skip: !canUseDocker() }
690+
);
691+
692+
test('pipenv py3.6 can package flask with slim & slimPatterns & slimPatternsAppendDefaults=false option', t => {
693+
process.chdir('tests/pipenv');
694+
copySync('_slimPatterns.yml', 'slimPatterns.yml');
695+
const path = npm(['pack', '../..']);
696+
npm(['i', path]);
697+
698+
sls(['--slim=true', '--slimPatternsAppendDefaults=false', 'package']);
699+
const zipfiles = listZipFiles('.serverless/sls-py-req-test.zip');
700+
t.true(zipfiles.includes(`flask${sep}__init__.py`), 'flask is packaged');
701+
t.true(
702+
zipfiles.filter(filename => filename.endsWith('.pyc')).length >= 1,
703+
'pyc files are packaged'
704+
);
705+
t.deepEqual(
706+
zipfiles.filter(filename => filename.includes('.egg-info')),
707+
[],
708+
'.egg-info folders are not packaged'
709+
);
710+
t.end();
711+
});

tests/base/serverless.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ custom:
1212
dockerizePip: ${opt:dockerizePip, self:custom.defaults.dockerizePip}
1313
slim: ${opt:slim, self:custom.defaults.slim}
1414
slimPatterns: ${file(./slimPatterns.yml):slimPatterns, self:custom.defaults.slimPatterns}
15+
slimPatternsAppendDefaults: ${opt:slimPatternsAppendDefaults, self:custom.defaults.slimPatternsAppendDefaults}
1516
vendor: ${opt:vendor, ''}
1617
fileName: ${opt:fileName, 'requirements.txt'}
1718
IndividuallyMoveUpModules: ${opt:moveup, self:custom.defaults.IndividuallyMoveUpModules}
@@ -21,6 +22,7 @@ custom:
2122
defaults:
2223
slim: false
2324
slimPatterns: false
25+
slimPatternsAppendDefaults: true
2426
zip: false
2527
dockerizePip: false
2628
individually: false

tests/pipenv/serverless.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ custom:
1111
zip: ${opt:zip, self:custom.defaults.zip}
1212
slim: ${opt:slim, self:custom.defaults.slim}
1313
slimPatterns: ${file(./slimPatterns.yml):slimPatterns, self:custom.defaults.slimPatterns}
14+
slimPatternsAppendDefaults: ${opt:slimPatternsAppendDefaults, self:custom.defaults.slimPatternsAppendDefaults}
1415
dockerizePip: ${opt:dockerizePip, self:custom.defaults.dockerizePip}
1516
defaults:
1617
zip: false
1718
slimPatterns: false
19+
slimPatternsAppendDefaults: true
1820
slim: false
1921
dockerizePip: false
2022

0 commit comments

Comments
 (0)