mirror of
https://github.com/kevin1024/vcrpy.git
synced 2025-12-08 16:53:23 +00:00
24 lines
756 B
Python
24 lines
756 B
Python
import tempfile
|
|
import os
|
|
|
|
|
|
class FilesystemPersister(object):
|
|
@classmethod
|
|
def _secure_write(cls, 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)
|
|
|
|
@classmethod
|
|
def write(cls, cassette_path, data):
|
|
dirname, filename = os.path.split(cassette_path)
|
|
if dirname and not os.path.exists(dirname):
|
|
os.makedirs(dirname)
|
|
cls._secure_write(cassette_path, data)
|