Skip to content

Commit 9a0415b

Browse files
committed
Merge branch 'master' of https://github.com/bbangert/beaker
2 parents 45b8c30 + 9642cd3 commit 9a0415b

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

beaker/container.py

+2-10
Original file line numberDiff line numberDiff line change
@@ -676,16 +676,8 @@ def do_open(self, flags, replace):
676676

677677
def do_close(self):
678678
if self.flags == 'c' or self.flags == 'w':
679-
if os.name == 'posix':
680-
tempname = '%s.temp' % (self.file)
681-
fh = open(tempname, 'wb')
682-
pickle.dump(self.hash, fh)
683-
fh.close()
684-
os.rename(tempname, self.file)
685-
else:
686-
fh = open(self.file, 'wb')
687-
pickle.dump(self.hash, fh)
688-
fh.close()
679+
pickled = pickle.dumps(self.hash)
680+
util.safe_write(self.file, pickled)
689681

690682
self.hash = {}
691683
self.flags = None

beaker/util.py

+13
Original file line numberDiff line numberDiff line change
@@ -492,3 +492,16 @@ def machine_identifier():
492492
else:
493493
machine_hash.update(socket.gethostname())
494494
return binascii.hexlify(machine_hash.digest()[0:3]).decode('ascii')
495+
496+
497+
def safe_write (filepath, contents):
498+
if os.name == 'posix':
499+
tempname = '%s.temp' % (filepath)
500+
fh = open(tempname, 'wb')
501+
fh.write(contents)
502+
fh.close()
503+
os.rename(tempname, filepath)
504+
else:
505+
fh = open(filepath, 'wb')
506+
fh.write(contents)
507+
fh.close()

0 commit comments

Comments
 (0)