import subprocess, os, re, collections, hashlib, time, json
root = r"C:\Users\decid\Documents\projects\spt-core"
out = subprocess.run(["git","status","--porcelain","--untracked-files=all"],cwd=root,capture_output=True,text=True).stdout
files = [l[3:] for l in out.splitlines() if l.startswith("??")]
rootfiles = [f for f in files if "/" not in f]
sub = [f for f in files if "/" in f]
print("untracked total", len(files), "root", len(rootfiles), "subdir", len(sub))
for f in sub: print("  SUBDIR", f)
# tracked corpus for citation grep
tracked = subprocess.run(["git","ls-files"],cwd=root,capture_output=True,text=True).stdout.splitlines()
corpus = {}
for t in tracked:
    p = os.path.join(root,t)
    try:
        with open(p,'rb') as fh: corpus[t] = fh.read()
    except Exception: pass
def prefix(f):
    base = f
    m = re.match(r"^([A-Za-z]+\d*[A-Za-z]*)[_\-.]", f)
    return m.group(1) if m else f
groups = collections.defaultdict(list)
for f in rootfiles: groups[prefix(f)].append(f)
rows = []
total = 0
for g, fs in sorted(groups.items(), key=lambda kv: -len(kv[1])):
    size = 0; mt = []; exts = collections.Counter()
    for f in fs:
        p = os.path.join(root,f)
        st = os.stat(p); size += st.st_size; mt.append(st.st_mtime)
        exts[os.path.splitext(f)[1] or "(none)"] += 1
    total += size
    cites = []
    for f in fs:
        b = f.encode()
        hits = [t for t,c in corpus.items() if b in c]
        if hits: cites.append((f, hits[:5]))
    rows.append(dict(group=g, n=len(fs), bytes=size, oldest=time.strftime("%m-%d %H:%MZ",time.gmtime(min(mt))), newest=time.strftime("%m-%d %H:%MZ",time.gmtime(max(mt))), exts=dict(exts), cites=cites))
print(f"\nTOTAL root untracked bytes {total} ({total/2**20:.1f} MiB)\n")
print(f"{'group':28} {'n':>4} {'MiB':>8} {'oldest':>12} {'newest':>12}  exts / cites")
for r in rows:
    print(f"{r['group']:28} {r['n']:>4} {r['bytes']/2**20:>8.2f} {r['oldest']:>12} {r['newest']:>12}  {r['exts']}" + (f"  CITED: {r['cites']}" if r['cites'] else ""))
json.dump(rows, open(os.path.join(os.path.dirname(os.path.abspath(__file__)),"root_census.json"),"w"), indent=1)
# largest 15
big = sorted(((os.stat(os.path.join(root,f)).st_size,f) for f in rootfiles), reverse=True)[:15]
print("\nLARGEST 15:")
for s,f in big: print(f"  {s/2**20:8.2f} MiB {f}")
