Skip to content

Commit

Permalink
Check for TrashPermissionError rather than guess
Browse files Browse the repository at this point in the history
_check_trash() was added (in jupyter#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: jupyter#3374
  • Loading branch information
stefanor committed Dec 1, 2020
1 parent 02024f0 commit 6a46458
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 19 deletions.
22 changes: 4 additions & 18 deletions notebook/services/contents/filemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import nbformat

from send2trash import send2trash
from send2trash.exceptions import TrashPermissionError
from tornado import web

from .filecheckpoints import FileCheckpoints
Expand Down Expand Up @@ -512,17 +513,6 @@ def delete_file(self, path):
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
Expand All @@ -538,16 +528,12 @@ def is_non_empty_dir(os_path):
# 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.
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
'nbformat',
'nbconvert',
'ipykernel', # bless IPython kernel for now
'Send2Trash',
'Send2Trash>=1.5.0',
'terminado>=0.8.3',
'prometheus_client'
],
Expand Down

0 comments on commit 6a46458

Please sign in to comment.