Skip to content

Commit

Permalink
Add build dependencies to package
Browse files Browse the repository at this point in the history
Relates-to: #2789
  • Loading branch information
abn committed Aug 15, 2020
1 parent fd3d2e0 commit cb72178
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 1 deletion.
36 changes: 36 additions & 0 deletions poetry/core/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
from typing import Optional

from .json import validate_object
from .packages import DirectoryDependency
from .packages import FileDependency
from .packages import dependency_from_pep_508
from .packages.dependency import Dependency
from .packages.project_package import ProjectPackage
from .poetry import Poetry
from .spdx import license_by_id
from .utils._compat import Path
from .utils.helpers import canonicalize_name
from .utils.toml_file import TomlFile


Expand All @@ -23,6 +27,8 @@ def create_poetry(self, cwd=None): # type: (Optional[Path]) -> Poetry
poetry_file = self.locate(cwd)

local_config = TomlFile(poetry_file.as_posix()).read()
build_system = local_config.get("build-system", {})

if "tool" not in local_config or "poetry" not in local_config["tool"]:
raise RuntimeError(
"[tool.poetry] section not found in {}".format(poetry_file.name)
Expand Down Expand Up @@ -93,6 +99,36 @@ def create_poetry(self, cwd=None): # type: (Optional[Path]) -> Poetry

package.add_dependency(name, constraint, category="dev")

for requirement in build_system.get("requires", []):
dependency = None
try:
dependency = dependency_from_pep_508(requirement)
except ValueError:
# PEP 517 requires can be path if not PEP 508
path = Path(requirement)
try:
if path.is_file():
dependency = FileDependency(
name=canonicalize_name(path.name), path=path
)
elif path.is_dir():
dependency = DirectoryDependency(
name=canonicalize_name(path.name), path=path
)
except OSError:
# compatibility Python < 3.8
# https://docs.python.org/3/library/pathlib.html#methods
pass

if dependency is None:
# skip since we could not determine requirement
continue

if dependency.name not in {"poetry", "poetry-core"}:
package.add_dependency(
dependency.name, dependency.constraint, category="build"
)

extras = local_config.get("extras", {})
for extra_name, requirements in extras.items():
package.extras[extra_name] = []
Expand Down
8 changes: 7 additions & 1 deletion poetry/core/packages/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def __init__(self, name, version, pretty_version=None):

self.requires = []
self.dev_requires = []
self.build_requires = []
self.extras = {}
self.requires_extras = []

Expand Down Expand Up @@ -164,7 +165,7 @@ def maintainer_email(self): # type: () -> str

@property
def all_requires(self):
return self.requires + self.dev_requires
return self.requires + self.dev_requires + self.build_requires

def _get_author(self): # type: () -> dict
if not self._authors:
Expand Down Expand Up @@ -404,6 +405,8 @@ def add_dependency(

if category == "dev":
self.dev_requires.append(dependency)
elif category == "build":
self.build_requires.append(dependency)
else:
self.requires.append(dependency)

Expand Down Expand Up @@ -447,6 +450,9 @@ def clone(self): # type: () -> Package
for dep in self.dev_requires:
clone.dev_requires.append(dep)

for dep in self.build_requires:
clone.build_requires.append(dep)

return clone

def __hash__(self):
Expand Down
22 changes: 22 additions & 0 deletions tests/fixtures/project_with_build_system_requires/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[build-system]
requires = [
"poetry-core",
"Cython~=0.29.6",
]
build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "poetry-cython-example"
version = "0.1.0"
description = ""
authors = []
include = [{ path = "project/**/*.so", format = "wheel" }]

[tool.poetry.build]
generate-setup-file = false
script = "build.py"

[tool.poetry.dependencies]
python = "^3.7"

[tool.poetry.dev-dependencies]
19 changes: 19 additions & 0 deletions tests/test_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,22 @@ def test_create_poetry_fails_on_invalid_configuration():
- 'description' is a required property
"""
assert expected == str(e.value)


def test_create_poetry_with_build_system_requires():
poetry = Factory().create_poetry(
fixtures_dir / "project_with_build_system_requires"
)
package = poetry.package

assert package.name == "poetry-cython-example"
assert package.version.text == "0.1.0"

assert not package.dev_requires
assert not package.requires

assert len(package.build_requires) == 1

dependency = package.build_requires[0]
assert dependency.category == "build"
assert dependency.to_pep_508() == "cython (>=0.29.6,<0.30.0)"

0 comments on commit cb72178

Please sign in to comment.