Skip to content
Open
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
20 changes: 19 additions & 1 deletion src/poetry/core/masonry/builders/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,13 @@ def find_excluded_files(self, fmt: str | None = None) -> set[str]:

def add_all_files(path: Path, target: set[str]) -> None:
all_files = (
[f for f in path.glob("**/*") if f.is_file()]
[f for f in path.glob("**/*") if f.is_file() or f.is_symlink()]
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Resolved
if path.is_dir()
else [path]
)
if path.is_dir() and path.is_symlink():
# the link itself, so that excluding or including it covers it
all_files.append(path)
target.update(f.relative_to(self._path).as_posix() for f in all_files)

explicitly_excluded: set[str] = set()
Expand Down Expand Up @@ -146,6 +149,13 @@ def is_excluded(self, filepath: str | Path) -> bool:

return False

def _is_excluded_unresolved(self, path: Path) -> bool:
# BuildIncludeFile resolves symlinks, so an excluded symlink
# that points outside the project must be skipped before that.
return path.is_relative_to(self._path) and self.is_excluded(
path.relative_to(self._path)
)

def find_files_to_add(self, exclude_build: bool = True) -> set[BuildIncludeFile]:
"""
Finds all files to add to the tarball
Expand Down Expand Up @@ -184,6 +194,9 @@ def find_files_to_add(self, exclude_build: bool = True) -> set[BuildIncludeFile]
if file.is_dir():
if self.format in formats:
for current_file in file.glob("**/*"):
if self._is_excluded_unresolved(current_file):
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Resolved
continue

include_file = BuildIncludeFile(
path=current_file,
project_root=self._path,
Expand All @@ -200,6 +213,11 @@ def find_files_to_add(self, exclude_build: bool = True) -> set[BuildIncludeFile]
to_add.add(include_file)
continue

if isinstance(include, PackageInclude) and self._is_excluded_unresolved(
file
):
continue

include_file = BuildIncludeFile(
path=file,
project_root=self._path,
Expand Down
23 changes: 23 additions & 0 deletions tests/masonry/builders/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,26 @@ def complete_with_pycache_and_pyc_files(tmp_path: Path) -> Path:
(pycache_location / "some_other_file").touch()

return tmp_root


@pytest.fixture
def excluded_symlinks(tmp_path: Path) -> Path:
root = fixtures_dir / "excluded_symlinks"
tmp_root = tmp_path / "excluded_symlinks"
shutil.copytree(root, tmp_root)

outside = tmp_path / "outside"
outside.mkdir()
(outside / "file.txt").touch()

package = tmp_root / "excluded_symlinks"
try:
# excluded via the "exclude" setting
(package / "tests" / "broken").symlink_to(tmp_path / "missing")
(package / "tests" / "outside").symlink_to(outside, target_is_directory=True)
# excluded via VCS (get_ignored_files has to be mocked)
(package / "vcs_ignored").symlink_to(outside, target_is_directory=True)
except OSError:
pytest.skip("Symlinks are not supported.")

return tmp_root
11 changes: 11 additions & 0 deletions tests/masonry/builders/fixtures/excluded_symlinks/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[tool.poetry]
name = "excluded-symlinks"
version = "1.2.3"
description = "Some description."
authors = [
"Poetry Maintainers <noreply@python-poetry.org>"
]
exclude = ["excluded_symlinks/tests"]

[tool.poetry.dependencies]
python = "^3.6"
54 changes: 54 additions & 0 deletions tests/masonry/builders/test_sdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,60 @@ def test_sdist_does_not_include_pycache_and_pyc_files(
assert not name.endswith(".pyc")


def test_sdist_excluded_symlinks(
excluded_symlinks: Path, mocker: MockerFixture
) -> None:
mocker.patch(
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Resolved
"poetry.core.vcs.git.Git.get_ignored_files",
return_value=["excluded_symlinks/vcs_ignored"],
)
builder = SdistBuilder(Factory().create_poetry(excluded_symlinks))
builder.build()

sdist = excluded_symlinks / "dist" / "excluded_symlinks-1.2.3.tar.gz"

assert sdist.exists()

with tarfile.open(str(sdist), "r") as tar:
prefix = "excluded_symlinks-1.2.3/excluded_symlinks/"
names = [n for n in tar.getnames() if n.startswith(prefix)]
assert names == [f"{prefix}__init__.py"]


def test_sdist_explicitly_included_vcs_ignored_dir_symlink(
tmp_path: Path, mocker: MockerFixture
) -> None:
project = tmp_path / "project"
(project / "foo").mkdir(parents=True)
(project / "foo" / "__init__.py").touch()
(project / "shared").mkdir()
(project / "shared" / "x.txt").touch()
(project / "pyproject.toml").write_text(
"""\
[project]
name = "foo"
version = "1.2.3"

[tool.poetry]
include = [{ path = "foo/assets", format = ["sdist", "wheel"] }]
""",
encoding="utf-8",
)
try:
(project / "foo" / "assets").symlink_to(
Path("..", "shared"), target_is_directory=True
)
except OSError:
pytest.skip("Symlinks are not supported.")
mocker.patch(
"poetry.core.vcs.git.Git.get_ignored_files", return_value=["foo/assets"]
)

builder = SdistBuilder(Factory().create_poetry(project))

assert "x.txt" in {f.path.name for f in builder.find_files_to_add()}


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

Expand Down
18 changes: 18 additions & 0 deletions tests/masonry/builders/test_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,24 @@ def test_wheel_excluded_nested_data() -> None:
assert "my_package/public/item2/itemdata2.txt" not in z.namelist()


def test_wheel_excluded_symlinks(
excluded_symlinks: Path, mocker: MockerFixture
) -> None:
mocker.patch(
"poetry.core.vcs.git.Git.get_ignored_files",
return_value=["excluded_symlinks/vcs_ignored"],
)
WheelBuilder.make(Factory().create_poetry(excluded_symlinks))

whl = excluded_symlinks / "dist" / "excluded_symlinks-1.2.3-py3-none-any.whl"

assert whl.exists()

with zipfile.ZipFile(str(whl)) as z:
names = [n for n in z.namelist() if n.startswith("excluded_symlinks/")]
assert names == ["excluded_symlinks/__init__.py"]


def test_include_excluded_code() -> None:
module_path = fixtures_dir / "include_excluded_code"
poetry = Factory().create_poetry(module_path)
Expand Down
Loading