mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 09:08:38 +00:00
Merge pull request #2299 from Ch1mpleo/fix/first-light-atomic-manifest
tools/first_light: write manifest.json atomically (FIRST-LIGHT-ATOMIC-MANIFEST)
This commit is contained in:
@ -1046,6 +1046,29 @@ class CameraTests(unittest.TestCase):
|
||||
else:
|
||||
os.environ[k] = v
|
||||
|
||||
def test_write_json_atomic_and_replace_failure_preserves_existing(self):
|
||||
import first_light
|
||||
import tempfile
|
||||
from unittest import mock
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
out = os.path.join(tmp, "manifest.json")
|
||||
first_light._write_json_atomic(out, {"ok": True})
|
||||
self.assertEqual(os.listdir(tmp), ["manifest.json"])
|
||||
with open(out, "rb") as f:
|
||||
raw = f.read()
|
||||
self.assertTrue(raw.endswith(b"\n"))
|
||||
self.assertNotIn(b"\r\n", raw)
|
||||
|
||||
with open(out, "w", encoding="utf-8", newline="\n") as f:
|
||||
f.write('{"keep": true}\n')
|
||||
with mock.patch.object(first_light, "replace_retry", side_effect=OSError("replace denied")):
|
||||
with self.assertRaises(OSError):
|
||||
first_light._write_json_atomic(out, {"ok": False})
|
||||
self.assertEqual(os.listdir(tmp), ["manifest.json"])
|
||||
with open(out, encoding="utf-8") as f:
|
||||
self.assertEqual(f.read(), '{"keep": true}\n')
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(unittest.main())
|
||||
|
||||
@ -45,14 +45,47 @@ import shutil
|
||||
import struct
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
|
||||
TOOLS = os.path.dirname(os.path.abspath(__file__))
|
||||
ROOT = os.path.dirname(TOOLS)
|
||||
sys.path.insert(0, os.path.join(ROOT, "runtime", "test", "shaders"))
|
||||
from compute_fixture import find_test_program # noqa: E402
|
||||
sys.path.insert(0, TOOLS) # python -I leaves the script's own folder off sys.path
|
||||
from file_mode import published_mode # noqa: E402
|
||||
from replace_retry import replace as replace_retry # noqa: E402
|
||||
from stale_build import newer_sources # noqa: E402
|
||||
|
||||
|
||||
def _write_json_atomic(path: str, payload: dict) -> None:
|
||||
dest = os.path.realpath(path)
|
||||
parent = os.path.dirname(dest) or "."
|
||||
os.makedirs(parent, exist_ok=True)
|
||||
fd, tmp = tempfile.mkstemp(dir=parent, prefix=".first_light-", suffix=".tmp")
|
||||
try:
|
||||
try:
|
||||
f = os.fdopen(fd, "w", encoding="utf-8", newline="\n")
|
||||
except BaseException:
|
||||
os.close(fd)
|
||||
raise
|
||||
with f:
|
||||
json.dump(payload, f, indent=1)
|
||||
f.write("\n")
|
||||
f.flush()
|
||||
os.fsync(f.fileno())
|
||||
os.chmod(tmp, published_mode(dest))
|
||||
replace_retry(tmp, dest)
|
||||
except BaseException:
|
||||
try:
|
||||
os.chmod(tmp, 0o600)
|
||||
except OSError:
|
||||
pass
|
||||
try:
|
||||
os.unlink(tmp)
|
||||
except OSError:
|
||||
pass
|
||||
raise
|
||||
|
||||
DEFAULT_VS = "VS_main_170e32e6_Wrapped"
|
||||
DEFAULT_PS = "$PS_Rage_Untextured_14f8529b_Wrapped"
|
||||
DEFAULT_TEXTURED_PS = "PS_Copy_3ea4b38a_Wrapped"
|
||||
@ -848,8 +881,7 @@ def run_draw(args, manifest: dict, out_dir: str) -> dict:
|
||||
os.unlink(path)
|
||||
if "result" in manifest:
|
||||
del manifest["result"]
|
||||
with open(manifest_path, "w", encoding="utf-8", newline="\n") as f:
|
||||
json.dump(manifest, f, indent=1)
|
||||
_write_json_atomic(manifest_path, manifest)
|
||||
subprocess.run(cmd, check=True, env=env)
|
||||
if not os.path.isfile(raw):
|
||||
raise SystemExit(f"the harness exited successfully but did not write {raw}")
|
||||
@ -870,8 +902,7 @@ def run_draw(args, manifest: dict, out_dir: str) -> dict:
|
||||
"ratio": result["ratio"],
|
||||
"ppm": ppm,
|
||||
}
|
||||
with open(manifest_path, "w", encoding="utf-8", newline="\n") as f:
|
||||
json.dump(manifest, f, indent=1)
|
||||
_write_json_atomic(manifest_path, manifest)
|
||||
return result
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user