Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions src/specify_cli/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import sys
import subprocess
import platform
import tempfile
from pathlib import Path
from typing import TYPE_CHECKING, Any

Expand Down Expand Up @@ -1727,7 +1728,21 @@ def _merge_toml_fragment(dst: Path, fragment: str) -> None:
flags=re.DOTALL,
)
dst.parent.mkdir(parents=True, exist_ok=True)
dst.write_text(existing.rstrip() + "\n\n" + fragment + "\n", encoding="utf-8")
fd, tmp = tempfile.mkstemp(
dir=str(dst.parent), prefix=f".{dst.name}.", suffix=".tmp"
)
try:
if dst.exists() and hasattr(os, "fchmod"):
os.fchmod(fd, dst.stat(follow_symlinks=False).st_mode & 0o7777)
with os.fdopen(fd, "w", encoding="utf-8") as f:
f.write(existing.rstrip() + "\n\n" + fragment + "\n")
os.replace(tmp, dst)
Comment on lines +1737 to +1739
Comment on lines +1731 to +1739
except BaseException:
try:
os.unlink(tmp)
except OSError:
pass
raise


def _remove_toml_entries(dst: Path) -> bool:
Expand Down Expand Up @@ -1757,7 +1772,21 @@ def _remove_toml_entries(dst: Path) -> bool:
if not stripped:
dst.unlink(missing_ok=True)
return True
dst.write_text(cleaned, encoding="utf-8")
fd, tmp = tempfile.mkstemp(
dir=str(dst.parent), prefix=f".{dst.name}.", suffix=".tmp"
)
try:
if dst.exists() and hasattr(os, "fchmod"):
os.fchmod(fd, dst.stat(follow_symlinks=False).st_mode & 0o7777)
with os.fdopen(fd, "w", encoding="utf-8") as f:
f.write(cleaned)
os.replace(tmp, dst)
except BaseException:
try:
os.unlink(tmp)
except OSError:
pass
raise
return False


Expand Down