#!/usr/bin/env python3
"""Ukur tempat-tempat yang biasanya menumpuk sampah: cache, log cron, berkas sementara."""
import glob, os, subprocess

WS = "/home/mantapman/.hermes/profiles/atun/workspace"
PROFIL = "/home/mantapman/.hermes/profiles/atun"
TARGET = [
    ("cache hermes (dokumen/spillover)", os.path.expanduser("~/.hermes/cache")),
    ("log cron (output job)", f"{PROFIL}/cron/output"),
    ("cache bahan artikel (baru)", f"{WS}/cache"),
    ("__pycache__ workspace", None),
    ("berkas sementara di /tmp (ss_/tsc_)", None),
    ("cadangan *.bak di workspace", None),
]


def ukuran(path):
    try:
        hasil = subprocess.run(["du", "-sk", path], capture_output=True, text=True, timeout=60)
        return int(hasil.stdout.split()[0]) * 1024
    except Exception:
        return 0


for nama, jalur in TARGET:
    if jalur and os.path.exists(jalur):
        print(f"   {ukuran(jalur) // 1024 // 1024:>5} MB · {nama} ({jalur.replace(os.path.expanduser('~'), '~')})")
    elif nama.startswith("__pycache__"):
        total = 0
        for p in glob.glob(f"{WS}/**/__pycache__", recursive=True):
            total += ukuran(p)
        print(f"   {total // 1024 // 1024:>5} MB · {nama} ({len(glob.glob(f'{WS}/**/__pycache__', recursive=True))} folder)")
    elif jalur is None:
        files = glob.glob("/tmp/ss_*") + glob.glob("/tmp/tsc_*") + glob.glob("/tmp/*.html")
        total = sum(os.path.getsize(f) for f in files if os.path.isfile(f))
        print(f"   {total // 1024 // 1024:>5} MB · {nama} ({len(files)} berkas)")
