Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions src/FileUtils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

import pickle
import os, stat
import tempfile
import shutil

def file_exists(path):
"Returns true if path refers to any existing object (including a dead symlink)"
Expand Down Expand Up @@ -88,15 +90,16 @@ def flush(self, verify=True):

if self.filename and self.items is not None and self.dirty:

tempname = temp_filename_for(self.filename)
with open(tempname, 'wb') as fl:
with tempfile.NamedTemporaryFile('wb', delete_on_close=False) as fl:
pickle.dump(self.items, fl)
if verify:
with open(tempname, 'rb') as fl:
items = pickle.load(fl)
assert items == self.items
os.rename(tempname, self.filename)
self.dirty = False
fl.close()

if verify:
with open(fl.name, 'rb') as ro_fl:
items = pickle.load(ro_fl)
assert items == self.items
shutil.copy(fl.name, self.filename)
self.dirty = False

return True
else:
Expand Down