Skip to content

Fix resolving requirements recursively #396

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 2 commits into from
Aug 13, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
27 changes: 23 additions & 4 deletions lib/pip.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,28 @@ function dockerPathForWin(path) {
return path;
}
}

/**
* get requirements from requirements.txt
* @param {string} source
* @return {string[]}
*/
function getRequirements(source) {
const requirements = fse
.readFileSync(source, { encoding: 'utf-8' })
.replace(/\\\n/g, ' ')
.split(/\r?\n/);

return requirements.reduce((acc, req) => {
req = req.trim();
if (!req.startsWith('-r')) {
return [...acc, req];
}
source = path.join(path.dirname(source), req.replace(/^-r\s+/, ''));
return [...acc, ...getRequirements(source)];
}, []);
}

/** create a filtered requirements.txt without anything from noDeploy
* then remove all comments and empty lines, and sort the list which
* assist with matching the static cache. The sorting will skip any
Expand All @@ -351,10 +373,7 @@ function dockerPathForWin(path) {
*/
function filterRequirementsFile(source, target, options) {
const noDeploy = new Set(options.noDeploy || []);
const requirements = fse
.readFileSync(source, { encoding: 'utf-8' })
.replace(/\\\n/g, ' ')
.split(/\r?\n/);
const requirements = getRequirements(source);
var prepend = [];
const filteredRequirements = requirements.filter(req => {
req = req.trim();
Expand Down
23 changes: 23 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,21 @@ test('py3.6 can package flask with hashes', t => {
t.end();
});

test('py3.6 can package flask with nested', t => {
process.chdir('tests/base');
const path = npm(['pack', '../..']);
npm(['i', path]);
sls([
`--pythonBin=${getPythonBin(3)}`,
'--fileName=requirements-w-nested.txt',
'package'
]);
const zipfiles = listZipFiles('.serverless/sls-py-req-test.zip');
t.true(zipfiles.includes(`flask${sep}__init__.py`), 'flask is packaged');
t.true(zipfiles.includes(`boto3${sep}__init__.py`), 'boto3 is packaged');
t.end();
});

test('py3.6 can package flask with zip option', t => {
process.chdir('tests/base');
const path = npm(['pack', '../..']);
Expand Down Expand Up @@ -1458,6 +1473,10 @@ test('py3.6 can package only requirements of module', t => {
zipfiles_hello.includes(`pyaml${sep}__init__.py`),
'pyaml is packaged in function hello1'
);
t.true(
zipfiles_hello.includes(`boto3${sep}__init__.py`),
'boto3 is packaged in function hello1'
);
t.false(
zipfiles_hello.includes(`flask${sep}__init__.py`),
'flask is NOT packaged in function hello1'
Expand All @@ -1478,6 +1497,10 @@ test('py3.6 can package only requirements of module', t => {
zipfiles_hello2.includes(`pyaml${sep}__init__.py`),
'pyaml is NOT packaged in function hello2'
);
t.false(
zipfiles_hello2.includes(`boto3${sep}__init__.py`),
'boto3 is NOT packaged in function hello2'
);
t.true(
zipfiles_hello2.includes(`flask${sep}__init__.py`),
'flask is packaged in function hello2'
Expand Down
1 change: 1 addition & 0 deletions tests/base/requirements-common.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
boto3
3 changes: 3 additions & 0 deletions tests/base/requirements-w-nested.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
flask
bottle
-r requirements-common.txt
1 change: 1 addition & 0 deletions tests/individually/module1/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
-r ../requirements-common.txt
pyaml
1 change: 1 addition & 0 deletions tests/individually/requirements-common.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
boto3