-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.py
More file actions
198 lines (164 loc) · 5.62 KB
/
Copy pathbuild.py
File metadata and controls
198 lines (164 loc) · 5.62 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
"""Build script to package semble as a standalone executable using PyInstaller."""
import argparse
import importlib.util
import platform
import subprocess
import sys
from pathlib import Path
def find_package_path(package_name: str) -> Path:
"""Find the installed path of a Python package."""
spec = importlib.util.find_spec(package_name)
if spec is None or spec.origin is None:
raise RuntimeError(f"Package '{package_name}' is not installed.")
return Path(spec.origin).parent
def find_tree_sitter_languages() -> list[str]:
"""Find tree-sitter language .so/.dylib/.dll files to bundle."""
try:
ts_lang_pack_path = find_package_path("tree_sitter_language_pack")
except RuntimeError:
print("Warning: tree_sitter_language_pack not found, skipping language bundling.")
return []
datas = []
# Include the entire package to capture all grammar .so files
datas.append(f"--add-data={ts_lang_pack_path}:tree_sitter_language_pack")
return datas
def find_semble_data() -> list[str]:
"""Find semble package data (agents/*.md files) to bundle."""
try:
semble_path = find_package_path("semble")
except RuntimeError:
raise RuntimeError("semble is not installed. Run: uv sync")
datas = []
agents_dir = semble_path / "agents"
if agents_dir.exists():
datas.append(f"--add-data={agents_dir}:semble/agents")
return datas
def find_model2vec_data() -> list[str]:
"""Find model2vec package data to bundle."""
try:
model2vec_path = find_package_path("model2vec")
except RuntimeError:
return []
datas = []
# Include the package extras metadata
datas.append(f"--add-data={model2vec_path}:model2vec")
return datas
def build_executable(
onefile: bool = True,
with_mcp: bool = False,
name: str = "semble",
) -> None:
"""Run PyInstaller to create the semble executable."""
# Use our own entry_point.py which imports semble.cli
entry_point = Path(__file__).parent / "entry_point.py"
if not entry_point.exists():
raise RuntimeError(f"Cannot find entry_point.py at {entry_point}")
# Base PyInstaller command
cmd = [
sys.executable, "-m", "PyInstaller",
str(entry_point),
"--name", name,
"--noconfirm",
"--clean",
"--additional-hooks-dir", str(Path(__file__).parent / "hooks"),
]
# One-file vs one-dir
if onefile:
cmd.append("--onefile")
else:
cmd.append("--onedir")
# Hidden imports that PyInstaller might miss
hidden_imports = [
"semble",
"semble.cli",
"semble.index",
"semble.cache",
"semble.stats",
"semble.types",
"semble.utils",
"model2vec",
"model2vec.utils",
"vicinity",
"bm25s",
"numpy",
"pathspec",
"tree_sitter",
"tree_sitter_language_pack",
"orjson",
]
if with_mcp:
hidden_imports.extend([
"semble.mcp",
"mcp",
"watchfiles",
])
for imp in hidden_imports:
cmd.extend(["--hidden-import", imp])
# Collect submodules for packages that have dynamic imports
cmd.extend(["--collect-submodules", "tree_sitter_language_pack"])
cmd.extend(["--collect-submodules", "semble"])
cmd.extend(["--collect-submodules", "model2vec"])
cmd.extend(["--collect-submodules", "vicinity"])
cmd.extend(["--collect-submodules", "bm25s"])
# Collect data files
cmd.extend(["--collect-data", "tree_sitter_language_pack"])
cmd.extend(["--collect-data", "semble"])
cmd.extend(["--collect-data", "model2vec"])
# Add semble agent markdown files
cmd.extend(find_semble_data())
# Add model2vec data
cmd.extend(find_model2vec_data())
# Add tree-sitter language grammars
cmd.extend(find_tree_sitter_languages())
# Platform-specific options
# Note: universal2 requires a fat Python interpreter; skip if not available
if platform.system() == "Darwin" and platform.machine() == "x86_64":
# Only set target-arch if running on Intel; on ARM, let it default
pass
print(f"Running: {' '.join(cmd)}")
subprocess.run(cmd, check=True)
# Report result
if onefile:
suffix = ".exe" if platform.system() == "Windows" else ""
binary = Path("dist") / f"{name}{suffix}"
else:
binary = Path("dist") / name
if binary.exists():
size_mb = binary.stat().st_size / (1024 * 1024)
print(f"\nBuild successful!")
print(f" Binary: {binary}")
print(f" Size: {size_mb:.1f} MB")
else:
print(f"\nBuild completed. Output in dist/")
def main() -> None:
"""Parse arguments and run the build."""
parser = argparse.ArgumentParser(
description="Build semble as a standalone executable."
)
mode_group = parser.add_mutually_exclusive_group()
mode_group.add_argument(
"--onefile",
action="store_true",
default=True,
help="Build a single portable binary (default).",
)
mode_group.add_argument(
"--onedir",
action="store_true",
help="Build a directory with all files (faster startup).",
)
parser.add_argument(
"--with-mcp",
action="store_true",
help="Include MCP server dependencies in the build.",
)
parser.add_argument(
"--name",
default="semble",
help="Output binary name (default: semble).",
)
args = parser.parse_args()
onefile = not args.onedir
build_executable(onefile=onefile, with_mcp=args.with_mcp, name=args.name)
if __name__ == "__main__":
main()