-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathqihse_launcher.py
More file actions
345 lines (279 loc) · 11.9 KB
/
Copy pathqihse_launcher.py
File metadata and controls
345 lines (279 loc) · 11.9 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/usr/bin/env python3
"""
QIHSE Launcher — Unified entry point for the QIHSE vector engine.
Dispatches subcommands to the appropriate Makefile targets, scripts, or
direct library calls. Mirrors the FRAMEWERX ``fw`` launcher pattern.
"""
from __future__ import annotations
import os
import shutil
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parent
SCRIPTS = ROOT / "scripts"
LIBQIHSE = ROOT / "libqihse.so"
# ── Helpers ──────────────────────────────────────────────────────────────
def _run(cmd: list[str], cwd: str | None = None, env: dict | None = None) -> int:
"""Run a command, streaming output to the terminal. Returns exit code."""
full_env = os.environ.copy()
if env:
full_env.update(env)
full_env.setdefault("LD_LIBRARY_PATH", str(ROOT))
return subprocess.call(cmd, cwd=cwd or str(ROOT), env=full_env)
def _run_make(target: str, *extra: str) -> int:
return _run(["make", "-j", str(os.cpu_count() or 4), target, *extra])
def _lib_info() -> dict:
info: dict = {"path": str(LIBQIHSE), "exists": LIBQIHSE.exists()}
if not LIBQIHSE.exists():
return info
info["size_kb"] = LIBQIHSE.stat().st_size // 1024
try:
import ctypes
lib = ctypes.CDLL(str(LIBQIHSE))
if hasattr(lib, "qihse_version"):
lib.qihse_version.restype = ctypes.c_char_p
info["version"] = lib.qihse_version().decode("utf-8", "replace")
if hasattr(lib, "qihse_build_info"):
lib.qihse_build_info.restype = ctypes.c_char_p
info["build_info"] = lib.qihse_build_info().decode("utf-8", "replace")
if hasattr(lib, "qihse_available"):
lib.qihse_available.restype = ctypes.c_bool
info["available"] = lib.qihse_available()
except Exception as exc:
info["load_error"] = str(exc)
return info
# ── Command handlers ─────────────────────────────────────────────────────
def cmd_build(args: list[str]) -> int:
"""Build libqihse.so via make (auto-detects SIMD features)."""
print("[qihse] Building libqihse.so ...")
return _run_make("lib")
def cmd_build_ctypes(args: list[str]) -> int:
"""Build ctypes-only variant (no Python C extension linking)."""
print("[qihse] Building libqihse.so (ctypes-only) ...")
return _run_make("lib-ctypes")
def cmd_build_native(args: list[str]) -> int:
"""Build via build-native.sh with full SIMD auto-detection."""
print("[qihse] Building via build-native.sh ...")
script = SCRIPTS / "build-native.sh"
if not script.exists():
print(f"[qihse] ERROR: {script} not found", file=sys.stderr)
return 1
os.chmod(script, 0o755)
return _run([str(script)] + args)
def cmd_build_tui(args: list[str]) -> int:
"""Launch interactive build configuration TUI."""
print("[qihse] Launching build TUI ...")
return _run([sys.executable, str(SCRIPTS / "build-tui.py")] + args)
def cmd_test(args: list[str]) -> int:
"""Run the full test suite."""
print("[qihse] Running test suite ...")
return _run_make("test")
def cmd_bench(args: list[str]) -> int:
"""Run the benchmark suite (validate-reference-workflow)."""
if args:
return _run_make(*args)
print("[qihse] Running benchmark suite ...")
return _run_make("benchmark")
def cmd_db(args: list[str]) -> int:
"""QIHSE vector DB CLI — delegates to scripts/qihse-db."""
script = SCRIPTS / "qihse-db"
if not script.exists():
print(f"[qihse] ERROR: {script} not found", file=sys.stderr)
return 1
return _run([sys.executable, str(script)] + args)
def cmd_server(args: list[str]) -> int:
"""Build and run the QIHSE test server."""
print("[qihse] Building and running test server ...")
return _run_make("server")
def cmd_keygen(args: list[str]) -> int:
"""Build and run the PQC key generator."""
print("[qihse] Building qihse_keygen ...")
rc = _run_make("keygen")
if rc != 0:
return rc
keygen = ROOT / "qihse_keygen"
if keygen.exists():
return _run([str(keygen)] + args)
print("[qihse] qihse_keygen binary not found after build", file=sys.stderr)
return 1
def cmd_demo(args: list[str]) -> int:
"""Run the Python SDK demo."""
print("[qihse] Running Python SDK demo ...")
env = {"LD_LIBRARY_PATH": str(ROOT)}
full_env = os.environ.copy()
full_env.update(env)
full_env["PYTHONPATH"] = str(ROOT / "python") + os.pathsep + full_env.get("PYTHONPATH", "")
return subprocess.call(
[sys.executable, str(SCRIPTS / "qihse_python_demo.py")] + args,
cwd=str(ROOT), env=full_env,
)
def cmd_python(args: list[str]) -> int:
"""Start a Python REPL with qihse importable."""
env = os.environ.copy()
env["LD_LIBRARY_PATH"] = str(ROOT) + os.pathsep + env.get("LD_LIBRARY_PATH", "")
env["PYTHONPATH"] = str(ROOT / "python") + os.pathsep + env.get("PYTHONPATH", "")
cmd = [sys.executable]
if args:
cmd.extend(args)
else:
cmd.append("-i")
cmd.append("-c")
cmd.append(
"import qihse; print(f'QIHSE {getattr(qihse,\"__version__\",\"?\")} loaded'); "
"print('Available: VectorDB, QihseDB, DistanceMetric'); "
"print('Try: db = qihse.QihseDB(); db.kv_set(\"k\",\"v\"); db.kv_get(\"k\")')"
)
return subprocess.call(cmd, cwd=str(ROOT), env=env)
def cmd_bootstrap(args: list[str]) -> int:
"""Initialize workspace directories."""
print("[qihse] Bootstrapping workspace ...")
script = SCRIPTS / "bootstrap-workspace.sh"
if not script.exists():
print(f"[qihse] ERROR: {script} not found", file=sys.stderr)
return 1
os.chmod(script, 0o755)
return _run(["sh", str(script)] + args)
def cmd_clean(args: list[str]) -> int:
"""Remove build artifacts."""
print("[qihse] Cleaning build artifacts ...")
return _run_make("clean")
def cmd_pristine(args: list[str]) -> int:
"""Deep clean — build artifacts, data, and results."""
print("[qihse] Pristine clean (build + data + results) ...")
rc = _run_make("clean")
if rc != 0:
return rc
for d in ("data", "results", "build"):
p = ROOT / d
if p.exists():
print(f" rm -rf {p}")
shutil.rmtree(p)
print("[qihse] Pristine clean done.")
return 0
def cmd_isa_info(args: list[str]) -> int:
"""Show CPU ISA detection and build flags."""
return _run_make("isa-info")
def cmd_check(args: list[str]) -> int:
"""Run upstream workflow checks."""
print("[qihse] Running workflow checks ...")
return _run_make("check")
def cmd_dev_setup(args: list[str]) -> int:
"""Check required toolchain."""
print("[qihse] Checking toolchain ...")
return _run_make("dev-setup")
def cmd_version(args: list[str]) -> int:
"""Show library version and build info."""
info = _lib_info()
if not info.get("exists"):
print("QIHSE library not built. Run: ./qihse build")
return 1
print(f"QIHSE {info.get('version', 'unknown')}")
print(f" path: {info['path']}")
print(f" size: {info.get('size_kb', '?')} KB")
print(f" build_info: {info.get('build_info', 'n/a')}")
print(f" available: {info.get('available', '?')}")
if "load_error" in info:
print(f" load_error: {info['load_error']}")
return 0
def cmd_status(args: list[str]) -> int:
"""Show build status, library info, and availability."""
info = _lib_info()
print("╔══════════════════════════════════════════════════╗")
print("║ QIHSE Vector Engine — Status ║")
print("╚══════════════════════════════════════════════════╝")
print()
print(f" Root: {ROOT}")
print(f" Library: {info['path']}")
print(f" Built: {'✓' if info['exists'] else '✗ (not built)'}")
if info.get("exists"):
print(f" Version: {info.get('version', '?')}")
print(f" Size: {info.get('size_kb', '?')} KB")
print(f" Available: {info.get('available', '?')}")
if "load_error" in info:
print(f" Load Error: {info['load_error']}")
print()
# Check toolchain
tools = {}
for tool in ("gcc", "make", "python3", "git"):
tools[tool] = shutil.which(tool) is not None
print(" Toolchain:")
for tool, ok in tools.items():
print(f" {tool:12s} {'✓' if ok else '✗'}")
print()
# Check Python bindings
py_bindings = ROOT / "python" / "qihse"
print(f" Python SDK: {'✓' if py_bindings.is_dir() else '✗'}")
# Check scripts
scripts = list(SCRIPTS.glob("*.py")) + list(SCRIPTS.glob("*.sh"))
print(f" Scripts: {len(scripts)} files in scripts/")
print()
if not info.get("exists"):
print(" → Run './qihse build' to build the native library.")
return 0
# ── Dispatch ─────────────────────────────────────────────────────────────
COMMANDS = {
"build": cmd_build,
"build-ctypes": cmd_build_ctypes,
"build-native": cmd_build_native,
"build-tui": cmd_build_tui,
"test": cmd_test,
"bench": cmd_bench,
"db": cmd_db,
"server": cmd_server,
"keygen": cmd_keygen,
"demo": cmd_demo,
"python": cmd_python,
"bootstrap": cmd_bootstrap,
"clean": cmd_clean,
"pristine": cmd_pristine,
"isa-info": cmd_isa_info,
"check": cmd_check,
"dev-setup": cmd_dev_setup,
"version": cmd_version,
"status": cmd_status,
}
def _help() -> None:
print(
"QIHSE Launcher — Unified entry point for the QIHSE vector engine\n"
"\n"
"Usage: ./qihse [command] [args...]\n"
"\n"
"Commands:\n"
" build Build libqihse.so (auto-detect SIMD)\n"
" build-ctypes Build ctypes-only variant (no Python extension)\n"
" build-native Build via build-native.sh (full SIMD auto-detect)\n"
" build-tui Interactive build configuration TUI\n"
" test Run full test suite\n"
" bench Run benchmark suite (or pass a specific bench-* target)\n"
" db QIHSE vector DB CLI (e.g. ./qihse db create --dims 128 --path /tmp/db)\n"
" server Build and run the test server\n"
" keygen Build and run PQC key generator\n"
" demo Run Python SDK demo\n"
" python Start Python REPL with qihse importable\n"
" bootstrap Initialize workspace directories\n"
" clean Remove build artifacts\n"
" pristine Deep clean (build + data + results)\n"
" isa-info Show CPU ISA detection and build flags\n"
" check Run upstream workflow checks\n"
" dev-setup Check required toolchain\n"
" version Show library version and build info\n"
" status Show build status and availability (default)\n"
)
def main() -> int:
args = sys.argv[1:]
if not args:
args = ["status"]
cmd = args[0]
rest = args[1:]
if cmd in ("-h", "--help", "help"):
_help()
return 0
handler = COMMANDS.get(cmd)
if handler is None:
print(f"Unknown command: {cmd}", file=sys.stderr)
_help()
return 1
return handler(rest)
if __name__ == "__main__":
sys.exit(main())