Repository navigation
Expand file tree
/
Copy pathhatch_build.py
More file actions
47 lines (41 loc) · 2.04 KB
/
Copy pathhatch_build.py
File metadata and controls
47 lines (41 loc) · 2.04 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
"""Select the package's Core stamp using the build target."""
import subprocess
from importlib.util import module_from_spec, spec_from_file_location
from pathlib import Path
from shutil import copyfile
from hatchling.builders.hooks.plugin.interface import BuildHookInterface
class CoreBuildHook(BuildHookInterface):
PLUGIN_NAME = "custom"
def initialize(self, version, build_data):
if version == "editable":
return
root = Path(self.root)
relative = "src/agent_comms/package_stamp.py"
source = root / relative
# Load the stamp under its package name, so the generated value imports its type
# from the same place at runtime.
spec = spec_from_file_location(
"agent_comms.installer.core_stamp", root / "src/agent_comms/installer/core_stamp.py"
)
types = module_from_spec(spec)
spec.loader.exec_module(types)
# Never overwrite a source checkout's stamp while building its wheel.
path = Path(self.directory) / ".core_stamp.py"
if (root / ".git").exists():
# Pin this source revision before stamping its producer digests.
commit = subprocess.check_output(
("git", "-C", str(root.resolve()), "rev-parse", "HEAD"),
text=True,
).strip()
types.WheelStamp.of_source(root / "src", commit).write(path)
elif not (commit := "$Format:%H$").startswith("$Format"):
types.WheelStamp.of_source(root / "src", commit).write(path)
else:
# A source distribution already carries its generated wheel value.
path.parent.mkdir(parents=True, exist_ok=True)
copyfile(source, path)
destination = relative.removeprefix("src/") if self.target_name == "wheel" else relative
self.build_config.build_reserved_paths.add(destination)
build_data["force_include"][str(path)] = destination
def finalize(self, version, build_data, artifact_path):
(Path(self.directory) / ".core_stamp.py").unlink(missing_ok=True)