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