|
|
|
|
@ -4,10 +4,11 @@
|
|
|
|
|
# Distributed under the terms of the Modified BSD License.
|
|
|
|
|
|
|
|
|
|
import base64
|
|
|
|
|
import errno
|
|
|
|
|
import io
|
|
|
|
|
import os
|
|
|
|
|
import glob
|
|
|
|
|
import shutil
|
|
|
|
|
from contextlib import contextmanager
|
|
|
|
|
|
|
|
|
|
from tornado import web
|
|
|
|
|
|
|
|
|
|
@ -16,9 +17,9 @@ from IPython import nbformat
|
|
|
|
|
from IPython.utils.io import atomic_writing
|
|
|
|
|
from IPython.utils.path import ensure_dir_exists
|
|
|
|
|
from IPython.utils.traitlets import Unicode, Bool, TraitError
|
|
|
|
|
from IPython.utils.py3compat import getcwd
|
|
|
|
|
from IPython.utils.py3compat import getcwd, str_to_unicode
|
|
|
|
|
from IPython.utils import tz
|
|
|
|
|
from IPython.html.utils import is_hidden, to_os_path, url_path_join
|
|
|
|
|
from IPython.html.utils import is_hidden, to_os_path, to_api_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FileContentsManager(ContentsManager):
|
|
|
|
|
@ -30,7 +31,38 @@ class FileContentsManager(ContentsManager):
|
|
|
|
|
return self.parent.notebook_dir
|
|
|
|
|
except AttributeError:
|
|
|
|
|
return getcwd()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
|
def perm_to_403(self, os_path=''):
|
|
|
|
|
"""context manager for turning permission errors into 403"""
|
|
|
|
|
try:
|
|
|
|
|
yield
|
|
|
|
|
except OSError as e:
|
|
|
|
|
if e.errno in {errno.EPERM, errno.EACCES}:
|
|
|
|
|
# make 403 error message without root prefix
|
|
|
|
|
# this may not work perfectly on unicode paths on Python 2,
|
|
|
|
|
# but nobody should be doing that anyway.
|
|
|
|
|
if not os_path:
|
|
|
|
|
os_path = str_to_unicode(e.filename or 'unknown file')
|
|
|
|
|
path = to_api_path(os_path, self.root_dir)
|
|
|
|
|
raise web.HTTPError(403, u'Permission denied: %s' % path)
|
|
|
|
|
else:
|
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
|
def open(self, os_path, *args, **kwargs):
|
|
|
|
|
"""wrapper around io.open that turns permission errors into 403"""
|
|
|
|
|
with self.perm_to_403(os_path):
|
|
|
|
|
with io.open(os_path, *args, **kwargs) as f:
|
|
|
|
|
yield f
|
|
|
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
|
def open_w(self, os_path, *args, **kwargs):
|
|
|
|
|
"""wrapper around atomic_writing that turns permission errors into 403"""
|
|
|
|
|
with self.perm_to_403(os_path):
|
|
|
|
|
with atomic_writing(os_path, *args, **kwargs) as f:
|
|
|
|
|
yield f
|
|
|
|
|
|
|
|
|
|
save_script = Bool(False, config=True, help='DEPRECATED, IGNORED')
|
|
|
|
|
def _save_script_changed(self):
|
|
|
|
|
self.log.warn("""
|
|
|
|
|
@ -172,6 +204,11 @@ class FileContentsManager(ContentsManager):
|
|
|
|
|
model['created'] = created
|
|
|
|
|
model['content'] = None
|
|
|
|
|
model['format'] = None
|
|
|
|
|
try:
|
|
|
|
|
model['writable'] = os.access(os_path, os.W_OK)
|
|
|
|
|
except OSError:
|
|
|
|
|
self.log.error("Failed to check write permissions on %s", os_path)
|
|
|
|
|
model['writable'] = False
|
|
|
|
|
return model
|
|
|
|
|
|
|
|
|
|
def _dir_model(self, path, content=True):
|
|
|
|
|
@ -181,7 +218,7 @@ class FileContentsManager(ContentsManager):
|
|
|
|
|
"""
|
|
|
|
|
os_path = self._get_os_path(path)
|
|
|
|
|
|
|
|
|
|
four_o_four = u'directory does not exist: %r' % os_path
|
|
|
|
|
four_o_four = u'directory does not exist: %r' % path
|
|
|
|
|
|
|
|
|
|
if not os.path.isdir(os_path):
|
|
|
|
|
raise web.HTTPError(404, four_o_four)
|
|
|
|
|
@ -232,7 +269,7 @@ class FileContentsManager(ContentsManager):
|
|
|
|
|
if not os.path.isfile(os_path):
|
|
|
|
|
# could be FIFO
|
|
|
|
|
raise web.HTTPError(400, "Cannot get content of non-file %s" % os_path)
|
|
|
|
|
with io.open(os_path, 'rb') as f:
|
|
|
|
|
with self.open(os_path, 'rb') as f:
|
|
|
|
|
bcontent = f.read()
|
|
|
|
|
|
|
|
|
|
if format != 'base64':
|
|
|
|
|
@ -261,7 +298,7 @@ class FileContentsManager(ContentsManager):
|
|
|
|
|
model['type'] = 'notebook'
|
|
|
|
|
if content:
|
|
|
|
|
os_path = self._get_os_path(path)
|
|
|
|
|
with io.open(os_path, 'r', encoding='utf-8') as f:
|
|
|
|
|
with self.open(os_path, 'r', encoding='utf-8') as f:
|
|
|
|
|
try:
|
|
|
|
|
nb = nbformat.read(f, as_version=4)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
@ -321,7 +358,7 @@ class FileContentsManager(ContentsManager):
|
|
|
|
|
|
|
|
|
|
self.check_and_sign(nb, path)
|
|
|
|
|
|
|
|
|
|
with atomic_writing(os_path, encoding='utf-8') as f:
|
|
|
|
|
with self.open_w(os_path, encoding='utf-8') as f:
|
|
|
|
|
nbformat.write(nb, f, version=nbformat.NO_CONVERT)
|
|
|
|
|
|
|
|
|
|
def _save_file(self, os_path, model, path=''):
|
|
|
|
|
@ -338,7 +375,7 @@ class FileContentsManager(ContentsManager):
|
|
|
|
|
bcontent = base64.decodestring(b64_bytes)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
raise web.HTTPError(400, u'Encoding error saving %s: %s' % (os_path, e))
|
|
|
|
|
with atomic_writing(os_path, text=False) as f:
|
|
|
|
|
with self.open_w(os_path, text=False) as f:
|
|
|
|
|
f.write(bcontent)
|
|
|
|
|
|
|
|
|
|
def _save_directory(self, os_path, model, path=''):
|
|
|
|
|
@ -346,7 +383,8 @@ class FileContentsManager(ContentsManager):
|
|
|
|
|
if is_hidden(os_path, self.root_dir):
|
|
|
|
|
raise web.HTTPError(400, u'Cannot create hidden directory %r' % os_path)
|
|
|
|
|
if not os.path.exists(os_path):
|
|
|
|
|
os.mkdir(os_path)
|
|
|
|
|
with self.perm_to_403():
|
|
|
|
|
os.mkdir(os_path)
|
|
|
|
|
elif not os.path.isdir(os_path):
|
|
|
|
|
raise web.HTTPError(400, u'Not a directory: %s' % (os_path))
|
|
|
|
|
else:
|
|
|
|
|
@ -379,7 +417,8 @@ class FileContentsManager(ContentsManager):
|
|
|
|
|
except web.HTTPError:
|
|
|
|
|
raise
|
|
|
|
|
except Exception as e:
|
|
|
|
|
raise web.HTTPError(400, u'Unexpected error while saving file: %s %s' % (os_path, e))
|
|
|
|
|
self.log.error(u'Error while saving file: %s %s', path, e, exc_info=True)
|
|
|
|
|
raise web.HTTPError(500, u'Unexpected error while saving file: %s %s' % (path, e))
|
|
|
|
|
|
|
|
|
|
validation_message = None
|
|
|
|
|
if model['type'] == 'notebook':
|
|
|
|
|
@ -423,14 +462,17 @@ class FileContentsManager(ContentsManager):
|
|
|
|
|
cp_path = self.get_checkpoint_path(checkpoint_id, path)
|
|
|
|
|
if os.path.isfile(cp_path):
|
|
|
|
|
self.log.debug("Unlinking checkpoint %s", cp_path)
|
|
|
|
|
os.unlink(cp_path)
|
|
|
|
|
with self.perm_to_403():
|
|
|
|
|
rm(cp_path)
|
|
|
|
|
|
|
|
|
|
if os.path.isdir(os_path):
|
|
|
|
|
self.log.debug("Removing directory %s", os_path)
|
|
|
|
|
shutil.rmtree(os_path)
|
|
|
|
|
with self.perm_to_403():
|
|
|
|
|
shutil.rmtree(os_path)
|
|
|
|
|
else:
|
|
|
|
|
self.log.debug("Unlinking file %s", os_path)
|
|
|
|
|
rm(os_path)
|
|
|
|
|
with self.perm_to_403():
|
|
|
|
|
rm(os_path)
|
|
|
|
|
|
|
|
|
|
def rename(self, old_path, new_path):
|
|
|
|
|
"""Rename a file."""
|
|
|
|
|
@ -448,7 +490,10 @@ class FileContentsManager(ContentsManager):
|
|
|
|
|
|
|
|
|
|
# Move the file
|
|
|
|
|
try:
|
|
|
|
|
shutil.move(old_os_path, new_os_path)
|
|
|
|
|
with self.perm_to_403():
|
|
|
|
|
shutil.move(old_os_path, new_os_path)
|
|
|
|
|
except web.HTTPError:
|
|
|
|
|
raise
|
|
|
|
|
except Exception as e:
|
|
|
|
|
raise web.HTTPError(500, u'Unknown error renaming file: %s %s' % (old_path, e))
|
|
|
|
|
|
|
|
|
|
@ -460,7 +505,8 @@ class FileContentsManager(ContentsManager):
|
|
|
|
|
new_cp_path = self.get_checkpoint_path(checkpoint_id, new_path)
|
|
|
|
|
if os.path.isfile(old_cp_path):
|
|
|
|
|
self.log.debug("Renaming checkpoint %s -> %s", old_cp_path, new_cp_path)
|
|
|
|
|
shutil.move(old_cp_path, new_cp_path)
|
|
|
|
|
with self.perm_to_403():
|
|
|
|
|
shutil.move(old_cp_path, new_cp_path)
|
|
|
|
|
|
|
|
|
|
# Checkpoint-related utilities
|
|
|
|
|
|
|
|
|
|
@ -477,7 +523,8 @@ class FileContentsManager(ContentsManager):
|
|
|
|
|
)
|
|
|
|
|
os_path = self._get_os_path(path=parent)
|
|
|
|
|
cp_dir = os.path.join(os_path, self.checkpoint_dir)
|
|
|
|
|
ensure_dir_exists(cp_dir)
|
|
|
|
|
with self.perm_to_403():
|
|
|
|
|
ensure_dir_exists(cp_dir)
|
|
|
|
|
cp_path = os.path.join(cp_dir, filename)
|
|
|
|
|
return cp_path
|
|
|
|
|
|
|
|
|
|
@ -505,7 +552,8 @@ class FileContentsManager(ContentsManager):
|
|
|
|
|
checkpoint_id = u"checkpoint"
|
|
|
|
|
cp_path = self.get_checkpoint_path(checkpoint_id, path)
|
|
|
|
|
self.log.debug("creating checkpoint for %s", path)
|
|
|
|
|
self._copy(src_path, cp_path)
|
|
|
|
|
with self.perm_to_403():
|
|
|
|
|
self._copy(src_path, cp_path)
|
|
|
|
|
|
|
|
|
|
# return the checkpoint info
|
|
|
|
|
return self.get_checkpoint_model(checkpoint_id, path)
|
|
|
|
|
@ -537,10 +585,11 @@ class FileContentsManager(ContentsManager):
|
|
|
|
|
)
|
|
|
|
|
# ensure notebook is readable (never restore from an unreadable notebook)
|
|
|
|
|
if cp_path.endswith('.ipynb'):
|
|
|
|
|
with io.open(cp_path, 'r', encoding='utf-8') as f:
|
|
|
|
|
with self.open(cp_path, 'r', encoding='utf-8') as f:
|
|
|
|
|
nbformat.read(f, as_version=4)
|
|
|
|
|
self._copy(cp_path, nb_path)
|
|
|
|
|
self.log.debug("copying %s -> %s", cp_path, nb_path)
|
|
|
|
|
with self.perm_to_403():
|
|
|
|
|
self._copy(cp_path, nb_path)
|
|
|
|
|
|
|
|
|
|
def delete_checkpoint(self, checkpoint_id, path):
|
|
|
|
|
"""delete a file's checkpoint"""
|
|
|
|
|
|