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: do not include __pycache__ and .pyc files in sdist and wheel #835

Merged
merged 1 commit into from
Feb 8, 2025
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
9 changes: 5 additions & 4 deletions src/poetry/core/masonry/builders/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ def find_excluded_files(self, fmt: str | None = None) -> set[str]:
def is_excluded(self, filepath: str | Path) -> bool:
exclude_path = Path(filepath)

if "__pycache__" in exclude_path.parts or exclude_path.suffix == ".pyc":
return True

while True:
if exclude_path.as_posix() in self.find_excluded_files(fmt=self.format):
return True
Expand All @@ -150,7 +153,8 @@ def find_files_to_add(self, exclude_build: bool = True) -> set[BuildIncludeFile]
formats = include.formats

for file in include.elements:
if "__pycache__" in str(file):
if "__pycache__" in file.parts:
# This is just a shortcut. It will be ignored later anyway.
continue

if (
Expand Down Expand Up @@ -202,9 +206,6 @@ def find_files_to_add(self, exclude_build: bool = True) -> set[BuildIncludeFile]
) and isinstance(include, PackageInclude):
continue

if file.suffix == ".pyc":
continue

logger.debug(f"Adding: {file}")
to_add.add(include_file)

Expand Down
1 change: 1 addition & 0 deletions src/poetry/core/masonry/builders/sdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ def find_nearest_pkg(rel_path: str) -> tuple[str, str]:

for path, _dirnames, filenames in os.walk(base, topdown=True):
if Path(path).name == "__pycache__":
# This is just a shortcut. It will be ignored later anyway.
continue

from_top_level = os.path.relpath(path, base)
Expand Down
27 changes: 27 additions & 0 deletions tests/masonry/builders/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from __future__ import annotations

import shutil

from pathlib import Path

import pytest


fixtures_dir = Path(__file__).parent / "fixtures"


@pytest.fixture
def complete_with_pycache_and_pyc_files(tmp_path: Path) -> Path:
root = fixtures_dir / "complete"
tmp_root = tmp_path / "complete" # not git repo!

shutil.copytree(root, tmp_root)
for location in (".", "sub_pkg1"):
abs_location = tmp_root / "my_package" / location
(abs_location / "module1.cpython-39.pyc").touch()
pycache_location = tmp_root / "my_package" / location / "__pycache__"
pycache_location.mkdir(parents=True)
(pycache_location / "module1.cpython-39.pyc").touch()
(pycache_location / "some_other_file").touch()

return tmp_root
19 changes: 19 additions & 0 deletions tests/masonry/builders/test_sdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,25 @@ def test_proper_python_requires_if_three_digits_precision_version_specified() ->
assert parsed["Requires-Python"] == "==2.7.15"


def test_sdist_does_not_include_pycache_and_pyc_files(
complete_with_pycache_and_pyc_files: Path,
) -> None:
poetry = Factory().create_poetry(complete_with_pycache_and_pyc_files)

builder = SdistBuilder(poetry)

builder.build()

sdist = complete_with_pycache_and_pyc_files / "dist" / "my_package-1.2.3.tar.gz"

assert sdist.exists()

with tarfile.open(str(sdist), "r") as tar:
for name in tar.getnames():
assert "__pycache__" not in name
assert not name.endswith(".pyc")


def test_includes() -> None:
poetry = Factory().create_poetry(project("with-include"))

Expand Down
17 changes: 17 additions & 0 deletions tests/masonry/builders/test_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,23 @@ def test_wheel_epoch() -> None:
assert "epoch-1!2.0.dist-info/METADATA" in z.namelist()


def test_wheel_does_not_include_pycache_and_pyc_files(
complete_with_pycache_and_pyc_files: Path,
) -> None:
WheelBuilder.make_in(Factory().create_poetry(complete_with_pycache_and_pyc_files))

whl = (
complete_with_pycache_and_pyc_files / "dist"
) / "my_package-1.2.3-py3-none-any.whl"

assert whl.exists()

with zipfile.ZipFile(str(whl)) as z:
for name in z.namelist():
assert "__pycache__" not in name
assert not name.endswith(".pyc")


def test_wheel_excluded_data() -> None:
module_path = fixtures_dir / "default_with_excluded_data_toml"
WheelBuilder.make(Factory().create_poetry(module_path))
Expand Down