Skip to content
This repository was archived by the owner on Oct 22, 2024. It is now read-only.

Commit 05d1819

Browse files
committed
fix resolving requirements recursively
1 parent ded3398 commit 05d1819

File tree

6 files changed

+52
-4
lines changed

6 files changed

+52
-4
lines changed

lib/pip.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,28 @@ function dockerPathForWin(path) {
338338
return path;
339339
}
340340
}
341+
342+
/**
343+
* get requirements from requirements.txt
344+
* @param {string} source
345+
* @return {string[]}
346+
*/
347+
function getRequirements(source) {
348+
const requirements = fse
349+
.readFileSync(source, { encoding: 'utf-8' })
350+
.replace(/\\\n/g, ' ')
351+
.split(/\r?\n/);
352+
353+
return requirements.reduce((acc, req) => {
354+
req = req.trim();
355+
if (!req.startsWith('-r')) {
356+
return [...acc, req];
357+
}
358+
source = path.join(path.dirname(source), req.replace(/^-r\s+/, ''));
359+
return [...acc, ...getRequirements(source)];
360+
}, []);
361+
}
362+
341363
/** create a filtered requirements.txt without anything from noDeploy
342364
* then remove all comments and empty lines, and sort the list which
343365
* assist with matching the static cache. The sorting will skip any
@@ -351,10 +373,7 @@ function dockerPathForWin(path) {
351373
*/
352374
function filterRequirementsFile(source, target, options) {
353375
const noDeploy = new Set(options.noDeploy || []);
354-
const requirements = fse
355-
.readFileSync(source, { encoding: 'utf-8' })
356-
.replace(/\\\n/g, ' ')
357-
.split(/\r?\n/);
376+
const requirements = getRequirements(source);
358377
var prepend = [];
359378
const filteredRequirements = requirements.filter(req => {
360379
req = req.trim();

test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,21 @@ test('py3.6 can package flask with hashes', t => {
157157
t.end();
158158
});
159159

160+
test('py3.6 can package flask with nested', t => {
161+
process.chdir('tests/base');
162+
const path = npm(['pack', '../..']);
163+
npm(['i', path]);
164+
sls([
165+
`--pythonBin=${getPythonBin(3)}`,
166+
'--fileName=requirements-w-nested.txt',
167+
'package'
168+
]);
169+
const zipfiles = listZipFiles('.serverless/sls-py-req-test.zip');
170+
t.true(zipfiles.includes(`flask${sep}__init__.py`), 'flask is packaged');
171+
t.true(zipfiles.includes(`boto3${sep}__init__.py`), 'boto3 is packaged');
172+
t.end();
173+
});
174+
160175
test('py3.6 can package flask with zip option', t => {
161176
process.chdir('tests/base');
162177
const path = npm(['pack', '../..']);
@@ -1458,6 +1473,10 @@ test('py3.6 can package only requirements of module', t => {
14581473
zipfiles_hello.includes(`pyaml${sep}__init__.py`),
14591474
'pyaml is packaged in function hello1'
14601475
);
1476+
t.true(
1477+
zipfiles_hello.includes(`boto3${sep}__init__.py`),
1478+
'boto3 is packaged in function hello1'
1479+
);
14611480
t.false(
14621481
zipfiles_hello.includes(`flask${sep}__init__.py`),
14631482
'flask is NOT packaged in function hello1'
@@ -1478,6 +1497,10 @@ test('py3.6 can package only requirements of module', t => {
14781497
zipfiles_hello2.includes(`pyaml${sep}__init__.py`),
14791498
'pyaml is NOT packaged in function hello2'
14801499
);
1500+
t.false(
1501+
zipfiles_hello2.includes(`boto3${sep}__init__.py`),
1502+
'boto3 is NOT packaged in function hello2'
1503+
);
14811504
t.true(
14821505
zipfiles_hello2.includes(`flask${sep}__init__.py`),
14831506
'flask is packaged in function hello2'

tests/base/requirements-common.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
boto3

tests/base/requirements-w-nested.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
flask
2+
bottle
3+
-r requirements-common.txt
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
-r ../requirements-common.txt
12
pyaml
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
boto3

0 commit comments

Comments
 (0)