|
| 1 | +""" |
| 2 | +Repair wheel |
| 3 | +""" |
| 4 | + |
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +import dataclasses |
| 8 | +import platform |
| 9 | +from abc import ABC, abstractmethod |
| 10 | +from importlib import import_module |
| 11 | +from typing import TYPE_CHECKING, ClassVar, Final |
| 12 | + |
| 13 | +from .._logging import logger |
| 14 | + |
| 15 | +if TYPE_CHECKING: |
| 16 | + from ..build._wheelfile import WheelWriter |
| 17 | + from ..builder.builder import Builder |
| 18 | + |
| 19 | + |
| 20 | +__all__ = [ |
| 21 | + "WheelRepairer", |
| 22 | +] |
| 23 | + |
| 24 | + |
| 25 | +@dataclasses.dataclass() |
| 26 | +class WheelRepairer(ABC): |
| 27 | + """Abstract wheel repairer.""" |
| 28 | + |
| 29 | + wheel: WheelWriter |
| 30 | + """The current wheel creator.""" |
| 31 | + builder: Builder |
| 32 | + """CMake builder used.""" |
| 33 | + _platform_repairers: ClassVar[dict[str, type[WheelRepairer]]] = {} |
| 34 | + """Dictionary of platform specific repairers""" |
| 35 | + _platform: ClassVar[str | None] = None |
| 36 | + """The ``platform.system()`` corresponding to the current repairer.""" |
| 37 | + _initialized: Final[bool] = False |
| 38 | + """Whether all ``WheelRepairer`` have been initialized.""" |
| 39 | + |
| 40 | + def __init_subclass__(cls) -> None: |
| 41 | + if cls._platform: |
| 42 | + WheelRepairer._platform_repairers[cls._platform] = cls |
| 43 | + |
| 44 | + @abstractmethod |
| 45 | + def repair_wheel(self) -> None: |
| 46 | + """Repair the current wheel.""" |
| 47 | + |
| 48 | + @classmethod |
| 49 | + def get_wheel_repairer( |
| 50 | + cls, |
| 51 | + wheel: WheelWriter, |
| 52 | + builder: Builder, |
| 53 | + ) -> WheelRepairer: |
| 54 | + """Construct the platform specific wheel repairer""" |
| 55 | + WheelRepairer.initialize() |
| 56 | + if not ( |
| 57 | + repairer_cls := WheelRepairer._platform_repairers.get(platform.system()) |
| 58 | + ): |
| 59 | + return NoopWheelRepairer( |
| 60 | + wheel=wheel, |
| 61 | + builder=builder, |
| 62 | + ) |
| 63 | + return repairer_cls( |
| 64 | + wheel=wheel, |
| 65 | + builder=builder, |
| 66 | + ) |
| 67 | + |
| 68 | + @classmethod |
| 69 | + def initialize(cls) -> None: |
| 70 | + """Get all known wheel repairers.""" |
| 71 | + if cls._initialized: |
| 72 | + return |
| 73 | + # TODO: Allow for other wheel repairers defined as entry-points |
| 74 | + try: |
| 75 | + if (platform_system := platform.system().lower()) in ( |
| 76 | + "linux", |
| 77 | + "darwin", |
| 78 | + "windows", |
| 79 | + ): |
| 80 | + import_module(f".{platform_system}", package=__name__) |
| 81 | + except ImportError: |
| 82 | + logger.error( |
| 83 | + "Could not load the platform specific wheel repairer. Make sure you include " |
| 84 | + "the optional dependency [wheel-repair] in the build-system.requires" |
| 85 | + ) |
| 86 | + raise |
| 87 | + |
| 88 | + |
| 89 | +class NoopWheelRepairer(WheelRepairer): |
| 90 | + """Dummy wheel repairer that just shows a warning.""" |
| 91 | + |
| 92 | + def repair_wheel(self) -> None: |
| 93 | + # Do nothing |
| 94 | + logger.warning("Unknown platform {}. Not doing any repair.", platform.system()) |
0 commit comments