upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/tools/script_qtable.py
diff options
context:
space:
mode:
Diffstat (limited to 'tools/script_qtable.py')
-rw-r--r--tools/script_qtable.py32
1 files changed, 23 insertions, 9 deletions
diff --git a/tools/script_qtable.py b/tools/script_qtable.py
index afa037245..6c29a5260 100644
--- a/tools/script_qtable.py
+++ b/tools/script_qtable.py
@@ -400,13 +400,15 @@ def parse_qtable(blob: bytes, *, root_hash: int | None = None, type_hash: int |
400 matching_arrays = [r for r in records if r["kind"] == "array" and r["element_type_hash"] == type_hash] 400 matching_arrays = [r for r in records if r["kind"] == "array" and r["element_type_hash"] == type_hash]
401 matching_structs = [r for r in records if r["kind"] == "struct" and r["struct_type_hash"] == type_hash] 401 matching_structs = [r for r in records if r["kind"] == "struct" and r["struct_type_hash"] == type_hash]
402 owner_roots = {tree_owner[r["index"]] for r in matching_arrays + matching_structs if tree_owner[r["index"]] >= 0} 402 owner_roots = {tree_owner[r["index"]] for r in matching_arrays + matching_structs if tree_owner[r["index"]] >= 0}
403 tq_strides = Counter(r["stride"] for r in matching_arrays)
404 tq_sizes = Counter(r["bytes"] for r in matching_structs)
403 result["type_query"] = { 405 result["type_query"] = {
404 "hash": f"{type_hash:#010x}", 406 "hash": f"{type_hash:#010x}",
405 "array_matches": len(matching_arrays), 407 "array_matches": len(matching_arrays),
406 "struct_matches": len(matching_structs), 408 "struct_matches": len(matching_structs),
407 "distinct_trees": len(owner_roots), 409 "distinct_trees": len(owner_roots),
408 "array_strides": dict(sorted(Counter(str(r["stride"]) for r in matching_arrays).items())), 410 "array_strides": {str(k): tq_strides[k] for k in sorted(tq_strides)},
409 "struct_sizes": dict(sorted(Counter(str(r["bytes"]) for r in matching_structs).items())), 411 "struct_sizes": {str(k): tq_sizes[k] for k in sorted(tq_sizes)},
410 } 412 }
411 413
412 return result 414 return result
@@ -465,20 +467,31 @@ def _join_qtable_from_source(source) -> tuple[bytes, dict]:
465 467
466 468
467class _BinFolderSource: 469class _BinFolderSource:
468 """Minimal sorted *.bin reader when a test/carve directory has no index.json.""" 470 """Minimal offset-ordered *.bin reader when a test/carve directory has no index.json."""
469 471
470 def __init__(self, directory: str): 472 def __init__(self, directory: str):
471 names = sorted(n for n in os.listdir(directory) if n.lower().endswith(".bin"))
472 self.directory = directory 473 self.directory = directory
473 self.rows = [] 474 hex_rows = []
474 for idx, name in enumerate(names): 475 other_rows = []
476 for name in sorted(os.listdir(directory)):
477 if not name.lower().endswith(".bin"):
478 continue
475 full = os.path.join(directory, name) 479 full = os.path.join(directory, name)
480 if not os.path.isfile(full):
481 continue
476 sz = os.path.getsize(full) 482 sz = os.path.getsize(full)
477 try: 483 try:
478 off = int(os.path.splitext(name)[0], 16) 484 off = int(os.path.splitext(name)[0], 16)
479 except ValueError: 485 except ValueError:
480 off = idx * 0x10000 486 other_rows.append((name, sz))
481 self.rows.append({"off": off, "stored": sz, "size": sz, "_name": name}) 487 else:
488 hex_rows.append((off, name, sz))
489 hex_rows.sort(key=lambda item: (item[0], item[1]))
490 next_off = (hex_rows[-1][0] + 0x10000) if hex_rows else 0
491 self.rows = [{"off": off, "stored": sz, "size": sz, "_name": name} for off, name, sz in hex_rows]
492 for name, sz in other_rows:
493 self.rows.append({"off": next_off, "stored": sz, "size": sz, "_name": name})
494 next_off += 0x10000
482 495
483 def read(self, row: dict) -> bytes: 496 def read(self, row: dict) -> bytes:
484 with open(os.path.join(self.directory, row["_name"]), "rb") as f: 497 with open(os.path.join(self.directory, row["_name"]), "rb") as f:
@@ -533,7 +546,8 @@ def check_report_path(out_path: str, target: str, rows: list[dict] | None = None
533 raise UsageError(f"--json would overwrite the input {target}") 546 raise UsageError(f"--json would overwrite the input {target}")
534 else: 547 else:
535 for name in os.listdir(target): 548 for name in os.listdir(target):
536 if name.lower().endswith(".bin") and same_report_path(out_path, os.path.join(target, name)): 549 full = os.path.join(target, name)
550 if name.lower().endswith(".bin") and os.path.isfile(full) and same_report_path(out_path, full):
537 raise UsageError(f"--json would overwrite the input {name}") 551 raise UsageError(f"--json would overwrite the input {name}")
538 elif same_report_path(out_path, target): 552 elif same_report_path(out_path, target):
539 raise UsageError(f"--json would overwrite the input {target}") 553 raise UsageError(f"--json would overwrite the input {target}")