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

Commit 882aedd

Browse files
committed
fix: check if pyproject has poetry earlier
During the evaluation in the package phase, we determine whether a `requirements.txt` file exists, or whether we need to generate one. Since the `pyproject.toml` file is used by poetry, but only if a stanza is contained inside the file, use the function `isPoetryProject()` along with the configuration value, thereby reducing the need for a project to have to declare a configuration override. Refs serverless#324 Refs serverless#344 Fixes serverless#400 Signed-off-by: Mike Fiedler <[email protected]>
1 parent 724955e commit 882aedd

File tree

8 files changed

+85
-3
lines changed

8 files changed

+85
-3
lines changed

lib/pip.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const { spawnSync } = require('child_process');
77
const { quote } = require('shell-quote');
88
const { buildImage, getBindPath, getDockerUid } = require('./docker');
99
const { getStripCommand, getStripMode, deleteFiles } = require('./slim');
10+
const { isPoetryProject } = require('./poetry');
1011
const {
1112
checkForAndDeleteMaxCacheVersions,
1213
sha256Path,
@@ -62,7 +63,8 @@ function generateRequirementsFile(
6263
) {
6364
if (
6465
options.usePoetry &&
65-
fse.existsSync(path.join(servicePath, 'pyproject.toml'))
66+
fse.existsSync(path.join(servicePath, 'pyproject.toml') &&
67+
isPoetryProject(servicePath))
6668
) {
6769
filterRequirementsFile(
6870
path.join(servicePath, '.serverless/requirements.txt'),
@@ -442,7 +444,8 @@ function copyVendors(vendorFolder, targetFolder, serverless) {
442444
function requirementsFileExists(servicePath, options, fileName) {
443445
if (
444446
options.usePoetry &&
445-
fse.existsSync(path.join(servicePath, 'pyproject.toml'))
447+
fse.existsSync(path.join(servicePath, 'pyproject.toml') &&
448+
isPoetryProject(servicePath))
446449
) {
447450
return true;
448451
}

lib/poetry.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,4 @@ function isPoetryProject(servicePath) {
9090
return false;
9191
}
9292

93-
module.exports = { pyprojectTomlToRequirements };
93+
module.exports = { pyprojectTomlToRequirements, isPoetryProject };

test.js

+10
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,16 @@ test('non build pyproject.toml uses requirements.txt', t => {
744744
t.end();
745745
});
746746

747+
test('non poetry pyproject.toml without requirements.txt packages handler only', t => {
748+
process.chdir('tests/non_poetry_pyproject');
749+
const path = npm(['pack', '../..']);
750+
npm(['i', path]);
751+
sls(['package']);
752+
const zipfiles = listZipFiles('.serverless/sls-py-req-test.zip');
753+
t.true(zipfiles.includes(`handler.py`), 'handler is packaged');
754+
t.end();
755+
});
756+
747757
test('poetry py3.6 can package flask with default options', t => {
748758
process.chdir('tests/poetry');
749759
const path = npm(['pack', '../..']);

tests/non_poetry_pyproject/.gitignore

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Distribution / packaging
2+
.Python
3+
env/
4+
build/
5+
develop-eggs/
6+
dist/
7+
downloads/
8+
eggs/
9+
.eggs/
10+
lib/
11+
lib64/
12+
parts/
13+
sdist/
14+
var/
15+
*.egg-info/
16+
.installed.cfg
17+
*.egg
18+
19+
# Serverless
20+
.serverless
21+
.requirements
22+
unzip_requirements.py

tests/non_poetry_pyproject/handler.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import requests
2+
3+
4+
def hello(event, context):
5+
return requests.get('https://httpbin.org/get').json()
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "example",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "",
10+
"license": "ISC",
11+
"dependencies": {
12+
"serverless-python-requirements": "file:serverless-python-requirements-5.1.0.tgz"
13+
}
14+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[tool.black]
2+
line-length = 79
3+
py36 = true
4+
skip-string-normalization = true
5+
exclude = '''
6+
/(
7+
\.serverless
8+
| node_modules
9+
)/
10+
'''
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
service: sls-py-req-test
2+
3+
provider:
4+
name: aws
5+
runtime: python3.6
6+
7+
plugins:
8+
- serverless-python-requirements
9+
10+
package:
11+
exclude:
12+
- '**/*'
13+
include:
14+
- handler.py
15+
16+
functions:
17+
hello:
18+
handler: handler.hello

0 commit comments

Comments
 (0)