Skip to content

Translate Pipfiles to Requirements in modules #341

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

Closed
Closed
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
2 changes: 0 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const {
const { injectAllRequirements } = require('./lib/inject');
const { layerRequirements } = require('./lib/layer');
const { installAllRequirements } = require('./lib/pip');
const { pipfileToRequirements } = require('./lib/pipenv');
const { pyprojectTomlToRequirements } = require('./lib/poetry');
const { cleanup, cleanupCache } = require('./lib/clean');

Expand Down Expand Up @@ -156,7 +155,6 @@ class ServerlessPythonRequirements {
return;
}
return BbPromise.bind(this)
.then(pipfileToRequirements)
.then(pyprojectTomlToRequirements)
.then(addVendorHelper)
.then(installAllRequirements)
Expand Down
23 changes: 13 additions & 10 deletions lib/pip.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const {
getRequirementsWorkingPath,
getUserCachePath
} = require('./shared');
const { pipfileToRequirements } = require('./pipenv');

/**
* Omit empty commands.
Expand Down Expand Up @@ -55,6 +56,7 @@ function mergeCommands(commands) {
*/
function generateRequirementsFile(
requirementsPath,
modulePath,
targetFile,
serverless,
servicePath,
Expand All @@ -74,13 +76,10 @@ function generateRequirementsFile(
);
} else if (
options.usePipenv &&
fse.existsSync(path.join(servicePath, 'Pipfile'))
fse.existsSync(path.join(servicePath, modulePath, 'Pipfile'))
) {
filterRequirementsFile(
path.join(servicePath, '.serverless/requirements.txt'),
targetFile,
options
);
pipfileToRequirements(modulePath, targetFile, serverless, options);
filterRequirementsFile(targetFile, targetFile, options);
serverless.cli.log(
`Parsed requirements.txt from Pipfile in ${targetFile}...`
);
Expand Down Expand Up @@ -439,19 +438,22 @@ function copyVendors(vendorFolder, targetFolder, serverless) {
* @param {Object} options
* @param {string} fileName
*/
function requirementsFileExists(servicePath, options, fileName) {
function requirementsFileExists(servicePath, modulePath, options) {
if (
options.usePoetry &&
fse.existsSync(path.join(servicePath, 'pyproject.toml'))
) {
return true;
}

if (options.usePipenv && fse.existsSync(path.join(servicePath, 'Pipfile'))) {
if (
options.usePipenv &&
fse.existsSync(path.join(servicePath, modulePath, 'Pipfile'))
) {
return true;
}

if (fse.existsSync(fileName)) {
if (fse.existsSync(path.join(servicePath, modulePath, options.fileName))) {
return true;
}

Expand Down Expand Up @@ -480,7 +482,7 @@ function installRequirementsIfNeeded(
const fileName = path.join(servicePath, modulePath, options.fileName);

// Skip requirements generation, if requirements file doesn't exist
if (!requirementsFileExists(servicePath, options, fileName)) {
if (!requirementsFileExists(servicePath, modulePath, options)) {
return false;
}

Expand All @@ -500,6 +502,7 @@ function installRequirementsIfNeeded(

generateRequirementsFile(
fileName,
modulePath,
slsReqsTxt,
serverless,
servicePath,
Expand Down
23 changes: 14 additions & 9 deletions lib/pipenv.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,29 @@ const { EOL } = require('os');
/**
* pipenv install
*/
function pipfileToRequirements() {
if (
!this.options.usePipenv ||
!fse.existsSync(path.join(this.servicePath, 'Pipfile'))
) {
function pipfileToRequirements(
modulePath,
outputRequirements,
serverless,
options
) {
const pipenvPath = path.join(modulePath, 'Pipfile');

// Stop if Pipfile file does not exist
if (!options.usePipenv || !fse.existsSync(pipenvPath)) {
return;
}

this.serverless.cli.log('Generating requirements.txt from Pipfile...');
serverless.cli.log('Generating requirements.txt from Pipfile...');

const res = spawnSync(
'pipenv',
['lock', '--requirements', '--keep-outdated'],
{
cwd: this.servicePath
cwd: modulePath
}
);

if (res.error) {
if (res.error.code === 'ENOENT') {
throw new Error(
Expand All @@ -34,9 +40,8 @@ function pipfileToRequirements() {
if (res.status !== 0) {
throw new Error(res.stderr);
}
fse.ensureDirSync(path.join(this.servicePath, '.serverless'));
fse.writeFileSync(
path.join(this.servicePath, '.serverless/requirements.txt'),
outputRequirements,
removeEditableFlagFromRequirementsString(res.stdout)
);
}
Expand Down
14 changes: 10 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1620,8 +1620,11 @@ test(
'foobar has retained its executable file permissions'
);

const zipfiles_hello2 = listZipFilesWithMetaData('.serverless/module2-sls-py-req-test-indiv-dev-hello2.zip');
const flaskPerm = statSync('.serverless/module2/requirements/bin/flask').mode;
const zipfiles_hello2 = listZipFilesWithMetaData(
'.serverless/module2-sls-py-req-test-indiv-dev-hello2.zip'
);
const flaskPerm = statSync('.serverless/module2/requirements/bin/flask')
.mode;

t.true(
zipfiles_hello2['bin/flask'].unixPermissions === flaskPerm,
Expand Down Expand Up @@ -1654,8 +1657,11 @@ test(
'foobar has retained its executable file permissions'
);

const zipfiles_hello2 = listZipFilesWithMetaData('.serverless/module2-sls-py-req-test-indiv-dev-hello2.zip');
const flaskPerm = statSync('.serverless/module2/requirements/bin/flask').mode;
const zipfiles_hello2 = listZipFilesWithMetaData(
'.serverless/module2-sls-py-req-test-indiv-dev-hello2.zip'
);
const flaskPerm = statSync('.serverless/module2/requirements/bin/flask')
.mode;

t.true(
zipfiles_hello2['bin/flask'].unixPermissions === flaskPerm,
Expand Down