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

feat: from-sdist for overrides #812

Merged
merged 1 commit into from
Jul 19, 2024
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
5 changes: 5 additions & 0 deletions docs/overrides.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ and `metadata_editable`. Takes a regex.
Note that you can build directly to wheel; you don't have to go through an
SDist.

### `from-sdist` (bool)

This will be true if the `PKG-INFO` file exists, that is, if this is coming
from an SDist. Takes a bool.

## Any matching condition

If you use `if.any` instead of `if`, then the override is true if any one of the
Expand Down
4 changes: 4 additions & 0 deletions src/scikit_build_core/resources/scikit-build.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,10 @@
"type": "string",
"description": "The state of the build, one of `sdist`, `wheel`, `editable`, `metadata_wheel`, and `metadata_editable`. Takes a regex."
},
"from-sdist": {
"type": "boolean",
"description": "Whether the build is from an sdist."
},
"env": {
"type": "object",
"patternProperties": {
Expand Down
29 changes: 26 additions & 3 deletions src/scikit_build_core/settings/skbuild_read_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def override_match(
current_state: Literal[
"sdist", "wheel", "editable", "metadata_wheel", "metadata_editable"
],
has_dist_info: bool,
python_version: str | None = None,
implementation_name: str | None = None,
implementation_version: str | None = None,
Expand All @@ -79,8 +80,14 @@ def override_match(
platform_node: str | None = None,
env: dict[str, str] | None = None,
state: str | None = None,
from_sdist: bool | None = None,
) -> bool:
# This makes a matches list. A match is a string; if it's an empty string,
# then it did not match. If it's not empty, it did match, and the string
# will be printed in the logs - it's the match reason. If match_all is True,
# then all must match, otherwise any match will count.
matches = []

if current_env is None:
current_env = os.environ

Expand Down Expand Up @@ -124,6 +131,12 @@ def override_match(
match_msg = regex_match(current_state, state)
matches.append(match_msg)

if from_sdist is not None:
if has_dist_info:
matches.append("from sdist due to PKG-INFO" if from_sdist else "")
else:
matches.append("" if from_sdist else "not from sdist, no PKG-INFO")

if env:
for key, value in env.items():
if isinstance(value, bool):
Expand Down Expand Up @@ -264,8 +277,10 @@ def process_overides(
env: Mapping[str, str] | None = None,
) -> None:
"""
Process overrides into the main dictionary if they match. Modifies the input dictionary.
Process overrides into the main dictionary if they match. Modifies the input
dictionary. Must be run from the package directory.
"""
has_dist_info = Path("PKG-INFO").is_file()

for override in tool_skb.pop("overrides", []):
matched = True
Expand All @@ -280,7 +295,11 @@ def process_overides(
any_override = if_override.pop("any")
select = {k.replace("-", "_"): v for k, v in any_override.items()}
matched = override_match(
match_all=False, current_env=env, current_state=state, **select
match_all=False,
current_env=env,
current_state=state,
has_dist_info=has_dist_info,
**select,
)

inherit_override = override.pop("inherit", {})
Expand All @@ -291,7 +310,11 @@ def process_overides(
select = {k.replace("-", "_"): v for k, v in if_override.items()}
if select:
matched = matched and override_match(
match_all=True, current_env=env, current_state=state, **select
match_all=True,
current_env=env,
current_state=state,
has_dist_info=has_dist_info,
**select,
)
if matched:
for key, value in override.items():
Expand Down
4 changes: 4 additions & 0 deletions src/scikit_build_core/settings/skbuild_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ def generate_skbuild_schema(tool_name: str = "scikit-build") -> dict[str, Any]:
"type": "string",
"description": "The state of the build, one of `sdist`, `wheel`, `editable`, `metadata_wheel`, and `metadata_editable`. Takes a regex.",
},
"from-sdist": {
"type": "boolean",
"description": "Whether the build is from an sdist.",
},
"env": {
"type": "object",
"patternProperties": {
Expand Down
40 changes: 40 additions & 0 deletions tests/test_settings_overrides.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,3 +490,43 @@ def test_skbuild_overrides_inherit(inherit: str, tmp_path: Path):
assert settings.wheel.exclude == ["xx", "yy", "x", "y"]
assert settings.install.components == ["c", "d", "a", "b"]
assert settings.cmake.define == {"a": "A", "b": "B", "c": "C"}


@pytest.mark.parametrize("from_sdist", [True, False])
def test_skbuild_overrides_from_sdist(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, from_sdist: bool
):
pyproject_toml = (
tmp_path / ("from_sdist" if from_sdist else "not_from_sdist") / "pyproject.toml"
)
pyproject_toml.parent.mkdir(exist_ok=True)
pyproject_toml.write_text(
dedent(
"""\
[tool.scikit-build]
cmake.version = ">=3.15"
wheel.cmake = false
sdist.cmake = false

[[tool.scikit-build.overrides]]
if.from-sdist = false
wheel.cmake = true

[[tool.scikit-build.overrides]]
if.from-sdist = true
sdist.cmake = true
"""
),
encoding="utf-8",
)

if from_sdist:
pyproject_toml.parent.joinpath("PKG-INFO").touch(exist_ok=True)

monkeypatch.chdir(pyproject_toml.parent)

settings_reader = SettingsReader.from_file(pyproject_toml, state="wheel")
settings = settings_reader.settings

assert settings.wheel.cmake != from_sdist
assert settings.sdist.cmake == from_sdist
Loading