mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-24 09:18:08 +02:00
Preserve stat attributes when modifying rc files atomically
This commit is contained in:
@@ -4,6 +4,8 @@
|
|||||||
|
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import shutil
|
||||||
|
from tempfile import mkstemp
|
||||||
from typing import Optional, Union
|
from typing import Optional, Union
|
||||||
|
|
||||||
from .constants import shell_integration_dir
|
from .constants import shell_integration_dir
|
||||||
@@ -20,10 +22,12 @@ posix_template = '''
|
|||||||
|
|
||||||
|
|
||||||
def atomic_write(path: str, data: Union[str, bytes]) -> None:
|
def atomic_write(path: str, data: Union[str, bytes]) -> None:
|
||||||
tmp = path + '_ksi_tmp'
|
mode = 'w' + ('b' if isinstance(data, bytes) else '')
|
||||||
with open(tmp, 'w' + ('b' if isinstance(data, bytes) else '')) as f:
|
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)
|
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:
|
def setup_integration(shell_name: str, rc_path: str, template: str = posix_template) -> None:
|
||||||
|
|||||||
Reference in New Issue
Block a user