blob: 15319f71818ff706a4455cc68e63501fa098a572 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
#!/usr/bin/python
# vi: ts=4 expandtab
import json
import os
import tempfile
def atomic_write_file(path, content, mode='w'):
tf = None
try:
tf = tempfile.NamedTemporaryFile(dir=os.path.dirname(path),
delete=False, mode=mode)
tf.write(content)
tf.close()
os.rename(tf.name, path)
except Exception as e:
if tf is not None:
os.unlink(tf.name)
raise e
def atomic_write_json(path, data):
return atomic_write_file(path, json.dumps(data, indent=1,
sort_keys=True) + "\n")
|