Skip to content

Commit 1672777

Browse files
authored
Merge pull request #5894 from stefanor/trashpermissionerror
Handle TrashPermissionError, now that it exists
2 parents 083df1b + 7bcc824 commit 1672777

File tree

3 files changed

+22
-20
lines changed

3 files changed

+22
-20
lines changed

notebook/services/contents/filemanager.py

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import nbformat
1616

1717
from send2trash import send2trash
18+
from send2trash.exceptions import TrashPermissionError
1819
from tornado import web
1920

2021
from .filecheckpoints import FileCheckpoints
@@ -512,17 +513,6 @@ def delete_file(self, path):
512513
if not os.path.exists(os_path):
513514
raise web.HTTPError(404, u'File or directory does not exist: %s' % os_path)
514515

515-
def _check_trash(os_path):
516-
if sys.platform in {'win32', 'darwin'}:
517-
return True
518-
519-
# It's a bit more nuanced than this, but until we can better
520-
# distinguish errors from send2trash, assume that we can only trash
521-
# files on the same partition as the home directory.
522-
file_dev = os.stat(os_path).st_dev
523-
home_dev = os.stat(os.path.expanduser('~')).st_dev
524-
return file_dev == home_dev
525-
526516
def is_non_empty_dir(os_path):
527517
if os.path.isdir(os_path):
528518
# A directory containing only leftover checkpoints is
@@ -538,16 +528,12 @@ def is_non_empty_dir(os_path):
538528
# send2trash can really delete files on Windows, so disallow
539529
# deleting non-empty files. See Github issue 3631.
540530
raise web.HTTPError(400, u'Directory %s not empty' % os_path)
541-
if _check_trash(os_path):
531+
try:
542532
self.log.debug("Sending %s to trash", os_path)
543-
# Looking at the code in send2trash, I don't think the errors it
544-
# raises let us distinguish permission errors from other errors in
545-
# code. So for now, just let them all get logged as server errors.
546533
send2trash(os_path)
547534
return
548-
else:
549-
self.log.warning("Skipping trash for %s, on different device "
550-
"to home directory", os_path)
535+
except TrashPermissionError as e:
536+
self.log.warning("Skipping trash for %s, %s", os_path, e)
551537

552538
if os.path.isdir(os_path):
553539
# Don't permanently delete non-empty directories.

notebook/services/contents/tests/test_contents_api.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
pjoin = os.path.join
1313

1414
import requests
15+
from send2trash import send2trash
16+
from send2trash.exceptions import TrashPermissionError
1517

1618
from ..filecheckpoints import GenericFileCheckpoints
1719

@@ -197,6 +199,14 @@ def isfile(self, api_path):
197199
def isdir(self, api_path):
198200
return os.path.isdir(self.to_os_path(api_path))
199201

202+
def can_send2trash(self, api_path):
203+
"""Send a path to trash, if possible. Return success."""
204+
try:
205+
send2trash(self.to_os_path(api_path))
206+
return True
207+
except TrashPermissionError as e:
208+
return False
209+
200210
def setUp(self):
201211
for d in (self.dirs + self.hidden_dirs):
202212
self.make_dir(d)
@@ -526,7 +536,13 @@ def test_delete_non_empty_dir(self):
526536
if sys.platform == 'win32':
527537
self.skipTest("Disabled deleting non-empty dirs on Windows")
528538
# Test that non empty directory can be deleted
529-
self.api.delete(u'å b')
539+
try:
540+
self.api.delete(u'å b')
541+
except requests.HTTPError as e:
542+
if e.response.status_code == 400:
543+
if not self.can_send2trash(u'å b'):
544+
self.skipTest("Dir can't be sent to trash")
545+
raise
530546
# Check if directory has actually been deleted
531547
with assert_http_error(404):
532548
self.api.list(u'å b')

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
'nbformat',
111111
'nbconvert',
112112
'ipykernel', # bless IPython kernel for now
113-
'Send2Trash',
113+
'Send2Trash>=1.5.0',
114114
'terminado>=0.8.3',
115115
'prometheus_client'
116116
],

0 commit comments

Comments
 (0)