fester/backend/cache.py

54 lines
1.1 KiB
Python

import os
import json
import hashlib
from pathlib import Path
CACHE_DIR = Path("/var/lib/fester/cache")
CACHE_DIR.mkdir(parents=True, exist_ok=True)
def hash_dict(d):
raw = json.dumps(d, sort_keys=True).encode()
return hashlib.sha256(raw).hexdigest()
def make_build_key(snapshot_hash, target, env):
return hashlib.sha256(
f"{snapshot_hash}:{target}:{hash_dict(env)}".encode()
).hexdigest()
def cache_hit(key):
return (CACHE_DIR / key / "done").exists()
def cache_store(key, log):
base = CACHE_DIR / key
base.mkdir(parents=True, exist_ok=True)
(base / "done").write_text("1")
(base / "log.txt").write_text(log)
def cache_log(key):
p = CACHE_DIR / key / "log.txt"
return p.read_text() if p.exists() else None
# ----------------------------
# ccache health probe
# ----------------------------
def ccache_stats():
try:
import subprocess
out = subprocess.check_output(
["ccache", "-s"],
stderr=subprocess.STDOUT
).decode()
return out
except:
return "ccache not available"