|
8 | 8 | import functools |
9 | 9 | import os |
10 | 10 | import platform |
| 11 | +import re |
| 12 | +import shutil |
11 | 13 | import sysconfig |
12 | 14 | import typing |
13 | 15 | from abc import ABC, abstractmethod |
@@ -90,6 +92,28 @@ def __init_subclass__(cls) -> None: |
90 | 92 | if cls._platform: |
91 | 93 | WheelRepairer._platform_repairers[cls._platform] = cls |
92 | 94 |
|
| 95 | + @functools.cached_property |
| 96 | + def bundled_libs_path(self) -> Path: |
| 97 | + """Staging path for the bundled library directory.""" |
| 98 | + return Path(self.wheel_dirs["platlib"]) / f"{self.name}.libs" |
| 99 | + |
| 100 | + @functools.cached_property |
| 101 | + def bundle_external(self) -> list[re.Pattern[str]]: |
| 102 | + """List of compiled regex patterns of the library files to bundle.""" |
| 103 | + patterns = [] |
| 104 | + for pattern_str in self.settings.wheel.repair.bundle_external: |
| 105 | + try: |
| 106 | + pattern = re.compile(pattern_str) |
| 107 | + except re.error as exc: |
| 108 | + logger.warning( |
| 109 | + 'Skipping "{pattern}" as an invalid pattern', |
| 110 | + pattern=pattern_str, |
| 111 | + ) |
| 112 | + logger.debug(str(exc)) |
| 113 | + continue |
| 114 | + patterns.append(pattern) |
| 115 | + return patterns |
| 116 | + |
93 | 117 | @functools.cached_property |
94 | 118 | def configuration(self) -> Configuration: |
95 | 119 | """Current file-api configuration.""" |
@@ -195,8 +219,91 @@ def get_library_dependencies(self, target: Target) -> list[Target]: |
195 | 219 | dependencies.append(dep_target) |
196 | 220 | return dependencies |
197 | 221 |
|
| 222 | + def try_bundle(self, external_lib: Path) -> Path | None: |
| 223 | + """ |
| 224 | + Try to bundle an external library file. |
| 225 | +
|
| 226 | + :param external_lib: path to actual external library to bundle |
| 227 | + :returns: ``None`` if the library is not bundled, otherwise the path |
| 228 | + to the bundled file |
| 229 | + """ |
| 230 | + assert external_lib.is_absolute() |
| 231 | + if not external_lib.exists(): |
| 232 | + logger.warning( |
| 233 | + "External library file does not exist: {external_lib}", |
| 234 | + external_lib=external_lib, |
| 235 | + ) |
| 236 | + return None |
| 237 | + if external_lib.is_dir(): |
| 238 | + logger.debug( |
| 239 | + "Skip bundling directory: {external_lib}", |
| 240 | + external_lib=external_lib, |
| 241 | + ) |
| 242 | + return None |
| 243 | + libname = external_lib.name |
| 244 | + bundled_lib = self.bundled_libs_path / libname |
| 245 | + if bundled_lib.exists(): |
| 246 | + # If we have already bundled the library no need to do it again |
| 247 | + return bundled_lib |
| 248 | + for pattern in self.bundle_external: |
| 249 | + if pattern.match(libname): |
| 250 | + logger.debug( |
| 251 | + 'Bundling library matching "{pattern}": {external_lib}', |
| 252 | + external_lib=external_lib, |
| 253 | + pattern=pattern.pattern, |
| 254 | + ) |
| 255 | + shutil.copy(external_lib, bundled_lib) |
| 256 | + return bundled_lib |
| 257 | + logger.debug( |
| 258 | + "Skip bundling: {external_lib}", |
| 259 | + external_lib=external_lib, |
| 260 | + ) |
| 261 | + return None |
| 262 | + |
| 263 | + def get_package_lib_path( |
| 264 | + self, original_lib: Path, relative_to: Path | None = None |
| 265 | + ) -> Path | None: |
| 266 | + """ |
| 267 | + Get the file path of a library to be used. |
| 268 | +
|
| 269 | + This checks for the settings in ``settings.wheel.repair`` returning either: |
| 270 | + - If the dependency should be skipped: ``None`` |
| 271 | + - If ``original_lib`` is a library in another wheel: a relative path to the original library file |
| 272 | + - If ``original_lib`` is a library to be bundled: a relative path to the bundled library file |
| 273 | +
|
| 274 | + The relative paths are relative to ``relative_to`` or the ``platlib`` wheel path if not passed. |
| 275 | + """ |
| 276 | + if not original_lib.is_absolute() or not original_lib.exists(): |
| 277 | + logger.debug( |
| 278 | + "Could not handle {original_lib} because it is either relative or does not exist.", |
| 279 | + original_lib=original_lib, |
| 280 | + ) |
| 281 | + return None |
| 282 | + if self.path_is_in_site_packages(original_lib): |
| 283 | + # The other library is in another wheel |
| 284 | + if not self.settings.wheel.repair.cross_wheel: |
| 285 | + logger.debug( |
| 286 | + "Skipping {original_lib} because it is in another wheel.", |
| 287 | + original_lib=original_lib, |
| 288 | + ) |
| 289 | + return None |
| 290 | + final_lib = original_lib |
| 291 | + # Otherwise, check if we need to bundle the external library |
| 292 | + elif not self.bundle_external or not ( |
| 293 | + final_lib := self.try_bundle(original_lib) # type: ignore[assignment] |
| 294 | + ): |
| 295 | + logger.debug( |
| 296 | + "Skipping {original_lib} because it is not being bundled.", |
| 297 | + original_lib=original_lib, |
| 298 | + ) |
| 299 | + return None |
| 300 | + return self.path_relative_site_packages(final_lib, relative_to=relative_to) |
| 301 | + |
198 | 302 | def repair_wheel(self) -> None: |
199 | 303 | """Repair the current wheel.""" |
| 304 | + if self.bundle_external: |
| 305 | + self.bundled_libs_path.mkdir(exist_ok=True) |
| 306 | + |
200 | 307 | for target in self.targets: |
201 | 308 | if self._filter_targets: |
202 | 309 | if target.type == "STATIC_LIBRARY": |
|
0 commit comments