Files
mandy/tools/classify_carve.py
bbareth d771bd88e2 classify_carve.py: a missing argument or a bad carve root is one line
main() read sys.argv[1] directly: no argument was an IndexError, a
missing root a FileNotFoundError from os.listdir and a file a
NotADirectoryError, all as tracebacks. A second argument was silently
ignored and the run went ahead. Now a wrong argument count prints the
usage line, and a missing or non-folder root names the path; both exit
2 before types.json is written.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 01:50:48 +01:00

103 lines
3.6 KiB
Python

"""Group every carved file by signature, using only the per-archive index.json (no file reads).
Classes:
bink2 Bink 2 video (KB2x)
rbf0 Rockstar binary metadata ('RBF0', carries its own field names)
respage resource page ('42 40 00 00': tagged-pointer structures for textures/meshes/etc.)
gpu-zerohdr starts with 16+ zero bytes (GPU data: textures, vertex/index buffers)
other:<hex> anything else, keyed by its first 4 bytes
Usage:
python -I classify_carve.py <work>\\carve
Writes types.json (per archive and overall counts/bytes per class, plus size stats) and prints a summary.
"""
import json
import os
import sys
import tempfile
from collections import defaultdict
def classify(r):
if r.get("type") == "bink2":
return "bink2"
h = r["head"]
if h.startswith("52424630"):
return "rbf0"
if h.startswith("42400000"):
return "respage"
if h.startswith("00000000000000000000000000000000"):
return "gpu-zerohdr"
return "other:" + h[:8]
def index_rows(path):
with open(path, encoding="utf-8") as f:
try:
document = json.load(f)
except ValueError:
raise ValueError("invalid JSON") from None
rows = document.get("files") if isinstance(document, dict) else None
if not isinstance(rows, list):
raise ValueError('expected a "files" list')
for number, row in enumerate(rows, 1):
if not isinstance(row, dict) or type(row.get("size")) is not int or row["size"] < 0:
raise ValueError(f"row {number} needs a nonnegative integer size")
if row.get("type") != "bink2" and not isinstance(row.get("head"), str):
raise ValueError(f"row {number} needs a string head")
yield row
def main():
if len(sys.argv) != 2:
print("usage: python -I classify_carve.py <work>\\carve", file=sys.stderr)
return 2
root = sys.argv[1]
if not os.path.isdir(root):
what = "is not a folder" if os.path.exists(root) else "does not exist"
print(f"{root}: {what}; give the carve folder (the one with an index.json per archive)", file=sys.stderr)
return 2
per = {}
total = defaultdict(lambda: [0, 0])
for name in sorted(os.listdir(root)):
p = os.path.join(root, name, "index.json")
if not os.path.isfile(p):
continue
c = defaultdict(lambda: [0, 0])
try:
for r in index_rows(p):
k = classify(r)
c[k][0] += 1
c[k][1] += r["size"]
total[k][0] += 1
total[k][1] += r["size"]
except (OSError, ValueError) as error:
print(f"{p}: {error}", file=sys.stderr)
return 2
per[name] = {k: {"files": v[0], "bytes": v[1]} for k, v in sorted(c.items(), key=lambda kv: -kv[1][1])}
top = sorted(total.items(), key=lambda kv: -kv[1][1])
out = {"overall": {k: {"files": v[0], "bytes": v[1]} for k, v in top}, "per_archive": per}
fd, temp = tempfile.mkstemp(dir=root, prefix=".types-", suffix=".tmp")
try:
with os.fdopen(fd, "w", encoding="utf-8") as f:
json.dump(out, f, indent=1)
f.flush()
os.fsync(f.fileno())
os.replace(temp, os.path.join(root, "types.json"))
except BaseException:
try:
os.unlink(temp)
except OSError:
pass
raise
print(f"{'class':24} {'files':>10} {'GiB':>8}")
for k, v in top[:25]:
print(f"{k:24} {v[0]:>10} {v[1] / 2**30:8.2f}")
print(f"{len(top)} classes in total")
return 0
if __name__ == "__main__":
sys.exit(main())