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 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
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
15 changes: 15 additions & 0 deletions tests/data/packages/pep517_wrapper_buildsys/mybuildsys.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import os

from setuptools.build_meta import build_sdist
from setuptools.build_meta import build_wheel as setuptools_build_wheel
from setuptools.build_meta import (get_requires_for_build_sdist,
get_requires_for_build_wheel,
prepare_metadata_for_build_wheel)


def build_wheel(*a, **kw):
# Create the marker file to record that the hook was called
with open(os.environ['PIP_TEST_MARKER_FILE'], 'wb'):
pass

return setuptools_build_wheel(*a, **kw)
4 changes: 4 additions & 0 deletions tests/data/packages/pep517_wrapper_buildsys/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[build-system]
requires = [ "setuptools" ]
build-backend = "mybuildsys" # setuptools.build_meta
backend-path = ["."]
3 changes: 3 additions & 0 deletions tests/data/packages/pep517_wrapper_buildsys/setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[metadata]
name = pep517-wrapper-buildsys
version = 1.0
3 changes: 3 additions & 0 deletions tests/data/packages/pep517_wrapper_buildsys/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from setuptools import setup

setup()
15 changes: 15 additions & 0 deletions tests/functional/test_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -1339,6 +1339,21 @@ def test_install_no_binary_builds_pep_517_wheel(script, data, with_wheel):
assert "Running setup.py install for pep517-set" not in str(res), str(res)


@pytest.mark.network
def test_install_no_binary_uses_local_backend(
script, data, with_wheel, tmpdir):
to_install = data.packages.joinpath('pep517_wrapper_buildsys')
script.environ['PIP_TEST_MARKER_FILE'] = marker = str(tmpdir / 'marker')
res = script.pip(
'install', '--no-binary=:all:', '-f', data.find_links, to_install
)
expected = "Successfully installed pep517-wrapper-buildsys"
# Must have installed the package
assert expected in str(res), str(res)

assert os.path.isfile(marker), "Local PEP 517 backend not used"


def test_install_no_binary_disables_cached_wheels(script, data, with_wheel):
# Seed the cache
script.pip(
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