Skip to content

Commit

Permalink
Add initial support for poetry (#433)
Browse files Browse the repository at this point in the history
* Add initial support for poetry
  • Loading branch information
marcelotrevisani authored Jan 28, 2023
1 parent 437d524 commit 60ef1cb
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 3 deletions.
45 changes: 45 additions & 0 deletions grayskull/strategy/py_toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,48 @@
from grayskull.utils import nested_dict


def add_poetry_metadata(metadata: dict, toml_metadata: dict) -> dict:
if not is_poetry_present(toml_metadata):
return metadata

def flat_deps(dict_deps: dict) -> list:
result = []
for pkg_name, version in dict_deps.items():
if isinstance(version, dict):
version_spec = version["version"].strip()
del version["version"]
version = (
f"{version_spec}{' ; '.join(f'{k} {v}' for k,v in version.items())}"
)
version = f"=={version}" if version and version[0].isdigit() else version
result.append(f"{pkg_name} {version}".strip())
return result

poetry_metadata = toml_metadata["tool"]["poetry"]
if poetry_run := flat_deps(poetry_metadata.get("dependencies", {})):
if not metadata["requirements"]["run"]:
metadata["requirements"]["run"] = []
metadata["requirements"]["run"].extend(poetry_run)

host_metadata = metadata["requirements"].get("host", [])
if "poetry" not in host_metadata and "poetry-core" not in host_metadata:
metadata["requirements"]["host"] = host_metadata + ["poetry-core"]

test_metadata = metadata["test"].get("requires", []) or []
if (
test_deps := poetry_metadata.get("group", {})
.get("test", {})
.get("dependencies", {})
):
test_deps = flat_deps(test_deps)
metadata["test"]["requires"] = test_metadata + test_deps
return metadata


def is_poetry_present(toml_metadata: dict) -> bool:
return "poetry" in toml_metadata.get("tool", {})


def get_all_toml_info(path_toml: Union[Path, str]) -> dict:
with open(path_toml, "rb") as f:
toml_metadata = tomli.load(f)
Expand Down Expand Up @@ -41,4 +83,7 @@ def get_all_toml_info(path_toml: Union[Path, str]) -> dict:
metadata["about"]["dev_url"] = all_urls.get("Source", None)
metadata["about"]["home"] = all_urls.get("Homepage", None)
metadata["about"]["summary"] = toml_metadata["project"].get("description")

add_poetry_metadata(metadata, toml_metadata)

return metadata
47 changes: 47 additions & 0 deletions tests/data/pyproject/poetry.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
[tool.poetry]
name = "poetry"
version = "1.4.0.dev0"
description = "Python dependency management and packaging made easy."

license = "MIT"

readme = "README.md"

packages = [
{ include = "poetry", from = "src" }
]
include = [
{ path = "tests", format = "sdist" }
]

homepage = "https://python-poetry.org/"
repository = "https://github.com/python-poetry/poetry"
documentation = "https://python-poetry.org/docs"

keywords = ["packaging", "dependency", "poetry"]

classifiers = [
"Topic :: Software Development :: Build Tools",
"Topic :: Software Development :: Libraries :: Python Modules"
]

[tool.poetry.urls]
Changelog = "https://python-poetry.org/history/"

# Requirements
[tool.poetry.dependencies]
python = "^3.7"
cleo = "^2.0.0"
html5lib = "^1.0"
urllib3 = "^1.26.0"

[tool.poetry.group.dev.dependencies]
pre-commit = "^2.6"

[tool.poetry.group.test.dependencies]
cachy = "0.3.0"
deepdiff = "^6.2"

[build-system]
requires = ["setuptools>=1.1.0"]
build-backend = "poetry.core.masonry.api"
4 changes: 2 additions & 2 deletions tests/test_pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,6 @@ def test_python_requires_setup_py():
def test_django_rest_framework_xml_license():
config = Configuration(name="djangorestframework-xml", version="1.4.0")
recipe = GrayskullFactory.create_recipe("pypi", config)
assert recipe["about"]["license"] == "BSD-3-Clause"
assert recipe["about"]["license_file"] == "LICENSE"
assert recipe["test"]["imports"][0] == "rest_framework_xml"

Expand Down Expand Up @@ -1220,7 +1219,8 @@ def test_create_recipe_from_local_sdist(pkg_pytest):
assert recipe["about"]["license_file"] == "LICENSE"


def test_400_for_python_selector():
@patch("grayskull.strategy.py_base.get_all_toml_info", return_value={})
def test_400_for_python_selector(monkeypatch):
recipe = create_python_recipe("pyquil", version="3.0.1")[0]
assert recipe["build"]["skip"].selector == "py>=400 or py2k"

Expand Down
41 changes: 40 additions & 1 deletion tests/test_pyproject.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
from pathlib import Path

from grayskull.strategy.py_toml import get_all_toml_info
from grayskull.strategy.py_toml import add_poetry_metadata, get_all_toml_info


def test_get_all_toml_info_poetry():
toml_path = Path(__file__).parent / "data" / "pyproject" / "poetry.toml"
result = get_all_toml_info(toml_path)

assert result["test"]["requires"] == ["cachy ==0.3.0", "deepdiff ^6.2"]
assert result["requirements"]["host"] == ["setuptools>=1.1.0", "poetry-core"]
assert result["requirements"]["run"] == [
"python ^3.7",
"cleo ^2.0.0",
"html5lib ^1.0",
"urllib3 ^1.26.0",
]


def test_get_all_toml_info():
Expand Down Expand Up @@ -52,3 +66,28 @@ def test_get_all_toml_info():
'typing-extensions>=4.4; python_version < "3.8"',
"python >=3.7",
]


def test_add_poetry_metadata():
toml_metadata = {
"tool": {
"poetry": {
"dependencies": {"tomli": ">=1.0.0", "requests": ""},
"group": {"test": {"dependencies": {"tox": ">=1.0.0", "pytest": ""}}},
}
}
}
metadata = {
"requirements": {
"host": ["pkg_host1 >=1.0.0", "pkg_host2"],
"run": ["pkg_run1", "pkg_run2 >=2.0.0"],
},
"test": {"requires": ["mock", "pkg_test >=1.0.0"]},
}
assert add_poetry_metadata(metadata, toml_metadata) == {
"requirements": {
"host": ["pkg_host1 >=1.0.0", "pkg_host2", "poetry-core"],
"run": ["pkg_run1", "pkg_run2 >=2.0.0", "tomli >=1.0.0", "requests"],
},
"test": {"requires": ["mock", "pkg_test >=1.0.0", "tox >=1.0.0", "pytest"]},
}

0 comments on commit 60ef1cb

Please sign in to comment.