Preserve stat attributes when modifying rc files atomically
This commit is contained in:
parent
f4f2013c2b
commit
040a152f1f
1 changed files with 7 additions and 3 deletions
|
|
@ -4,6 +4,8 @@
|
|||
|
||||
|
||||
import os
|
||||
import shutil
|
||||
from tempfile import mkstemp
|
||||
from typing import Optional, Union
|
||||
|
||||
from .constants import shell_integration_dir
|
||||
|
|
@ -20,10 +22,12 @@
|
|||
|
||||
|
||||
def atomic_write(path: str, data: Union[str, bytes]) -> None:
|
||||
tmp = path + '_ksi_tmp'
|
||||
with open(tmp, 'w' + ('b' if isinstance(data, bytes) else '')) as f:
|
||||
mode = 'w' + ('b' if isinstance(data, bytes) else '')
|
||||
fd, tpath = mkstemp(dir=os.path.dirname(path), text=isinstance(data, str))
|
||||
shutil.copystat(path, tpath)
|
||||
with open(fd, mode) as f:
|
||||
f.write(data)
|
||||
os.rename(tmp, path)
|
||||
os.rename(tpath, path)
|
||||
|
||||
|
||||
def setup_integration(shell_name: str, rc_path: str, template: str = posix_template) -> None:
|
||||
|
|
|
|||
Loading…
Reference in a new issue