Repository navigation
Expand file tree
/
Copy pathhatch_build.py
More file actions
45 lines (36 loc) · 1.75 KB
/
Copy pathhatch_build.py
File metadata and controls
45 lines (36 loc) · 1.75 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
"""Write python-sbom's own SBOM into its wheel with the hook the package ships."""
from __future__ import annotations
import sys
from functools import cached_property
from pathlib import Path
from typing import TYPE_CHECKING, Any
from hatchling.builders.hooks.plugin.interface import BuildHookInterface
if TYPE_CHECKING:
from python_sbom.hatch import SbomBuildHook
class SelfSbomHook(BuildHookInterface): # simplint: ignore[SMP409] # hatchling loads it from this script
def initialize(
self,
version: str,
build_data: dict[str, Any], # simplint: ignore[SMP602] # hatchling's signature
) -> None:
self._hook.initialize(version, build_data)
@cached_property
def _hook(self) -> SbomBuildHook:
# python-sbom cannot be a build requirement of itself, so its hook comes from this source tree; by now the vcs
# hook has written the version module the package imports. One hook serves the whole build, so finalize reuses
# the revision initialize read and removes the revision file it wrote
sys.path.insert(0, str(Path(self.root) / "src"))
from python_sbom.hatch import SbomBuildHook # ruff: ignore[import-outside-top-level] # see above
hook = SbomBuildHook(
self.root, self.config, self.build_config, self.metadata, self.directory, self.target_name, self.app
)
# as hatchling names a custom hook, so the SBOM lists this script among the build tools
hook.PLUGIN_NAME = self.PLUGIN_NAME
return hook
def finalize(
self,
version: str,
build_data: dict[str, Any], # simplint: ignore[SMP602] # hatchling's signature
artifact_path: str,
) -> None:
self._hook.finalize(version, build_data, artifact_path)