mirror of
ssh://git.janware.com/srv/git/janware/proj/jw-python
synced 2026-01-15 09:53:32 +01:00
misc.py: Add atomic_store()
Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
a9aadbad5a
commit
4a200c8851
1 changed files with 23 additions and 0 deletions
|
|
@ -1,4 +1,12 @@
|
||||||
import os, errno
|
import os, errno
|
||||||
|
import atexit
|
||||||
|
import tempfile
|
||||||
|
|
||||||
|
_tmpfiles = set()
|
||||||
|
|
||||||
|
def _cleanup():
|
||||||
|
for f in _tmpfiles:
|
||||||
|
os.unlink(f)
|
||||||
|
|
||||||
def silentremove(filename): #export
|
def silentremove(filename): #export
|
||||||
try:
|
try:
|
||||||
|
|
@ -15,3 +23,18 @@ def pad(token, total_size, right_align = False):
|
||||||
if right_align:
|
if right_align:
|
||||||
return space + token
|
return space + token
|
||||||
return token + space
|
return token + space
|
||||||
|
|
||||||
|
def atomic_store(contents, path): # export
|
||||||
|
if path[0:3] == '/dev':
|
||||||
|
with open(path, 'w') as outfile:
|
||||||
|
outfile.write(contents)
|
||||||
|
return
|
||||||
|
outfile = tempfile.NamedTemporaryFile(prefix=os.path.basename(path), delete=False, dir=os.path.dirname(path))
|
||||||
|
name = outfile.name
|
||||||
|
_tmpfiles.add(name)
|
||||||
|
outfile.write(contents)
|
||||||
|
outfile.close()
|
||||||
|
os.rename(name, path)
|
||||||
|
_tmpfiles.remove(name)
|
||||||
|
|
||||||
|
atexit.register(_cleanup)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue