-
-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathhatch_build.py
More file actions
66 lines (53 loc) · 2.47 KB
/
Copy pathhatch_build.py
File metadata and controls
66 lines (53 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from __future__ import annotations
import os
import shutil
from functools import cached_property
from importlib.metadata import PackagePath, distribution
from typing import Any
import _cffi_backend # noqa: PLC2701
from hatchling.builders.hooks.plugin.interface import BuildHookInterface
class CustomBuildHook(BuildHookInterface):
"""
A build hook that copies the `_cffi_backend` extension module into the wheel so that
the `cffi` package is not required as a runtime dependency.
"""
LICENSE_NAME = "LICENSE-cffi"
LICENSE_MAJOR_VERSION = "2"
FALLBACK_LICENSE = "LICENSE-cffi-2"
@cached_property
def local_cffi_license(self) -> str:
return os.path.join(self.root, self.LICENSE_NAME)
@staticmethod
def get_cffi_distribution_license_files() -> list[PackagePath]:
return [
file
for file in distribution("cffi").files or []
if file.name == "LICENSE" and file.parts[0].endswith(".dist-info")
]
def locate_cffi_license(self) -> str:
cffi_distribution = distribution("cffi")
license_files = self.get_cffi_distribution_license_files()
if len(license_files) == 1:
return str(license_files[0].locate())
major_version = cffi_distribution.version.partition(".")[0]
if major_version != self.LICENSE_MAJOR_VERSION:
message = (
f"Could not locate the CFFI license for version {cffi_distribution.version}, "
"and no matching fallback is available"
)
raise RuntimeError(message)
return os.path.join(self.root, self.FALLBACK_LICENSE)
def initialize(self, version: str, build_data: dict[str, Any]) -> None: # noqa: ARG002
if os.environ.get("COINCURVE_VENDOR_CFFI", "1") != "1":
return
cffi_shared_lib = _cffi_backend.__file__
if cffi_shared_lib is None:
message = "Could not locate the _cffi_backend extension module"
raise RuntimeError(message)
relative_path = f"coincurve/{os.path.basename(cffi_shared_lib)}"
build_data["force_include"][cffi_shared_lib] = relative_path
shutil.copy2(self.locate_cffi_license(), self.local_cffi_license)
self.metadata.core.license_files.append(self.LICENSE_NAME)
def finalize(self, version: str, build_data: dict[str, Any], artifact: str) -> None: # noqa: ARG002
if os.path.isfile(self.local_cffi_license):
os.remove(self.local_cffi_license)