Skip to content

Commit 8301d2c

Browse files
committed
Implement the build_dir format variables
Signed-off-by: Cristian Le <[email protected]>
1 parent 9a9e61b commit 8301d2c

File tree

4 files changed

+52
-23
lines changed

4 files changed

+52
-23
lines changed

src/scikit_build_core/build/_file_processor.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import pathspec
99

10+
from scikit_build_core.format import DummyFormatter
11+
1012
if TYPE_CHECKING:
1113
from collections.abc import Generator, Sequence
1214

@@ -51,12 +53,7 @@ def each_unignored_file(
5153
if p != Path(".gitignore")
5254
}
5355

54-
exclude_build_dir = build_dir.format(
55-
cache_tag="*",
56-
wheel_tag="*",
57-
build_type="*",
58-
state="*",
59-
)
56+
exclude_build_dir = build_dir.format(**DummyFormatter())
6057

6158
exclude_lines = (
6259
[*EXCLUDE_LINES, exclude_build_dir] if exclude_build_dir else EXCLUDE_LINES

src/scikit_build_core/build/wheel.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import dataclasses
44
import os
55
import shutil
6-
import sys
76
import sysconfig
87
import tempfile
98
from collections.abc import Mapping
@@ -20,6 +19,7 @@
2019
from ..builder.wheel_tag import WheelTag
2120
from ..cmake import CMake, CMaker
2221
from ..errors import FailedLiveProcessError
22+
from ..format import Formatter
2323
from ..settings.skbuild_read_settings import SettingsReader
2424
from ._editable import editable_redirect, libdir_to_installed, mapping_to_modules
2525
from ._init import setup_logging
@@ -279,10 +279,11 @@ def _build_wheel_impl_impl(
279279
build_dir = (
280280
Path(
281281
settings.build_dir.format(
282-
cache_tag=sys.implementation.cache_tag,
283-
wheel_tag=str(tags),
284-
build_type=settings.cmake.build_type,
285-
state=state,
282+
**Formatter(
283+
settings=settings,
284+
tags=tags,
285+
state=state,
286+
)
286287
)
287288
)
288289
if settings.build_dir

src/scikit_build_core/format.py

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@
33
from __future__ import annotations
44

55
import dataclasses
6+
import sys
67
from collections.abc import Mapping
7-
from typing import TYPE_CHECKING, Any
8+
from typing import TYPE_CHECKING, Any, Literal
89

910
if TYPE_CHECKING:
1011
from collections.abc import Iterator
1112

13+
from scikit_build_core.builder.wheel_tag import WheelTag
1214
from scikit_build_core.settings.skbuild_model import ScikitBuildSettings
1315

1416
__all__ = [
17+
"DummyFormatter",
1518
"Formatter",
1619
]
1720

@@ -22,18 +25,45 @@ class Formatter(Mapping[str, Any]):
2225

2326
_dict: dict[str, Any] = dataclasses.field(init=False)
2427
"""Dict storage of the acceptable variable keys and their expanded values."""
25-
settings: ScikitBuildSettings
28+
settings: ScikitBuildSettings | None = None
2629
"""Parsed settings."""
30+
state: (
31+
Literal["sdist", "wheel", "editable", "metadata_wheel", "metadata_editable"]
32+
| None
33+
) = None
34+
tags: WheelTag | None = None
2735

2836
def __getitem__(self, key: str) -> Any:
2937
return self._dict[key]
3038

3139
def __len__(self) -> int:
32-
return len(self._dict)
40+
return len(list(iter(self)))
3341

34-
def __iter__(self) -> Iterator[Any]:
35-
return iter(self._dict)
42+
def __iter__(self) -> Iterator[str]:
43+
# Filter out all the unknown/unset values so that when unpacking
44+
# only the known/populated key-value pairs are expanded
45+
for key in self._dict:
46+
if self._dict[key] is not None:
47+
yield key
3648

3749
def __post_init__(self) -> None:
38-
# TODO: Initialize the container with all known values
39-
self._dict = {}
50+
# For consistency and so that DummyFormatter can override all know keys,
51+
# we define all the known keys or populate them with a `None`
52+
self._dict = {
53+
"cache_tag": sys.implementation.cache_tag,
54+
"wheel_tag": str(self.tags) if self.tags else None,
55+
"build_type": self.settings.cmake.build_type if self.settings else None,
56+
"state": self.state if self.state else None,
57+
}
58+
59+
60+
class DummyFormatter(Formatter):
61+
"""Dummy formatter that evaluates the formatting keys to dummy values."""
62+
63+
def __getitem__(self, key: str) -> str:
64+
if key in self._dict:
65+
return "*"
66+
raise KeyError
67+
68+
def __iter__(self) -> Iterator[str]:
69+
return iter(self._dict)

src/scikit_build_core/hatch/plugin.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import importlib.metadata
77
import os
88
import shutil
9-
import sys
109
import sysconfig
1110
import tempfile
1211
import typing
@@ -24,6 +23,7 @@
2423
from ..builder.get_requires import GetRequires
2524
from ..builder.wheel_tag import WheelTag
2625
from ..cmake import CMake, CMaker
26+
from ..format import Formatter
2727
from ..settings.skbuild_read_settings import SettingsReader
2828

2929
__all__ = ["ScikitBuildHook"]
@@ -162,10 +162,11 @@ def _initialize(self, *, build_data: dict[str, Any]) -> None:
162162
build_dir = (
163163
Path(
164164
settings.build_dir.format(
165-
cache_tag=sys.implementation.cache_tag,
166-
wheel_tag=str(tags),
167-
build_type=settings.cmake.build_type,
168-
state=state,
165+
**Formatter(
166+
settings=settings,
167+
tags=tags,
168+
state=state,
169+
)
169170
)
170171
)
171172
if settings.build_dir

0 commit comments

Comments
 (0)