Skip to content

Commit

Permalink
implement PEP 660 hook (port from master)
Browse files Browse the repository at this point in the history
  • Loading branch information
finswimmer committed Feb 10, 2022
1 parent 10e3ec7 commit eb1a769
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 9 deletions.
13 changes: 13 additions & 0 deletions poetry/core/masonry/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,16 @@ def build_sdist(
path = SdistBuilder(poetry).build(Path(sdist_directory))

return unicode(path.name)


def build_editable(
wheel_directory, config_settings=None, metadata_directory=None,
) -> str: # type: (str, Optional[Dict[str, Any]], Optional[str]) -> str

poetry = Factory().create_poetry(Path(".").resolve(), with_dev=False)

return unicode(WheelBuilder.make_in(poetry, Path(wheel_directory), editable=True))


get_requires_for_build_editable = get_requires_for_build_wheel
prepare_metadata_for_build_editable = prepare_metadata_for_build_wheel
45 changes: 36 additions & 9 deletions poetry/core/masonry/builders/wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from ..utils.helpers import escape_name
from ..utils.helpers import escape_version
from ..utils.helpers import normalize_file_permissions
from ..utils.package_include import PackageInclude
from .builder import Builder
from .sdist import SdistBuilder

Expand All @@ -52,22 +53,27 @@ class WheelBuilder(Builder):
format = "wheel"

def __init__(
self, poetry, target_dir=None, original=None, executable=None
): # type: ("Poetry", Optional[Path], Optional[Path], Optional[str]) -> None
self, poetry, target_dir=None, original=None, executable=None, editable=False,
): # type: ("Poetry", Optional[Path], Optional[Path], Optional[str], bool) -> None
super(WheelBuilder, self).__init__(poetry, executable=executable)

self._records = []
self._original_path = self._path
self._target_dir = target_dir or (self._poetry.file.parent / "dist")
if original:
self._original_path = original.file.parent
self._editable = editable

@classmethod
def make_in(
cls, poetry, directory=None, original=None, executable=None
): # type: ("Poetry", Path, Path, str) -> str
cls, poetry, directory=None, original=None, executable=None, editable=False,
): # type: ("Poetry", Optional[Path], Optional[Path], Optional[str], bool) -> str
wb = WheelBuilder(
poetry, target_dir=directory, original=original, executable=executable
poetry,
target_dir=directory,
original=original,
executable=executable,
editable=editable,
)
wb.build()

Expand Down Expand Up @@ -95,12 +101,16 @@ def build(self): # type: () -> None
with zipfile.ZipFile(
fd_file, mode="w", compression=zipfile.ZIP_DEFLATED
) as zip_file:
if not self._poetry.package.build_should_generate_setup():
self._build(zip_file)
self._copy_module(zip_file)
if not self._editable:
if not self._poetry.package.build_should_generate_setup():
self._build(zip_file)
self._copy_module(zip_file)
else:
self._copy_module(zip_file)
self._build(zip_file)
else:
self._copy_module(zip_file)
self._build(zip_file)
self._add_pth(zip_file)

self._write_metadata(zip_file)
self._write_record(zip_file)
Expand All @@ -112,6 +122,23 @@ def build(self): # type: () -> None

logger.info("Built {}".format(self.wheel_filename))

def _add_pth(self, wheel): # type: (zipfile.ZipFile) -> None
paths = set()
for include in self._module.includes:
if isinstance(include, PackageInclude) and (
include.is_module() or include.is_package()
):
paths.add(include.base.resolve().as_posix())

content = ""
for path in paths:
content += path + os.linesep

pth_file = Path(self._module.name).with_suffix(".pth")

with self._write_to_zip(wheel, str(pth_file)) as f:
f.write(content)

def _build(self, wheel): # type: (zipfile.ZipFile) -> None
if self._package.build_script:
if not self._poetry.package.build_should_generate_setup():
Expand Down
19 changes: 19 additions & 0 deletions tests/masonry/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
import platform
import sys
import zipfile

from contextlib import contextmanager

Expand Down Expand Up @@ -216,3 +217,21 @@ def test_prepare_metadata_for_build_wheel_with_bad_path_dep_succeeds():
):
api.prepare_metadata_for_build_wheel(tmp_dir)
assert "does not exist" in str(err.value)


def test_build_editable_wheel():
pkg_dir = Path(fixtures) / "complete"

with temporary_directory() as tmp_dir, cwd(pkg_dir):
filename = api.build_editable(tmp_dir)
wheel_pth = Path(tmp_dir) / filename

validate_wheel_contents(
name="my_package", version="1.2.3", path=str(wheel_pth),
)

with zipfile.ZipFile(str(wheel_pth)) as z:
namelist = z.namelist()

assert "my_package.pth" in namelist
assert pkg_dir.as_posix() == z.read("my_package.pth").decode().strip()

0 comments on commit eb1a769

Please sign in to comment.