forked from lobster-trap/Kickama
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbuild.py
More file actions
781 lines (684 loc) · 26.6 KB
/
Copy pathbuild.py
File metadata and controls
781 lines (684 loc) · 26.6 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
#!/usr/bin/env python3
import argparse
import datetime
import getpass
import json
import os
import platform
import shutil
import subprocess
import sys
import time
from dataclasses import dataclass
from pathlib import Path
from typing import Optional
ROOT = Path(__file__).resolve().parent
DIAGNOSTIC_DIR = ROOT / "diagnostic"
DIAGNOSTIC_CHUNK_SIZE = 40 * 1024 * 1024
def current_commit_id() -> str:
"""Return the first 4 bytes (8 hex chars) of HEAD for stable per-commit diagnostics."""
try:
result = subprocess.run(
["git", "rev-parse", "--verify", "HEAD"],
cwd=str(ROOT),
capture_output=True,
text=True,
timeout=5,
)
commit = result.stdout.strip()
if result.returncode == 0 and len(commit) >= 8:
return commit[:8]
except Exception:
pass
return "00000000"
def diagnostic_paths_for_commit() -> tuple[Path, Path, str]:
"""Return stable diagnostic artifact paths under diagnostic/ for the current commit."""
DIAGNOSTIC_DIR.mkdir(parents=True, exist_ok=True)
commit_id = current_commit_id()
logd_path = DIAGNOSTIC_DIR / f"build-{commit_id}.logd"
metadata_path = DIAGNOSTIC_DIR / f"build-{commit_id}.json"
return logd_path, metadata_path, commit_id
def split_diagnostic_logd(logd_path: Path, chunk_size: int = DIAGNOSTIC_CHUNK_SIZE) -> list[Path]:
"""Split an oversized .logd into numbered .logd chunks and remove the original."""
if logd_path.stat().st_size <= chunk_size:
return [logd_path]
chunks: list[Path] = []
stem = logd_path.stem
with logd_path.open("rb") as source:
index = 1
while True:
data = source.read(chunk_size)
if not data:
break
chunk_path = logd_path.with_name(f"{stem}-part{index:03d}.logd")
chunk_path.write_bytes(data)
chunks.append(chunk_path)
index += 1
logd_path.unlink()
return chunks
@dataclass
class Module:
name: str
language: str
dir: Path
build_cmd: list[str]
clean_cmd: list[str]
build_dir: Optional[Path] = None
env: Optional[dict[str, str]] = None
MODULES = [
Module(
name="backend",
language="Rust",
dir=ROOT / "backend",
build_cmd=["cargo", "build"],
clean_cmd=["cargo", "clean"],
build_dir=ROOT / "backend" / "target",
env={"CARGO_TERM_COLOR": "always"},
),
Module(
name="frontend",
language="TypeScript",
dir=ROOT / "frontend",
build_cmd=["npm", "run", "build"],
clean_cmd=["rm", "-rf", "node_modules", "dist"],
build_dir=ROOT / "frontend" / "dist",
env={"NODE_ENV": "production"},
),
Module(
name="market",
language="Go",
dir=ROOT / "market",
build_cmd=["go", "build", "-o", "market", "."],
clean_cmd=["rm", "-f", "market"],
build_dir=ROOT / "market" / "market",
),
Module(
name="frailbox",
language="C",
dir=ROOT / "frailbox",
build_cmd=["make"],
clean_cmd=["make", "distclean"],
build_dir=ROOT / "frailbox" / "frailbox",
),
Module(
name="engine",
language="C++",
dir=ROOT / "frailbox" / "engine",
build_cmd=["cmake", "--build", "build"],
clean_cmd=["rm", "-rf", "build"],
build_dir=ROOT / "frailbox" / "engine" / "build" / "trial-engine",
),
Module(
name="compliance",
language="Java",
dir=ROOT / "compliance",
build_cmd=["javac", "-d", "build", "ComplianceAuditor.java"],
clean_cmd=["rm", "-rf", "build"],
build_dir=ROOT / "compliance" / "build",
),
Module(
name="v2-market-stream",
language="Ruby",
dir=ROOT / "v2" / "services",
build_cmd=["ruby", "-c", "market_stream.rb"],
clean_cmd=["echo", "Ruby has no build artifacts to clean"],
build_dir=None,
),
Module(
name="nfc-scanner",
language="Lua",
dir=ROOT / "frailbox" / "nfc",
build_cmd=["luac", "-p", "scanner.lua"],
clean_cmd=["echo", "Lua has no build artifacts to clean"],
build_dir=None,
),
Module(
name="openapi-haskell",
language="Haskell",
dir=ROOT / "docs" / "openapi",
build_cmd=["ghc", "-fno-code", "Types.hs", "Server.hs", "Validate.hs", "Generate.hs"],
clean_cmd=["rm", "-f", "*.hi", "*.o", "*.hie"],
build_dir=None,
),
Module(
name="openapi-tools",
language="Lua",
dir=ROOT / "tools",
build_cmd=["luac", "-p", "openapi_diff.lua", "openapi_mock.lua", "openapi_pact.lua"],
clean_cmd=["echo", "Nothing to clean"],
build_dir=None,
),
]
ENCRYPTLY_DIR = ROOT / "tools" / "encryptly"
ENCRYPTLY_BINARIES = {
"linux-x64": ENCRYPTLY_DIR / "linux-x64" / "encryptly",
"linux-arm64": ENCRYPTLY_DIR / "linux-arm64" / "encryptly",
"macos-arm64": ENCRYPTLY_DIR / "macos-arm64" / "encryptly",
"windows-x64": ENCRYPTLY_DIR / "windows-x64" / "encryptly.exe",
"windows-arm64": ENCRYPTLY_DIR / "windows-arm64" / "encryptly.exe",
}
LEGACY_ENCRYPTLY_BIN = ENCRYPTLY_DIR / "encryptly"
def _normalize_arch(machine: str) -> Optional[str]:
machine = machine.lower()
if machine in {"x86_64", "amd64"}:
return "x64"
if machine in {"aarch64", "arm64"}:
return "arm64"
return None
def _normalize_os() -> Optional[str]:
system = platform.system().lower()
if system == "linux":
return "linux"
if system == "darwin":
return "macos"
if system == "windows":
return "windows"
return None
def detect_encryptly_platform() -> Optional[str]:
os_name = _normalize_os()
arch = _normalize_arch(platform.machine())
if os_name is None or arch is None:
return None
return f"{os_name}-{arch}"
def get_encryptly_bin() -> Optional[Path]:
target = detect_encryptly_platform()
if target is not None:
binary = ENCRYPTLY_BINARIES.get(target)
if binary is not None and binary.exists():
return binary
if LEGACY_ENCRYPTLY_BIN.exists():
return LEGACY_ENCRYPTLY_BIN
return None
def encryptly_platform_help() -> str:
detected = detect_encryptly_platform() or "unsupported"
available = ", ".join(sorted(ENCRYPTLY_BINARIES))
return f"detected {detected}; available: {available}"
class Colors:
GREEN = "\033[92m"
YELLOW = "\033[93m"
RED = "\033[91m"
CYAN = "\033[96m"
BOLD = "\033[1m"
RESET = "\033[0m"
GRAY = "\033[90m"
def color(text: str, code: str) -> str:
if not sys.stdout.isatty():
return text
return f"{code}{text}{Colors.RESET}"
def check_prerequisites() -> list[str]:
required = {
"cargo": "Rust",
"npm": "Node.js",
"go": "Go",
"gcc": "C (GCC)",
"g++": "C++ (GCC)",
"cmake": "CMake",
"make": "Make",
"python3": "Python",
"javac": "Java (JDK)",
"ruby": "Ruby",
"luac": "Lua",
"ghc": "GHC (Haskell)",
}
missing = []
for cmd, label in required.items():
if shutil.which(cmd) is None:
missing.append(f"{label} ({cmd})")
return missing
def build_module(
module: Module,
release: bool = False,
verbose: bool = False,
) -> tuple[bool, float, str]:
print(f"\n {color('▸', Colors.CYAN)} Building {color(module.name, Colors.BOLD)} ({module.language})...")
env = os.environ.copy()
if module.env:
env.update(module.env)
start = time.time()
if module.name == "frontend":
node_modules = module.dir / "node_modules"
if not node_modules.exists():
print(f" {color('npm install...', Colors.GRAY)}")
try:
install_result = subprocess.run(
["npm", "install"],
cwd=str(module.dir),
capture_output=not verbose,
text=True,
timeout=120,
env={k: v for k, v in env.items() if k != "NODE_ENV"},
)
if install_result.returncode != 0:
return False, time.time() - start, f"npm install failed:\n{install_result.stderr}"
except subprocess.TimeoutExpired:
return False, time.time() - start, "npm install TIMEOUT (120s)"
except FileNotFoundError as e:
return False, 0, f"Command not found: {e}"
if module.name == "engine":
build_type = "Release" if release else "Debug"
try:
cfg_result = subprocess.run(
["cmake", "-S", ".", "-B", "build",
f"-DCMAKE_BUILD_TYPE={build_type}"],
cwd=str(module.dir),
capture_output=True,
text=True,
timeout=120,
env=env,
)
except subprocess.TimeoutExpired:
return False, time.time() - start, "CMake configure TIMEOUT (120s)"
except FileNotFoundError as e:
return False, 0, f"Command not found: {e}"
if cfg_result.returncode != 0:
output_lines = []
if cfg_result.stdout:
output_lines.append(cfg_result.stdout.strip())
if cfg_result.stderr:
output_lines.append(cfg_result.stderr.strip())
output = "\n".join(output_lines)
return False, time.time() - start, (
f"CMake configure failed:\n{output}")
if verbose:
print(f" {color('cmake configured', Colors.GRAY)}")
cmd = ["cmake", "--build", "build"]
if release:
cmd.append("--config")
cmd.append("Release")
else:
cmd = list(module.build_cmd)
if release and module.name == "backend":
cmd.append("--release")
try:
result = subprocess.run(
cmd,
cwd=str(module.dir),
capture_output=True,
text=True,
env=env,
timeout=300,
)
except subprocess.TimeoutExpired:
return False, time.time() - start, "BUILD TIMEOUT (300s)"
except FileNotFoundError as e:
return False, 0, f"Command not found: {e}"
elapsed = time.time() - start
output_lines = []
if result.stdout:
output_lines.append(result.stdout.strip())
if result.stderr:
output_lines.append(result.stderr.strip())
output = "\n".join(output_lines)
success = result.returncode == 0
return success, elapsed, output
def clean_module(module: Module, verbose: bool = False) -> bool:
print(f" {color('▸', Colors.YELLOW)} Cleaning {module.name}...")
try:
subprocess.run(
module.clean_cmd,
cwd=str(module.dir),
capture_output=not verbose,
text=True,
timeout=60,
env=os.environ.copy(),
)
return True
except Exception as e:
print(f" {color('✗', Colors.RED)} Clean failed: {e}")
return False
def verify_binary(module: Module) -> Optional[str]:
if module.build_dir is None:
return None
path = module.build_dir
if module.name == "backend":
target = path / "debug" / module.name
if not target.exists():
target = path / "release" / module.name
if target.exists():
return str(target)
if path.exists():
return str(path)
return None
def run_cmd(cmd: list[str], **kwargs) -> tuple[bool, str]:
try:
result = subprocess.run(
cmd, capture_output=True, text=True, check=False, **kwargs
)
output = result.stdout
if result.stderr:
output += "\n" + result.stderr
return result.returncode == 0, output.strip()
except Exception as e:
return False, str(e)
def collect_system_info() -> str:
lines = [
"Tent of Trials - System Diagnostic Snapshot",
"=" * 50,
f"generated_at: {datetime.datetime.now(datetime.timezone.utc).isoformat()}",
f"hostname: {platform.node()}",
f"user: {getpass.getuser()}",
f"python: {sys.version}",
f"platform: {platform.platform()}",
f"processor: {platform.processor() or 'unknown'}",
f"cpu_count: {os.cpu_count()}",
"",
"--- uname ---",
]
ok, out = run_cmd(["uname", "-a"])
lines.append(out if ok else "unavailable")
lines.extend(["", "--- /etc/os-release ---"])
try:
lines.append((Path("/etc/os-release")).read_text(encoding="utf-8", errors="replace").strip())
except Exception as e:
lines.append(f"unavailable: {e}")
lines.extend(["", "--- memory ---"])
ok, out = run_cmd(["free", "-h"])
lines.append(out if ok else "unavailable")
lines.extend(["", "--- disk ---"])
ok, out = run_cmd(["df", "-h"])
lines.append(out if ok else "unavailable")
lines.extend(["", "--- build environment ---"])
for key in ["SHELL", "LANG", "TERM", "XDG_SESSION_TYPE", "DISPLAY", "EDITOR"]:
value = os.environ.get(key)
if value:
lines.append(f"{key}={value}")
lines.append("")
return "\n".join(lines)
def build_diagnostic_report(
results: list[tuple[str, bool, float, str, Optional[str]]],
commit_id: str,
logd_relpaths: Optional[list[str]] = None,
password: Optional[str] = None,
logd_error: Optional[str] = None,
chunked: bool = False,
) -> dict:
diagnostic_logd: Optional[str | list[str]]
if not logd_relpaths:
diagnostic_logd = None
elif len(logd_relpaths) == 1:
diagnostic_logd = logd_relpaths[0]
else:
diagnostic_logd = logd_relpaths
decrypt_target = logd_relpaths[0] if logd_relpaths and len(logd_relpaths) == 1 else None
if logd_relpaths and len(logd_relpaths) > 1:
decrypt_target = str((DIAGNOSTIC_DIR / f"build-{commit_id}.logd").relative_to(ROOT))
report = {
"generated_at": datetime.datetime.now(datetime.timezone.utc).isoformat(),
"commit": commit_id,
"diagnostic_logd": diagnostic_logd,
"diagnostic_logd_error": logd_error,
"chunked": chunked,
"chunk_size_bytes": DIAGNOSTIC_CHUNK_SIZE if chunked else None,
"password": password,
"decrypt_command": (
f"encryptly unpack {decrypt_target} <outdir> --password {password}"
if decrypt_target and password else None
),
"total_modules": len(results),
"passed": sum(1 for _, s, _, _, _ in results if s),
"failed": sum(1 for _, s, _, _, _ in results if not s),
"modules": [
{
"name": name,
"status": "PASS" if success else "FAIL",
"elapsed_seconds": round(elapsed, 3),
"artifact": binary,
"output": output,
}
for name, success, elapsed, output, binary in results
],
"pr_note": (
(f"Include the encrypted diagnostic logd artifact(s): {', '.join(logd_relpaths)}. " if logd_relpaths else "Encrypted diagnostic logd artifact was not created; include this JSON report showing why. ")
+ "The encrypted .logd is the required diagnostic content for PR review; this JSON file is metadata. "
+ "Maintainers may ask you to remove these diagnostic artifacts before merging."
),
}
return report
def write_diagnostic_report(metadata_path: Path, report: dict) -> None:
metadata_path.write_text(json.dumps(report, indent=2) + "\n", encoding="utf-8")
print(f" {color('✓', Colors.GREEN)} {metadata_path.relative_to(ROOT)} created")
def generate_logd(
results: list[tuple[str, bool, float, str, Optional[str]]],
verbose: bool = False,
) -> bool:
logd_path, metadata_path, commit_id = diagnostic_paths_for_commit()
display_logd = logd_path.relative_to(ROOT)
print(f"\n {color('▸', Colors.CYAN)} Finalizing diagnostics for {color(str(display_logd), Colors.BOLD)}...")
# Always write the JSON report first. The encrypted .logd is useful, but the
# report is required even when the build failed before compilation started or
# when encryptly itself is unavailable.
write_diagnostic_report(metadata_path, build_diagnostic_report(results, commit_id))
encryptly_bin = get_encryptly_bin()
if encryptly_bin is None:
error = f"encryptly binary not found ({encryptly_platform_help()}); cannot create {display_logd}"
print(f" {color('✗', Colors.RED)} {error}")
write_diagnostic_report(metadata_path, build_diagnostic_report(results, commit_id, logd_error=error))
return False
# Workspace must live under $HOME because encryptly refuses paths outside home.
home = Path.home()
workspace = home / ".cache" / "tent-of-trials" / "logd-workspace"
safe_dir = workspace / "safe"
try:
shutil.rmtree(workspace, ignore_errors=True)
safe_dir.mkdir(parents=True, exist_ok=True)
(safe_dir / "system-info.txt").write_text(
collect_system_info(), encoding="utf-8"
)
summary_lines = [
"Tent of Trials - Build Summary",
"=" * 50,
f"generated_at: {datetime.datetime.now(datetime.timezone.utc).isoformat()}",
f"total_modules: {len(results)}",
f"passed: {sum(1 for _, s, _, _, _ in results if s)}",
f"failed: {sum(1 for _, s, _, _, _ in results if not s)}",
"",
"module results:",
]
for name, success, elapsed, _, binary in results:
summary_lines.append(
f" {name}: {'PASS' if success else 'FAIL'} ({elapsed:.2f}s)"
f"{f' [{binary}]' if binary else ''}"
)
(safe_dir / "build-summary.txt").write_text(
"\n".join(summary_lines), encoding="utf-8"
)
log_lines = []
for name, success, elapsed, output, binary in results:
log_lines.append(
f"\n{'=' * 50}\n{name} ({'PASS' if success else 'FAIL'}, {elapsed:.2f}s)\n"
f"{'=' * 50}"
)
if binary:
log_lines.append(f"artifact: {binary}")
if output:
log_lines.append(output)
(safe_dir / "build.log").write_text("\n".join(log_lines), encoding="utf-8")
sr = subprocess.run(
[
str(encryptly_bin),
"pack",
str(logd_path),
"--include",
str(workspace),
"--max-file-size",
"10000",
],
cwd=str(ROOT),
capture_output=True,
text=True,
timeout=300,
)
if sr.returncode != 0:
error = sr.stderr.strip() or sr.stdout.strip() or "encryptly pack failed"
print(
f" {color('✗', Colors.RED)} {logd_path.relative_to(ROOT)} creation failed: "
f"{error}"
)
if logd_path.exists():
logd_path.unlink()
write_diagnostic_report(
metadata_path,
build_diagnostic_report(results, commit_id, logd_error=error),
)
return False
safe_pw = sr.stdout.strip()
logd_files = split_diagnostic_logd(logd_path)
logd_relpaths = [str(path.relative_to(ROOT)) for path in logd_files]
decrypt_target = logd_relpaths[0] if len(logd_relpaths) == 1 else str(logd_path.relative_to(ROOT))
write_diagnostic_report(
metadata_path,
build_diagnostic_report(
results,
commit_id,
logd_relpaths=logd_relpaths,
password=safe_pw,
chunked=len(logd_files) > 1,
),
)
for path in logd_files:
size_kb = path.stat().st_size / 1024.0
print(
f" {color('✓', Colors.GREEN)} {path.relative_to(ROOT)} created "
f"({size_kb:.1f} KiB)"
)
if len(logd_files) > 1:
print(
f" {color('✓', Colors.GREEN)} split oversized diagnostic log into "
f"{len(logd_files)} chunks of at most {DIAGNOSTIC_CHUNK_SIZE // (1024 * 1024)} MiB"
)
if safe_pw:
print()
print(f" {color('Password', Colors.BOLD)} - this is required to decrypt the diagnostic log,")
print(f" which is required to submit a PR. Upload the")
print(f" diagnostic log file(s) and metadata file with this password.")
if len(logd_files) > 1:
print(f" Reassemble chunks in order before unpacking:")
print(f" cat {' '.join(logd_relpaths)} > {logd_path.relative_to(ROOT)}")
print(f" {color(safe_pw, Colors.CYAN)}")
print(f" {color(f'encryptly unpack {decrypt_target} <outdir> --password {safe_pw}', Colors.GRAY)}")
return True
finally:
shutil.rmtree(workspace, ignore_errors=True)
def print_summary(results: list[tuple[str, bool, float, str, Optional[str]]]):
print(f" {color('Build Summary', Colors.BOLD)}")
total = len(results)
passed = sum(1 for _, s, _, _, _ in results if s)
failed = total - passed
total_time = sum(t for _, _, t, _, _ in results)
for name, success, elapsed, output, binary in results:
status_icon = color("✓", Colors.GREEN) if success else color("✗", Colors.RED)
status_text = color("PASS", Colors.GREEN) if success else color("FAIL", Colors.RED)
time_str = f"{elapsed:.1f}s" if elapsed < 60 else f"{elapsed / 60:.1f}m"
print(f"\n {status_icon} {color(name + ':', Colors.BOLD)} {status_text} ({time_str})")
if binary:
print(f" artifact: {color(binary, Colors.GRAY)}")
if not success and output:
lines = output.strip().split("\n")
print(f" {color('last output:', Colors.RED)}")
for line in lines[-5:]:
print(f" {color(line, Colors.GRAY)}")
print(f"\n {color('─' * 40, Colors.GRAY)}")
print(f" {color('Total:', Colors.BOLD)} {total} modules, "
f"{color(str(passed) + ' passed', Colors.GREEN)}, "
f"{color(str(failed) + ' failed', Colors.RED)}, "
f"{total_time:.1f}s total")
def main():
parser = argparse.ArgumentParser(
description="Tent of Trials - Multi-Language Build System",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
python3 build.py Build all modules
python3 build.py -m backend Build only backend
python3 build.py -m frontend,market Build frontend and market
python3 build.py --clean Clean all artifacts
python3 build.py --release Release build (Rust only)
python3 build.py --verbose Verbose output
Diagnostic bundle:
python3 build.py
""",
)
parser.add_argument(
"-m", "--module",
help="Module(s) to build (comma-separated, or 'all')",
default="all",
)
parser.add_argument(
"--clean", action="store_true",
help="Clean build artifacts instead of building",
)
parser.add_argument(
"--release", action="store_true",
help="Build in release mode (Rust backend)",
)
parser.add_argument(
"--verbose", "-v", action="store_true",
help="Show detailed build output",
)
parser.add_argument(
"--list", action="store_true",
help="List available modules and exit",
)
args = parser.parse_args()
print(f"\n {color('Tent of Trials: building', Colors.CYAN)}")
print(f" Working directory: {ROOT}")
print()
if args.list:
print(f" {color('Available modules:', Colors.BOLD)}")
for m in MODULES:
print(f" {color(m.name, Colors.CYAN)} ({m.language})")
print(f" dir: {m.dir.relative_to(ROOT)}")
print(f" build: {' '.join(m.build_cmd)}")
return 0
print(f" {color('Checking prerequisites...', Colors.GRAY)}")
missing = check_prerequisites()
if missing:
print(f"\n {color('⚠ Some tools missing - will try anyway:', Colors.YELLOW)}")
for m in missing:
print(f" {m}")
print(" " + color("Not all modules will build. That's fine.", Colors.GRAY))
else:
print(f" {color('✓ All prerequisites found', Colors.GREEN)}")
if args.module == "all":
selected = MODULES
else:
names = [n.strip() for n in args.module.split(",")]
selected = [m for m in MODULES if m.name in names]
not_found = set(names) - {m.name for m in MODULES}
if not_found:
print(f" {color('✗ Unknown modules:', Colors.RED)} {', '.join(not_found)}")
print(f" Available: {', '.join(m.name for m in MODULES)}")
return 1
if not selected:
print(f" No modules selected.")
return 0
if args.clean:
print(f"\n {color('Cleaning build artifacts...', Colors.YELLOW)}")
for module in selected:
clean_module(module, args.verbose)
diagnostic_artifacts = [ROOT / "build.logd"]
if DIAGNOSTIC_DIR.exists():
diagnostic_artifacts.extend(DIAGNOSTIC_DIR.glob("build-[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f].logd"))
diagnostic_artifacts.extend(DIAGNOSTIC_DIR.glob("build-[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]-part*.logd"))
diagnostic_artifacts.extend(DIAGNOSTIC_DIR.glob("build-[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f].json"))
diagnostic_artifacts.extend(DIAGNOSTIC_DIR.glob("build-[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]-metadata.json"))
for artifact in diagnostic_artifacts:
if artifact.exists():
if artifact.is_dir():
shutil.rmtree(artifact)
else:
artifact.unlink()
print(f" {color('▸', Colors.YELLOW)} Removed {artifact.relative_to(ROOT)}")
print(f"\n {color('Clean complete.', Colors.GREEN)}")
return 0
print(f"\n {color(f'Building {len(selected)} module(s) | release={args.release}', Colors.GRAY)}")
results: list[tuple[str, bool, float, str, Optional[str]]] = []
for module in selected:
success, elapsed, output = build_module(module, args.release, args.verbose)
binary = verify_binary(module) if success else None
results.append((module.name, success, elapsed, output, binary))
print_summary(results)
generate_logd(results, args.verbose)
return 0 if all(r[1] for r in results) else 1
if __name__ == "__main__":
sys.exit(main())