upleb.uk

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

summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-23 17:41:00 -0500
committerItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-23 17:41:00 -0500
commit3f2292bcfef8f105db33d4e80beda0e6d1bd8c9f (patch)
tree837906874f00d81e7a2ea0c3d76ba169fd4b5b41
parent65a84a39a41b9dc66a8db5e8abcc9d49a8408d8f (diff)
parentec5415bff8bcaa68ddae73355bad28c916d19b8e (diff)
Merge remote-tracking branch 'origin/pr/8974'
-rw-r--r--tests/test_bvh_match.py13
-rw-r--r--tools/bvh_match.py53
2 files changed, 58 insertions, 8 deletions
diff --git a/tests/test_bvh_match.py b/tests/test_bvh_match.py
index eb9bd41b4..14c78d62a 100644
--- a/tests/test_bvh_match.py
+++ b/tests/test_bvh_match.py
@@ -13,6 +13,7 @@ import struct
13import sys 13import sys
14import tempfile 14import tempfile
15import unittest 15import unittest
16from unittest import mock
16 17
17ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 18ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
18sys.path.insert(0, os.path.join(ROOT, "tools")) 19sys.path.insert(0, os.path.join(ROOT, "tools"))
@@ -508,6 +509,18 @@ class TestPieces(unittest.TestCase):
508 self.assertEqual(rc, 2) 509 self.assertEqual(rc, 2)
509 self.assertIn("is a directory", err.getvalue()) 510 self.assertIn("is a directory", err.getvalue())
510 511
512 def test_json_write_failure_preserves_previous_report(self):
513 with tempfile.TemporaryDirectory() as directory:
514 previous = '{"previous": true}'
515 path = os.path.join(directory, "report.json")
516 with open(path, "w", encoding="utf-8", newline="\n") as f:
517 f.write(previous)
518 with mock.patch.object(M, "replace_retry", side_effect=OSError(13, "Permission denied")):
519 with self.assertRaises(OSError):
520 M.write_json_report(path, {"signature": [], "consistent": False})
521 with open(path, encoding="utf-8") as f:
522 self.assertEqual(f.read(), previous)
523
511 524
512if __name__ == "__main__": 525if __name__ == "__main__":
513 unittest.main() 526 unittest.main()
diff --git a/tools/bvh_match.py b/tools/bvh_match.py
index 365654b56..d156c2444 100644
--- a/tools/bvh_match.py
+++ b/tools/bvh_match.py
@@ -42,13 +42,17 @@ tools/bvh_rehydrate.py takes: the transform is recorded in it rather than applie
42import argparse 42import argparse
43import json 43import json
44import os 44import os
45import stat
45import struct 46import struct
46import sys 47import sys
48import tempfile
47from collections import Counter 49from collections import Counter
48 50
49sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) 51sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
50import bvh_geometry_check as C # noqa: E402 52import bvh_geometry_check as C # noqa: E402
51import bvh_rehydrate as R # noqa: E402 53import bvh_rehydrate as R # noqa: E402
54from file_mode import published_mode # noqa: E402
55from replace_retry import replace as replace_retry # noqa: E402
52from report_paths import same_report_path # noqa: E402 56from report_paths import same_report_path # noqa: E402
53 57
54INSTANCE_TRANSFORM_OFFSET = 0x50 58INSTANCE_TRANSFORM_OFFSET = 0x50
@@ -282,6 +286,39 @@ def describe(candidate, names):
282 return "%sstream 0x%-10x model %d geometry %s%s" % (label, item["Offset"], candidate["model"], used, note) 286 return "%sstream 0x%-10x model %d geometry %s%s" % (label, item["Offset"], candidate["model"], used, note)
283 287
284 288
289def write_json_report(path, report):
290 """Publish a complete regular file, or write directly to a non-regular destination."""
291 destination = os.path.realpath(path)
292 parent = os.path.dirname(destination) or "."
293 try:
294 mode = os.stat(destination).st_mode
295 except FileNotFoundError:
296 mode = None
297 if mode is not None and not stat.S_ISREG(mode):
298 with open(path, "w", encoding="utf-8", newline="\n") as f:
299 json.dump(report, f, indent=1)
300 return
301 if mode is not None:
302 # Retain the direct writer's permission check without truncating the report.
303 os.close(os.open(destination, os.O_WRONLY))
304
305 staged = tempfile.NamedTemporaryFile(
306 mode="w", encoding="utf-8", newline="\n", delete=False,
307 dir=parent, prefix=".bvh_match-", suffix=".tmp",
308 )
309 try:
310 with staged:
311 json.dump(report, staged, indent=1)
312 if os.name != "nt":
313 os.chmod(staged.name, published_mode(destination))
314 replace_retry(staged.name, destination)
315 finally:
316 try:
317 os.unlink(staged.name)
318 except FileNotFoundError:
319 pass
320
321
285def main(argv=None): 322def main(argv=None):
286 ap = argparse.ArgumentParser(description="Find the drawable geometry a dehydrated BLAS was built from.") 323 ap = argparse.ArgumentParser(description="Find the drawable geometry a dehydrated BLAS was built from.")
287 ap.add_argument("dehydrated") 324 ap.add_argument("dehydrated")
@@ -396,14 +433,14 @@ def main(argv=None):
396 if a.json: 433 if a.json:
397 match = consistent[0][3] if consistent else None 434 match = consistent[0][3] if consistent else None
398 try: 435 try:
399 with open(a.json, "w", encoding="utf-8", newline="\n") as f: 436 write_json_report(a.json, {
400 json.dump({"signature": signature, "consistent": bool(consistent), "scored": len(results), 437 "signature": signature, "consistent": bool(consistent), "scored": len(results),
401 "matched": None if match is None else { 438 "matched": None if match is None else {
402 "offset": match["item"]["Offset"], "model": match["model"], 439 "offset": match["item"]["Offset"], "model": match["model"],
403 "geometry": [match["kept"][k][2] for k in match["used"]], 440 "geometry": [match["kept"][k][2] for k in match["used"]],
404 "triangles": [match["kept"][k][3] for k in match["used"]], 441 "triangles": [match["kept"][k][3] for k in match["used"]],
405 "name": match["item"]["Name"] if a.names else None}}, 442 "name": match["item"]["Name"] if a.names else None},
406 f, indent=1) 443 })
407 except OSError as e: 444 except OSError as e:
408 print("error: cannot write %s: %s" % (a.json, e.strerror or e), file=sys.stderr) 445 print("error: cannot write %s: %s" % (a.json, e.strerror or e), file=sys.stderr)
409 return 2 446 return 2