Skip to content

Commit cf627ff

Browse files
committed
Enhance poetry related logging and add new flag
* 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.
1 parent a4cd36b commit cf627ff

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,15 @@ custom:
131131
usePoetry: false
132132
```
133133

134+
Be aware that if no `poetry.lock` file is present, a new one will be generated on the fly. To help having predictable builds,
135+
you can set the `requirePoetryLockFile` flag to true to throw an error when `poetry.lock` is missing.
136+
137+
```yaml
138+
custom:
139+
pythonRequirements:
140+
requirePoetryLockFile: false
141+
```
142+
134143
### Poetry with git dependencies
135144

136145
Poetry by default generates the exported requirements.txt file with `-e` and that breaks pip with `-t` parameter

index.js

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class ServerlessPythonRequirements {
5757
pipCmdExtraArgs: [],
5858
noDeploy: [],
5959
vendor: '',
60+
requirePoetryLockFile: false
6061
},
6162
(this.serverless.service.custom &&
6263
this.serverless.service.custom.pythonRequirements) ||

lib/poetry.js

+23-5
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,30 @@ async function pyprojectTomlToRequirements(modulePath, pluginInstance) {
2121
generateRequirementsProgress = progress.get(
2222
'python-generate-requirements-toml'
2323
);
24-
generateRequirementsProgress.update(
25-
'Generating requirements.txt from "pyproject.toml"'
24+
}
25+
26+
const emitMsg = (msg) => {
27+
if (generateRequirementsProgress) {
28+
generateRequirementsProgress.update(msg)
29+
log.info(msg);
30+
} else {
31+
serverless.cli.log(msg);
32+
}
33+
}
34+
35+
try {
36+
await spawn("test", ["-f", "poetry.lock"])
37+
emitMsg('Generating requirements.txt from poetry.lock...');
38+
} catch (e) {
39+
if (options.requirePoetryLockFile) {
40+
throw new Error(
41+
"poetry.lock file not found - set requirePoetryLockFile to false to "
42+
+ "disable this error"
43+
);
44+
}
45+
emitMsg(
46+
'Generating poetry.lock and requirements.txt from pyproject.toml...'
2647
);
27-
log.info('Generating requirements.txt from "pyproject.toml"');
28-
} else {
29-
serverless.cli.log('Generating requirements.txt from pyproject.toml...');
3048
}
3149

3250
try {

0 commit comments

Comments
 (0)