26 lines
613 B
Python
26 lines
613 B
Python
import json
|
|
import os
|
|
|
|
|
|
def make_private(folder):
|
|
mask = os.umask(0o077)
|
|
try:
|
|
os.makedirs(folder, exist_ok=True)
|
|
finally:
|
|
os.umask(mask)
|
|
|
|
|
|
def replace_json(path, data, **dump):
|
|
staged = f"{path}.{os.getpid()}"
|
|
try:
|
|
mode = os.stat(path).st_mode & 0o7777 if os.path.exists(path) else 0o600
|
|
with open(os.open(staged, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600), "w", encoding="utf-8") as handle:
|
|
os.fchmod(handle.fileno(), mode)
|
|
json.dump(data, handle, **dump)
|
|
os.replace(staged, path)
|
|
except OSError as failure:
|
|
if os.path.exists(staged):
|
|
os.remove(staged)
|
|
return failure
|
|
return None
|