2014-05-31 04:01:21 +08:00
|
|
|
"""A contents manager that uses the local file system for storage."""
|
2011-08-18 04:21:19 +08:00
|
|
|
|
2014-04-28 06:54:31 +08:00
|
|
|
# Copyright (c) IPython Development Team.
|
|
|
|
# Distributed under the terms of the Modified BSD License.
|
2011-08-05 04:55:04 +08:00
|
|
|
|
2014-06-03 04:47:11 +08:00
|
|
|
import base64
|
2012-01-31 05:43:48 +08:00
|
|
|
import io
|
2011-08-05 04:55:04 +08:00
|
|
|
import os
|
2011-08-19 10:31:16 +08:00
|
|
|
import glob
|
2013-04-10 04:49:10 +08:00
|
|
|
import shutil
|
2013-07-04 01:35:30 +08:00
|
|
|
|
2011-08-05 04:55:04 +08:00
|
|
|
from tornado import web
|
|
|
|
|
2014-05-31 04:01:21 +08:00
|
|
|
from .manager import ContentsManager
|
2014-01-28 03:26:14 +08:00
|
|
|
from IPython.nbformat import current
|
2014-04-28 06:54:31 +08:00
|
|
|
from IPython.utils.path import ensure_dir_exists
|
2014-03-01 03:38:28 +08:00
|
|
|
from IPython.utils.traitlets import Unicode, Bool, TraitError
|
2014-02-13 15:03:43 +08:00
|
|
|
from IPython.utils.py3compat import getcwd
|
2013-07-04 03:16:22 +08:00
|
|
|
from IPython.utils import tz
|
2014-02-13 15:03:43 +08:00
|
|
|
from IPython.html.utils import is_hidden, to_os_path
|
2011-08-05 04:55:04 +08:00
|
|
|
|
|
|
|
|
2014-05-31 04:01:21 +08:00
|
|
|
class FileContentsManager(ContentsManager):
|
2014-05-31 03:39:11 +08:00
|
|
|
|
2014-05-31 04:01:21 +08:00
|
|
|
root_dir = Unicode(getcwd(), config=True)
|
2014-05-31 03:39:11 +08:00
|
|
|
|
2014-05-31 04:01:21 +08:00
|
|
|
def _root_dir_changed(self, name, old, new):
|
|
|
|
"""Do a bit of validation of the root_dir."""
|
2014-02-13 15:03:43 +08:00
|
|
|
if not os.path.isabs(new):
|
|
|
|
# If we receive a non-absolute path, make it absolute.
|
2014-05-31 04:01:21 +08:00
|
|
|
self.root_dir = os.path.abspath(new)
|
2014-02-13 15:03:43 +08:00
|
|
|
return
|
|
|
|
if not os.path.exists(new) or not os.path.isdir(new):
|
2014-05-31 04:01:21 +08:00
|
|
|
raise TraitError("%r is not a directory" % new)
|
2014-05-31 03:39:11 +08:00
|
|
|
|
2014-04-24 02:44:29 +08:00
|
|
|
checkpoint_dir = Unicode('.ipynb_checkpoints', config=True,
|
2014-06-17 03:47:00 +08:00
|
|
|
help="""The directory name in which to keep file checkpoints
|
2014-05-31 03:39:11 +08:00
|
|
|
|
2014-06-17 03:47:00 +08:00
|
|
|
This is a path relative to the file's own directory.
|
2014-05-31 03:39:11 +08:00
|
|
|
|
2014-04-24 02:44:29 +08:00
|
|
|
By default, it is .ipynb_checkpoints
|
2013-04-10 04:49:10 +08:00
|
|
|
"""
|
|
|
|
)
|
2014-05-31 03:39:11 +08:00
|
|
|
|
2014-03-15 01:05:30 +08:00
|
|
|
def _copy(self, src, dest):
|
|
|
|
"""copy src to dest
|
2014-05-31 03:39:11 +08:00
|
|
|
|
2014-03-20 03:21:55 +08:00
|
|
|
like shutil.copy2, but log errors in copystat
|
2014-03-15 01:05:30 +08:00
|
|
|
"""
|
2014-03-20 03:21:55 +08:00
|
|
|
shutil.copyfile(src, dest)
|
|
|
|
try:
|
|
|
|
shutil.copystat(src, dest)
|
|
|
|
except OSError as e:
|
|
|
|
self.log.debug("copystat on %s failed", dest, exc_info=True)
|
2014-05-31 03:39:11 +08:00
|
|
|
|
2014-06-03 04:47:11 +08:00
|
|
|
def _get_os_path(self, name=None, path=''):
|
|
|
|
"""Given a filename and a URL path, return its file system
|
|
|
|
path.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
name : string
|
|
|
|
A filename
|
|
|
|
path : string
|
|
|
|
The relative URL path (with '/' as separator) to the named
|
|
|
|
file.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
path : string
|
|
|
|
API path to be evaluated relative to root_dir.
|
|
|
|
"""
|
|
|
|
if name is not None:
|
|
|
|
path = path + '/' + name
|
|
|
|
return to_os_path(path, self.root_dir)
|
2013-07-13 00:15:08 +08:00
|
|
|
|
2013-10-08 03:38:34 +08:00
|
|
|
def path_exists(self, path):
|
2014-06-03 04:47:11 +08:00
|
|
|
"""Does the API-style path refer to an extant directory?
|
2014-05-31 03:39:11 +08:00
|
|
|
|
2013-10-08 03:38:34 +08:00
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
path : string
|
|
|
|
The path to check. This is an API path (`/` separated,
|
2014-05-31 04:01:21 +08:00
|
|
|
relative to root_dir).
|
2014-05-31 03:39:11 +08:00
|
|
|
|
2013-10-08 03:38:34 +08:00
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
exists : bool
|
|
|
|
Whether the path is indeed a directory.
|
|
|
|
"""
|
2013-10-09 02:39:03 +08:00
|
|
|
path = path.strip('/')
|
2014-02-13 15:03:43 +08:00
|
|
|
os_path = self._get_os_path(path=path)
|
2013-10-08 03:38:34 +08:00
|
|
|
return os.path.isdir(os_path)
|
2014-02-06 05:09:55 +08:00
|
|
|
|
|
|
|
def is_hidden(self, path):
|
|
|
|
"""Does the API style path correspond to a hidden directory or file?
|
2014-05-31 03:39:11 +08:00
|
|
|
|
2014-02-06 05:09:55 +08:00
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
path : string
|
|
|
|
The path to check. This is an API path (`/` separated,
|
2014-05-31 04:01:21 +08:00
|
|
|
relative to root_dir).
|
2014-05-31 03:39:11 +08:00
|
|
|
|
2014-02-06 05:09:55 +08:00
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
exists : bool
|
|
|
|
Whether the path is hidden.
|
2014-05-31 03:39:11 +08:00
|
|
|
|
2014-02-06 05:09:55 +08:00
|
|
|
"""
|
|
|
|
path = path.strip('/')
|
2014-02-13 15:03:43 +08:00
|
|
|
os_path = self._get_os_path(path=path)
|
2014-05-31 04:01:21 +08:00
|
|
|
return is_hidden(os_path, self.root_dir)
|
2014-02-06 05:09:55 +08:00
|
|
|
|
2014-06-03 04:47:11 +08:00
|
|
|
def file_exists(self, name, path=''):
|
|
|
|
"""Returns True if the file exists, else returns False.
|
2013-10-08 03:38:34 +08:00
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
name : string
|
2014-06-03 04:47:11 +08:00
|
|
|
The name of the file you are checking.
|
2013-10-08 03:38:34 +08:00
|
|
|
path : string
|
2014-06-03 04:47:11 +08:00
|
|
|
The relative path to the file's directory (with '/' as separator)
|
2013-10-08 03:38:34 +08:00
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
2014-06-03 04:47:11 +08:00
|
|
|
bool
|
2013-10-08 03:38:34 +08:00
|
|
|
"""
|
2014-06-03 04:47:11 +08:00
|
|
|
path = path.strip('/')
|
|
|
|
nbpath = self._get_os_path(name, path=path)
|
|
|
|
return os.path.isfile(nbpath)
|
2013-10-08 03:38:34 +08:00
|
|
|
|
2014-06-03 04:47:11 +08:00
|
|
|
def exists(self, name=None, path=''):
|
|
|
|
"""Returns True if the path [and name] exists, else returns False.
|
2013-08-27 02:18:34 +08:00
|
|
|
|
2013-08-14 04:40:51 +08:00
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
name : string
|
2014-05-31 04:01:21 +08:00
|
|
|
The name of the file you are checking.
|
2013-08-14 04:40:51 +08:00
|
|
|
path : string
|
2014-05-31 04:01:21 +08:00
|
|
|
The relative path to the file's directory (with '/' as separator)
|
2013-08-27 02:18:34 +08:00
|
|
|
|
2013-08-14 04:40:51 +08:00
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
bool
|
|
|
|
"""
|
2013-10-09 02:39:03 +08:00
|
|
|
path = path.strip('/')
|
2014-06-03 04:47:11 +08:00
|
|
|
os_path = self._get_os_path(name, path=path)
|
|
|
|
return os.path.exists(os_path)
|
2013-07-04 01:35:30 +08:00
|
|
|
|
2014-06-03 04:47:11 +08:00
|
|
|
def _base_model(self, name, path=''):
|
|
|
|
"""Build the common base of a contents model"""
|
2014-02-13 15:03:43 +08:00
|
|
|
os_path = self._get_os_path(name, path)
|
2014-02-03 07:50:57 +08:00
|
|
|
info = os.stat(os_path)
|
|
|
|
last_modified = tz.utcfromtimestamp(info.st_mtime)
|
|
|
|
created = tz.utcfromtimestamp(info.st_ctime)
|
2014-06-17 03:47:00 +08:00
|
|
|
# Create the base model.
|
2014-06-03 04:47:11 +08:00
|
|
|
model = {}
|
2014-02-03 07:50:57 +08:00
|
|
|
model['name'] = name
|
|
|
|
model['path'] = path
|
|
|
|
model['last_modified'] = last_modified
|
|
|
|
model['created'] = created
|
2014-06-03 04:47:11 +08:00
|
|
|
model['content'] = None
|
|
|
|
model['format'] = None
|
|
|
|
return model
|
|
|
|
|
|
|
|
def _dir_model(self, name, path='', content=True):
|
|
|
|
"""Build a model for a directory
|
|
|
|
|
|
|
|
if content is requested, will include a listing of the directory
|
|
|
|
"""
|
|
|
|
os_path = self._get_os_path(name, path)
|
|
|
|
|
|
|
|
if not os.path.isdir(os_path):
|
|
|
|
raise web.HTTPError(404, u'directory does not exist: %r' % os_path)
|
|
|
|
elif is_hidden(os_path, self.root_dir):
|
|
|
|
self.log.info("Refusing to serve hidden directory, via 404 Error")
|
|
|
|
raise web.HTTPError(404, u'directory does not exist: %r' % os_path)
|
|
|
|
|
|
|
|
if name is None:
|
|
|
|
if '/' in path:
|
|
|
|
path, name = path.rsplit('/', 1)
|
|
|
|
else:
|
|
|
|
name = ''
|
|
|
|
model = self._base_model(name, path)
|
2014-02-03 07:50:57 +08:00
|
|
|
model['type'] = 'directory'
|
2014-06-03 04:47:11 +08:00
|
|
|
dir_path = u'{}/{}'.format(path, name)
|
|
|
|
if content:
|
2014-06-17 03:47:00 +08:00
|
|
|
model['content'] = contents = []
|
2014-06-03 04:47:11 +08:00
|
|
|
for os_path in glob.glob(self._get_os_path('*', dir_path)):
|
|
|
|
name = os.path.basename(os_path)
|
|
|
|
if self.should_list(name) and not is_hidden(os_path, self.root_dir):
|
|
|
|
contents.append(self.get_model(name=name, path=dir_path, content=False))
|
|
|
|
|
2014-06-04 02:35:45 +08:00
|
|
|
model['format'] = 'json'
|
2014-06-03 04:47:11 +08:00
|
|
|
|
2014-02-03 07:50:57 +08:00
|
|
|
return model
|
|
|
|
|
2014-06-03 04:47:11 +08:00
|
|
|
def _file_model(self, name, path='', content=True):
|
|
|
|
"""Build a model for a file
|
2014-05-31 03:39:11 +08:00
|
|
|
|
2014-06-03 04:47:11 +08:00
|
|
|
if content is requested, include the file contents.
|
2014-06-17 03:47:00 +08:00
|
|
|
UTF-8 text files will be unicode, binary files will be base64-encoded.
|
2014-06-03 04:47:11 +08:00
|
|
|
"""
|
|
|
|
model = self._base_model(name, path)
|
|
|
|
model['type'] = 'file'
|
|
|
|
if content:
|
|
|
|
os_path = self._get_os_path(name, path)
|
|
|
|
try:
|
|
|
|
with io.open(os_path, 'r', encoding='utf-8') as f:
|
|
|
|
model['content'] = f.read()
|
|
|
|
except UnicodeError as e:
|
|
|
|
with io.open(os_path, 'rb') as f:
|
|
|
|
bcontent = f.read()
|
|
|
|
model['content'] = base64.encodestring(bcontent).decode('ascii')
|
|
|
|
model['format'] = 'base64'
|
|
|
|
else:
|
|
|
|
model['format'] = 'text'
|
|
|
|
return model
|
2014-05-31 03:39:11 +08:00
|
|
|
|
2014-06-03 04:47:11 +08:00
|
|
|
|
|
|
|
def _notebook_model(self, name, path='', content=True):
|
|
|
|
"""Build a notebook model
|
|
|
|
|
|
|
|
if content is requested, the notebook content will be populated
|
|
|
|
as a JSON structure (not double-serialized)
|
2013-08-28 11:16:29 +08:00
|
|
|
"""
|
2014-06-03 04:47:11 +08:00
|
|
|
model = self._base_model(name, path)
|
|
|
|
model['type'] = 'notebook'
|
|
|
|
if content:
|
|
|
|
os_path = self._get_os_path(name, path)
|
|
|
|
with io.open(os_path, 'r', encoding='utf-8') as f:
|
|
|
|
try:
|
|
|
|
nb = current.read(f, u'json')
|
|
|
|
except Exception as e:
|
|
|
|
raise web.HTTPError(400, u"Unreadable Notebook: %s %s" % (os_path, e))
|
|
|
|
self.mark_trusted_cells(nb, name, path)
|
|
|
|
model['content'] = nb
|
|
|
|
model['format'] = 'json'
|
|
|
|
return model
|
2013-08-27 02:18:34 +08:00
|
|
|
|
2014-06-03 04:47:11 +08:00
|
|
|
def get_model(self, name, path='', content=True):
|
|
|
|
""" Takes a path and name for an entity and returns its model
|
2014-05-31 03:39:11 +08:00
|
|
|
|
2013-08-28 11:16:29 +08:00
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
name : str
|
2014-06-03 04:47:11 +08:00
|
|
|
the name of the target
|
2013-08-28 11:16:29 +08:00
|
|
|
path : str
|
2014-06-17 03:47:00 +08:00
|
|
|
the URL path that describes the relative path for the target
|
2014-05-31 03:39:11 +08:00
|
|
|
|
2013-08-28 11:16:29 +08:00
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
model : dict
|
2014-06-03 04:47:11 +08:00
|
|
|
the contents model. If content=True, returns the contents
|
|
|
|
of the file or directory as well.
|
2013-08-28 11:16:29 +08:00
|
|
|
"""
|
2013-10-09 02:39:03 +08:00
|
|
|
path = path.strip('/')
|
2014-06-03 04:47:11 +08:00
|
|
|
|
|
|
|
if not self.exists(name=name, path=path):
|
|
|
|
raise web.HTTPError(404, u'No such file or directory: %s/%s' % (path, name))
|
|
|
|
|
2014-02-13 15:03:43 +08:00
|
|
|
os_path = self._get_os_path(name, path)
|
2014-06-03 04:47:11 +08:00
|
|
|
if os.path.isdir(os_path):
|
|
|
|
model = self._dir_model(name, path, content)
|
|
|
|
elif name.endswith('.ipynb'):
|
|
|
|
model = self._notebook_model(name, path, content)
|
|
|
|
else:
|
|
|
|
model = self._file_model(name, path, content)
|
2013-08-27 02:18:34 +08:00
|
|
|
return model
|
2013-07-13 00:15:08 +08:00
|
|
|
|
2014-06-04 02:35:45 +08:00
|
|
|
def _save_notebook(self, os_path, model, name='', path=''):
|
2014-06-17 03:47:00 +08:00
|
|
|
"""save a notebook file"""
|
2014-06-04 02:35:45 +08:00
|
|
|
# Save the notebook file
|
|
|
|
nb = current.to_notebook_json(model['content'])
|
|
|
|
|
|
|
|
self.check_and_sign(nb, name, path)
|
|
|
|
|
|
|
|
if 'name' in nb['metadata']:
|
|
|
|
nb['metadata']['name'] = u''
|
|
|
|
|
|
|
|
with io.open(os_path, 'w', encoding='utf-8') as f:
|
|
|
|
current.write(nb, f, u'json')
|
|
|
|
|
|
|
|
def _save_file(self, os_path, model, name='', path=''):
|
2014-06-17 03:47:00 +08:00
|
|
|
"""save a non-notebook file"""
|
2014-06-04 02:35:45 +08:00
|
|
|
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))
|
|
|
|
with io.open(os_path, 'wb') as f:
|
|
|
|
f.write(bcontent)
|
|
|
|
|
|
|
|
def _save_directory(self, os_path, model, name='', path=''):
|
2014-06-17 03:47:00 +08:00
|
|
|
"""create a directory"""
|
2014-06-04 02:35:45 +08:00
|
|
|
if not os.path.exists(os_path):
|
|
|
|
os.mkdir(os_path)
|
|
|
|
elif not os.path.isdir(os_path):
|
|
|
|
raise web.HTTPError(400, u'Not a directory: %s' % (os_path))
|
|
|
|
|
2014-05-31 04:01:21 +08:00
|
|
|
def save(self, model, name='', path=''):
|
2014-06-04 02:35:45 +08:00
|
|
|
"""Save the file model and return the model with no content."""
|
2013-10-09 02:39:03 +08:00
|
|
|
path = path.strip('/')
|
2013-08-27 02:18:34 +08:00
|
|
|
|
|
|
|
if 'content' not in model:
|
2014-06-04 02:35:45 +08:00
|
|
|
raise web.HTTPError(400, u'No file content provided')
|
|
|
|
if 'type' not in model:
|
|
|
|
raise web.HTTPError(400, u'No file type provided')
|
2014-02-06 04:01:11 +08:00
|
|
|
|
2013-10-24 01:37:49 +08:00
|
|
|
# One checkpoint should always exist
|
2014-05-31 04:01:21 +08:00
|
|
|
if self.file_exists(name, path) and not self.list_checkpoints(name, path):
|
2013-10-24 01:37:49 +08:00
|
|
|
self.create_checkpoint(name, path)
|
2013-08-27 02:18:34 +08:00
|
|
|
|
2013-10-08 03:19:14 +08:00
|
|
|
new_path = model.get('path', path).strip('/')
|
2013-08-27 02:18:34 +08:00
|
|
|
new_name = model.get('name', name)
|
|
|
|
|
|
|
|
if path != new_path or name != new_name:
|
2014-05-31 04:01:21 +08:00
|
|
|
self.rename(name, path, new_name, new_path)
|
2013-06-29 10:35:20 +08:00
|
|
|
|
2014-02-13 15:03:43 +08:00
|
|
|
os_path = self._get_os_path(new_name, new_path)
|
2014-06-04 02:35:45 +08:00
|
|
|
self.log.debug("Saving %s", os_path)
|
2011-08-05 04:55:04 +08:00
|
|
|
try:
|
2014-06-04 02:35:45 +08:00
|
|
|
if model['type'] == 'notebook':
|
|
|
|
self._save_notebook(os_path, model, new_name, new_path)
|
|
|
|
elif model['type'] == 'file':
|
|
|
|
self._save_file(os_path, model, new_name, new_path)
|
|
|
|
elif model['type'] == 'directory':
|
|
|
|
self._save_directory(os_path, model, new_name, new_path)
|
|
|
|
else:
|
|
|
|
raise web.HTTPError(400, "Unhandled contents type: %s" % model['type'])
|
|
|
|
except web.HTTPError:
|
|
|
|
raise
|
2011-12-13 13:09:42 +08:00
|
|
|
except Exception as e:
|
2014-06-04 02:35:45 +08:00
|
|
|
raise web.HTTPError(400, u'Unexpected error while saving file: %s %s' % (os_path, e))
|
2012-06-27 07:15:42 +08:00
|
|
|
|
2014-06-03 04:47:11 +08:00
|
|
|
model = self.get_model(new_name, new_path, content=False)
|
2013-08-27 02:18:34 +08:00
|
|
|
return model
|
|
|
|
|
2014-05-31 04:01:21 +08:00
|
|
|
def update(self, model, name, path=''):
|
|
|
|
"""Update the file's path and/or name"""
|
2013-10-09 02:39:03 +08:00
|
|
|
path = path.strip('/')
|
2013-08-27 02:18:34 +08:00
|
|
|
new_name = model.get('name', name)
|
2013-10-09 02:39:03 +08:00
|
|
|
new_path = model.get('path', path).strip('/')
|
2013-08-27 02:18:34 +08:00
|
|
|
if path != new_path or name != new_name:
|
2014-05-31 04:01:21 +08:00
|
|
|
self.rename(name, path, new_name, new_path)
|
2014-06-03 04:47:11 +08:00
|
|
|
model = self.get_model(new_name, new_path, content=False)
|
2013-08-27 02:18:34 +08:00
|
|
|
return model
|
|
|
|
|
2014-05-31 04:01:21 +08:00
|
|
|
def delete(self, name, path=''):
|
|
|
|
"""Delete file by name and path."""
|
2013-10-09 02:39:03 +08:00
|
|
|
path = path.strip('/')
|
2014-02-13 15:03:43 +08:00
|
|
|
os_path = self._get_os_path(name, path)
|
2013-08-28 12:53:01 +08:00
|
|
|
if not os.path.isfile(os_path):
|
2014-05-31 04:01:21 +08:00
|
|
|
raise web.HTTPError(404, u'File does not exist: %s' % os_path)
|
2014-05-31 03:39:11 +08:00
|
|
|
|
2013-04-10 04:49:10 +08:00
|
|
|
# clear checkpoints
|
2013-08-28 12:53:01 +08:00
|
|
|
for checkpoint in self.list_checkpoints(name, path):
|
2013-10-18 05:45:25 +08:00
|
|
|
checkpoint_id = checkpoint['id']
|
2013-08-27 02:18:34 +08:00
|
|
|
cp_path = self.get_checkpoint_path(checkpoint_id, name, path)
|
|
|
|
if os.path.isfile(cp_path):
|
|
|
|
self.log.debug("Unlinking checkpoint %s", cp_path)
|
|
|
|
os.unlink(cp_path)
|
2014-05-31 03:39:11 +08:00
|
|
|
|
2014-05-31 04:01:21 +08:00
|
|
|
self.log.debug("Unlinking file %s", os_path)
|
2013-08-29 01:06:58 +08:00
|
|
|
os.unlink(os_path)
|
2011-08-05 04:55:04 +08:00
|
|
|
|
2014-05-31 04:01:21 +08:00
|
|
|
def rename(self, old_name, old_path, new_name, new_path):
|
|
|
|
"""Rename a file."""
|
2013-10-09 02:39:03 +08:00
|
|
|
old_path = old_path.strip('/')
|
|
|
|
new_path = new_path.strip('/')
|
2013-08-27 02:18:34 +08:00
|
|
|
if new_name == old_name and new_path == old_path:
|
|
|
|
return
|
2014-05-31 03:39:11 +08:00
|
|
|
|
2014-02-13 15:03:43 +08:00
|
|
|
new_os_path = self._get_os_path(new_name, new_path)
|
|
|
|
old_os_path = self._get_os_path(old_name, old_path)
|
2013-08-27 02:18:34 +08:00
|
|
|
|
|
|
|
# Should we proceed with the move?
|
2013-08-28 12:53:01 +08:00
|
|
|
if os.path.isfile(new_os_path):
|
2013-08-29 05:04:13 +08:00
|
|
|
raise web.HTTPError(409, u'Notebook with name already exists: %s' % new_os_path)
|
2013-08-27 02:18:34 +08:00
|
|
|
|
2014-05-31 04:01:21 +08:00
|
|
|
# Move the file
|
2013-08-27 02:18:34 +08:00
|
|
|
try:
|
2014-04-07 23:37:55 +08:00
|
|
|
shutil.move(old_os_path, new_os_path)
|
2013-08-28 12:53:01 +08:00
|
|
|
except Exception as e:
|
2014-05-31 04:01:21 +08:00
|
|
|
raise web.HTTPError(500, u'Unknown error renaming file: %s %s' % (old_os_path, e))
|
2013-08-27 02:18:34 +08:00
|
|
|
|
|
|
|
# Move the checkpoints
|
|
|
|
old_checkpoints = self.list_checkpoints(old_name, old_path)
|
|
|
|
for cp in old_checkpoints:
|
2013-10-18 05:45:25 +08:00
|
|
|
checkpoint_id = cp['id']
|
2013-08-29 05:04:13 +08:00
|
|
|
old_cp_path = self.get_checkpoint_path(checkpoint_id, old_name, old_path)
|
|
|
|
new_cp_path = self.get_checkpoint_path(checkpoint_id, new_name, new_path)
|
2013-08-27 02:18:34 +08:00
|
|
|
if os.path.isfile(old_cp_path):
|
|
|
|
self.log.debug("Renaming checkpoint %s -> %s", old_cp_path, new_cp_path)
|
2014-04-07 23:37:55 +08:00
|
|
|
shutil.move(old_cp_path, new_cp_path)
|
2013-08-27 02:18:34 +08:00
|
|
|
|
2013-04-10 04:49:10 +08:00
|
|
|
# Checkpoint-related utilities
|
2014-05-31 03:39:11 +08:00
|
|
|
|
2013-10-09 02:39:03 +08:00
|
|
|
def get_checkpoint_path(self, checkpoint_id, name, path=''):
|
2013-08-27 02:18:34 +08:00
|
|
|
"""find the path to a checkpoint"""
|
2013-10-09 02:39:03 +08:00
|
|
|
path = path.strip('/')
|
2014-05-31 04:01:21 +08:00
|
|
|
basename, ext = os.path.splitext(name)
|
2013-05-29 02:47:05 +08:00
|
|
|
filename = u"{name}-{checkpoint_id}{ext}".format(
|
2013-10-24 01:37:08 +08:00
|
|
|
name=basename,
|
2013-04-10 04:49:10 +08:00
|
|
|
checkpoint_id=checkpoint_id,
|
2014-05-31 04:01:21 +08:00
|
|
|
ext=ext,
|
2013-04-10 04:49:10 +08:00
|
|
|
)
|
2014-04-24 02:44:29 +08:00
|
|
|
os_path = self._get_os_path(path=path)
|
|
|
|
cp_dir = os.path.join(os_path, self.checkpoint_dir)
|
2014-04-28 06:54:31 +08:00
|
|
|
ensure_dir_exists(cp_dir)
|
2014-04-24 02:44:29 +08:00
|
|
|
cp_path = os.path.join(cp_dir, filename)
|
2013-08-27 02:18:34 +08:00
|
|
|
return cp_path
|
|
|
|
|
2013-10-09 02:39:03 +08:00
|
|
|
def get_checkpoint_model(self, checkpoint_id, name, path=''):
|
2013-04-11 07:29:04 +08:00
|
|
|
"""construct the info dict for a given checkpoint"""
|
2013-10-09 02:39:03 +08:00
|
|
|
path = path.strip('/')
|
2013-08-27 02:18:34 +08:00
|
|
|
cp_path = self.get_checkpoint_path(checkpoint_id, name, path)
|
|
|
|
stats = os.stat(cp_path)
|
2013-07-04 03:16:22 +08:00
|
|
|
last_modified = tz.utcfromtimestamp(stats.st_mtime)
|
2013-04-11 07:29:04 +08:00
|
|
|
info = dict(
|
2013-10-18 05:45:25 +08:00
|
|
|
id = checkpoint_id,
|
2013-04-11 07:29:04 +08:00
|
|
|
last_modified = last_modified,
|
|
|
|
)
|
|
|
|
return info
|
2014-05-31 03:39:11 +08:00
|
|
|
|
2013-04-10 04:49:10 +08:00
|
|
|
# public checkpoint API
|
2014-05-31 03:39:11 +08:00
|
|
|
|
2013-10-09 02:39:03 +08:00
|
|
|
def create_checkpoint(self, name, path=''):
|
2014-05-31 04:01:21 +08:00
|
|
|
"""Create a checkpoint from the current state of a file"""
|
2013-10-09 02:39:03 +08:00
|
|
|
path = path.strip('/')
|
2014-05-31 04:01:21 +08:00
|
|
|
src_path = self._get_os_path(name, path)
|
2013-04-11 07:29:04 +08:00
|
|
|
# only the one checkpoint ID:
|
2013-05-29 02:47:05 +08:00
|
|
|
checkpoint_id = u"checkpoint"
|
2013-08-27 02:18:34 +08:00
|
|
|
cp_path = self.get_checkpoint_path(checkpoint_id, name, path)
|
2014-06-17 03:47:00 +08:00
|
|
|
self.log.debug("creating checkpoint for %s", name)
|
2014-05-31 04:01:21 +08:00
|
|
|
self._copy(src_path, cp_path)
|
2014-05-31 03:39:11 +08:00
|
|
|
|
2013-04-11 07:29:04 +08:00
|
|
|
# return the checkpoint info
|
2013-08-27 02:18:34 +08:00
|
|
|
return self.get_checkpoint_model(checkpoint_id, name, path)
|
2014-05-31 03:39:11 +08:00
|
|
|
|
2013-10-09 02:39:03 +08:00
|
|
|
def list_checkpoints(self, name, path=''):
|
2014-05-31 04:01:21 +08:00
|
|
|
"""list the checkpoints for a given file
|
2014-05-31 03:39:11 +08:00
|
|
|
|
2014-05-31 04:01:21 +08:00
|
|
|
This contents manager currently only supports one checkpoint per file.
|
2013-04-10 04:49:10 +08:00
|
|
|
"""
|
2013-10-09 02:39:03 +08:00
|
|
|
path = path.strip('/')
|
2013-07-04 01:35:30 +08:00
|
|
|
checkpoint_id = "checkpoint"
|
2014-04-24 02:44:29 +08:00
|
|
|
os_path = self.get_checkpoint_path(checkpoint_id, name, path)
|
|
|
|
if not os.path.exists(os_path):
|
2013-04-10 04:49:10 +08:00
|
|
|
return []
|
2013-04-11 07:29:04 +08:00
|
|
|
else:
|
2013-08-27 02:18:34 +08:00
|
|
|
return [self.get_checkpoint_model(checkpoint_id, name, path)]
|
2014-05-31 03:39:11 +08:00
|
|
|
|
|
|
|
|
2013-10-09 02:39:03 +08:00
|
|
|
def restore_checkpoint(self, checkpoint_id, name, path=''):
|
2014-05-31 04:01:21 +08:00
|
|
|
"""restore a file to a checkpointed state"""
|
2013-10-09 02:39:03 +08:00
|
|
|
path = path.strip('/')
|
2014-05-31 04:01:21 +08:00
|
|
|
self.log.info("restoring %s from checkpoint %s", name, checkpoint_id)
|
2014-02-13 15:03:43 +08:00
|
|
|
nb_path = self._get_os_path(name, path)
|
2013-08-27 02:18:34 +08:00
|
|
|
cp_path = self.get_checkpoint_path(checkpoint_id, name, path)
|
2013-04-10 04:49:10 +08:00
|
|
|
if not os.path.isfile(cp_path):
|
2013-04-11 07:29:04 +08:00
|
|
|
self.log.debug("checkpoint file does not exist: %s", cp_path)
|
2013-04-10 04:49:10 +08:00
|
|
|
raise web.HTTPError(404,
|
2014-05-31 04:01:21 +08:00
|
|
|
u'checkpoint does not exist: %s-%s' % (name, checkpoint_id)
|
2013-04-10 04:49:10 +08:00
|
|
|
)
|
|
|
|
# ensure notebook is readable (never restore from an unreadable notebook)
|
2014-05-31 04:01:21 +08:00
|
|
|
if cp_path.endswith('.ipynb'):
|
|
|
|
with io.open(cp_path, 'r', encoding='utf-8') as f:
|
|
|
|
current.read(f, u'json')
|
2014-03-15 01:05:30 +08:00
|
|
|
self._copy(cp_path, nb_path)
|
2013-04-10 04:49:10 +08:00
|
|
|
self.log.debug("copying %s -> %s", cp_path, nb_path)
|
2014-05-31 03:39:11 +08:00
|
|
|
|
2013-10-09 02:39:03 +08:00
|
|
|
def delete_checkpoint(self, checkpoint_id, name, path=''):
|
2014-05-31 04:01:21 +08:00
|
|
|
"""delete a file's checkpoint"""
|
2013-10-09 02:39:03 +08:00
|
|
|
path = path.strip('/')
|
2013-08-27 02:18:34 +08:00
|
|
|
cp_path = self.get_checkpoint_path(checkpoint_id, name, path)
|
|
|
|
if not os.path.isfile(cp_path):
|
2013-04-10 04:49:10 +08:00
|
|
|
raise web.HTTPError(404,
|
2014-05-31 04:01:21 +08:00
|
|
|
u'Checkpoint does not exist: %s%s-%s' % (path, name, checkpoint_id)
|
2013-04-10 04:49:10 +08:00
|
|
|
)
|
2013-08-27 02:18:34 +08:00
|
|
|
self.log.debug("unlinking %s", cp_path)
|
|
|
|
os.unlink(cp_path)
|
2014-05-31 03:39:11 +08:00
|
|
|
|
2013-03-25 01:53:59 +08:00
|
|
|
def info_string(self):
|
2014-05-31 04:01:21 +08:00
|
|
|
return "Serving notebooks from local directory: %s" % self.root_dir
|
2014-03-30 05:39:15 +08:00
|
|
|
|
|
|
|
def get_kernel_path(self, name, path='', model=None):
|
2014-05-31 04:01:21 +08:00
|
|
|
"""Return the initial working dir a kernel associated with a given notebook"""
|
|
|
|
return os.path.join(self.root_dir, path)
|