Save without atomic writing and configuration (fix #739)

pull/747/head
Pierre Gerold 10 years ago
parent b10326e93c
commit f849e310c8

@ -22,6 +22,8 @@ import nbformat
from ipython_genutils.py3compat import str_to_unicode
from traitlets.config import Configurable
from traitlets import Bool
def copy2_safe(src, dst, log=None):
"""copy src to dst
@ -39,24 +41,24 @@ def copy2_safe(src, dst, log=None):
@contextmanager
def atomic_writing(path, text=True, encoding='utf-8', log=None, **kwargs):
"""Context manager to write to a file only if the entire write is successful.
This works by copying the previous file contents to a temporary file in the
same directory, and renaming that file back to the target if the context
exits with an error. If the context is successful, the new data is synced to
disk and the temporary file is removed.
Parameters
----------
path : str
The target file to write to.
text : bool, optional
Whether to open the file in text mode (i.e. to write unicode). Default is
True.
encoding : str, optional
The encoding to use for files opened in text mode. Default is UTF-8.
**kwargs
Passed to :func:`io.open`.
"""
@ -100,7 +102,55 @@ def atomic_writing(path, text=True, encoding='utf-8', log=None, **kwargs):
os.remove(tmp_path)
class FileManagerMixin(object):
@contextmanager
def _simple_writing(path, text=True, encoding='utf-8', **kwargs):
"""Context manager to write file without doing atomic writing
( for weird filesystem eg: nfs).
Parameters
----------
path : str
The target file to write to.
text : bool, optional
Whether to open the file in text mode (i.e. to write unicode). Default is
True.
encoding : str, optional
The encoding to use for files opened in text mode. Default is UTF-8.
**kwargs
Passed to :func:`io.open`.
"""
# realpath doesn't work on Windows: http://bugs.python.org/issue9949
# Luckily, we only need to resolve the file itself being a symlink, not
# any of its directories, so this will suffice:
if os.path.islink(path):
path = os.path.join(os.path.dirname(path), os.readlink(path))
if text:
# Make sure that text files have Unix linefeeds by default
kwargs.setdefault('newline', '\n')
fileobj = io.open(path, 'w', encoding=encoding, **kwargs)
else:
fileobj = io.open(path, 'wb', **kwargs)
try:
yield fileobj
except:
fileobj.close()
raise
# Flush to disk
fileobj.flush()
os.fsync(fileobj.fileno())
fileobj.close()
class FileManagerMixin(Configurable):
"""
Mixin for ContentsAPI classes that interact with the filesystem.
@ -119,6 +169,9 @@ class FileManagerMixin(object):
log : logging.Logger
"""
use_atomic_writing = Bool(True, config=True, help="""flag to deactivate
atomic writing on slow (like networked) file system.""")
@contextmanager
def open(self, os_path, *args, **kwargs):
"""wrapper around io.open that turns permission errors into 403"""
@ -130,8 +183,12 @@ class FileManagerMixin(object):
def atomic_writing(self, os_path, *args, **kwargs):
"""wrapper around atomic_writing that turns permission errors to 403"""
with self.perm_to_403(os_path):
with atomic_writing(os_path, *args, log=self.log, **kwargs) as f:
yield f
if self.use_atomic_writing:
with atomic_writing(os_path, *args, log=self.log, **kwargs) as f:
yield f
else:
with simple_writing(os_path, *args, log=self.log, **kwargs) as f:
yield f
@contextmanager
def perm_to_403(self, os_path=''):

Loading…
Cancel
Save