From 86bf2412ecd471dd901dbee5271e5a84fde243fd Mon Sep 17 00:00:00 2001
From: Tristan Partin <tristan@partin.io>
Date: Wed, 17 Jun 2026 19:06:42 +0000
Subject: [PATCH v1 2/7] Use json to parse ccache statistics

Instead of relying on regex to parse ccache CLI output, we can ask
ccache to output its statistics in JSON, which Python natively supports.

Signed-off-by: Tristan Partin <tristan@partin.io>
---
 src/tools/ci/gha_ccache_decide.py | 24 ++++--------------------
 1 file changed, 4 insertions(+), 20 deletions(-)

diff --git a/src/tools/ci/gha_ccache_decide.py b/src/tools/ci/gha_ccache_decide.py
index ea9fbe9452..b4874517fb 100644
--- a/src/tools/ci/gha_ccache_decide.py
+++ b/src/tools/ci/gha_ccache_decide.py
@@ -1,7 +1,7 @@
 #!/usr/bin/env python3
 
+import json
 import os
-import re
 import shutil
 import subprocess
 
@@ -14,24 +14,6 @@ def run(cmd, check=True):
         stderr=subprocess.STDOUT,
     ).stdout
 
-def parse_ccache_stats():
-    out = run(["ccache", "--print-stats"])
-    hits = 0
-    misses = 0
-
-    for line in out.splitlines():
-        line = line.strip()
-        m = re.match(r"^local_storage_hit\s+(\d+)$", line)
-        if m:
-            hits = int(m.group(1))
-            continue
-        m = re.match(r"^local_storage_miss\s+(\d+)$", line)
-        if m:
-            misses = int(m.group(1))
-            continue
-
-    return hits, misses
-
 def append_github_output(key, value):
     output_path = os.environ["GITHUB_OUTPUT"]
     with open(output_path, "a", encoding="utf-8") as f:
@@ -53,7 +35,9 @@ def main():
     print("::endgroup::")
 
     # compute cache hit ratio
-    hits, misses = parse_ccache_stats()
+    ccache_stats = json.loads(run(["ccache", "--print-stats", "--format", "json"]))
+    hits = ccache_stats["local_storage_hit"]
+    misses = ccache_stats["local_storage_miss"]
     total = hits + misses
     hit_pct = int((hits / total) * 100) if total > 0 else 100
 
-- 
Tristan Partin
https://tristan.partin.io

