Skip to content

Adds option to leave python libraries out of the deployment #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 16, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,40 @@ class ServerlessPythonRequirements {
}
};

/**
* parse requirements.txt into .requirements.txt, leaving out #no-deploy lines
* @return true
*/
parseRequirements() {
if (!fse.existsSync(path.join(this.serverless.config.servicePath,
'requirements.txt'))) {
return BbPromise.resolve();
}

this.serverless.cli.log(
`Parsing Python requirements.txt`);

var fs = require('fs');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just use fs-extra which is already imported as fse.

var reqs = fs.readFileSync("requirements.txt").toString().split('\n');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const please.


var newReqs = ''
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let please.

for (var i in reqs) {
Copy link
Contributor

@dschep dschep Jun 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for (.. of ..) and template strings and triple equal:

for (const req of reqs) {
  if (req.indexOf('#no-deploy') === -1) {
    newReqs += `${req}\n`;
  }
}

if(reqs[i].indexOf('#no-deploy') == -1) {
newReqs += reqs[i] + '\n'
}
}
fs.writeFileSync(".requirements.txt", newReqs, 'utf8');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Build tools should use mktemp to avoid clobbering existing files whenever possible.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(BTW, just using mktemp as an example, you obviously can't write this into /tmp and have it be visible.)


return true
};

/**
* pip install the requirements to the .requirements directory
* @return {Promise}
*/
installRequirements() {
if (!fse.existsSync(path.join(this.serverless.config.servicePath,
'requirements.txt'))) {
'.requirements.txt'))) {
return BbPromise.resolve();
}

Expand All @@ -58,7 +85,7 @@ class ServerlessPythonRequirements {
let options;
const pipCmd = [
runtime, '-m', 'pip', '--isolated', 'install',
'-t', '.requirements', '-r', 'requirements.txt',
'-t', '.requirements', '-r', '.requirements.txt',
];
if (!this.custom().dockerizePip) {
const pipTestRes = spawnSync(runtime, ['-m', 'pip', 'help', 'install']);
Expand Down Expand Up @@ -204,6 +231,7 @@ class ServerlessPythonRequirements {

let before = () => BbPromise.bind(this)
.then(this.addVendorHelper)
.then(this.parseRequirements)
.then(this.packRequirements)
.then(this.linkRequirements);

Expand All @@ -218,6 +246,7 @@ class ServerlessPythonRequirements {
'after:deploy:function:packageFunction': after,
'requirements:install:install': () => BbPromise.bind(this)
.then(this.addVendorHelper)
.then(this.parseRequirements)
.then(this.packRequirements),
'requirements:clean:clean': () => BbPromise.bind(this)
.then(this.cleanup)
Expand Down