Skip to content

Parse pyproject.toml to determine poetry use. #344

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
Apr 15, 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
32 changes: 28 additions & 4 deletions lib/poetry.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
const fs = require('fs');
const fse = require('fs-extra');
const path = require('path');
const { spawnSync } = require('child_process');
const tomlParse = require('@iarna/toml/parse-string');

/**
* poetry install
*/
function pyprojectTomlToRequirements() {
if (
!this.options.usePoetry ||
!fse.existsSync(path.join(this.servicePath, 'pyproject.toml'))
) {
if (!this.options.usePoetry || !isPoetryProject(this.servicePath)) {
return;
}

Expand Down Expand Up @@ -40,4 +39,29 @@ function pyprojectTomlToRequirements() {
);
}

/**
* Check if pyproject.toml file exists and is a poetry project.
*/
function isPoetryProject(servicePath) {
const pyprojectPath = path.join(servicePath, 'pyproject.toml');

if (!fse.existsSync(pyprojectPath)) {
return false;
}

const pyprojectToml = fs.readFileSync(pyprojectPath);
const pyproject = tomlParse(pyprojectToml);

const buildSystemReqs =
(pyproject['build-system'] && pyproject['build-system']['requires']) || [];

for (var i = 0; i < buildSystemReqs.length; i++) {
if (buildSystemReqs[i].startsWith('poetry')) {
return true;
}
}

return false;
}

module.exports = { pyprojectTomlToRequirements };
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"tape": "*"
},
"dependencies": {
"@iarna/toml": "^2.2.3",
"appdirectory": "^0.1.0",
"bluebird": "^3.0.6",
"fs-extra": "^7.0.0",
Expand All @@ -61,8 +62,8 @@
"lodash.uniqby": "^4.0.0",
"lodash.values": "^4.3.0",
"rimraf": "^2.6.2",
"shell-quote": "^1.6.1",
"sha256-file": "1.0.0"
"sha256-file": "1.0.0",
"shell-quote": "^1.6.1"
},
"eslintConfig": {
"extends": "eslint:recommended",
Expand Down
11 changes: 11 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,17 @@ test("pipenv py3.6 doesn't package bottle with noDeploy option", t => {
t.end();
});

test('non build pyproject.toml uses requirements.txt', t => {
process.chdir('tests/non_build_pyproject');
const path = npm(['pack', '../..']);
npm(['i', path]);
sls(['package']);
const zipfiles = listZipFiles('.serverless/sls-py-req-test.zip');
t.true(zipfiles.includes(`flask${sep}__init__.py`), 'flask is packaged');
t.false(zipfiles.includes(`boto3${sep}__init__.py`), 'boto3 is NOT packaged');
t.end();
});

test('poetry py3.6 can package flask with default options', t => {
process.chdir('tests/poetry');
const path = npm(['pack', '../..']);
Expand Down
22 changes: 22 additions & 0 deletions tests/non_build_pyproject/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg

# Serverless
.serverless
.requirements
unzip_requirements.py
5 changes: 5 additions & 0 deletions tests/non_build_pyproject/handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import requests


def hello(event, context):
return requests.get('https://httpbin.org/get').json()
14 changes: 14 additions & 0 deletions tests/non_build_pyproject/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "example",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"serverless-python-requirements": "file:serverless-python-requirements-4.2.5.tgz"
}
}
10 changes: 10 additions & 0 deletions tests/non_build_pyproject/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[tool.black]
line-length = 79
py36 = true
skip-string-normalization = true
exclude = '''
/(
\.serverless
| node_modules
)/
'''
2 changes: 2 additions & 0 deletions tests/non_build_pyproject/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
flask
boto3
21 changes: 21 additions & 0 deletions tests/non_build_pyproject/serverless.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
service: sls-py-req-test

provider:
name: aws
runtime: python3.6

plugins:
- serverless-python-requirements
custom:
pythonRequirements:
usePoetry: false

package:
exclude:
- '**/*'
include:
- handler.py

functions:
hello:
handler: handler.hello