Skip to content

Commit

Permalink
fix!: exclude installed files if listed in exclude (#652)
Browse files Browse the repository at this point in the history
As mentioned in our recent Community Meeting.

---------

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
  • Loading branch information
henryiii authored Mar 4, 2024
1 parent e6ae46d commit 50a9386
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 22 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,9 @@ wheel.cmake = true
wheel.platlib = ""

# A set of patterns to exclude from the wheel. This is additive to the SDist
# exclude patterns. This applies to the source files, not the final paths.
# Editable installs may not respect this exclusion.
# exclude patterns. This applies to the final paths in the wheel, and can
# exclude files from CMake output as well. Editable installs may not respect
# this exclusion.
wheel.exclude = []

# The build tag to use for the wheel. If empty, no build tag is used.
Expand Down
7 changes: 7 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,13 @@ as well (not guaranteed to be respected by editable installs):
wheel.exclude = ["**.pyx"]
```

:::{versionchanged} 0.9

Before scikit-build-core 0.9, these were matched on the source path, rather than
the wheel path, and didn't apply to CMake output.

:::

:::{note}

There are two more settings that are primarily intended for `overrides` (see
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ test = [
"build[virtualenv]",
"cattrs >=22.2.0",
"pathspec >=0.10.1",
"pip",
"pybind11",
"pyproject-metadata >=0.5",
"pytest >=7.0", # 7.2+ recommended for better tracebacks with ExceptionGroup
Expand Down
15 changes: 10 additions & 5 deletions src/scikit_build_core/build/_pathutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from pathlib import Path
from typing import TYPE_CHECKING

import pathspec

from ._file_processor import each_unignored_file

if TYPE_CHECKING:
Expand Down Expand Up @@ -37,20 +39,23 @@ def packages_to_file_mapping(
packages: Sequence[str],
platlib_dir: Path,
include: Sequence[str],
exclude: Sequence[str],
src_exclude: Sequence[str],
target_exclude: Sequence[str],
) -> dict[str, str]:
mapping = {}
exclude_spec = pathspec.GitIgnoreSpec.from_lines(target_exclude)
for package in packages:
source_package = Path(package)
base_path = source_package.parent
for filepath in each_unignored_file(
source_package,
include=include,
exclude=exclude,
exclude=src_exclude,
):
package_dir = platlib_dir / filepath.relative_to(base_path)
if not package_dir.is_file():
mapping[str(filepath)] = str(package_dir)
rel_path = filepath.relative_to(base_path)
target_path = platlib_dir / rel_path
if not exclude_spec.match_file(rel_path) and not target_path.is_file():
mapping[str(filepath)] = str(target_path)

return mapping

Expand Down
26 changes: 18 additions & 8 deletions src/scikit_build_core/build/_wheelfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
from zipfile import ZipInfo

import packaging.utils
import pathspec

from .. import __version__

if TYPE_CHECKING:
from collections.abc import Mapping, Set
from collections.abc import Mapping, Sequence, Set

from packaging.tags import Tag
from pyproject_metadata import StandardMetadata
Expand Down Expand Up @@ -141,7 +142,9 @@ def dist_info_contents(self) -> dict[str, bytes]:
**license_entries,
}

def build(self, wheel_dirs: dict[str, Path]) -> None:
def build(
self, wheel_dirs: Mapping[str, Path], exclude: Sequence[str] = ()
) -> None:
(targetlib,) = {"platlib", "purelib"} & set(wheel_dirs)
assert {targetlib, "data", "headers", "scripts", "null"} >= wheel_dirs.keys()

Expand All @@ -152,14 +155,21 @@ def build(self, wheel_dirs: dict[str, Path]) -> None:
for key in sorted({"data", "headers", "scripts"} & wheel_dirs.keys()):
plans[key] = wheel_dirs[key]

exclude_spec = pathspec.GitIgnoreSpec.from_lines(exclude)

for key, path in plans.items():
for filename in sorted(path.glob("**/*")):
is_in_dist_info = any(x.endswith(".dist-info") for x in filename.parts)
is_python_cache = filename.suffix in {".pyc", ".pyo"}
if filename.is_file() and not is_in_dist_info and not is_python_cache:
relpath = filename.relative_to(path)
target = Path(data_dir) / key / relpath if key else relpath
self.write(str(filename), str(target))
if not filename.is_file():
continue
if any(x.endswith(".dist-info") for x in filename.parts):
continue
if filename.suffix in {".pyc", ".pyo"}:
continue
relpath = filename.relative_to(path)
if exclude_spec.match_file(relpath):
continue
target = Path(data_dir) / key / relpath if key else relpath
self.write(str(filename), str(target))

dist_info_contents = self.dist_info_contents()
for key, data in dist_info_contents.items():
Expand Down
5 changes: 3 additions & 2 deletions src/scikit_build_core/build/wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,8 @@ def _build_wheel_impl(
packages=packages,
platlib_dir=wheel_dirs[targetlib],
include=settings.sdist.include,
exclude=[*settings.sdist.exclude, *settings.wheel.exclude],
src_exclude=settings.sdist.exclude,
target_exclude=settings.wheel.exclude,
)

if not editable:
Expand All @@ -364,7 +365,7 @@ def _build_wheel_impl(
),
license_files=license_files,
) as wheel:
wheel.build(wheel_dirs)
wheel.build(wheel_dirs, exclude=settings.wheel.exclude)

str_pkgs = (str(Path.cwd().joinpath(p).parent.resolve()) for p in packages)
if editable and settings.editable.mode == "redirect":
Expand Down
2 changes: 1 addition & 1 deletion src/scikit_build_core/resources/scikit-build.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@
"items": {
"type": "string"
},
"description": "A set of patterns to exclude from the wheel. This is additive to the SDist exclude patterns. This applies to the source files, not the final paths. Editable installs may not respect this exclusion."
"description": "A set of patterns to exclude from the wheel. This is additive to the SDist exclude patterns. This applies to the final paths in the wheel, and can exclude files from CMake output as well. Editable installs may not respect this exclusion."
},
"build-tag": {
"type": "string",
Expand Down
5 changes: 3 additions & 2 deletions src/scikit_build_core/settings/skbuild_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,9 @@ class WheelSettings:
exclude: List[str] = dataclasses.field(default_factory=list)
"""
A set of patterns to exclude from the wheel. This is additive to the SDist
exclude patterns. This applies to the source files, not the final paths.
Editable installs may not respect this exclusion.
exclude patterns. This applies to the final paths in the wheel, and can
exclude files from CMake output as well. Editable installs may not respect
this exclusion.
"""

build_tag: str = ""
Expand Down
4 changes: 4 additions & 0 deletions tests/packages/simplest_c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@ install(
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/generated_ignored.txt "Testing")
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/generated_ignored.txt
DESTINATION ${SKBUILD_PROJECT_NAME})

file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/generated_no_wheel.txt "Testing")
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/generated_no_wheel.txt
DESTINATION ${SKBUILD_PROJECT_NAME})
3 changes: 2 additions & 1 deletion tests/test_editable_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ def test_navigate_editable_pkg(editable_package: EditablePackage, virtualenv: VE
packages=packages,
platlib_dir=site_packages,
include=[],
exclude=[],
src_exclude=[],
target_exclude=[],
)
assert mapping == {
str(Path("pkg/__init__.py")): str(pkg_dir / "__init__.py"),
Expand Down
6 changes: 5 additions & 1 deletion tests/test_simplest_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def test_pep517_wheel(tmp_path, monkeypatch, virtualenv, component):

if not component:
expected_wheel_files.add("generated_ignored.txt")
expected_wheel_files.add("generated_no_wheel.txt")

if not component or "Generated" in component:
expected_wheel_files.add("generated.txt")
Expand Down Expand Up @@ -110,7 +111,10 @@ def test_pep517_wheel_incexl(tmp_path, monkeypatch, virtualenv):
{
"sdist.include": "src/simplest/*included*.txt",
"sdist.exclude": "src/simplest/*excluded*.txt",
"wheel.exclude": "src/simplest/sdist_only.txt",
"wheel.exclude": [
"simplest/sdist_only.txt",
"simplest/generated_no_wheel.txt",
],
"wheel.packages": ["src/simplest", "src/not_a_package"],
},
)
Expand Down

0 comments on commit 50a9386

Please sign in to comment.