Skip to content

Commit b9f436c

Browse files
author
Jon Medwig
committed
Adds option to leave python libraries out of the deployment on a line-by-line basis by adding a #no-deploy comment beside it in requirements.txt. Creates a parsed .requirements.txt file and retargets pip install to that file.
1 parent 44586e4 commit b9f436c

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

index.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,40 @@ class ServerlessPythonRequirements {
3939
}
4040
};
4141

42+
/**
43+
* parse requirements.txt into .requirements.txt, leaving out #no-deploy lines
44+
* @return true
45+
*/
46+
parseRequirements() {
47+
if (!fse.existsSync(path.join(this.serverless.config.servicePath,
48+
'requirements.txt'))) {
49+
return BbPromise.resolve();
50+
}
51+
52+
this.serverless.cli.log(
53+
`Parsing Python requirements.txt`);
54+
55+
var fs = require('fs');
56+
var reqs = fs.readFileSync("requirements.txt").toString().split('\n');
57+
58+
var newReqs = ''
59+
for (var i in reqs) {
60+
if(reqs[i].indexOf('#no-deploy') == -1) {
61+
newReqs += reqs[i] + '\n'
62+
}
63+
}
64+
fs.writeFileSync(".requirements.txt", newReqs, 'utf8');
65+
66+
return true
67+
};
68+
4269
/**
4370
* pip install the requirements to the .requirements directory
4471
* @return {Promise}
4572
*/
4673
installRequirements() {
4774
if (!fse.existsSync(path.join(this.serverless.config.servicePath,
48-
'requirements.txt'))) {
75+
'.requirements.txt'))) {
4976
return BbPromise.resolve();
5077
}
5178

@@ -58,7 +85,7 @@ class ServerlessPythonRequirements {
5885
let options;
5986
const pipCmd = [
6087
runtime, '-m', 'pip', '--isolated', 'install',
61-
'-t', '.requirements', '-r', 'requirements.txt',
88+
'-t', '.requirements', '-r', '.requirements.txt',
6289
];
6390
if (!this.custom().dockerizePip) {
6491
const pipTestRes = spawnSync(runtime, ['-m', 'pip', 'help', 'install']);
@@ -204,6 +231,7 @@ class ServerlessPythonRequirements {
204231

205232
let before = () => BbPromise.bind(this)
206233
.then(this.addVendorHelper)
234+
.then(this.parseRequirements)
207235
.then(this.packRequirements)
208236
.then(this.linkRequirements);
209237

@@ -218,6 +246,7 @@ class ServerlessPythonRequirements {
218246
'after:deploy:function:packageFunction': after,
219247
'requirements:install:install': () => BbPromise.bind(this)
220248
.then(this.addVendorHelper)
249+
.then(this.parseRequirements)
221250
.then(this.packRequirements),
222251
'requirements:clean:clean': () => BbPromise.bind(this)
223252
.then(this.cleanup)

0 commit comments

Comments
 (0)