Skip to content

Commit

Permalink
fix: Explicitly define the build/lib* path to ensure the correct one …
Browse files Browse the repository at this point in the history
…is used.
  • Loading branch information
robbotorigami committed Aug 31, 2023
1 parent 43a2db9 commit adc4aed
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 5 deletions.
31 changes: 26 additions & 5 deletions src/poetry/core/masonry/builders/wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import stat
import subprocess
import sys
import sysconfig
import tempfile
import zipfile

Expand Down Expand Up @@ -182,16 +183,13 @@ def _build(self, wheel: zipfile.ZipFile) -> None:
finally:
os.chdir(current_path)

build_dir = self._path / "build"
libs: list[Path] = list(build_dir.glob("lib.*"))
if not libs:
lib = self._get_build_lib_dir()
if lib is None:
# The result of building the extensions
# does not exist, this may due to conditional
# builds, so we assume that it's okay
return

lib = libs[0]

for pkg in sorted(lib.glob("**/*")):
if pkg.is_dir() or self.is_excluded(pkg):
continue
Expand All @@ -205,6 +203,25 @@ def _build(self, wheel: zipfile.ZipFile) -> None:

self._add_file(wheel, pkg, rel_path)

def _get_build_purelib_dir(self) -> Path:
return self._path / "build/lib"

def _get_build_platlib_dir(self) -> Path:
# Roughly equivalent to the naming convention in used by distutils, see:
# distutils.command.build.build.finalize_options
plat_specifier = f"{sysconfig.get_platform()}-{sys.implementation.cache_tag}"
return self._path / f"build/lib.{plat_specifier}"

def _get_build_lib_dir(self) -> Path | None:
# Either the purelib or platlib path will have been used when building
build_platlib = self._get_build_platlib_dir()
build_purelib = self._get_build_purelib_dir()
if build_platlib.exists():
return build_platlib
elif build_purelib.exists():
return build_purelib
return None

def _copy_file_scripts(self, wheel: zipfile.ZipFile) -> None:
file_scripts = self.convert_script_files()

Expand All @@ -223,6 +240,10 @@ def _run_build_command(self, setup: Path) -> None:
"build",
"-b",
str(self._path / "build"),
"--build-purelib",
str(self._get_build_purelib_dir()),
"--build-platlib",
str(self._get_build_platlib_dir()),
]
)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Module 1
========
15 changes: 15 additions & 0 deletions tests/masonry/builders/fixtures/build_with_build_py_only/build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from pathlib import Path
from setuptools.command.build_py import build_py


class BuildPyCommand(build_py):
def run(self):
gen_file = Path("build_with_build_py_only/generated/file.py")
gen_file.touch()
ret = super().run()
gen_file.unlink()
return ret


def build(setup_kwargs):
setup_kwargs["cmdclass"] = {"build_py": BuildPyCommand}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[tool.poetry]
name = "build_with_build_py_only"
version = "0.1"
description = "Some description."
authors = [
"Robert Belter <[email protected]>"
]
license = "MIT"

readme = "README.rst"

homepage = "https://python-poetry.org/"

[tool.poetry.build]
script = "build.py"
generate-setup-file = true
15 changes: 15 additions & 0 deletions tests/masonry/builders/test_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,18 @@ def test_tag(in_venv_build: bool, mocker: MockerFixture) -> None:
get_sys_tags_spy.assert_not_called()
else:
get_sys_tags_spy.assert_called()


def test_build_py_only_included() -> None:
"""Tests that a build.py that only defined the command build_py (which generates a
lib folder) will have its artifacts included.
"""
root = fixtures_dir / "build_with_build_py_only"
WheelBuilder.make(Factory().create_poetry(root))

whl = next((root / "dist").glob("build_with_build_py_only-0.1-*.whl"))

assert whl.exists()

with zipfile.ZipFile(str(whl)) as z:
assert "build_with_build_py_only/generated/file.py" in z.namelist()

0 comments on commit adc4aed

Please sign in to comment.