Skip to content
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

Fix building packages with backend-path in pyproject.toml #7394

Merged
merged 7 commits into from
Nov 25, 2019
Merged
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions news/6599.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix building packages which specify ``backend-path`` in pyproject.toml.
15 changes: 12 additions & 3 deletions src/pip/_internal/pyproject.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io
import os
import sys
from collections import namedtuple

from pip._vendor import pytoml, six
from pip._vendor.packaging.requirements import InvalidRequirement, Requirement
Expand All @@ -11,7 +12,7 @@
from pip._internal.utils.typing import MYPY_CHECK_RUNNING

if MYPY_CHECK_RUNNING:
from typing import Any, Tuple, Optional, List
from typing import Any, Optional, List


def _is_list_of_str(obj):
Expand All @@ -33,13 +34,18 @@ def make_pyproject_path(unpacked_source_directory):
return path


BuildSystemDetails = namedtuple('BuildSystemDetails', [
'requires', 'backend', 'check', 'backend_path'
chrahunt marked this conversation as resolved.
Show resolved Hide resolved
])


def load_pyproject_toml(
use_pep517, # type: Optional[bool]
pyproject_toml, # type: str
setup_py, # type: str
req_name # type: str
):
# type: (...) -> Optional[Tuple[List[str], str, List[str]]]
# type: (...) -> Optional[BuildSystemDetails]
"""Load the pyproject.toml file.

Parameters:
Expand All @@ -57,6 +63,8 @@ def load_pyproject_toml(
name of PEP 517 backend,
requirements we should check are installed after setting
up the build environment
directory paths to import the backend from (backend-path),
relative to the project root.
)
"""
has_pyproject = os.path.isfile(pyproject_toml)
Expand Down Expand Up @@ -167,6 +175,7 @@ def load_pyproject_toml(
)

backend = build_system.get("build-backend")
backend_path = build_system.get("backend-path", [])
check = [] # type: List[str]
if backend is None:
# If the user didn't specify a backend, we assume they want to use
Expand All @@ -184,4 +193,4 @@ def load_pyproject_toml(
backend = "setuptools.build_meta:__legacy__"
check = ["setuptools>=40.8.0", "wheel"]

return (requires, backend, check)
return BuildSystemDetails(requires, backend, check, backend_path)
4 changes: 2 additions & 2 deletions src/pip/_internal/req/req_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,11 +601,11 @@ def load_pyproject_toml(self):
return

self.use_pep517 = True
requires, backend, check = pyproject_toml_data
requires, backend, check, backend_path = pyproject_toml_data
self.requirements_to_check = check
self.pyproject_requires = requires
self.pep517_backend = Pep517HookCaller(
self.unpacked_source_directory, backend
self.unpacked_source_directory, backend, backend_path=backend_path,
)

def prepare_metadata(self):
Expand Down
48 changes: 47 additions & 1 deletion tests/functional/test_pep517.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
from tests.lib import make_test_finder, path_to_url


def make_project(tmpdir, requires=[], backend=None):
def make_project(tmpdir, requires=[], backend=None, backend_path=None):
project_dir = tmpdir / 'project'
project_dir.mkdir()
buildsys = {'requires': requires}
if backend:
buildsys['build-backend'] = backend
if backend_path:
buildsys['backend-path'] = backend_path
data = pytoml.dumps({'build-system': buildsys})
project_dir.joinpath('pyproject.toml').write_text(data)
return project_dir
Expand All @@ -32,6 +34,50 @@ def test_backend(tmpdir, data):
assert req.pep517_backend.build_wheel("dir") == "Backend called"


dummy_backend_code = """\
def build_wheel(
wheel_directory,
config_settings=None,
metadata_directory=None
):
return "Backend called"
"""


def test_backend_path(tmpdir, data):
"""Check we can call a backend inside the project"""
project_dir = make_project(
tmpdir, backend="dummy_backend", backend_path=['.']
)
(project_dir / 'dummy_backend.py').write_text(dummy_backend_code)
req = InstallRequirement(None, None, source_dir=project_dir)
req.load_pyproject_toml()

env = BuildEnvironment()
assert hasattr(req.pep517_backend, 'build_wheel')
with env:
assert req.pep517_backend.build_wheel("dir") == "Backend called"


def test_backend_path_and_dep(tmpdir, data):
"""Check we can call a requirement's backend successfully"""
project_dir = make_project(
tmpdir, backend="dummy_internal_backend", backend_path=['.']
)
(project_dir / 'dummy_internal_backend.py').write_text(
"from dummy_backend import build_wheel"
)
req = InstallRequirement(None, None, source_dir=project_dir)
req.load_pyproject_toml()
env = BuildEnvironment()
finder = make_test_finder(find_links=[data.backends])
env.install_requirements(finder, ["dummy_backend"], 'normal', "Installing")

assert hasattr(req.pep517_backend, 'build_wheel')
with env:
assert req.pep517_backend.build_wheel("dir") == "Backend called"


def test_pep517_install(script, tmpdir, data):
"""Check we can build with a custom backend"""
project_dir = make_project(
Expand Down