Files
troupe-standup/wizard-work/ledger/generate_ledger_extract.py
T

60 lines
2.0 KiB
Python

#!/usr/bin/env python3
"""Generate the per-extension file census table from the wizardwork file manifest.
Reads : /root/troupe-standup/wizard-work/ledger/wizardwork-file-manifest.tsv
tab-separated, NO header: path, size_bytes, extension, top_dir
Writes: /root/troupe-standup/wizard-work/ledger/ledger-extract.tsv
tab-separated: extension, file_count, bytes_total, bytes_mean,
bytes_median, bytes_min, bytes_max
sorted by file_count desc, then extension asc (deterministic).
"""
import csv
import statistics
from collections import defaultdict
MANIFEST = "/root/troupe-standup/wizard-work/ledger/wizardwork-file-manifest.tsv"
OUT = "/root/troupe-standup/wizard-work/ledger/ledger-extract.tsv"
def norm_ext(ext):
return "(no ext)" if ext == "" else ext
def main():
counts = defaultdict(int)
sizes = defaultdict(list)
with open(MANIFEST, newline="") as f:
for line in f:
parts = line.rstrip("\n").split("\t")
if len(parts) < 3:
continue
try:
size = int(parts[1])
except ValueError:
continue
ext = norm_ext(parts[2])
counts[ext] += 1
sizes[ext].append(size)
rows = []
for ext, szs in sizes.items():
total = sum(szs)
mean = total / counts[ext]
med = statistics.median(szs)
rows.append((ext, counts[ext], total, mean, med, min(szs), max(szs)))
rows.sort(key=lambda r: (-r[1], r[0]))
with open(OUT, "w", newline="") as f:
w = csv.writer(f, delimiter="\t", lineterminator="\n")
w.writerow(["extension", "file_count", "bytes_total", "bytes_mean",
"bytes_median", "bytes_min", "bytes_max"])
for ext, cnt, total, mean, med, mn, mx in rows:
w.writerow([ext, cnt, total, "%.1f" % mean, "%.1f" % med, mn, mx])
print("Wrote %d extension rows to %s" % (len(rows), OUT))
if __name__ == "__main__":
main()