|
10 | 10 |
|
11 | 11 | from .. import __version__ |
12 | 12 | from .._compat.importlib import metadata, resources |
| 13 | +from .._compat.importlib.readers import MultiplexedPath |
13 | 14 | from .._logging import logger |
14 | 15 | from ..resources import find_python |
15 | 16 | from .generator import set_environment_for_gen |
|
23 | 24 |
|
24 | 25 | if TYPE_CHECKING: |
25 | 26 | from collections.abc import Generator, Iterable, Mapping, Sequence |
| 27 | + from typing import Any |
26 | 28 |
|
27 | 29 | from packaging.version import Version |
28 | 30 |
|
@@ -84,6 +86,68 @@ def _filter_env_cmake_args(env_cmake_args: list[str]) -> Generator[str, None, No |
84 | 86 | yield arg |
85 | 87 |
|
86 | 88 |
|
| 89 | +# Type-hinting for Traversable is rather hard because they were introduced in python 3.11. |
| 90 | +# This avoids introducing importlib_resources dependency that may not be used in the actual package loader |
| 91 | +def _sanitize_path(path: Any) -> list[Path] | None: |
| 92 | + if isinstance(path, MultiplexedPath): |
| 93 | + # pylint: disable-next=protected-access |
| 94 | + return path._paths |
| 95 | + if isinstance(path, Path): |
| 96 | + return [path] |
| 97 | + logger.warning("Unknown path type: [{}] {}", type(path), path) |
| 98 | + return None |
| 99 | + |
| 100 | + |
| 101 | +def _handle_search_paths( |
| 102 | + entry_point: str, |
| 103 | + settings_val: list[str] | dict[str, str] | None, |
| 104 | + output: list[Path] | dict[str, list[Path]], |
| 105 | +) -> None: |
| 106 | + # Sanity checks |
| 107 | + if isinstance(output, dict): |
| 108 | + assert isinstance(settings_val, dict) |
| 109 | + |
| 110 | + # Get the search paths from the entry points |
| 111 | + search_paths_dict = {} |
| 112 | + eps = metadata.entry_points(group=entry_point) |
| 113 | + if eps: |
| 114 | + logger.debug( |
| 115 | + "Loading search paths {} from entry-points: {}", entry_point, len(eps) |
| 116 | + ) |
| 117 | + for ep in eps: |
| 118 | + ep_value = _sanitize_path(resources.files(ep.load())) |
| 119 | + logger.debug("{}: {} -> {}", ep.name, ep.value, ep_value) |
| 120 | + if ep_value: |
| 121 | + search_paths_dict[ep.name] = ep_value |
| 122 | + |
| 123 | + # Update the search paths from the settings options |
| 124 | + if isinstance(settings_val, dict) and settings_val: |
| 125 | + logger.debug( |
| 126 | + "Overriding search paths {} from config: {}", entry_point, settings_val |
| 127 | + ) |
| 128 | + for key, val in settings_val.items(): |
| 129 | + if val: |
| 130 | + # TODO: Allow settings_val to be dict[str, list[str]]? |
| 131 | + search_paths_dict[key] = [Path(val)] |
| 132 | + else: |
| 133 | + search_paths_dict.pop(key) |
| 134 | + |
| 135 | + # Write to the output |
| 136 | + if isinstance(output, list): |
| 137 | + search_paths_list = [ |
| 138 | + path for ep_values in search_paths_dict.values() for path in ep_values |
| 139 | + ] |
| 140 | + # If the settings options was a list the values are appended as-is |
| 141 | + if isinstance(settings_val, list) and settings_val: |
| 142 | + logger.debug( |
| 143 | + "Appending search paths {} with config: {}", entry_point, settings_val |
| 144 | + ) |
| 145 | + search_paths_list += map(Path, settings_val) |
| 146 | + output.extend(search_paths_list) |
| 147 | + return |
| 148 | + output.update(search_paths_dict) |
| 149 | + |
| 150 | + |
87 | 151 | @dataclasses.dataclass |
88 | 152 | class Builder: |
89 | 153 | settings: ScikitBuildSettings |
@@ -123,47 +187,20 @@ def configure( |
123 | 187 | } |
124 | 188 |
|
125 | 189 | # Add any extra CMake modules |
126 | | - module_dirs_dict = { |
127 | | - ep.name: resources.files(ep.load()) |
128 | | - for ep in metadata.entry_points(group="cmake.module") |
129 | | - } |
130 | | - if isinstance(self.settings.search.modules, dict): |
131 | | - # Allow to override any entry-point definition |
132 | | - module_dirs_dict.update(self.settings.search.modules) |
133 | | - module_dirs = list(module_dirs_dict.values()) |
134 | | - if isinstance(self.settings.search.modules, list): |
135 | | - # If it was a list, append to the entry-point definitions |
136 | | - module_dirs += self.settings.search.modules |
137 | | - # Remove any empty paths |
138 | | - module_dirs = [path for path in module_dirs if path] |
139 | | - self.config.module_dirs.extend(module_dirs) |
| 190 | + _handle_search_paths( |
| 191 | + "cmake.module", self.settings.search.modules, self.config.module_dirs |
| 192 | + ) |
140 | 193 |
|
141 | 194 | # Add any extra CMake prefixes |
142 | | - prefix_dirs_dict = { |
143 | | - ep.name: resources.files(ep.load()) |
144 | | - for ep in metadata.entry_points(group="cmake.prefix") |
145 | | - } |
146 | | - if isinstance(self.settings.search.prefixes, dict): |
147 | | - # Allow to override any entry-point definition |
148 | | - prefix_dirs_dict.update(self.settings.search.prefixes) |
149 | | - prefix_dirs = list(prefix_dirs_dict.values()) |
150 | | - if isinstance(self.settings.search.prefixes, list): |
151 | | - # If it was a list, append to the entry-point definitions |
152 | | - prefix_dirs += self.settings.search.prefixes |
153 | | - # Remove any empty paths |
154 | | - prefix_dirs = [path for path in prefix_dirs if path] |
155 | | - self.config.prefix_dirs.extend(prefix_dirs) |
| 195 | + _handle_search_paths( |
| 196 | + "cmake.prefix", self.settings.search.prefixes, self.config.prefix_dirs |
| 197 | + ) |
156 | 198 |
|
157 | 199 | # Add all CMake roots |
158 | | - prefix_roots = { |
159 | | - ep.name: resources.files(ep.load()) |
160 | | - for ep in metadata.entry_points(group="cmake.root") |
161 | | - } |
162 | 200 | # TODO: Check for unique uppercase names |
163 | | - prefix_roots.update(self.settings.search.roots) |
164 | | - # Remove any empty paths |
165 | | - prefix_roots = {pkg: path for pkg, path in prefix_roots.items() if path} |
166 | | - self.config.prefix_roots.update(prefix_roots) |
| 201 | + _handle_search_paths( |
| 202 | + "cmake.root", self.settings.search.roots, self.config.prefix_roots |
| 203 | + ) |
167 | 204 |
|
168 | 205 | # Add site-packages to the prefix path for CMake |
169 | 206 | site_packages = Path(sysconfig.get_path("purelib")) |
|
0 commit comments