-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhatch_build.py
More file actions
74 lines (59 loc) · 2.8 KB
/
Copy pathhatch_build.py
File metadata and controls
74 lines (59 loc) · 2.8 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
67
68
69
70
71
72
73
74
"""Hatch build hook to bundle framework content, excluding test fixtures."""
from __future__ import annotations
from pathlib import Path
from typing import Any
from hatchling.builders.hooks.plugin.interface import BuildHookInterface
# Framework content to bundle into the wheel.
# Source paths are relative to repo root; destinations are inside the wheel.
FRAMEWORK_INCLUDES = {
"framework/rules": "reporails_cli/rules",
"framework/sources.yml": "reporails_cli/sources.yml",
}
# Bundled ML assets (gitignored, but must be in wheel).
# Populated by scripts/fetch_bundled_model.py before build.
BUNDLED_MODEL = "src/reporails_cli/bundled/models"
BUNDLED_MODEL_DEST = "reporails_cli/bundled/models"
BUNDLED_SPACY = "src/reporails_cli/bundled/spacy"
BUNDLED_SPACY_DEST = "reporails_cli/bundled/spacy"
# Directory names to skip when bundling (rule test fixtures are dev-only)
SKIP_DIRS = {"tests"}
class CustomBuildHook(BuildHookInterface):
PLUGIN_NAME = "custom"
def initialize(self, version: str, build_data: dict[str, Any]) -> None:
root = Path(self.root)
force_include = build_data["force_include"]
for src_rel, dest_rel in FRAMEWORK_INCLUDES.items():
src = root / src_rel
if src.is_file():
force_include[str(src)] = dest_rel
elif src.is_dir():
for path in src.rglob("*"):
if path.is_file() and not _in_skip_dir(path, src):
rel = path.relative_to(src)
force_include[str(path)] = f"{dest_rel}/{rel}"
# Bundle ONNX model (gitignored but required at runtime)
model_dir = root / BUNDLED_MODEL
if model_dir.is_dir():
for path in model_dir.rglob("*"):
if not path.is_file():
continue
# Skip HF download cache metadata
if ".cache" in path.parts:
continue
rel = path.relative_to(root / "src")
force_include[str(path)] = str(rel)
# Bundle spaCy en_core_web_sm model (gitignored but required at runtime)
spacy_dir = root / BUNDLED_SPACY
if spacy_dir.is_dir():
for path in spacy_dir.rglob("*"):
if not path.is_file():
continue
rel = path.relative_to(root / "src")
force_include[str(path)] = str(rel)
# Remove the pyproject.toml force-include entries (they'd double-include)
# — handled by clearing them from config before this hook, or by
# removing them from pyproject.toml entirely.
def _in_skip_dir(path: Path, base: Path) -> bool:
"""Check if any parent directory between base and path is in SKIP_DIRS."""
rel = path.relative_to(base)
return any(part in SKIP_DIRS for part in rel.parts)