| 1 |
#!/usr/bin/env bash |
| 2 |
|
| 3 |
set -euo pipefail |
| 4 |
|
| 5 |
target="${1:-.}" |
| 6 |
bin="${THOTH_BIN:-thoth}" |
| 7 |
critical_threshold="${THOTH_CRITICAL_THRESHOLD:-0.65}" |
| 8 |
|
| 9 |
if [[ $# -gt 1 ]]; then |
| 10 |
echo "usage: $0 [target]" >&2 |
| 11 |
exit 2 |
| 12 |
fi |
| 13 |
if [[ ! -e "$target" ]]; then |
| 14 |
echo "error: target not found: $target" >&2 |
| 15 |
exit 1 |
| 16 |
fi |
| 17 |
if [[ ! -d "$target" ]]; then |
| 18 |
echo "error: target must be a directory: $target" >&2 |
| 19 |
exit 1 |
| 20 |
fi |
| 21 |
if ! command -v "$bin" >/dev/null 2>&1; then |
| 22 |
echo "error: thoth binary not found: $bin; set THOTH_BIN or install thoth" >&2 |
| 23 |
exit 1 |
| 24 |
fi |
| 25 |
|
| 26 |
report="$(mktemp)" |
| 27 |
trap 'rm -f "$report"' EXIT |
| 28 |
|
| 29 |
"$bin" "$target" --json > "$report" |
| 30 |
|
| 31 |
python3 - "$report" "$critical_threshold" <<'PY' |
| 32 |
import json |
| 33 |
import sys |
| 34 |
from pathlib import Path |
| 35 |
|
| 36 |
report_path = Path(sys.argv[1]) |
| 37 |
threshold_text = sys.argv[2] |
| 38 |
|
| 39 |
try: |
| 40 |
threshold = float(threshold_text) |
| 41 |
except ValueError: |
| 42 |
print(f"error: invalid THOTH_CRITICAL_THRESHOLD: {threshold_text}", file=sys.stderr) |
| 43 |
sys.exit(2) |
| 44 |
|
| 45 |
try: |
| 46 |
data = json.loads(report_path.read_text()) |
| 47 |
except Exception as exc: |
| 48 |
print(f"error: thoth --json did not return valid JSON: {exc}", file=sys.stderr) |
| 49 |
sys.exit(1) |
| 50 |
|
| 51 |
files = data.get("files") |
| 52 |
if not isinstance(files, list): |
| 53 |
print("error: thoth --json missing files array", file=sys.stderr) |
| 54 |
sys.exit(1) |
| 55 |
|
| 56 |
critical = [] |
| 57 |
for entry in files: |
| 58 |
try: |
| 59 |
risk = float(entry.get("risk", 0.0)) |
| 60 |
except (TypeError, ValueError): |
| 61 |
print(f"error: invalid risk value in thoth output: {entry!r}", file=sys.stderr) |
| 62 |
sys.exit(1) |
| 63 |
if risk >= threshold: |
| 64 |
critical.append((risk, entry)) |
| 65 |
|
| 66 |
critical.sort(reverse=True, key=lambda item: item[0]) |
| 67 |
|
| 68 |
if critical: |
| 69 |
print( |
| 70 |
f"thoth critical check failed: {len(critical)} file(s) at or above risk {threshold:.3f}", |
| 71 |
file=sys.stderr, |
| 72 |
) |
| 73 |
for risk, entry in critical: |
| 74 |
print( |
| 75 |
f"- {entry.get('path', '<unknown>')}: " |
| 76 |
f"risk={risk:.3f} sloc={entry.get('sloc', 0)} " |
| 77 |
f"funcs={entry.get('funcs', 0)} worst={entry.get('worst', '')}", |
| 78 |
file=sys.stderr, |
| 79 |
) |
| 80 |
sys.exit(1) |
| 81 |
|
| 82 |
print(f"thoth critical check passed: 0 files at or above risk {threshold:.3f}") |
| 83 |
PY |