Skip to content

Commit f22699a

Browse files
committed
Refactor deploy omittion. closes #57
1 parent 6daeb4a commit f22699a

File tree

2 files changed

+24
-50
lines changed

2 files changed

+24
-50
lines changed

README.md

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,20 @@ 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+
noDeploy:
79+
- boto3
80+
- botocore
81+
- docutils
82+
- jmespath
83+
- python-dateutil
84+
- s3transfer
85+
- six
8486
```
8587

8688
## extra pip arguments

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)

0 commit comments

Comments
 (0)