Skip to content

Commit 99c72c5

Browse files
committed
Merge branch 'optional-packaging' of https://github.com/medwig/serverless-python-requirements into medwig-optional-packaging
2 parents caa5ca2 + b9f436c commit 99c72c5

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']);
@@ -208,6 +235,7 @@ class ServerlessPythonRequirements {
208235

209236
let before = () => BbPromise.bind(this)
210237
.then(this.addVendorHelper)
238+
.then(this.parseRequirements)
211239
.then(this.packRequirements)
212240
.then(this.linkRequirements);
213241

@@ -222,6 +250,7 @@ class ServerlessPythonRequirements {
222250
'after:deploy:function:packageFunction': after,
223251
'requirements:install:install': () => BbPromise.bind(this)
224252
.then(this.addVendorHelper)
253+
.then(this.parseRequirements)
225254
.then(this.packRequirements),
226255
'requirements:clean:clean': () => BbPromise.bind(this)
227256
.then(this.cleanup)

0 commit comments

Comments
 (0)