add ensure_write
This commit is contained in:
parent
e0a66f5c99
commit
e8513240ea
@ -10,6 +10,8 @@ import yaml
|
|||||||
import json
|
import json
|
||||||
import shutil
|
import shutil
|
||||||
import gzip
|
import gzip
|
||||||
|
import contextlib
|
||||||
|
import tempfile
|
||||||
|
|
||||||
import pwnagotchi
|
import pwnagotchi
|
||||||
|
|
||||||
@ -345,6 +347,17 @@ def extract_from_pcap(path, fields):
|
|||||||
|
|
||||||
return results
|
return results
|
||||||
|
|
||||||
|
@contextlib.contextmanager
|
||||||
|
def ensure_write(filename, mode='w'):
|
||||||
|
path = os.path.dirname(filename)
|
||||||
|
fd, tmp = tempfile.mkstemp(dir=path)
|
||||||
|
|
||||||
|
with os.fdopen(fd, mode) as f:
|
||||||
|
yield f
|
||||||
|
f.flush()
|
||||||
|
os.fsync(f.fileno())
|
||||||
|
|
||||||
|
os.replace(tmp, filename)
|
||||||
|
|
||||||
class StatusFile(object):
|
class StatusFile(object):
|
||||||
def __init__(self, path, data_format='raw'):
|
def __init__(self, path, data_format='raw'):
|
||||||
@ -378,7 +391,7 @@ class StatusFile(object):
|
|||||||
def update(self, data=None):
|
def update(self, data=None):
|
||||||
self._updated = datetime.now()
|
self._updated = datetime.now()
|
||||||
self.data = data
|
self.data = data
|
||||||
with open(self._path, 'w') as fp:
|
with ensure_write(self._path, 'w') as fp:
|
||||||
if data is None:
|
if data is None:
|
||||||
fp.write(str(self._updated))
|
fp.write(str(self._updated))
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user