Skip to content

Fixes weird bug, where root & submodule requirements weren't packaged correctly #303

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
Jan 4, 2019
Merged
Show file tree
Hide file tree
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
44 changes: 19 additions & 25 deletions lib/pip.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ function mergeCommands(commands) {
* @param {Object} options
* @return {undefined}
*/
function installRequirementsFile(
function generateRequirementsFile(
requirementsPath,
targetFile,
serverless,
servicePath,
options
) {
if (options.usePipenv && fse.existsSync(path.join(servicePath, 'Pipfile'))) {
generateRequirementsFile(
filterRequirementsFile(
path.join(servicePath, '.serverless/requirements.txt'),
targetFile,
options
Expand All @@ -70,7 +70,7 @@ function installRequirementsFile(
`Parsed requirements.txt from Pipfile in ${targetFile}...`
);
} else {
generateRequirementsFile(requirementsPath, targetFile, options);
filterRequirementsFile(requirementsPath, targetFile, options);
serverless.cli.log(
`Generated requirements from ${requirementsPath} in ${targetFile}...`
);
Expand Down Expand Up @@ -306,7 +306,6 @@ function dockerPathForWin(path) {
return path;
}
}

/** 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 @@ -318,7 +317,7 @@ function dockerPathForWin(path) {
* @param {string} target requirements where results are written
* @param {Object} options
*/
function generateRequirementsFile(source, target, options) {
function filterRequirementsFile(source, target, options) {
const noDeploy = new Set(options.noDeploy || []);
const requirements = fse
.readFileSync(source, { encoding: 'utf-8' })
Expand Down Expand Up @@ -413,11 +412,21 @@ function installRequirementsIfNeeded(
}
}

// First, generate the requirements file to our local .serverless folder
fse.ensureDirSync(path.join(servicePath, '.serverless'));
const slsReqsTxt = path.join(servicePath, '.serverless', 'requirements.txt');
let requirementsTxtDirectory;
// Copy our requirements to another path in .serverless (incase of individually packaged)
if (modulePath && modulePath !== '.') {
requirementsTxtDirectory = path.join(
servicePath,
'.serverless',
modulePath
);
} else {
requirementsTxtDirectory = path.join(servicePath, '.serverless');
}
fse.ensureDirSync(requirementsTxtDirectory);
const slsReqsTxt = path.join(requirementsTxtDirectory, 'requirements.txt');

installRequirementsFile(
generateRequirementsFile(
fileName,
slsReqsTxt,
serverless,
Expand All @@ -433,28 +442,13 @@ function installRequirementsIfNeeded(
return false;
}

// Copy our requirements to another filename in .serverless (incase of individually packaged)
if (modulePath && modulePath != '.') {
fse.existsSync(path.join(servicePath, '.serverless', modulePath));
const destinationFile = path.join(
servicePath,
'.serverless',
modulePath,
'requirements.txt'
);
serverless.cli.log(
`Copying from ${slsReqsTxt} into ${destinationFile} ...`
);
fse.copySync(slsReqsTxt, destinationFile);
}

// Then generate our MD5 Sum of this requirements file to determine where it should "go" to and/or pull cache from
const reqChecksum = md5Path(slsReqsTxt);

// Then figure out where this cache should be, if we're caching, if we're in a module, etc
const workingReqsFolder = getRequirementsWorkingPath(
reqChecksum,
servicePath,
requirementsTxtDirectory,
options
);

Expand Down
8 changes: 6 additions & 2 deletions lib/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ function checkForAndDeleteMaxCacheVersions(options, serverless) {
* @param {Object} options
* @return {string}
*/
function getRequirementsWorkingPath(subfolder, servicePath, options) {
function getRequirementsWorkingPath(
subfolder,
requirementsTxtDirectory,
options
) {
// If we want to use the static cache
if (options && options.useStaticCache) {
if (subfolder) {
Expand All @@ -69,7 +73,7 @@ function getRequirementsWorkingPath(subfolder, servicePath, options) {
}

// If we don't want to use the static cache, then fallback to the way things used to work
return path.join(servicePath, '.serverless', 'requirements');
return path.join(requirementsTxtDirectory, 'requirements');
}

/**
Expand Down
158 changes: 157 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1008,35 +1008,79 @@ test('py3.6 can package flask with package individually option', t => {
sls(['--individually=true', 'package']);

const zipfiles_hello = listZipFiles('.serverless/hello.zip');
t.false(
zipfiles_hello.includes(`fn2${sep}__init__.py`),
'fn2 is NOT packaged in function hello'
);
t.true(
zipfiles_hello.includes('handler.py'),
'handler.py is packaged in function hello'
);
t.false(
zipfiles_hello.includes(`dataclasses.py`),
'dataclasses is NOT packaged in function hello'
);
t.true(
zipfiles_hello.includes(`flask${sep}__init__.py`),
'flask is packaged in function hello'
);

const zipfiles_hello2 = listZipFiles('.serverless/hello2.zip');
t.false(
zipfiles_hello2.includes(`fn2${sep}__init__.py`),
'fn2 is NOT packaged in function hello2'
);
t.true(
zipfiles_hello2.includes('handler.py'),
'handler.py is packaged in function hello2'
);
t.false(
zipfiles_hello2.includes(`dataclasses.py`),
'dataclasses is NOT packaged in function hello2'
);
t.true(
zipfiles_hello2.includes(`flask${sep}__init__.py`),
'flask is packaged in function hello2'
);

const zipfiles_hello3 = listZipFiles('.serverless/hello3.zip');
t.false(
zipfiles_hello3.includes(`fn2${sep}__init__.py`),
'fn2 is NOT packaged in function hello3'
);
t.true(
zipfiles_hello3.includes('handler.py'),
'handler.py is packaged in function hello2'
'handler.py is packaged in function hello3'
);
t.false(
zipfiles_hello3.includes(`dataclasses.py`),
'dataclasses is NOT packaged in function hello3'
);
t.false(
zipfiles_hello3.includes(`flask${sep}__init__.py`),
'flask is NOT packaged in function hello3'
);

const zipfiles_hello4 = listZipFiles(
'.serverless/fn2-sls-py-req-test-dev-hello4.zip'
);
t.false(
zipfiles_hello4.includes(`fn2${sep}__init__.py`),
'fn2 is NOT packaged in function hello4'
);
t.true(
zipfiles_hello4.includes('fn2_handler.py'),
'fn2_handler is packaged in the zip-root in function hello4'
);
t.true(
zipfiles_hello4.includes(`dataclasses.py`),
'dataclasses is packaged in function hello4'
);
t.false(
zipfiles_hello4.includes(`flask${sep}__init__.py`),
'flask is NOT packaged in function hello4'
);

t.end();
});

Expand All @@ -1060,6 +1104,10 @@ test('py3.6 can package flask with package individually & slim option', t => {
zipfiles_hello.includes(`flask${sep}__init__.py`),
'flask is packaged in function hello'
);
t.false(
zipfiles_hello.includes(`dataclasses.py`),
'dataclasses is NOT packaged in function hello'
);

const zipfiles_hello2 = listZipFiles('.serverless/hello2.zip');
t.true(
Expand All @@ -1075,6 +1123,10 @@ test('py3.6 can package flask with package individually & slim option', t => {
zipfiles_hello2.includes(`flask${sep}__init__.py`),
'flask is packaged in function hello2'
);
t.false(
zipfiles_hello2.includes(`dataclasses.py`),
'dataclasses is NOT packaged in function hello2'
);

const zipfiles_hello3 = listZipFiles('.serverless/hello3.zip');
t.true(
Expand All @@ -1091,6 +1143,27 @@ test('py3.6 can package flask with package individually & slim option', t => {
'flask is NOT packaged in function hello3'
);

const zipfiles_hello4 = listZipFiles(
'.serverless/fn2-sls-py-req-test-dev-hello4.zip'
);
t.true(
zipfiles_hello4.includes('fn2_handler.py'),
'fn2_handler is packaged in the zip-root in function hello4'
);
t.true(
zipfiles_hello4.includes(`dataclasses.py`),
'dataclasses is packaged in function hello4'
);
t.false(
zipfiles_hello4.includes(`flask${sep}__init__.py`),
'flask is NOT packaged in function hello4'
);
t.deepEqual(
zipfiles_hello4.filter(filename => filename.endsWith('.pyc')),
[],
'no pyc files packaged in function hello4'
);

t.end();
});

Expand All @@ -1109,6 +1182,10 @@ test('py2.7 can package flask with package individually option', t => {
zipfiles_hello.includes(`flask${sep}__init__.py`),
'flask is packaged in function hello'
);
t.false(
zipfiles_hello.includes(`dataclasses.py`),
'dataclasses is NOT packaged in function hello'
);

const zipfiles_hello2 = listZipFiles('.serverless/hello2.zip');
t.true(
Expand All @@ -1119,6 +1196,10 @@ test('py2.7 can package flask with package individually option', t => {
zipfiles_hello2.includes(`flask${sep}__init__.py`),
'flask is packaged in function hello2'
);
t.false(
zipfiles_hello2.includes(`dataclasses.py`),
'dataclasses is NOT packaged in function hello2'
);

const zipfiles_hello3 = listZipFiles('.serverless/hello3.zip');
t.true(
Expand All @@ -1129,6 +1210,26 @@ test('py2.7 can package flask with package individually option', t => {
zipfiles_hello3.includes(`flask${sep}__init__.py`),
'flask is NOT packaged in function hello3'
);
t.false(
zipfiles_hello3.includes(`dataclasses.py`),
'dataclasses is NOT packaged in function hello3'
);

const zipfiles_hello4 = listZipFiles(
'.serverless/fn2-sls-py-req-test-dev-hello4.zip'
);
t.true(
zipfiles_hello4.includes('fn2_handler.py'),
'fn2_handler is packaged in the zip-root in function hello4'
);
t.true(
zipfiles_hello4.includes(`dataclasses.py`),
'dataclasses is packaged in function hello4'
);
t.false(
zipfiles_hello4.includes(`flask${sep}__init__.py`),
'flask is NOT packaged in function hello4'
);

t.end();
});
Expand All @@ -1153,6 +1254,10 @@ test('py2.7 can package flask with package individually & slim option', t => {
zipfiles_hello.includes(`flask${sep}__init__.py`),
'flask is packaged in function hello'
);
t.false(
zipfiles_hello.includes(`dataclasses.py`),
'dataclasses is NOT packaged in function hello'
);

const zipfiles_hello2 = listZipFiles('.serverless/hello2.zip');
t.true(
Expand All @@ -1168,6 +1273,10 @@ test('py2.7 can package flask with package individually & slim option', t => {
zipfiles_hello2.includes(`flask${sep}__init__.py`),
'flask is packaged in function hello2'
);
t.false(
zipfiles_hello2.includes(`dataclasses.py`),
'dataclasses is NOT packaged in function hello2'
);

const zipfiles_hello3 = listZipFiles('.serverless/hello3.zip');
t.true(
Expand All @@ -1183,6 +1292,26 @@ test('py2.7 can package flask with package individually & slim option', t => {
zipfiles_hello3.includes(`flask${sep}__init__.py`),
'flask is NOT packaged in function hello3'
);
t.false(
zipfiles_hello3.includes(`dataclasses.py`),
'dataclasses is NOT packaged in function hello3'
);

const zipfiles_hello4 = listZipFiles(
'.serverless/fn2-sls-py-req-test-dev-hello4.zip'
);
t.true(
zipfiles_hello4.includes('fn2_handler.py'),
'fn2_handler is packaged in the zip-root in function hello4'
);
t.true(
zipfiles_hello4.includes(`dataclasses.py`),
'dataclasses is packaged in function hello4'
);
t.false(
zipfiles_hello4.includes(`flask${sep}__init__.py`),
'flask is NOT packaged in function hello4'
);

t.end();
});
Expand Down Expand Up @@ -1255,6 +1384,10 @@ test('py3.6 can package lambda-decorators using vendor and invidiually option',
zipfiles_hello.includes(`lambda_decorators.py`),
'lambda_decorators.py is packaged in function hello'
);
t.false(
zipfiles_hello.includes(`dataclasses.py`),
'dataclasses is NOT packaged in function hello'
);

const zipfiles_hello2 = listZipFiles('.serverless/hello2.zip');
t.true(
Expand All @@ -1269,6 +1402,10 @@ test('py3.6 can package lambda-decorators using vendor and invidiually option',
zipfiles_hello2.includes(`lambda_decorators.py`),
'lambda_decorators.py is packaged in function hello2'
);
t.false(
zipfiles_hello2.includes(`dataclasses.py`),
'dataclasses is NOT packaged in function hello2'
);

const zipfiles_hello3 = listZipFiles('.serverless/hello3.zip');
t.true(
Expand All @@ -1283,7 +1420,26 @@ test('py3.6 can package lambda-decorators using vendor and invidiually option',
zipfiles_hello3.includes(`lambda_decorators.py`),
'lambda_decorators.py is NOT packaged in function hello3'
);
t.false(
zipfiles_hello3.includes(`dataclasses.py`),
'dataclasses is NOT packaged in function hello3'
);

const zipfiles_hello4 = listZipFiles(
'.serverless/fn2-sls-py-req-test-dev-hello4.zip'
);
t.true(
zipfiles_hello4.includes('fn2_handler.py'),
'fn2_handler is packaged in the zip-root in function hello4'
);
t.true(
zipfiles_hello4.includes(`dataclasses.py`),
'dataclasses is packaged in function hello4'
);
t.false(
zipfiles_hello4.includes(`flask${sep}__init__.py`),
'flask is NOT packaged in function hello4'
);
t.end();
});

Expand Down
Empty file added tests/base/fn2/__init__.py
Empty file.
Empty file added tests/base/fn2/fn2_handler.py
Empty file.
Loading