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

Capture any scripts generated by setup/build and include in wheel #531

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
64 changes: 43 additions & 21 deletions src/poetry/core/masonry/builders/wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import shutil
import stat
import subprocess
import sys
import tempfile
import zipfile

Expand Down Expand Up @@ -180,28 +181,48 @@ def _build(self, wheel: zipfile.ZipFile) -> None:
finally:
os.chdir(current_path)

python_version_major = sys.version_info[0]
python_version_minor = sys.version_info[1]
build_dir = self._path / "build"
libs: list[Path] = list(build_dir.glob("lib.*"))
if not libs:
# 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 lib.glob("**/*"):
if pkg.is_dir() or self.is_excluded(pkg):
continue

rel_path = str(pkg.relative_to(lib))

if rel_path in wheel.namelist():
continue

logger.debug(f"Adding: {rel_path}")

self._add_file(wheel, pkg, rel_path)
scripts: list[Path] = list(
build_dir.glob(
f"scripts-{python_version_major}.{python_version_minor}/*"
)
)
if libs:
lib = libs[0]

for pkg in lib.glob("**/*"):
if pkg.is_dir() or self.is_excluded(pkg):
continue

rel_path = str(pkg.relative_to(lib))

if rel_path in wheel.namelist():
continue

logger.debug(f"Adding: {rel_path}")

self._add_file(wheel, pkg, rel_path)

if scripts:
for abs_path in scripts:
logger.debug(f"Adding: scripts/{abs_path.name}")
self._add_file(
wheel,
abs_path,
Path.joinpath(
Path(self.wheel_data_folder),
"scripts",
abs_path.name,
),
)

# The result of building the extensions
# does not exist, this may due to conditional
# builds, so we assume that it's okay
return

def _copy_file_scripts(self, wheel: zipfile.ZipFile) -> None:
file_scripts = self.convert_script_files()
Expand Down Expand Up @@ -308,7 +329,8 @@ def dist_info(self) -> str:

@property
def wheel_data_folder(self) -> str:
return f"{self._package.name}-{self._meta.version}.data"
name = distribution_name(self._package.name)
return f"{name}-{self._meta.version}.data"

@property
def wheel_filename(self) -> str:
Expand Down
21 changes: 21 additions & 0 deletions tests/masonry/builders/fixtures/script_generated/build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from pathlib import Path
from setuptools.command.build_py import build_py


class BuildPyCommand(build_py):
def run(self):
with open('script_generated/generated/file.py', 'w') as out:
print("#!/bin/env python3\nprint('Success!')\n", file=out)
return super().run()


def build(setup_kwargs):
setup_kwargs.update(
{
"cmdclass": {
"build_py": BuildPyCommand,
},
"scripts": ["script_generated/generated/file.py"]
}

)
16 changes: 16 additions & 0 deletions tests/masonry/builders/fixtures/script_generated/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[tool.poetry]
name = "script_generated"
version = "0.1"
description = ""
authors = ["Evgenii Gorchakov <[email protected]>", "Rob Taylor <[email protected]>"]

[tool.poetry.dependencies]
python = "^3.8"

[build-system]
requires = ["poetry-core", "setuptools"]
build-backend = "poetry.core.masonry.api"

[tool.poetry.build]
generate-setup-file = true
script = "build.py"
6 changes: 3 additions & 3 deletions tests/masonry/builders/test_complete.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,10 @@ def test_complete() -> None:

try:
assert "my_package/sub_pgk1/extra_file.xml" not in zip.namelist()
assert "my-package-1.2.3.data/scripts/script.sh" in zip.namelist()
assert "my_package-1.2.3.data/scripts/script.sh" in zip.namelist()
assert (
"Hello World"
in zip.read("my-package-1.2.3.data/scripts/script.sh").decode()
in zip.read("my_package-1.2.3.data/scripts/script.sh").decode()
)

entry_points = zip.read("my_package-1.2.3.dist-info/entry_points.txt")
Expand Down Expand Up @@ -342,7 +342,7 @@ def test_complete_no_vcs() -> None:
"my_package/sub_pkg1/__init__.py",
"my_package/sub_pkg2/__init__.py",
"my_package/sub_pkg2/data2/data.json",
"my-package-1.2.3.data/scripts/script.sh",
"my_package-1.2.3.data/scripts/script.sh",
"my_package/sub_pkg3/foo.py",
"my_package-1.2.3.dist-info/entry_points.txt",
"my_package-1.2.3.dist-info/LICENSE",
Expand Down
21 changes: 21 additions & 0 deletions tests/masonry/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,27 @@ def test_build_wheel_extended() -> None:
validate_wheel_contents(name="extended", version="0.1", path=whl.as_posix())


@pytest.mark.skipif(
sys.platform == "win32"
and sys.version_info <= (3, 6)
or platform.python_implementation().lower() == "pypy",
reason="Disable test on Windows for Python <=3.6 and for PyPy",
)
def test_build_wheel_script_generated() -> None:
with temporary_directory() as tmp_dir, cwd(
os.path.join(fixtures, "script_generated")
):
filename = api.build_wheel(tmp_dir)
whl = Path(tmp_dir) / filename
assert whl.exists()
validate_wheel_contents(
name="script_generated",
version="0.1",
path=whl.as_posix(),
data_files=["scripts/file.py"],
)


def test_build_sdist() -> None:
with temporary_directory() as tmp_dir, cwd(os.path.join(fixtures, "complete")):
filename = api.build_sdist(tmp_dir)
Expand Down
10 changes: 9 additions & 1 deletion tests/testutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,24 @@ def subprocess_run(*args: str, **kwargs: Any) -> subprocess.CompletedProcess[str


def validate_wheel_contents(
name: str, version: str, path: str, files: list[str] | None = None
name: str,
version: str,
path: str,
files: list[str] | None = None,
data_files: list[str] | None = None,
) -> None:
dist_info = f"{name}-{version}.dist-info"
dist_data = f"{name}-{version}.data"
files = files or []
data_files = data_files or []

with zipfile.ZipFile(path) as z:
namelist = z.namelist()
# we use concatenation here for PY2 compat
for filename in ["WHEEL", "METADATA", "RECORD"] + files:
assert f"{dist_info}/{filename}" in namelist
for filename in data_files:
assert f"{dist_data}/{filename}" in namelist


def validate_sdist_contents(
Expand Down