Skip to content

Commit 31befc9

Browse files
authored
Merge pull request #58 from UnitedIncome/refactored-nodeploy
Refactor deploy omittion. closes #57
2 parents 6daeb4a + 3b947d1 commit 31befc9

File tree

5 files changed

+41
-50
lines changed

5 files changed

+41
-50
lines changed

README.md

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,21 @@ custom:
6969
```
7070

7171
## Omitting Packages
72-
You can omit a package from deployment by adding `#no-deploy` to the
73-
requirement's line in `requirements.txt`. For example, this will not install
74-
the AWS SDKs that are already installed on Lambda, but will install numpy:
75-
```
76-
numpy
77-
boto3 #no-deploy
78-
botocore #no-deploy
79-
docutils #no-deploy
80-
jmespath #no-deploy
81-
python-dateutil #no-deploy
82-
s3transfer #no-deploy
83-
six #no-deploy
72+
You can omit a package from deployment with the `noDeploy` option. Note that
73+
dependencies of omitted packages must explicitly be omitted too.
74+
For example, this will not install the AWS SDKs that are already installed on
75+
Lambda:
76+
```yaml
77+
custom:
78+
pythonRequirements:
79+
noDeploy:
80+
- boto3
81+
- botocore
82+
- docutils
83+
- jmespath
84+
- python-dateutil
85+
- s3transfer
86+
- six
8487
```
8588

8689
## extra pip arguments

example/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
requests
2+
six

example/serverless.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ custom:
1111
zip: false
1212
cleanupZipHelper: true
1313
dockerizePip: false
14+
noDeploy: []
1415

1516
package:
1617
exclude:

index.js

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -41,41 +41,13 @@ class ServerlessPythonRequirements {
4141
}
4242
};
4343

44-
/**
45-
* parse requirements.txt into .requirements.txt, leaving out #no-deploy lines
46-
* @return {true}
47-
*/
48-
parseRequirements() {
49-
if (!fse.existsSync(path.join(this.serverless.config.servicePath,
50-
'requirements.txt'))) {
51-
return true;
52-
}
53-
54-
this.serverless.cli.log(
55-
`Parsing Python requirements.txt`);
56-
57-
const reqs = fse.readFileSync('requirements.txt').toString().split('\n');
58-
59-
let newReqs = '';
60-
for (const req of reqs) {
61-
if (req.indexOf('#no-deploy') === -1) {
62-
newReqs += `${req}\n`;
63-
}
64-
}
65-
if (!fse.existsSync('.serverless'))
66-
fse.mkdirSync('.serverless');
67-
fse.writeFileSync('.serverless/requirements.txt', newReqs, 'utf8');
68-
69-
return true;
70-
};
71-
7244
/**
7345
* pip install the requirements to the .requirements directory
7446
* @return {Promise}
7547
*/
7648
installRequirements() {
7749
if (!fse.existsSync(path.join(this.serverless.config.servicePath,
78-
'.serverless/requirements.txt'))) {
50+
'requirements.txt'))) {
7951
return BbPromise.resolve();
8052
}
8153

@@ -88,7 +60,7 @@ class ServerlessPythonRequirements {
8860
let options;
8961
const pipCmd = [
9062
runtime, '-m', 'pip', '--isolated', 'install',
91-
'-t', '.requirements', '-r', '.serverless/requirements.txt',
63+
'-t', '.requirements', '-r', 'requirements.txt',
9264
];
9365
if (this.custom().pipCmdExtraArgs) {
9466
pipCmd.push(...this.custom().pipCmdExtraArgs);
@@ -148,9 +120,12 @@ class ServerlessPythonRequirements {
148120
linkRequirements() {
149121
if (!this.custom().zip) {
150122
this.serverless.cli.log('Linking required Python packages...');
123+
const noDeploy = new Set(this.custom().noDeploy || []);
151124
fse.readdirSync('.requirements').map((file) => {
152-
this.serverless.service.package.include.push(file);
153-
this.serverless.service.package.include.push(`${file}/**`);
125+
if (noDeploy.has(file))
126+
return;
127+
this.serverless.service.package.include.push(file);
128+
this.serverless.service.package.include.push(`${file}/**`);
154129
try {
155130
fse.symlinkSync(`.requirements/${file}`, `./${file}`);
156131
} catch (exception) {
@@ -159,11 +134,10 @@ class ServerlessPythonRequirements {
159134
linkDest = fse.readlinkSync(`./${file}`);
160135
} catch (e) {}
161136
if (linkDest !== `.requirements/${file}`)
162-
throw new Error(`Unable to link dependency '${file}' because a file
163-
by the same name exists in this service`);
137+
throw new Error(`Unable to link dependency '${file}' because a file
138+
by the same name exists in this service`);
164139
}
165-
}
166-
);
140+
});
167141
}
168142
}
169143

@@ -244,7 +218,6 @@ class ServerlessPythonRequirements {
244218

245219
let before = () => BbPromise.bind(this)
246220
.then(this.addVendorHelper)
247-
.then(this.parseRequirements)
248221
.then(this.packRequirements)
249222
.then(this.linkRequirements);
250223

@@ -270,7 +243,6 @@ class ServerlessPythonRequirements {
270243
'after:deploy:function:packageFunction': after,
271244
'requirements:install:install': () => BbPromise.bind(this)
272245
.then(this.addVendorHelper)
273-
.then(this.parseRequirements)
274246
.then(this.packRequirements),
275247
'requirements:clean:clean': () => BbPromise.bind(this)
276248
.then(this.cleanup)

test.bats

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ teardown() {
2929
ls puck/.requirements.zip puck/unzip_requirements.py
3030
}
3131

32+
@test "py3.6 doesn't package six with noDeploy option" {
33+
sed -i'.bak' -e 's/noDeploy:.*/noDeploy: [six]/' serverless.yml
34+
sls package
35+
unzip .serverless/sls-py-req-test.zip -d puck
36+
! ls puck/six
37+
}
38+
3239
@test "py3.6 can package requests with zip & dockerizePip option" {
3340
[ -z "$CIRCLE_BRANCH" ] || skip "Volumes are weird in CircleCI https://circleci.com/docs/2.0/building-docker-images/#mounting-folders"
3441
! uname -sm|grep Linux || groups|grep docker || id -u|egrep '^0$' || skip "can't dockerize on linux if not root & not in docker group"
@@ -61,6 +68,13 @@ teardown() {
6168
ls puck/.requirements.zip puck/unzip_requirements.py
6269
}
6370

71+
@test "py2.7 doesn't package six with noDeploy option" {
72+
sed -i'.bak' -e 's/runtime: *python3.6/runtime: python2.7/' -e 's/noDeploy:.*/noDeploy: [six]/' serverless.yml
73+
sls package
74+
unzip .serverless/sls-py-req-test.zip -d puck
75+
! ls puck/six
76+
}
77+
6478
@test "py2.7 can package requests with zip & dockerizePip option" {
6579
[ -z "$CIRCLE_BRANCH" ] || skip "Volumes are weird in CircleCI https://circleci.com/docs/2.0/building-docker-images/#mounting-folders"
6680
! uname -sm|grep Linux || groups|grep docker || id -u|egrep '^0$' || skip "can't dockerize on linux if not root & not in docker group"

0 commit comments

Comments
 (0)