filemanager.py
472 lines
| 16.1 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 | |||
Scott Sanderson
|
r19838 | |||
Thomas Kluyver
|
r6030 | import io | ||
Brian E. Granger
|
r4484 | import os | ||
MinRK
|
r10497 | import shutil | ||
Thomas Kluyver
|
r19011 | import mimetypes | ||
Zachary Sailer
|
r12984 | |||
Brian E. Granger
|
r4484 | from tornado import web | ||
Scott Sanderson
|
r19839 | from .filecheckpoints import FileCheckpoints | ||
Scott Sanderson
|
r19838 | from .fileio import FileManagerMixin | ||
from .manager import ContentsManager | ||||
MinRK
|
r18607 | from IPython import nbformat | ||
Min RK
|
r19299 | from IPython.utils.importstring import import_item | ||
from IPython.utils.traitlets import Any, Unicode, Bool, TraitError | ||||
Scott Sanderson
|
r19838 | from IPython.utils.py3compat import getcwd, string_types | ||
MinRK
|
r11145 | from IPython.utils import tz | ||
Scott Sanderson
|
r19727 | from IPython.html.utils import ( | ||
is_hidden, | ||||
to_api_path, | ||||
) | ||||
Brian E. Granger
|
r4484 | |||
Min RK
|
r19300 | _script_exporter = None | ||
Min RK
|
r19301 | |||
Scott Sanderson
|
r19838 | |||
Min RK
|
r19300 | def _post_save_script(model, os_path, contents_manager, **kwargs): | ||
"""convert notebooks to Python script after save with nbconvert | ||||
replaces `ipython notebook --script` | ||||
""" | ||||
Min RK
|
r19301 | from IPython.nbconvert.exporters.script import ScriptExporter | ||
Min RK
|
r19300 | |||
if model['type'] != 'notebook': | ||||
return | ||||
Min RK
|
r19301 | |||
Min RK
|
r19300 | global _script_exporter | ||
if _script_exporter is None: | ||||
Min RK
|
r19301 | _script_exporter = ScriptExporter(parent=contents_manager) | ||
Min RK
|
r19300 | log = contents_manager.log | ||
base, ext = os.path.splitext(os_path) | ||||
py_fname = base + '.py' | ||||
Min RK
|
r19301 | script, resources = _script_exporter.from_filename(os_path) | ||
script_fname = base + resources.get('output_extension', '.txt') | ||||
log.info("Saving script /%s", to_api_path(script_fname, contents_manager.root_dir)) | ||||
with io.open(script_fname, 'w', encoding='utf-8') as f: | ||||
f.write(script) | ||||
Brian E. Granger
|
r4484 | |||
MinRK
|
r17523 | |||
Scott Sanderson
|
r19727 | class FileContentsManager(FileManagerMixin, ContentsManager): | ||
root_dir = Unicode(config=True) | ||||
def _root_dir_default(self): | ||||
try: | ||||
return self.parent.notebook_dir | ||||
except AttributeError: | ||||
return getcwd() | ||||
save_script = Bool(False, config=True, help='DEPRECATED, use post_save_hook') | ||||
def _save_script_changed(self): | ||||
self.log.warn(""" | ||||
`--script` is deprecated. You can trigger nbconvert via pre- or post-save hooks: | ||||
ContentsManager.pre_save_hook | ||||
FileContentsManager.post_save_hook | ||||
A post-save hook has been registered that calls: | ||||
ipython nbconvert --to script [notebook] | ||||
which behaves similarly to `--script`. | ||||
""") | ||||
self.post_save_hook = _post_save_script | ||||
post_save_hook = Any(None, config=True, | ||||
help="""Python callable or importstring thereof | ||||
to be called on the path of a file just saved. | ||||
This can be used to process the file on disk, | ||||
such as converting the notebook to a script or HTML via nbconvert. | ||||
It will be called as (all arguments passed by keyword): | ||||
hook(os_path=os_path, model=model, contents_manager=instance) | ||||
path: the filesystem path to the file just written | ||||
model: the model representing the file | ||||
contents_manager: this ContentsManager instance | ||||
""" | ||||
) | ||||
def _post_save_hook_changed(self, name, old, new): | ||||
if new and isinstance(new, string_types): | ||||
self.post_save_hook = import_item(self.post_save_hook) | ||||
elif new: | ||||
if not callable(new): | ||||
raise TraitError("post_save_hook must be callable") | ||||
def run_post_save_hook(self, model, os_path): | ||||
"""Run the post-save hook if defined, and log errors""" | ||||
if self.post_save_hook: | ||||
try: | ||||
self.log.debug("Running post-save hook on %s", os_path) | ||||
self.post_save_hook(os_path=os_path, model=model, contents_manager=self) | ||||
except Exception: | ||||
self.log.error("Post-save hook failed on %s", os_path, exc_info=True) | ||||
def _root_dir_changed(self, name, old, new): | ||||
"""Do a bit of validation of the root_dir.""" | ||||
if not os.path.isabs(new): | ||||
# If we receive a non-absolute path, make it absolute. | ||||
self.root_dir = os.path.abspath(new) | ||||
return | ||||
if not os.path.isdir(new): | ||||
raise TraitError("%r is not a directory" % new) | ||||
Scott Sanderson
|
r19839 | def _checkpoints_class_default(self): | ||
return FileCheckpoints | ||||
Scott Sanderson
|
r19727 | |||
Scott Sanderson
|
r19747 | def is_hidden(self, path): | ||
"""Does the API style path correspond to a hidden directory or file? | ||||
Parameters | ||||
---------- | ||||
path : string | ||||
The path to check. This is an API path (`/` separated, | ||||
relative to root_dir). | ||||
Returns | ||||
------- | ||||
hidden : bool | ||||
Whether the path exists and is hidden. | ||||
""" | ||||
path = path.strip('/') | ||||
os_path = self._get_os_path(path=path) | ||||
return is_hidden(os_path, self.root_dir) | ||||
def file_exists(self, path): | ||||
"""Returns True if the file exists, else returns False. | ||||
API-style wrapper for os.path.isfile | ||||
Parameters | ||||
---------- | ||||
path : string | ||||
The relative path to the file (with '/' as separator) | ||||
Returns | ||||
------- | ||||
exists : bool | ||||
Whether the file exists. | ||||
""" | ||||
path = path.strip('/') | ||||
os_path = self._get_os_path(path) | ||||
return os.path.isfile(os_path) | ||||
def dir_exists(self, path): | ||||
"""Does the API-style path refer to an extant directory? | ||||
API-style wrapper for os.path.isdir | ||||
Parameters | ||||
---------- | ||||
path : string | ||||
The path to check. This is an API path (`/` separated, | ||||
relative to root_dir). | ||||
Returns | ||||
------- | ||||
exists : bool | ||||
Whether the path is indeed a directory. | ||||
""" | ||||
path = path.strip('/') | ||||
os_path = self._get_os_path(path=path) | ||||
return os.path.isdir(os_path) | ||||
def exists(self, path): | ||||
"""Returns True if the path exists, else returns False. | ||||
API-style wrapper for os.path.exists | ||||
Parameters | ||||
---------- | ||||
path : string | ||||
The API path to the file (with '/' as separator) | ||||
Returns | ||||
------- | ||||
exists : bool | ||||
Whether the target exists. | ||||
""" | ||||
path = path.strip('/') | ||||
os_path = self._get_os_path(path=path) | ||||
return os.path.exists(os_path) | ||||
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) | ||||
MinRK
|
r17525 | if content: | ||
Scott Sanderson
|
r19786 | content, format = self._read_file(os_path, format) | ||
default_mime = { | ||||
'text': 'text/plain', | ||||
'base64': 'application/octet-stream' | ||||
}[format] | ||||
model.update( | ||||
content=content, | ||||
format=format, | ||||
mimetype=mimetypes.guess_type(os_path)[0] or default_mime, | ||||
) | ||||
Thomas Kluyver
|
r18788 | |||
MinRK
|
r17525 | return model | ||
MinRK
|
r17523 | |||
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) | ||
Scott Sanderson
|
r19747 | nb = self._read_notebook(os_path, as_version=4) | ||
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 | |||
Min RK
|
r19391 | 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 | ||||
Min RK
|
r19391 | type : str, optional | ||
Thomas Kluyver
|
r18781 | 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): | ||
Min RK
|
r19391 | if type not in (None, 'directory'): | ||
Thomas Kluyver
|
r18781 | raise web.HTTPError(400, | ||
Min RK
|
r19391 | u'%s is a directory, not a %s' % (path, type), reason='bad type') | ||
MinRK
|
r18749 | model = self._dir_model(path, content=content) | ||
Min RK
|
r19391 | elif type == 'notebook' or (type is None and path.endswith('.ipynb')): | ||
MinRK
|
r18749 | model = self._notebook_model(path, content=content) | ||
MinRK
|
r17525 | else: | ||
Min RK
|
r19391 | if type == 'directory': | ||
Thomas Kluyver
|
r18781 | raise web.HTTPError(400, | ||
Min RK
|
r19337 | u'%s is not a directory', reason='bad type') | ||
Thomas Kluyver
|
r18788 | model = self._file_model(path, content=content, format=format) | ||
Zachary Sailer
|
r13046 | return model | ||
Zachary Sailer
|
r12997 | |||
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 | |||
Min RK
|
r19299 | self.run_pre_save_hook(model=model, path=path) | ||
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': | ||
Scott Sanderson
|
r19786 | nb = nbformat.from_dict(model['content']) | ||
self.check_and_sign(nb, path) | ||||
self._save_notebook(os_path, nb) | ||||
# One checkpoint should always exist for notebooks. | ||||
Scott Sanderson
|
r19839 | if not self.checkpoints.list_checkpoints(path): | ||
Scott Sanderson
|
r19828 | self.create_checkpoint(path) | ||
MinRK
|
r17527 | elif model['type'] == 'file': | ||
Scott Sanderson
|
r19786 | # Missing format will be handled internally by _save_file. | ||
self._save_file(os_path, model['content'], model.get('format')) | ||||
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 | ||||
Min RK
|
r19299 | |||
self.run_post_save_hook(model=model, os_path=os_path) | ||||
Zachary Sailer
|
r13046 | return model | ||
Scott Sanderson
|
r19727 | def delete_file(self, path): | ||
MinRK
|
r18749 | """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) | ||||
Scott Sanderson
|
r19727 | # Don't delete non-empty directories. | ||
# A directory containing only leftover checkpoints is | ||||
# considered empty. | ||||
Scott Sanderson
|
r19839 | cp_dir = getattr(self.checkpoints, 'checkpoint_dir', None) | ||
Scott Sanderson
|
r19727 | for entry in listing: | ||
if entry != cp_dir: | ||||
raise web.HTTPError(400, u'Directory %s not empty' % os_path) | ||||
MinRK
|
r17530 | elif not os.path.isfile(os_path): | ||
MinRK
|
r17524 | raise web.HTTPError(404, u'File does not exist: %s' % os_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 | |||
Scott Sanderson
|
r19727 | def rename_file(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 | |||
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): | ||
Matthias Bussonnier
|
r19093 | """Return the initial API path of 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 = '' | ||
Matthias Bussonnier
|
r19093 | return parent_dir | ||