Skip to content

Commit

Permalink
Enhance poetry related logging and add new flag
Browse files Browse the repository at this point in the history
* Distinct logging whether the requirements.txt file is generated from
  poetry.lock vs pyproject.toml.
* New boolean flag: requirePoetryLockFile - When set to true, fail the build
  when poetry.lock is missing. This helps with creating reproducible
  builds where you want to have a fixed poetry.lock file instead of one
  generated on the fly.
  • Loading branch information
FinchPowers committed Oct 5, 2022
1 parent 6fbdde1 commit 794a646
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 5 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,15 @@ custom:
usePoetry: false
```

Be aware that if no `poetry.lock` file is present, a new one will be generated on the fly. To help having predictable builds,
you can set the `requirePoetryLockFile` flag to true to throw an error when `poetry.lock` is missing.

```yaml
custom:
pythonRequirements:
requirePoetryLockFile: false
```

### Poetry with git dependencies

Poetry by default generates the exported requirements.txt file with `-e` and that breaks pip with `-t` parameter
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class ServerlessPythonRequirements {
pipCmdExtraArgs: [],
noDeploy: [],
vendor: '',
requirePoetryLockFile: false,
},
(this.serverless.service.custom &&
this.serverless.service.custom.pythonRequirements) ||
Expand Down
25 changes: 20 additions & 5 deletions lib/poetry.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,27 @@ async function pyprojectTomlToRequirements(modulePath, pluginInstance) {
generateRequirementsProgress = progress.get(
'python-generate-requirements-toml'
);
generateRequirementsProgress.update(
'Generating requirements.txt from "pyproject.toml"'
);
log.info('Generating requirements.txt from "pyproject.toml"');
}

const emitMsg = (msg) => {
if (generateRequirementsProgress) {
generateRequirementsProgress.update(msg);
log.info(msg);
} else {
serverless.cli.log(msg);
}
};

if (fs.existsSync('poetry.lock')) {
emitMsg('Generating requirements.txt from poetry.lock');
} else {
serverless.cli.log('Generating requirements.txt from pyproject.toml...');
if (options.requirePoetryLockFile) {
throw new serverless.classes.Error(
'poetry.lock file not found - set requirePoetryLockFile to false to ' +
'disable this error'
);
}
emitMsg('Generating poetry.lock and requirements.txt from pyproject.toml');
}

try {
Expand Down
24 changes: 24 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1634,3 +1634,27 @@ test('py3.7 can ignore functions defined with `image`', async (t) => {

t.end();
});

test(
'poetry py3.6 fails packaging if poetry.lock is missing and flag requirePoetryLockFile is set to true',
async (t) => {
copySync('tests/poetry', 'tests/base with a space');
process.chdir('tests/base with a space');
removeSync('poetry.lock');

const path = npm(['pack', '../..']);
npm(['i', path]);
const stdout = sls(['package'], {
env: { requirePoetryLockFile: 'true', slim: 'true' },
noThrow: true,
});
t.true(
stdout.includes(
'poetry.lock file not found - set requirePoetryLockFile to false to disable this error'
),
'flag works and error is properly reported'
);
t.end();
},
{ skip: !hasPython(3.6) }
);
1 change: 1 addition & 0 deletions tests/poetry/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ custom:
slimPatterns: ${file(./slimPatterns.yml):slimPatterns, self:custom.defaults.slimPatterns}
slimPatternsAppendDefaults: ${env:slimPatternsAppendDefaults, self:custom.defaults.slimPatternsAppendDefaults}
dockerizePip: ${env:dockerizePip, self:custom.defaults.dockerizePip}
requirePoetryLockFile: ${env:requirePoetryLockFile, false}
defaults:
zip: false
slimPatterns: false
Expand Down

0 comments on commit 794a646

Please sign in to comment.