Skip to content

Speed up requirements installation by skipping noDeploy #146

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
Mar 5, 2018
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
29 changes: 19 additions & 10 deletions lib/pip.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,19 @@ function installRequirements(requirementsPath, targetFolder, serverless, service
fse.ensureDirSync(targetRequirementsFolder);

const dotSlsReqs = path.join(targetFolder, 'requirements.txt');
let fileName = requirementsPath;
if (options.usePipenv && fse.existsSync(path.join(servicePath, 'Pipfile'))) {
fileName = dotSlsReqs;
generateRequirementsFile(dotSlsReqs, dotSlsReqs, options);
} else {
generateRequirementsFile(requirementsPath, dotSlsReqs, options);
}

serverless.cli.log(`Installing requirements of ${requirementsPath} in ${targetFolder}...`);

// In case the requirements file is a symlink, copy it to targetFolder
// if using docker to avoid errors
if (options.dockerizePip && fileName !== dotSlsReqs) {
fse.copySync(fileName, dotSlsReqs);
fileName = dotSlsReqs;
}

let cmd;
let cmdOptions;
let pipCmd = [
options.pythonBin, '-m', 'pip', '--isolated', 'install',
'-t', targetRequirementsFolder, '-r', fileName,
'-t', targetRequirementsFolder, '-r', dotSlsReqs,
...options.pipCmdExtraArgs,
];
if (!options.dockerizePip) {
Expand Down Expand Up @@ -122,6 +116,21 @@ function installRequirements(requirementsPath, targetFolder, serverless, service
}
};

/**
* create a filtered requirements.txt without anything from noDeploy
* @param {string} source requirements
* @param {string} target requirements where results are written
* @param {Object} options
*/
function generateRequirementsFile(source, target, options) {
const noDeploy = new Set(options.noDeploy || []);
const requirements = fse.readFileSync(source, {encoding: 'utf-8'}).split(/\r?\n/);
const filteredRequirements = requirements.filter((req) => {
return !noDeploy.has(req.split(/[=<> \t]/)[0].trim());
});
fse.writeFileSync(target, filteredRequirements.join('\n'));
}

/**
* pip install the requirements to the .serverless/requirements directory
* @return {undefined}
Expand Down