1
0
mirror of https://github.com/kevin1024/vcrpy.git synced 2025-12-08 16:53:23 +00:00

add back the secure write

This commit is contained in:
Kevin McCarthy
2013-08-06 19:06:55 -10:00
parent 166facd0d0
commit 7221141cdd

View File

@@ -1,3 +1,4 @@
import tempfile
import os
import yaml
@@ -7,13 +8,22 @@ try:
except ImportError:
from yaml import Loader, Dumper
def _secure_write(path, contents):
"""
We'll overwrite the old version securely by writing out a temporary
version and then moving it to replace the old version
"""
dirname, filename = os.path.split(path)
fd, name = tempfile.mkstemp(dir=dirname, prefix=filename)
with os.fdopen(fd, 'w') as fout:
fout.write(contents)
os.rename(name, path)
def load_cassette(cassette_path):
return yaml.load(open(cassette_path), Loader=Loader)
def save_cassette(cassette_path, data):
#TODO: safe overwrite using tmpfile
dirname, filename = os.path.split(cassette_path)
if not os.path.exists(dirname):
os.makedirs(dirname)
with open(cassette_path, 'w') as cassette_file:
cassette_file.write(yaml.dump(data, Dumper=Dumper))
_secure_write(cassette_path, yaml.dump(data, Dumper=Dumper))