Writes content to a file atomically using a temp file and os.replace, ensuring readers see only complete versions. Defaults to restrictive 0o600 permissions to protect sensitive data like API keys.
def atomic_write(path: Path | str, content: str, *, mode: int | None = 0o600) -> None:
"""Write ``content`` to ``path`` atomically via temp + ``os.replace``.
The replace is POSIX-atomic on the same filesystem, so a reader that
opens ``path`` either sees the old version or the new — never a
partial write. Creates parent directories as needed.
File mode defaults to ``0o600`` — owner read/write only. Files we
write under ``~/.freeride/`` (cooldown.json, config.json, .env from
`freeride init`, etc.) frequently contain provider API keys; world-
or group-readable would leak them on multi-user systems. Pass
``mode=None`` to skip the chmod (useful for tests on Windows where
POSIX modes don't apply meaningfully).
"""
p = Path(path)
p.parent.mkdi
... (truncated -- full source via MCP)
See the full source, get the GitHub permalink, and search 40K more like it.
Get a free API key