filemanager.py
619 lines
| 21.9 KiB
| text/x-python
|
PythonLexer
MinRK
|
r17524 | """A contents manager that uses the local file system for storage.""" | ||
Brian E. Granger
|
r4609 | |||
MinRK
|
r16486 | # Copyright (c) IPython Development Team. | ||
# Distributed under the terms of the Modified BSD License. | ||||
Brian E. Granger
|
r4484 | |||
MinRK
|
r17525 | import base64 | ||
Min RK
|
r19005 | import errno | ||
Thomas Kluyver
|
r6030 | import io | ||
Brian E. Granger
|
r4484 | import os | ||
MinRK
|
r10497 | import shutil | ||
Min RK
|
r19005 | from contextlib import contextmanager | ||
Thomas Kluyver
|
r19011 | import mimetypes | ||
Zachary Sailer
|
r12984 | |||
Brian E. Granger
|
r4484 | from tornado import web | ||
MinRK
|
r17524 | from .manager import ContentsManager | ||
MinRK
|
r18607 | from IPython import nbformat | ||
Thomas Kluyver
|
r17557 | from IPython.utils.io import atomic_writing | ||
MinRK
|
r16486 | from IPython.utils.path import ensure_dir_exists | ||
Thomas Kluyver
|
r15526 | from IPython.utils.traitlets import Unicode, Bool, TraitError | ||
Min RK
|
r19005 | from IPython.utils.py3compat import getcwd, str_to_unicode | ||
MinRK
|
r11145 | from IPython.utils import tz | ||
Min RK
|
r19005 | from IPython.html.utils import is_hidden, to_os_path, to_api_path | ||
Brian E. Granger
|
r4484 | |||
MinRK
|
r17524 | class FileContentsManager(ContentsManager): | ||
MinRK
|
r17523 | |||
Thomas Kluyver
|
r18970 | root_dir = Unicode(config=True) | ||
def _root_dir_default(self): | ||||
Thomas Kluyver
|
r18971 | try: | ||
return self.parent.notebook_dir | ||||
except AttributeError: | ||||
return getcwd() | ||||
Min RK
|
r19005 | |||
@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 | ||||
Min RK
|
r19006 | def atomic_writing(self, os_path, *args, **kwargs): | ||
Min RK
|
r19005 | """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 | ||||
MinRK
|
r17537 | save_script = Bool(False, config=True, help='DEPRECATED, IGNORED') | ||
def _save_script_changed(self): | ||||
self.log.warn(""" | ||||
Automatically saving notebooks as scripts has been removed. | ||||
Use `ipython nbconvert --to python [notebook]` instead. | ||||
""") | ||||
MinRK
|
r17524 | def _root_dir_changed(self, name, old, new): | ||
"""Do a bit of validation of the root_dir.""" | ||||
MinRK
|
r15420 | if not os.path.isabs(new): | ||
# If we receive a non-absolute path, make it absolute. | ||||
MinRK
|
r17524 | self.root_dir = os.path.abspath(new) | ||
MinRK
|
r15420 | return | ||
MinRK
|
r17537 | if not os.path.isdir(new): | ||
MinRK
|
r17524 | raise TraitError("%r is not a directory" % new) | ||
MinRK
|
r17523 | |||
MinRK
|
r16447 | checkpoint_dir = Unicode('.ipynb_checkpoints', config=True, | ||
MinRK
|
r17529 | help="""The directory name in which to keep file checkpoints | ||
MinRK
|
r17523 | |||
MinRK
|
r17529 | This is a path relative to the file's own directory. | ||
MinRK
|
r17523 | |||
MinRK
|
r16447 | By default, it is .ipynb_checkpoints | ||
MinRK
|
r10497 | """ | ||
) | ||||
MinRK
|
r17523 | |||
MinRK
|
r15827 | def _copy(self, src, dest): | ||
"""copy src to dest | ||||
MinRK
|
r17523 | |||
MinRK
|
r15865 | like shutil.copy2, but log errors in copystat | ||
MinRK
|
r15827 | """ | ||
MinRK
|
r15865 | shutil.copyfile(src, dest) | ||
try: | ||||
shutil.copystat(src, dest) | ||||
except OSError as e: | ||||
self.log.debug("copystat on %s failed", dest, exc_info=True) | ||||
MinRK
|
r17523 | |||
Min RK
|
r18758 | def _get_os_path(self, path): | ||
"""Given an API path, return its file system path. | ||||
MinRK
|
r17525 | |||
Parameters | ||||
---------- | ||||
path : string | ||||
MinRK
|
r17535 | The relative API path to the named file. | ||
MinRK
|
r17525 | |||
Returns | ||||
------- | ||||
path : string | ||||
MinRK
|
r18749 | Native, absolute OS path to for a file. | ||
MinRK
|
r17525 | """ | ||
return to_os_path(path, self.root_dir) | ||||
Zachary Sailer
|
r12997 | |||
MinRK
|
r18749 | def dir_exists(self, path): | ||
MinRK
|
r17525 | """Does the API-style path refer to an extant directory? | ||
MinRK
|
r17523 | |||
MinRK
|
r17535 | API-style wrapper for os.path.isdir | ||
MinRK
|
r13070 | Parameters | ||
---------- | ||||
path : string | ||||
The path to check. This is an API path (`/` separated, | ||||
MinRK
|
r17524 | relative to root_dir). | ||
MinRK
|
r17523 | |||
MinRK
|
r13070 | Returns | ||
------- | ||||
exists : bool | ||||
Whether the path is indeed a directory. | ||||
""" | ||||
MinRK
|
r13078 | path = path.strip('/') | ||
MinRK
|
r15420 | os_path = self._get_os_path(path=path) | ||
MinRK
|
r13070 | return os.path.isdir(os_path) | ||
Brian E. Granger
|
r15097 | |||
def is_hidden(self, path): | ||||
"""Does the API style path correspond to a hidden directory or file? | ||||
MinRK
|
r17523 | |||
Brian E. Granger
|
r15097 | Parameters | ||
---------- | ||||
path : string | ||||
The path to check. This is an API path (`/` separated, | ||||
MinRK
|
r17524 | relative to root_dir). | ||
MinRK
|
r17523 | |||
Brian E. Granger
|
r15097 | Returns | ||
------- | ||||
MinRK
|
r18749 | hidden : bool | ||
Whether the path exists and is hidden. | ||||
Brian E. Granger
|
r15097 | """ | ||
path = path.strip('/') | ||||
MinRK
|
r15420 | os_path = self._get_os_path(path=path) | ||
MinRK
|
r17524 | return is_hidden(os_path, self.root_dir) | ||
Brian E. Granger
|
r15097 | |||
MinRK
|
r18749 | def file_exists(self, path): | ||
MinRK
|
r17525 | """Returns True if the file exists, else returns False. | ||
MinRK
|
r13070 | |||
MinRK
|
r17535 | API-style wrapper for os.path.isfile | ||
MinRK
|
r13070 | Parameters | ||
---------- | ||||
path : string | ||||
MinRK
|
r18749 | The relative path to the file (with '/' as separator) | ||
MinRK
|
r13070 | |||
Returns | ||||
------- | ||||
MinRK
|
r17535 | exists : bool | ||
Whether the file exists. | ||||
MinRK
|
r13070 | """ | ||
MinRK
|
r17525 | path = path.strip('/') | ||
Min RK
|
r18758 | os_path = self._get_os_path(path) | ||
return os.path.isfile(os_path) | ||||
MinRK
|
r13070 | |||
MinRK
|
r18749 | def exists(self, path): | ||
"""Returns True if the path exists, else returns False. | ||||
Zachary Sailer
|
r13046 | |||
MinRK
|
r17535 | API-style wrapper for os.path.exists | ||
Zachary Sailer
|
r13032 | Parameters | ||
---------- | ||||
path : string | ||||
MinRK
|
r18749 | The API path to the file (with '/' as separator) | ||
Zachary Sailer
|
r13046 | |||
Zachary Sailer
|
r13032 | Returns | ||
------- | ||||
MinRK
|
r17535 | exists : bool | ||
Whether the target exists. | ||||
Zachary Sailer
|
r13032 | """ | ||
MinRK
|
r13078 | path = path.strip('/') | ||
MinRK
|
r18749 | os_path = self._get_os_path(path=path) | ||
MinRK
|
r17525 | return os.path.exists(os_path) | ||
Zachary Sailer
|
r12984 | |||
MinRK
|
r18749 | def _base_model(self, path): | ||
MinRK
|
r17525 | """Build the common base of a contents model""" | ||
MinRK
|
r18749 | os_path = self._get_os_path(path) | ||
Brian E. Granger
|
r15069 | info = os.stat(os_path) | ||
last_modified = tz.utcfromtimestamp(info.st_mtime) | ||||
created = tz.utcfromtimestamp(info.st_ctime) | ||||
MinRK
|
r17529 | # Create the base model. | ||
MinRK
|
r17525 | model = {} | ||
MinRK
|
r18749 | model['name'] = path.rsplit('/', 1)[-1] | ||
Brian E. Granger
|
r15069 | model['path'] = path | ||
model['last_modified'] = last_modified | ||||
model['created'] = created | ||||
MinRK
|
r17525 | model['content'] = None | ||
model['format'] = None | ||||
Thomas Kluyver
|
r19011 | model['mimetype'] = None | ||
Min RK
|
r19005 | 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 | ||||
MinRK
|
r17525 | return model | ||
MinRK
|
r18749 | def _dir_model(self, path, content=True): | ||
MinRK
|
r17525 | """Build a model for a directory | ||
if content is requested, will include a listing of the directory | ||||
""" | ||||
MinRK
|
r18749 | os_path = self._get_os_path(path) | ||
MinRK
|
r17525 | |||
Min RK
|
r19005 | four_o_four = u'directory does not exist: %r' % path | ||
MinRK
|
r17537 | |||
MinRK
|
r17525 | if not os.path.isdir(os_path): | ||
MinRK
|
r17537 | raise web.HTTPError(404, four_o_four) | ||
MinRK
|
r17525 | elif is_hidden(os_path, self.root_dir): | ||
MinRK
|
r17537 | self.log.info("Refusing to serve hidden directory %r, via 404 Error", | ||
os_path | ||||
) | ||||
raise web.HTTPError(404, four_o_four) | ||||
MinRK
|
r17525 | |||
MinRK
|
r18749 | model = self._base_model(path) | ||
Brian E. Granger
|
r15069 | model['type'] = 'directory' | ||
MinRK
|
r17525 | if content: | ||
MinRK
|
r17529 | model['content'] = contents = [] | ||
Min RK
|
r18758 | os_dir = self._get_os_path(path) | ||
for name in os.listdir(os_dir): | ||||
os_path = os.path.join(os_dir, name) | ||||
MinRK
|
r17710 | # skip over broken symlinks in listing | ||
if not os.path.exists(os_path): | ||||
self.log.warn("%s doesn't exist", os_path) | ||||
continue | ||||
Min RK
|
r18758 | elif not os.path.isfile(os_path) and not os.path.isdir(os_path): | ||
self.log.debug("%s not a regular file", os_path) | ||||
continue | ||||
MinRK
|
r17525 | if self.should_list(name) and not is_hidden(os_path, self.root_dir): | ||
Thomas Kluyver
|
r18791 | contents.append(self.get( | ||
MinRK
|
r18749 | path='%s/%s' % (path, name), | ||
content=False) | ||||
) | ||||
MinRK
|
r17525 | |||
MinRK
|
r17527 | model['format'] = 'json' | ||
MinRK
|
r17525 | |||
Brian E. Granger
|
r15069 | return model | ||
Thomas Kluyver
|
r18788 | def _file_model(self, path, content=True, format=None): | ||
MinRK
|
r17525 | """Build a model for a file | ||
MinRK
|
r17523 | |||
MinRK
|
r17525 | if content is requested, include the file contents. | ||
Thomas Kluyver
|
r18788 | |||
format: | ||||
If 'text', the contents will be decoded as UTF-8. | ||||
If 'base64', the raw bytes contents will be encoded as base64. | ||||
If not specified, try to decode as UTF-8, and fall back to base64 | ||||
MinRK
|
r17525 | """ | ||
MinRK
|
r18749 | model = self._base_model(path) | ||
MinRK
|
r17525 | model['type'] = 'file' | ||
Thomas Kluyver
|
r19011 | |||
os_path = self._get_os_path(path) | ||||
model['mimetype'] = mimetypes.guess_type(os_path)[0] or 'text/plain' | ||||
MinRK
|
r17525 | if content: | ||
Min RK
|
r18758 | if not os.path.isfile(os_path): | ||
# could be FIFO | ||||
raise web.HTTPError(400, "Cannot get content of non-file %s" % os_path) | ||||
Min RK
|
r19005 | with self.open(os_path, 'rb') as f: | ||
MinRK
|
r17537 | bcontent = f.read() | ||
Thomas Kluyver
|
r18788 | |||
if format != 'base64': | ||||
try: | ||||
model['content'] = bcontent.decode('utf8') | ||||
except UnicodeError as e: | ||||
if format == 'text': | ||||
raise web.HTTPError(400, "%s is not UTF-8 encoded" % path) | ||||
else: | ||||
model['format'] = 'text' | ||||
if model['content'] is None: | ||||
MinRK
|
r17537 | model['content'] = base64.encodestring(bcontent).decode('ascii') | ||
model['format'] = 'base64' | ||||
Thomas Kluyver
|
r18788 | |||
MinRK
|
r17525 | return model | ||
MinRK
|
r17523 | |||
MinRK
|
r17525 | |||
MinRK
|
r18749 | def _notebook_model(self, path, content=True): | ||
MinRK
|
r17525 | """Build a notebook model | ||
if content is requested, the notebook content will be populated | ||||
as a JSON structure (not double-serialized) | ||||
Zachary Sailer
|
r13048 | """ | ||
MinRK
|
r18749 | model = self._base_model(path) | ||
MinRK
|
r17525 | model['type'] = 'notebook' | ||
if content: | ||||
MinRK
|
r18749 | os_path = self._get_os_path(path) | ||
Min RK
|
r19005 | with self.open(os_path, 'r', encoding='utf-8') as f: | ||
MinRK
|
r17525 | try: | ||
MinRK
|
r18607 | nb = nbformat.read(f, as_version=4) | ||
MinRK
|
r17525 | except Exception as e: | ||
MinRK
|
r18596 | raise web.HTTPError(400, u"Unreadable Notebook: %s %r" % (os_path, e)) | ||
MinRK
|
r18749 | self.mark_trusted_cells(nb, path) | ||
MinRK
|
r17525 | model['content'] = nb | ||
model['format'] = 'json' | ||||
MinRK
|
r18249 | self.validate_notebook_model(model) | ||
MinRK
|
r17525 | return model | ||
Zachary Sailer
|
r13046 | |||
Thomas Kluyver
|
r18791 | def get(self, path, content=True, type_=None, format=None): | ||
MinRK
|
r18749 | """ Takes a path for an entity and returns its model | ||
MinRK
|
r17523 | |||
Zachary Sailer
|
r13048 | Parameters | ||
---------- | ||||
path : str | ||||
MinRK
|
r17535 | the API path that describes the relative path for the target | ||
Thomas Kluyver
|
r18781 | content : bool | ||
Whether to include the contents in the reply | ||||
type_ : str, optional | ||||
The requested type - 'file', 'notebook', or 'directory'. | ||||
Thomas Kluyver
|
r18790 | Will raise HTTPError 400 if the content doesn't match. | ||
Thomas Kluyver
|
r18788 | format : str, optional | ||
The requested format for file contents. 'text' or 'base64'. | ||||
Ignored if this returns a notebook or directory model. | ||||
MinRK
|
r17523 | |||
Zachary Sailer
|
r13048 | Returns | ||
------- | ||||
model : dict | ||||
MinRK
|
r17525 | the contents model. If content=True, returns the contents | ||
of the file or directory as well. | ||||
Zachary Sailer
|
r13048 | """ | ||
MinRK
|
r13078 | path = path.strip('/') | ||
MinRK
|
r17525 | |||
MinRK
|
r18749 | if not self.exists(path): | ||
raise web.HTTPError(404, u'No such file or directory: %s' % path) | ||||
MinRK
|
r17525 | |||
MinRK
|
r18749 | os_path = self._get_os_path(path) | ||
MinRK
|
r17525 | if os.path.isdir(os_path): | ||
Thomas Kluyver
|
r18781 | if type_ not in (None, 'directory'): | ||
raise web.HTTPError(400, | ||||
u'%s is a directory, not a %s' % (path, type_)) | ||||
MinRK
|
r18749 | model = self._dir_model(path, content=content) | ||
Thomas Kluyver
|
r18781 | elif type_ == 'notebook' or (type_ is None and path.endswith('.ipynb')): | ||
MinRK
|
r18749 | model = self._notebook_model(path, content=content) | ||
MinRK
|
r17525 | else: | ||
Thomas Kluyver
|
r18781 | if type_ == 'directory': | ||
raise web.HTTPError(400, | ||||
u'%s is not a directory') | ||||
Thomas Kluyver
|
r18788 | model = self._file_model(path, content=content, format=format) | ||
Zachary Sailer
|
r13046 | return model | ||
Zachary Sailer
|
r12997 | |||
MinRK
|
r18749 | def _save_notebook(self, os_path, model, path=''): | ||
MinRK
|
r17529 | """save a notebook file""" | ||
MinRK
|
r17527 | # Save the notebook file | ||
MinRK
|
r18607 | nb = nbformat.from_dict(model['content']) | ||
MinRK
|
r17527 | |||
MinRK
|
r18749 | self.check_and_sign(nb, path) | ||
MinRK
|
r17527 | |||
Min RK
|
r19006 | with self.atomic_writing(os_path, encoding='utf-8') as f: | ||
Min RK
|
r18613 | nbformat.write(nb, f, version=nbformat.NO_CONVERT) | ||
MinRK
|
r17527 | |||
MinRK
|
r18749 | def _save_file(self, os_path, model, path=''): | ||
MinRK
|
r17529 | """save a non-notebook file""" | ||
MinRK
|
r17527 | fmt = model.get('format', None) | ||
if fmt not in {'text', 'base64'}: | ||||
raise web.HTTPError(400, "Must specify format of file contents as 'text' or 'base64'") | ||||
try: | ||||
content = model['content'] | ||||
if fmt == 'text': | ||||
bcontent = content.encode('utf8') | ||||
else: | ||||
b64_bytes = content.encode('ascii') | ||||
bcontent = base64.decodestring(b64_bytes) | ||||
except Exception as e: | ||||
raise web.HTTPError(400, u'Encoding error saving %s: %s' % (os_path, e)) | ||||
Min RK
|
r19006 | with self.atomic_writing(os_path, text=False) as f: | ||
MinRK
|
r17527 | f.write(bcontent) | ||
MinRK
|
r18749 | def _save_directory(self, os_path, model, path=''): | ||
MinRK
|
r17529 | """create a directory""" | ||
MinRK
|
r17537 | if is_hidden(os_path, self.root_dir): | ||
raise web.HTTPError(400, u'Cannot create hidden directory %r' % os_path) | ||||
MinRK
|
r17527 | if not os.path.exists(os_path): | ||
Min RK
|
r19005 | with self.perm_to_403(): | ||
os.mkdir(os_path) | ||||
MinRK
|
r17527 | elif not os.path.isdir(os_path): | ||
raise web.HTTPError(400, u'Not a directory: %s' % (os_path)) | ||||
MinRK
|
r17537 | else: | ||
self.log.debug("Directory %r already exists", os_path) | ||||
MinRK
|
r17527 | |||
MinRK
|
r18749 | def save(self, model, path=''): | ||
MinRK
|
r17527 | """Save the file model and return the model with no content.""" | ||
MinRK
|
r13078 | path = path.strip('/') | ||
Zachary Sailer
|
r13046 | |||
MinRK
|
r17527 | if 'type' not in model: | ||
raise web.HTTPError(400, u'No file type provided') | ||||
MinRK
|
r17532 | if 'content' not in model and model['type'] != 'directory': | ||
raise web.HTTPError(400, u'No file content provided') | ||||
Brian E. Granger
|
r15093 | |||
MinRK
|
r13245 | # One checkpoint should always exist | ||
MinRK
|
r18749 | if self.file_exists(path) and not self.list_checkpoints(path): | ||
self.create_checkpoint(path) | ||||
Zachary Sailer
|
r13046 | |||
MinRK
|
r18749 | os_path = self._get_os_path(path) | ||
MinRK
|
r17527 | self.log.debug("Saving %s", os_path) | ||
Brian E. Granger
|
r4484 | try: | ||
MinRK
|
r17527 | if model['type'] == 'notebook': | ||
MinRK
|
r18749 | self._save_notebook(os_path, model, path) | ||
MinRK
|
r17527 | elif model['type'] == 'file': | ||
MinRK
|
r18749 | self._save_file(os_path, model, path) | ||
MinRK
|
r17527 | elif model['type'] == 'directory': | ||
MinRK
|
r18749 | self._save_directory(os_path, model, path) | ||
MinRK
|
r17527 | else: | ||
raise web.HTTPError(400, "Unhandled contents type: %s" % model['type']) | ||||
except web.HTTPError: | ||||
raise | ||||
MinRK
|
r5709 | except Exception as e: | ||
Min RK
|
r19005 | 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)) | ||||
Brian Granger
|
r8180 | |||
MinRK
|
r18249 | validation_message = None | ||
if model['type'] == 'notebook': | ||||
self.validate_notebook_model(model) | ||||
validation_message = model.get('message', None) | ||||
Thomas Kluyver
|
r18791 | model = self.get(path, content=False) | ||
MinRK
|
r18249 | if validation_message: | ||
model['message'] = validation_message | ||||
Zachary Sailer
|
r13046 | return model | ||
MinRK
|
r18749 | def update(self, model, path): | ||
"""Update the file's path | ||||
MinRK
|
r17535 | |||
For use in PATCH requests, to enable renaming a file without | ||||
re-uploading its contents. Only used for renaming at the moment. | ||||
""" | ||||
MinRK
|
r13078 | path = path.strip('/') | ||
new_path = model.get('path', path).strip('/') | ||||
MinRK
|
r18749 | if path != new_path: | ||
self.rename(path, new_path) | ||||
Thomas Kluyver
|
r18791 | model = self.get(new_path, content=False) | ||
Zachary Sailer
|
r13046 | return model | ||
MinRK
|
r18749 | def delete(self, path): | ||
"""Delete file at path.""" | ||||
MinRK
|
r13078 | path = path.strip('/') | ||
MinRK
|
r18749 | os_path = self._get_os_path(path) | ||
MinRK
|
r17530 | rm = os.unlink | ||
if os.path.isdir(os_path): | ||||
listing = os.listdir(os_path) | ||||
# don't delete non-empty directories (checkpoints dir doesn't count) | ||||
MinRK
|
r17537 | if listing and listing != [self.checkpoint_dir]: | ||
MinRK
|
r17530 | raise web.HTTPError(400, u'Directory %s not empty' % os_path) | ||
elif not os.path.isfile(os_path): | ||||
MinRK
|
r17524 | raise web.HTTPError(404, u'File does not exist: %s' % os_path) | ||
MinRK
|
r17523 | |||
MinRK
|
r10497 | # clear checkpoints | ||
MinRK
|
r18749 | for checkpoint in self.list_checkpoints(path): | ||
MinRK
|
r13122 | checkpoint_id = checkpoint['id'] | ||
MinRK
|
r18749 | cp_path = self.get_checkpoint_path(checkpoint_id, path) | ||
Zachary Sailer
|
r13046 | if os.path.isfile(cp_path): | ||
self.log.debug("Unlinking checkpoint %s", cp_path) | ||||
Min RK
|
r19005 | with self.perm_to_403(): | ||
rm(cp_path) | ||||
MinRK
|
r17523 | |||
MinRK
|
r17530 | if os.path.isdir(os_path): | ||
self.log.debug("Removing directory %s", os_path) | ||||
Min RK
|
r19005 | with self.perm_to_403(): | ||
shutil.rmtree(os_path) | ||||
MinRK
|
r17530 | else: | ||
self.log.debug("Unlinking file %s", os_path) | ||||
Min RK
|
r19005 | with self.perm_to_403(): | ||
rm(os_path) | ||||
Brian E. Granger
|
r4484 | |||
MinRK
|
r18749 | def rename(self, old_path, new_path): | ||
MinRK
|
r17524 | """Rename a file.""" | ||
MinRK
|
r13078 | old_path = old_path.strip('/') | ||
new_path = new_path.strip('/') | ||||
MinRK
|
r18749 | if new_path == old_path: | ||
Zachary Sailer
|
r13046 | return | ||
MinRK
|
r17523 | |||
MinRK
|
r18749 | new_os_path = self._get_os_path(new_path) | ||
old_os_path = self._get_os_path(old_path) | ||||
Zachary Sailer
|
r13046 | |||
# Should we proceed with the move? | ||||
Min RK
|
r18758 | if os.path.exists(new_os_path): | ||
raise web.HTTPError(409, u'File already exists: %s' % new_path) | ||||
Zachary Sailer
|
r13046 | |||
MinRK
|
r17524 | # Move the file | ||
Zachary Sailer
|
r13046 | try: | ||
Min RK
|
r19005 | with self.perm_to_403(): | ||
shutil.move(old_os_path, new_os_path) | ||||
except web.HTTPError: | ||||
raise | ||||
Brian E. Granger
|
r13051 | except Exception as e: | ||
Min RK
|
r18758 | raise web.HTTPError(500, u'Unknown error renaming file: %s %s' % (old_path, e)) | ||
Zachary Sailer
|
r13046 | |||
# Move the checkpoints | ||||
MinRK
|
r18749 | old_checkpoints = self.list_checkpoints(old_path) | ||
Zachary Sailer
|
r13046 | for cp in old_checkpoints: | ||
MinRK
|
r13122 | checkpoint_id = cp['id'] | ||
MinRK
|
r18749 | old_cp_path = self.get_checkpoint_path(checkpoint_id, old_path) | ||
new_cp_path = self.get_checkpoint_path(checkpoint_id, new_path) | ||||
Zachary Sailer
|
r13046 | if os.path.isfile(old_cp_path): | ||
self.log.debug("Renaming checkpoint %s -> %s", old_cp_path, new_cp_path) | ||||
Min RK
|
r19005 | with self.perm_to_403(): | ||
shutil.move(old_cp_path, new_cp_path) | ||||
Zachary Sailer
|
r13046 | |||
MinRK
|
r10497 | # Checkpoint-related utilities | ||
MinRK
|
r17523 | |||
MinRK
|
r18749 | def get_checkpoint_path(self, checkpoint_id, path): | ||
Zachary Sailer
|
r13046 | """find the path to a checkpoint""" | ||
MinRK
|
r13078 | path = path.strip('/') | ||
MinRK
|
r18749 | parent, name = ('/' + path).rsplit('/', 1) | ||
parent = parent.strip('/') | ||||
MinRK
|
r17524 | basename, ext = os.path.splitext(name) | ||
MinRK
|
r10777 | filename = u"{name}-{checkpoint_id}{ext}".format( | ||
MinRK
|
r13244 | name=basename, | ||
MinRK
|
r10497 | checkpoint_id=checkpoint_id, | ||
MinRK
|
r17524 | ext=ext, | ||
MinRK
|
r10497 | ) | ||
MinRK
|
r18749 | os_path = self._get_os_path(path=parent) | ||
MinRK
|
r16447 | cp_dir = os.path.join(os_path, self.checkpoint_dir) | ||
Min RK
|
r19005 | with self.perm_to_403(): | ||
ensure_dir_exists(cp_dir) | ||||
MinRK
|
r16447 | cp_path = os.path.join(cp_dir, filename) | ||
Zachary Sailer
|
r13046 | return cp_path | ||
MinRK
|
r18749 | def get_checkpoint_model(self, checkpoint_id, path): | ||
MinRK
|
r10500 | """construct the info dict for a given checkpoint""" | ||
MinRK
|
r13078 | path = path.strip('/') | ||
MinRK
|
r18749 | cp_path = self.get_checkpoint_path(checkpoint_id, path) | ||
Zachary Sailer
|
r13046 | stats = os.stat(cp_path) | ||
MinRK
|
r11145 | last_modified = tz.utcfromtimestamp(stats.st_mtime) | ||
MinRK
|
r10500 | info = dict( | ||
MinRK
|
r13122 | id = checkpoint_id, | ||
MinRK
|
r10500 | last_modified = last_modified, | ||
) | ||||
return info | ||||
MinRK
|
r17523 | |||
MinRK
|
r10497 | # public checkpoint API | ||
MinRK
|
r17523 | |||
MinRK
|
r18749 | def create_checkpoint(self, path): | ||
MinRK
|
r17524 | """Create a checkpoint from the current state of a file""" | ||
MinRK
|
r13078 | path = path.strip('/') | ||
Min RK
|
r18758 | if not self.file_exists(path): | ||
raise web.HTTPError(404) | ||||
MinRK
|
r18749 | src_path = self._get_os_path(path) | ||
MinRK
|
r10500 | # only the one checkpoint ID: | ||
MinRK
|
r10777 | checkpoint_id = u"checkpoint" | ||
MinRK
|
r18749 | cp_path = self.get_checkpoint_path(checkpoint_id, path) | ||
self.log.debug("creating checkpoint for %s", path) | ||||
Min RK
|
r19005 | with self.perm_to_403(): | ||
self._copy(src_path, cp_path) | ||||
MinRK
|
r17523 | |||
MinRK
|
r10500 | # return the checkpoint info | ||
MinRK
|
r18749 | return self.get_checkpoint_model(checkpoint_id, path) | ||
MinRK
|
r17523 | |||
MinRK
|
r18749 | def list_checkpoints(self, path): | ||
MinRK
|
r17524 | """list the checkpoints for a given file | ||
MinRK
|
r17523 | |||
MinRK
|
r17524 | This contents manager currently only supports one checkpoint per file. | ||
MinRK
|
r10497 | """ | ||
MinRK
|
r13078 | path = path.strip('/') | ||
Zachary Sailer
|
r12984 | checkpoint_id = "checkpoint" | ||
MinRK
|
r18749 | os_path = self.get_checkpoint_path(checkpoint_id, path) | ||
MinRK
|
r16447 | if not os.path.exists(os_path): | ||
MinRK
|
r10497 | return [] | ||
MinRK
|
r10500 | else: | ||
MinRK
|
r18749 | return [self.get_checkpoint_model(checkpoint_id, path)] | ||
MinRK
|
r17523 | |||
MinRK
|
r18749 | def restore_checkpoint(self, checkpoint_id, path): | ||
MinRK
|
r17524 | """restore a file to a checkpointed state""" | ||
MinRK
|
r13078 | path = path.strip('/') | ||
MinRK
|
r18749 | self.log.info("restoring %s from checkpoint %s", path, checkpoint_id) | ||
nb_path = self._get_os_path(path) | ||||
cp_path = self.get_checkpoint_path(checkpoint_id, path) | ||||
MinRK
|
r10497 | if not os.path.isfile(cp_path): | ||
MinRK
|
r10500 | self.log.debug("checkpoint file does not exist: %s", cp_path) | ||
MinRK
|
r10497 | raise web.HTTPError(404, | ||
MinRK
|
r18749 | u'checkpoint does not exist: %s@%s' % (path, checkpoint_id) | ||
MinRK
|
r10497 | ) | ||
# ensure notebook is readable (never restore from an unreadable notebook) | ||||
MinRK
|
r17524 | if cp_path.endswith('.ipynb'): | ||
Min RK
|
r19005 | with self.open(cp_path, 'r', encoding='utf-8') as f: | ||
MinRK
|
r18607 | nbformat.read(f, as_version=4) | ||
MinRK
|
r10497 | self.log.debug("copying %s -> %s", cp_path, nb_path) | ||
Min RK
|
r19005 | with self.perm_to_403(): | ||
self._copy(cp_path, nb_path) | ||||
MinRK
|
r17523 | |||
MinRK
|
r18749 | def delete_checkpoint(self, checkpoint_id, path): | ||
MinRK
|
r17524 | """delete a file's checkpoint""" | ||
MinRK
|
r13078 | path = path.strip('/') | ||
MinRK
|
r18749 | cp_path = self.get_checkpoint_path(checkpoint_id, path) | ||
Zachary Sailer
|
r13046 | if not os.path.isfile(cp_path): | ||
MinRK
|
r10497 | raise web.HTTPError(404, | ||
MinRK
|
r18749 | u'Checkpoint does not exist: %s@%s' % (path, checkpoint_id) | ||
MinRK
|
r10497 | ) | ||
Zachary Sailer
|
r13046 | self.log.debug("unlinking %s", cp_path) | ||
os.unlink(cp_path) | ||||
MinRK
|
r17523 | |||
Paul Ivanov
|
r10019 | def info_string(self): | ||
MinRK
|
r17524 | return "Serving notebooks from local directory: %s" % self.root_dir | ||
Dale Jung
|
r16052 | |||
MinRK
|
r18749 | def get_kernel_path(self, path, model=None): | ||
MinRK
|
r17524 | """Return the initial working dir a kernel associated with a given notebook""" | ||
MinRK
|
r18749 | if '/' in path: | ||
Min RK
|
r18758 | parent_dir = path.rsplit('/', 1)[0] | ||
MinRK
|
r18749 | else: | ||
Min RK
|
r18758 | parent_dir = '' | ||
return self._get_os_path(parent_dir) | ||||