Skip to content

Commit

Permalink
poetry: expose build-system dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
abn committed Apr 27, 2022
1 parent 26f2f5d commit 99e8da4
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
43 changes: 41 additions & 2 deletions src/poetry/core/poetry.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
from __future__ import annotations

import contextlib

from pathlib import Path
from typing import TYPE_CHECKING
from typing import Any

from poetry.core.packages.dependency import Dependency
from poetry.core.packages.directory_dependency import DirectoryDependency
from poetry.core.packages.file_dependency import FileDependency
from poetry.core.utils.helpers import canonicalize_name

if TYPE_CHECKING:
from pathlib import Path

if TYPE_CHECKING:
from poetry.core.packages.project_package import ProjectPackage
from poetry.core.pyproject.toml import PyProjectTOML
from poetry.core.toml import TOMLFile
Expand All @@ -24,6 +30,7 @@ def __init__(
self._pyproject = PyProjectTOML(file)
self._package = package
self._local_config = local_config
self._build_system_dependencies: list[Dependency] | None = None

@property
def pyproject(self) -> PyProjectTOML:
Expand All @@ -43,3 +50,35 @@ def local_config(self) -> dict[str, Any]:

def get_project_config(self, config: str, default: Any = None) -> Any:
return self._local_config.get("config", {}).get(config, default)

@property
def build_system_dependencies(self) -> list[Dependency]:
if self._build_system_dependencies is None:
build_system = self.pyproject.build_system
self._build_system_dependencies = []

for requirement in build_system.requires:
dependency = None
try:
dependency = Dependency.create_from_pep_508(requirement)
except ValueError:
# PEP 517 requires can be path if not PEP 508
path = Path(requirement)

with contextlib.suppress(OSError):
# suppress OSError for compatibility with Python < 3.8
# https://docs.python.org/3/library/pathlib.html#methods
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
)

# skip since we could not determine requirement
if dependency:
self._build_system_dependencies.append(dependency)

return self._build_system_dependencies
16 changes: 16 additions & 0 deletions tests/test_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import pytest

from poetry.core.factory import Factory
from poetry.core.pyproject.tables import BuildSystem
from poetry.core.semver.helpers import parse_constraint
from poetry.core.toml import TOMLFile

Expand Down Expand Up @@ -320,3 +321,18 @@ def test_create_dependency_marker_variants(
assert dep.python_versions == exp_python
assert dep.python_constraint == parse_constraint(exp_python)
assert str(dep.marker) == exp_marker


@pytest.mark.parametrize(
"project, requries",
[
("sample_project", set(BuildSystem().requires)),
("project_with_build_system_requires", {"poetry-core", "cython"}),
],
)
def test_poetry_build_system_dependencies(project: str, requries: set[str]):
poetry = Factory().create_poetry(fixtures_dir / "sample_project")

assert set(BuildSystem().requires) == {
dependency.name for dependency in poetry.build_system_dependencies
}

0 comments on commit 99e8da4

Please sign in to comment.