refactor(macos): use shared atomic_write_text for the anchor marker

/simplify-code reuse finding: _write_marker reimplemented the
mkstemp->write->os.replace pattern that utils.atomic_write_text already
provides as the repo's shared atomic-text-write helper (and the shared
version adds fsync + cross-device/busy-file fallbacks).
This commit is contained in:
kshitijk4poor
2026-08-28 09:00:26 +05:30
committed by kshitij
parent 0976ceaa98
commit 9c87a7c79e
+7 -12
View File
@@ -51,6 +51,7 @@ from pathlib import Path
from hermes_constants import venv_python_path
from hermes_cli.managed_uv import _RUNTIME_DIR_NAME
from utils import atomic_write_text
logger = logging.getLogger(__name__)
@@ -164,23 +165,17 @@ def _anchor_marker(venv_bin: Path) -> Path:
def _write_marker(venv_bin: Path, source_file: Path) -> None:
"""Write the anchor marker atomically (write-then-rename).
"""Write the anchor marker atomically via the shared helper.
A concurrent ensure (update + doctor --fix) must never observe a
partially-written marker: a torn read would compare unequal and trigger
a spurious reinstall, and ``write_text`` alone is not atomic.
"""
fd, tmp_name = tempfile.mkstemp(prefix=f"{_MARKER_NAME}.", dir=str(venv_bin))
try:
with os.fdopen(fd, "w", encoding="utf-8") as fh:
fh.write(_marker_value(source_file))
os.replace(tmp_name, _anchor_marker(venv_bin))
except OSError:
try:
os.unlink(tmp_name)
except OSError:
pass
raise
atomic_write_text(
_anchor_marker(venv_bin),
_marker_value(source_file),
tmp_prefix=f"{_MARKER_NAME}.",
)
def _store_root(source_file: Path) -> Path: