Repository navigation
Expand file tree
/
Copy pathbuild_python_packages.py
More file actions
231 lines (185 loc) · 7.21 KB
/
Copy pathbuild_python_packages.py
File metadata and controls
231 lines (185 loc) · 7.21 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/usr/bin/env python3
from __future__ import annotations
import argparse
import logging
import os
import shutil
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
SIDECAR_ROOT = ROOT / "packages" / "jiuwenswarm-tui"
SIDE_CAR_DIST = SIDECAR_ROOT / "dist"
JIUWENBOX_ROOT = ROOT / "jiuwenbox"
JIUWENBOX_DIST = JIUWENBOX_ROOT / "dist"
TUI_ROOT = ROOT / "jiuwenswarm" / "channels" / "tui" / "frontend"
TUI_TARGETS = {
"linux-x64": "linux_x86_64",
"linux-arm64": "linux_aarch64",
#"macos-x64": "macosx_10_15_x86_64",
"macos-arm64": "macosx_11_0_arm64",
"windows-x64": "win_amd64",
#"windows-arm64": "win_arm64",
}
def run(cmd: list[str], cwd: Path, env: dict[str, str] | None = None) -> None:
logging.info(f"[build] ({cwd.relative_to(ROOT) if cwd != ROOT else '.'}) {' '.join(cmd)}")
subprocess.run(cmd, cwd=cwd, check=True, env=env)
def remove_path(path: Path) -> None:
if path.is_dir():
shutil.rmtree(path)
return
if path.exists():
path.unlink()
def clean_root() -> None:
for relative in ("build", "jiuwenswarm.egg-info"):
remove_path(ROOT / relative)
def clean_sidecar() -> None:
for relative in ("dist", "build", "jiuwenswarm_tui.egg-info"):
remove_path(SIDECAR_ROOT / relative)
def clean_jiuwenbox() -> None:
for relative in ("dist", "build", "jiuwenbox.egg-info"):
remove_path(JIUWENBOX_ROOT / relative)
def build_root_wheel() -> None:
SIDE_CAR_DIST.mkdir(parents=True, exist_ok=True)
run(["uv", "build", "--wheel", "--out-dir", str(SIDE_CAR_DIST)], ROOT)
def build_sidecar_wheel(platform_tag: str | None = None) -> None:
for relative in ("build", "jiuwenswarm_tui.egg-info"):
remove_path(SIDECAR_ROOT / relative)
env = os.environ.copy()
if platform_tag:
env["JWC_TUI_WHEEL_PLATFORM"] = platform_tag
run(["uv", "build", "--wheel"], SIDECAR_ROOT, env=env)
def build_jiuwenbox_wheel() -> None:
for relative in ("build", "jiuwenbox.egg-info"):
remove_path(JIUWENBOX_ROOT / relative)
JIUWENBOX_DIST.mkdir(parents=True, exist_ok=True)
run(["uv", "build", "--wheel", "--out-dir", str(JIUWENBOX_DIST)], JIUWENBOX_ROOT)
def build_tui_binary(target: str, clean: bool) -> None:
cmd = [sys.executable, "scripts/build_tui.py", "--target", target]
if clean:
cmd.append("--clean")
run(cmd, ROOT)
class MissingJsDependenciesError(RuntimeError):
"""Raised when TUI frontend node_modules is missing and auto-install is disabled."""
class InvalidTargetError(ValueError):
"""Raised when --target contains unknown TUI build target(s)."""
def ensure_js_dependencies(install: bool) -> None:
node_modules = TUI_ROOT / "node_modules"
if node_modules.exists():
return
if not install:
raise MissingJsDependenciesError(
"\n".join(
[
"missing JavaScript dependencies for jiuwenswarm/channels/tui/frontend",
f"expected: {node_modules}",
"run one of:",
" cd jiuwenswarm/channels/tui/frontend && npm install",
" python scripts/build_python_packages.py --install-js-deps",
]
)
)
if (TUI_ROOT / "package-lock.json").exists():
run(["npm", "install"], TUI_ROOT)
return
if (TUI_ROOT / "bun.lock").exists() or (TUI_ROOT / "bun.lockb").exists():
run(["bun", "install"], TUI_ROOT)
return
run(["npm", "install"], TUI_ROOT)
def resolve_requested_targets(raw: str) -> list[str]:
if raw == "all":
return list(TUI_TARGETS.keys())
values = [part.strip() for part in raw.split(",") if part.strip()]
if raw == "current":
return ["current"]
unknown = [value for value in values if value not in TUI_TARGETS]
if unknown:
valid = ", ".join(["current", "all", *TUI_TARGETS.keys()])
raise InvalidTargetError(f"unknown target(s): {', '.join(unknown)}; valid: {valid}")
return values
def main() -> None:
parser = argparse.ArgumentParser(
description=(
"Build JiuwenSwarm Python distributions "
"(main package, optional TUI sidecar, and optional jiuwenbox wheel)."
),
)
parser.add_argument(
"--target",
default="current",
help="TUI binary target passed to build_tui.py (default: current)",
)
parser.add_argument(
"--out-dir",
default="./packages/jiuwenswarm-tui/dist",
help="Directory to output the built TUI binary (default: ./packages/jiuwenswarm-tui/dist)",
)
parser.add_argument(
"--skip-binary",
action="store_true",
help="Skip building the native TUI binary before building the sidecar wheel",
)
parser.add_argument(
"--skip-root",
action="store_true",
help="Skip building the main jiuwenswarm wheel",
)
parser.add_argument(
"--skip-sidecar",
action="store_true",
help="Skip building the jiuwenswarm-tui sidecar wheel",
)
parser.add_argument(
"--skip-jiuwenbox",
action="store_true",
help="Skip building the jiuwenbox wheel",
)
parser.add_argument(
"--clean",
action="store_true",
help="Clean dist/build/egg-info before building",
)
parser.add_argument(
"--install-js-deps",
action="store_true",
help="Install jiuwenswarm/channels/tui/frontend JavaScript dependencies if node_modules is missing",
)
args = parser.parse_args()
targets = resolve_requested_targets(args.target)
if args.clean:
if not args.skip_root:
clean_root()
if not args.skip_sidecar:
clean_sidecar()
if not args.skip_jiuwenbox:
clean_jiuwenbox()
if args.skip_sidecar and args.skip_root and args.skip_jiuwenbox:
raise SystemExit("nothing to build: --skip-root, --skip-sidecar, and --skip-jiuwenbox were all set")
if args.skip_binary and not args.skip_sidecar and len(targets) > 1:
raise SystemExit("--skip-binary only supports a single sidecar wheel target")
if not args.skip_sidecar and not args.skip_binary:
ensure_js_dependencies(args.install_js_deps)
if not args.skip_root:
build_root_wheel()
if not args.skip_sidecar:
for index, target in enumerate(targets):
if not args.skip_binary:
build_tui_binary(target, clean=args.clean or index > 0)
platform_tag = None if target == "current" else TUI_TARGETS[target]
build_sidecar_wheel(platform_tag=platform_tag)
if not args.skip_jiuwenbox:
build_jiuwenbox_wheel()
logging.info("\n[build] done")
if not args.skip_root:
logging.info(f"[build] main wheel: {SIDE_CAR_DIST}")
if not args.skip_sidecar:
logging.info(f"[build] tui wheel(s): {SIDE_CAR_DIST}")
if not args.skip_jiuwenbox:
logging.info(f"[build] jiuwenbox wheel: {JIUWENBOX_DIST}")
if __name__ == "__main__":
try:
main()
except (MissingJsDependenciesError, InvalidTargetError) as exc:
raise SystemExit(str(exc)) from exc
except subprocess.CalledProcessError as exc:
raise SystemExit(exc.returncode) from exc