Repository navigation
Expand file tree
/
Copy pathdashboard.py
More file actions
602 lines (527 loc) · 23.7 KB
/
Copy pathdashboard.py
File metadata and controls
602 lines (527 loc) · 23.7 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
#!/usr/bin/env python3
"""
SentinelCommit dashboard - a single-page local view of the repository.
Shows, on one page:
* headline stats (commits, audits, blocks, free Tier 1 skips)
* the commit DAG, drawn as a lane graph with branch and tag refs
* every commit, with the audit verdict that gated it
* the full SentinelCommit audit trail, including commits that never happened
Standard library only - no Flask, no npm, no build step.
Usage:
python dashboard.py # serve on http://127.0.0.1:8765
python dashboard.py --port 9000
python dashboard.py --no-browser
"""
import argparse
import html
import json
import os
import re
import subprocess
import sys
import threading
import webbrowser
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
SEP = "\x1f" # unit separator - safe inside commit messages
REC = "\x1e" # record separator
LANE_COLOURS = [
"#58a6ff", "#3fb950", "#d29922", "#bc8cff",
"#f85149", "#39c5cf", "#db6d28", "#7ee787",
]
# --------------------------------------------------------------------------
# Git
# --------------------------------------------------------------------------
def git(*args):
result = subprocess.run(
["git"] + list(args),
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
text=True, encoding="utf-8", errors="replace",
)
if result.returncode != 0:
raise RuntimeError("git " + " ".join(args) + ": " + result.stderr.strip())
return result.stdout
def repo_root():
return git("rev-parse", "--show-toplevel").strip()
def current_branch():
try:
return git("rev-parse", "--abbrev-ref", "HEAD").strip()
except Exception:
return "(detached)"
FILES_RE = re.compile(r"(\d+) files? changed")
def load_commits(limit):
"""
Read the commit DAG, newest-first, in a single git invocation.
`--shortstat` appends the diff summary after each record, which avoids one
`git show` per commit. That N+1 pattern cost ~11s for a handful of commits
on Windows, where spawning a process is expensive.
The format ends with a trailing SEP so the shortstat lands in its own field
rather than being glued onto the ref list.
"""
fields = ["%H", "%h", "%P", "%an", "%ae", "%aI", "%s", "%D"]
fmt = REC + SEP.join(fields) + SEP
try:
raw = git("log", "--all", "--date-order", "-n", str(limit),
"--shortstat", "--pretty=format:" + fmt)
except RuntimeError:
return [] # no commits yet
commits = []
for chunk in raw.split(REC):
if not chunk.strip():
continue
parts = chunk.split(SEP)
if len(parts) < len(fields):
continue
sha, short, parents, an, ae, when, subject, refs = parts[:8]
stat = " ".join(parts[8].split()) if len(parts) > 8 else ""
commits.append({
"sha": sha,
"short": short,
"parents": parents.split() if parents else [],
"author": an,
"email": ae,
"date": when,
"subject": subject,
"refs": [r.strip() for r in refs.split(",") if r.strip()],
# Merge commits produce no shortstat unless -m is passed; empty is correct.
"stat": stat,
"files": int(FILES_RE.search(stat).group(1)) if FILES_RE.search(stat) else 0,
})
return commits
def assign_lanes(commits):
"""
Lay the DAG out in vertical lanes, the way `git log --graph` does.
Walking newest-first, each commit takes the leftmost lane already
reserved for it (by a child), or the leftmost free lane. Its first parent
inherits that lane so mainline stays straight; further parents (merges)
branch into new lanes.
"""
active = [] # active[i] = sha expected to appear in lane i, or None
for c in commits:
if c["sha"] in active:
lane = active.index(c["sha"])
else:
lane = active.index(None) if None in active else len(active)
if lane == len(active):
active.append(None)
c["lane"] = lane
# Lanes this commit connects down to.
active[lane] = None
outgoing = []
for i, parent in enumerate(c["parents"]):
if parent in active:
plane = active.index(parent)
elif i == 0:
plane = lane
active[lane] = parent
else:
plane = active.index(None) if None in active else len(active)
if plane == len(active):
active.append(None)
active[plane] = parent
outgoing.append(plane)
c["outgoing"] = outgoing
while active and active[-1] is None:
active.pop()
return commits
# --------------------------------------------------------------------------
# Audit trail
# --------------------------------------------------------------------------
def load_audit():
path = os.environ.get("SENTINEL_AUDIT_LOG", "")
if not path:
try:
path = os.path.join(git("rev-parse", "--absolute-git-dir").strip(),
"sentinel-audit.jsonl")
except Exception:
return []
if not os.path.exists(path):
return []
events = []
with open(path, encoding="utf-8") as fh:
for line in fh:
line = line.strip()
if not line:
continue
try:
events.append(json.loads(line))
except json.JSONDecodeError:
continue # tolerate a partially written final line
events.reverse() # newest first
return events
def summarise(commits, events):
outcomes = {}
for e in events:
outcomes[e.get("outcome", "?")] = outcomes.get(e.get("outcome", "?"), 0) + 1
hazards = {}
for e in events:
if e.get("outcome") == "blocked":
h = e.get("hazard_type", "UNKNOWN")
hazards[h] = hazards.get(h, 0) + 1
# Only a real verdict costs a call. Mock runs reach Tier 2 but never leave
# the machine, so counting them here would overstate spend.
api_calls = sum(1 for e in events
if e.get("outcome") in ("blocked", "passed") and not e.get("mock"))
mocked = sum(1 for e in events if e.get("mock"))
return {
"commits": len(commits),
"audits": len(events),
"blocked": outcomes.get("blocked", 0),
"passed": outcomes.get("passed", 0),
"skipped_docs": outcomes.get("skipped_docs", 0),
"failed_open": outcomes.get("failed_open", 0),
"api_calls": api_calls,
"mocked": mocked,
"hazards": hazards,
"branch": current_branch(),
}
# --------------------------------------------------------------------------
# Rendering
# --------------------------------------------------------------------------
ROW_H = 56
GRAPH_X0 = 22
LANE_W = 20
def e(text):
return html.escape(str(text if text is not None else ""))
def render_graph(commits):
"""SVG DAG: one row per commit, edges drawn to each parent's row."""
if not commits:
return '<p class="empty">No commits yet.</p>'
row_of = {c["sha"]: i for i, c in enumerate(commits)}
lanes = max((c["lane"] for c in commits), default=0) + 1
width = GRAPH_X0 + lanes * LANE_W + 10
height = len(commits) * ROW_H + 20
def cx(lane):
return GRAPH_X0 + lane * LANE_W
def cy(row):
return row * ROW_H + ROW_H // 2
parts = [f'<svg class="dag" width="{width}" height="{height}" '
f'viewBox="0 0 {width} {height}" aria-hidden="true">']
# Edges first, so nodes sit on top.
for i, c in enumerate(commits):
for parent, plane in zip(c["parents"], c.get("outgoing", [])):
j = row_of.get(parent)
colour = LANE_COLOURS[plane % len(LANE_COLOURS)]
x1, y1 = cx(c["lane"]), cy(i)
if j is None:
# Parent is outside the loaded window: stub the edge downward.
parts.append(f'<path d="M{x1} {y1} V{y1 + ROW_H // 2}" '
f'stroke="{colour}" stroke-width="2" fill="none" '
f'stroke-dasharray="3 3" opacity=".5"/>')
continue
x2, y2 = cx(plane), cy(j)
if x1 == x2:
d = f"M{x1} {y1} V{y2}"
else:
mid = y1 + ROW_H // 2
d = (f"M{x1} {y1} V{mid - 8} "
f"Q{x1} {mid} {x1 + (8 if x2 > x1 else -8)} {mid} "
f"H{x2 - (8 if x2 > x1 else -8)} "
f"Q{x2} {mid} {x2} {mid + 8} V{y2}")
parts.append(f'<path d="{d}" stroke="{colour}" stroke-width="2" fill="none"/>')
for i, c in enumerate(commits):
colour = LANE_COLOURS[c["lane"] % len(LANE_COLOURS)]
merge = len(c["parents"]) > 1
parts.append(
f'<circle cx="{cx(c["lane"])}" cy="{cy(i)}" r="{6 if merge else 5}" '
f'fill="{"#0d1117" if merge else colour}" stroke="{colour}" stroke-width="2.5"/>'
)
parts.append("</svg>")
return "".join(parts)
def render_ref(ref):
cls = "ref-branch"
if ref.startswith("HEAD"):
cls = "ref-head"
elif ref.startswith("tag:"):
cls = "ref-tag"
elif ref.startswith("origin/"):
cls = "ref-remote"
return f'<span class="ref {cls}">{e(ref)}</span>'
def render_rows(commits, events):
"""Commit rows, aligned 1:1 with the SVG graph rows."""
# Match audits to commits by subject where we can - the audit fires before
# the commit exists, so there is no sha to join on.
passed_by_subject = {}
for ev in reversed(events):
if ev.get("outcome") in ("passed", "skipped_docs"):
passed_by_subject.setdefault(ev.get("detail", ""), ev)
out = []
for c in commits:
refs = "".join(render_ref(r) for r in c["refs"])
out.append(
f'<li class="row">'
f'<div class="row-main">'
f'<code class="sha">{e(c["short"])}</code>'
f'<span class="subject">{e(c["subject"])}</span>'
f'{refs}'
f'</div>'
f'<div class="row-meta">'
f'<span>{e(c["author"])}</span>'
f'<span class="dot">·</span>'
f'<time datetime="{e(c["date"])}">{e(c["date"][:16].replace("T", " "))}</time>'
f'{"<span class=dot>·</span><span>" + e(c["stat"]) + "</span>" if c["stat"] else ""}'
f'</div>'
f'</li>'
)
return "".join(out)
OUTCOME_META = {
"blocked": ("blocked", "COMMIT BLOCKED"),
"passed": ("passed", "passed"),
"skipped_docs": ("skipped", "Tier 1 skip - no API call"),
"failed_open": ("failopen", "failed open"),
}
def render_audit(events):
if not events:
return ('<p class="empty">No audits recorded yet. Make a commit with the '
'hook installed, then refresh.</p>')
out = []
for ev in events:
cls, label = OUTCOME_META.get(ev.get("outcome", ""), ("skipped", ev.get("outcome", "?")))
hazard = ev.get("hazard_type")
badge = (f'<span class="hazard">{e(hazard)}</span>'
if hazard and hazard != "NONE" else "")
sev = (f'<span class="sev sev-{e(str(ev.get("severity","")).lower())}">'
f'{e(ev.get("severity"))}</span>' if ev.get("severity") else "")
mock = '<span class="mock">MOCK</span>' if ev.get("mock") else ""
files = ev.get("files") or []
filelist = ("".join(f'<code>{e(f)}</code>' for f in files[:6])
+ (f'<span class="more">+{len(files) - 6}</span>' if len(files) > 6 else ""))
reason = f'<p class="reason">{e(ev.get("reason"))}</p>' if ev.get("reason") else ""
detail = (f'<p class="detail">{e(ev.get("detail"))}</p>'
if ev.get("detail") and not ev.get("reason") else "")
fix = ev.get("suggested_fix")
fixblock = (f'<details><summary>Suggested remediation</summary>'
f'<pre>{e(fix)}</pre></details>') if fix else ""
out.append(
f'<li class="audit {cls}">'
f'<div class="audit-head">'
f'<span class="status">{e(label)}</span>{badge}{sev}{mock}'
f'<time>{e(str(ev.get("ts", "")).replace("T", " ").replace("+00:00", "Z"))}</time>'
f'</div>'
f'<div class="files">{filelist}</div>'
f'{reason}{detail}{fixblock}'
f'</li>'
)
return "".join(out)
def render_hazards(stats):
if not stats["hazards"]:
return ""
total = sum(stats["hazards"].values())
bars = []
for hazard, n in sorted(stats["hazards"].items(), key=lambda kv: -kv[1]):
pct = n * 100 // max(total, 1)
bars.append(
f'<div class="bar-row"><span class="bar-label">{e(hazard)}</span>'
f'<span class="bar"><span class="bar-fill" style="width:{max(pct, 4)}%"></span></span>'
f'<span class="bar-n">{n}</span></div>'
)
return f'<section class="card"><h2>Hazards blocked</h2>{"".join(bars)}</section>'
def page(commits, events, stats, root):
saved = stats["skipped_docs"]
return f"""<!DOCTYPE html>
<html lang="en"><head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>SentinelCommit · {e(os.path.basename(root))}</title>
<style>
:root {{
--bg:#0d1117; --panel:#161b22; --line:#30363d; --fg:#e6edf3; --muted:#8b949e;
--blue:#58a6ff; --green:#3fb950; --red:#f85149; --yellow:#d29922; --purple:#bc8cff;
}}
* {{ box-sizing:border-box; }}
body {{ margin:0; background:var(--bg); color:var(--fg);
font:14px/1.5 -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Helvetica,Arial,sans-serif; }}
code,pre,.sha,.dag+ol .subject {{ font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace; }}
header {{ padding:22px 28px; border-bottom:1px solid var(--line); background:var(--panel);
display:flex; align-items:baseline; gap:14px; flex-wrap:wrap; }}
header h1 {{ margin:0; font-size:17px; letter-spacing:.2px; }}
header .repo {{ color:var(--muted); font-size:13px; }}
header .branch {{ margin-left:auto; color:var(--blue); background:#131d2e;
border:1px solid #1f6feb55; padding:2px 10px; border-radius:20px; font-size:12px; }}
main {{ max-width:1180px; margin:0 auto; padding:24px 28px 64px; }}
.stats {{ display:grid; grid-template-columns:repeat(auto-fit,minmax(150px,1fr));
gap:12px; margin-bottom:26px; }}
.stat {{ background:var(--panel); border:1px solid var(--line); border-radius:8px; padding:14px 16px; }}
.stat b {{ display:block; font-size:26px; line-height:1.15; font-variant-numeric:tabular-nums; }}
.stat span {{ color:var(--muted); font-size:12px; }}
.stat.red b {{ color:var(--red); }} .stat.green b {{ color:var(--green); }}
.stat.blue b {{ color:var(--blue); }} .stat.yellow b {{ color:var(--yellow); }}
.card {{ background:var(--panel); border:1px solid var(--line); border-radius:10px;
padding:18px 20px; margin-bottom:22px; }}
.card h2 {{ margin:0 0 14px; font-size:13px; text-transform:uppercase;
letter-spacing:.9px; color:var(--muted); font-weight:600; }}
.graph-wrap {{ display:flex; align-items:flex-start; overflow-x:auto; }}
.dag {{ flex:0 0 auto; }}
.rows {{ list-style:none; margin:0; padding:0; flex:1 1 auto; min-width:0; }}
.row {{ height:{ROW_H}px; display:flex; flex-direction:column; justify-content:center;
padding:0 6px; border-bottom:1px solid #21262d; }}
.row:last-child {{ border-bottom:0; }}
.row-main {{ display:flex; align-items:center; gap:10px; min-width:0; }}
.sha {{ color:var(--yellow); font-size:12.5px; flex:0 0 auto; }}
.subject {{ overflow:hidden; text-overflow:ellipsis; white-space:nowrap; }}
.row-meta {{ color:var(--muted); font-size:12px; display:flex; gap:7px; flex-wrap:wrap; }}
.dot {{ opacity:.5; }}
.ref {{ font-size:11px; padding:1px 8px; border-radius:20px; white-space:nowrap; flex:0 0 auto; }}
.ref-head {{ background:#132d1c; color:var(--green); border:1px solid #2ea04355; }}
.ref-branch {{ background:#131d2e; color:var(--blue); border:1px solid #1f6feb55; }}
.ref-remote {{ background:#2b2213; color:var(--yellow); border:1px solid #9e6a0355; }}
.ref-tag {{ background:#241a35; color:var(--purple); border:1px solid #8957e555; }}
.audits {{ list-style:none; margin:0; padding:0; }}
.audit {{ border-left:3px solid var(--line); padding:12px 0 12px 14px; margin-bottom:12px;
background:#0f141b; border-radius:0 8px 8px 0; }}
.audit.blocked {{ border-left-color:var(--red); }}
.audit.passed {{ border-left-color:var(--green); }}
.audit.skipped {{ border-left-color:var(--muted); }}
.audit.failopen {{ border-left-color:var(--yellow); }}
.audit-head {{ display:flex; align-items:center; gap:9px; flex-wrap:wrap; }}
.status {{ font-weight:600; font-size:12.5px; }}
.blocked .status {{ color:var(--red); }} .passed .status {{ color:var(--green); }}
.skipped .status {{ color:var(--muted); }} .failopen .status {{ color:var(--yellow); }}
.audit time {{ margin-left:auto; color:var(--muted); font-size:11.5px; }}
.hazard {{ font-size:11px; background:#2d1518; color:#ff9492; border:1px solid #f8514955;
padding:1px 8px; border-radius:20px; }}
.sev {{ font-size:11px; padding:1px 8px; border-radius:20px; border:1px solid var(--line);
color:var(--muted); }}
.sev-critical {{ color:#ff9492; border-color:#f8514955; }}
.sev-high {{ color:#ffa657; border-color:#db6d2855; }}
.mock {{ font-size:10px; letter-spacing:.7px; color:var(--muted);
border:1px dashed var(--line); padding:1px 6px; border-radius:4px; }}
.files {{ margin:7px 0 0; display:flex; gap:6px; flex-wrap:wrap; }}
.files code {{ font-size:11.5px; background:#161b22; border:1px solid var(--line);
padding:1px 7px; border-radius:5px; color:var(--muted); }}
.more {{ color:var(--muted); font-size:11.5px; }}
.reason {{ margin:9px 0 0; color:#c9d1d9; max-width:80ch; }}
.detail {{ margin:7px 0 0; color:var(--muted); font-size:12.5px; }}
details {{ margin-top:9px; }}
summary {{ cursor:pointer; color:var(--blue); font-size:12.5px; }}
pre {{ background:#0d1117; border:1px solid var(--line); border-radius:7px;
padding:12px 14px; overflow:auto; font-size:12.5px; margin:9px 0 0; }}
.bar-row {{ display:flex; align-items:center; gap:12px; margin-bottom:9px; }}
.bar-label {{ flex:0 0 170px; font-size:12.5px; color:var(--muted); }}
.bar {{ flex:1; height:9px; background:#0d1117; border-radius:5px; overflow:hidden; }}
.bar-fill {{ display:block; height:100%; background:linear-gradient(90deg,#f85149,#db6d28); }}
.bar-n {{ flex:0 0 28px; text-align:right; font-variant-numeric:tabular-nums; }}
.empty {{ color:var(--muted); margin:0; }}
footer {{ color:var(--muted); font-size:12px; text-align:center; padding:0 0 28px; }}
</style></head><body>
<header>
<h1>SentinelCommit</h1>
<span class="repo">{e(root)}</span>
<span class="branch">{e(stats["branch"])}</span>
</header>
<main>
<div class="stats">
<div class="stat blue"><b>{stats["commits"]}</b><span>commits</span></div>
<div class="stat red"><b>{stats["blocked"]}</b><span>commits blocked</span></div>
<div class="stat green"><b>{stats["passed"]}</b><span>audits passed</span></div>
<div class="stat yellow"><b>{saved}</b><span>Tier 1 skips (free)</span></div>
<div class="stat"><b>{stats["api_calls"]}</b><span>API calls made{
' · ' + str(stats["mocked"]) + ' mocked' if stats["mocked"] else ''}</span></div>
<div class="stat"><b>{stats["failed_open"]}</b><span>failed open</span></div>
</div>
{render_hazards(stats)}
<section class="card">
<h2>Commit graph</h2>
<div class="graph-wrap">
{render_graph(commits)}
<ol class="rows">{render_rows(commits, events)}</ol>
</div>
</section>
<section class="card">
<h2>Audit trail</h2>
<ol class="audits">{render_audit(events)}</ol>
</section>
</main>
<footer>Auto-refreshes every 10s · served locally by dashboard.py</footer>
<script>setTimeout(function () {{ location.reload(); }}, 10000);</script>
</body></html>
"""
# --------------------------------------------------------------------------
# Server
# --------------------------------------------------------------------------
class Handler(BaseHTTPRequestHandler):
limit = 80
def do_GET(self):
if self.path.startswith("/api/"):
return self._json()
if self.path not in ("/", "/index.html"):
self.send_error(404)
return
try:
commits = assign_lanes(load_commits(self.limit))
events = load_audit()
body = page(commits, events, summarise(commits, events), repo_root())
except Exception as exc: # surface errors in the page, not the console
body = (f"<!DOCTYPE html><meta charset=utf-8>"
f"<body style='background:#0d1117;color:#f85149;"
f"font:14px monospace;padding:40px'>"
f"<h2>dashboard error</h2><pre>{html.escape(str(exc))}</pre>")
self._send(body.encode("utf-8"), "text/html; charset=utf-8")
def _json(self):
try:
commits = assign_lanes(load_commits(self.limit))
events = load_audit()
payload = {"commits": commits, "audit": events,
"stats": summarise(commits, events)}
self._send(json.dumps(payload, indent=2).encode("utf-8"),
"application/json; charset=utf-8")
except Exception as exc:
self._send(json.dumps({"error": str(exc)}).encode("utf-8"),
"application/json; charset=utf-8", status=500)
def _send(self, body, ctype, status=200):
try:
self.send_response(status)
self.send_header("Content-Type", ctype)
self.send_header("Content-Length", str(len(body)))
self.send_header("Cache-Control", "no-store")
self.end_headers()
self.wfile.write(body)
except (ConnectionAbortedError, ConnectionResetError, BrokenPipeError):
# The browser navigated away or the auto-refresh superseded this
# request. Routine, and not worth a stack trace in the terminal.
pass
def handle_one_request(self):
try:
super().handle_one_request()
except (ConnectionAbortedError, ConnectionResetError, BrokenPipeError):
self.close_connection = True
def log_message(self, *args):
pass # keep the terminal clean
def main():
ap = argparse.ArgumentParser(description="Serve the SentinelCommit dashboard.")
ap.add_argument("--port", type=int, default=8765)
ap.add_argument("--host", default="127.0.0.1")
ap.add_argument("--limit", type=int, default=80, help="commits to load")
ap.add_argument("--no-browser", action="store_true")
args = ap.parse_args()
try:
root = repo_root()
except Exception:
print("error: not inside a git repository", file=sys.stderr)
return 1
Handler.limit = args.limit
try:
# Threading matters here, not for load. A browser keeps an idle
# connection open, and a single-threaded server blocks inside
# handle_one_request() waiting on it - stalling every other request by
# ~10s. Leaving the page open is the intended use, so that is the
# normal case, not an edge case.
server = ThreadingHTTPServer((args.host, args.port), Handler)
except OSError as exc:
print(f"error: cannot bind {args.host}:{args.port} ({exc})", file=sys.stderr)
print("hint: pass --port to pick another.", file=sys.stderr)
return 1
url = f"http://{args.host}:{args.port}/"
print(f"SentinelCommit dashboard -> {url}")
print(f"repository: {root}")
print("Ctrl+C to stop.")
if not args.no_browser:
threading.Timer(0.4, lambda: webbrowser.open(url)).start()
try:
server.serve_forever()
except KeyboardInterrupt:
print("\nstopped.")
finally:
server.server_close()
return 0
if __name__ == "__main__":
sys.exit(main())