diff options
| author | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-23 17:41:00 -0500 |
|---|---|---|
| committer | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-23 17:41:00 -0500 |
| commit | 3f2292bcfef8f105db33d4e80beda0e6d1bd8c9f (patch) | |
| tree | 837906874f00d81e7a2ea0c3d76ba169fd4b5b41 | |
| parent | 65a84a39a41b9dc66a8db5e8abcc9d49a8408d8f (diff) | |
| parent | ec5415bff8bcaa68ddae73355bad28c916d19b8e (diff) | |
Merge remote-tracking branch 'origin/pr/8974'
| -rw-r--r-- | tests/test_bvh_match.py | 13 | ||||
| -rw-r--r-- | tools/bvh_match.py | 53 |
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 | |||
| 13 | import sys | 13 | import sys |
| 14 | import tempfile | 14 | import tempfile |
| 15 | import unittest | 15 | import unittest |
| 16 | from unittest import mock | ||
| 16 | 17 | ||
| 17 | ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | 18 | ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 18 | sys.path.insert(0, os.path.join(ROOT, "tools")) | 19 | sys.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 | ||
| 512 | if __name__ == "__main__": | 525 | if __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 | |||
| 42 | import argparse | 42 | import argparse |
| 43 | import json | 43 | import json |
| 44 | import os | 44 | import os |
| 45 | import stat | ||
| 45 | import struct | 46 | import struct |
| 46 | import sys | 47 | import sys |
| 48 | import tempfile | ||
| 47 | from collections import Counter | 49 | from collections import Counter |
| 48 | 50 | ||
| 49 | sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) | 51 | sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) |
| 50 | import bvh_geometry_check as C # noqa: E402 | 52 | import bvh_geometry_check as C # noqa: E402 |
| 51 | import bvh_rehydrate as R # noqa: E402 | 53 | import bvh_rehydrate as R # noqa: E402 |
| 54 | from file_mode import published_mode # noqa: E402 | ||
| 55 | from replace_retry import replace as replace_retry # noqa: E402 | ||
| 52 | from report_paths import same_report_path # noqa: E402 | 56 | from report_paths import same_report_path # noqa: E402 |
| 53 | 57 | ||
| 54 | INSTANCE_TRANSFORM_OFFSET = 0x50 | 58 | INSTANCE_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 | ||
| 289 | def 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 | |||
| 285 | def main(argv=None): | 322 | def 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 |