Check for TrashPermissionError rather than guess

_check_trash() was added (in #3304) because TrashPermissionError didn't
exist, yet.

Now that it does, we can use it, and stop guessing what will cause a
permission problem.

Closes: #3374
pull/5894/head
Stefano Rivera 5 years ago
parent 02024f0b9a
commit 6a46458f1f

@ -15,6 +15,7 @@ import mimetypes
import nbformat
from send2trash import send2trash
from send2trash.exceptions import TrashPermissionError
from tornado import web
from .filecheckpoints import FileCheckpoints
@ -512,17 +513,6 @@ class FileContentsManager(FileManagerMixin, ContentsManager):
if not os.path.exists(os_path):
raise web.HTTPError(404, u'File or directory does not exist: %s' % os_path)
def _check_trash(os_path):
if sys.platform in {'win32', 'darwin'}:
return True
# It's a bit more nuanced than this, but until we can better
# distinguish errors from send2trash, assume that we can only trash
# files on the same partition as the home directory.
file_dev = os.stat(os_path).st_dev
home_dev = os.stat(os.path.expanduser('~')).st_dev
return file_dev == home_dev
def is_non_empty_dir(os_path):
if os.path.isdir(os_path):
# A directory containing only leftover checkpoints is
@ -538,16 +528,12 @@ class FileContentsManager(FileManagerMixin, ContentsManager):
# send2trash can really delete files on Windows, so disallow
# deleting non-empty files. See Github issue 3631.
raise web.HTTPError(400, u'Directory %s not empty' % os_path)
if _check_trash(os_path):
try:
self.log.debug("Sending %s to trash", os_path)
# Looking at the code in send2trash, I don't think the errors it
# raises let us distinguish permission errors from other errors in
# code. So for now, just let them all get logged as server errors.
send2trash(os_path)
return
else:
self.log.warning("Skipping trash for %s, on different device "
"to home directory", os_path)
except TrashPermissionError as e:
self.log.warning("Skipping trash for %s, %s", os_path, e)
if os.path.isdir(os_path):
# Don't permanently delete non-empty directories.

@ -110,7 +110,7 @@ for more information.
'nbformat',
'nbconvert',
'ipykernel', # bless IPython kernel for now
'Send2Trash',
'Send2Trash>=1.5.0',
'terminado>=0.8.3',
'prometheus_client'
],

Loading…
Cancel
Save