15
15
import nbformat
16
16
17
17
from send2trash import send2trash
18
+ from send2trash .exceptions import TrashPermissionError
18
19
from tornado import web
19
20
20
21
from .filecheckpoints import FileCheckpoints
@@ -512,17 +513,6 @@ def delete_file(self, path):
512
513
if not os .path .exists (os_path ):
513
514
raise web .HTTPError (404 , u'File or directory does not exist: %s' % os_path )
514
515
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
-
526
516
def is_non_empty_dir (os_path ):
527
517
if os .path .isdir (os_path ):
528
518
# A directory containing only leftover checkpoints is
@@ -538,16 +528,12 @@ def is_non_empty_dir(os_path):
538
528
# send2trash can really delete files on Windows, so disallow
539
529
# deleting non-empty files. See Github issue 3631.
540
530
raise web .HTTPError (400 , u'Directory %s not empty' % os_path )
541
- if _check_trash ( os_path ) :
531
+ try :
542
532
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.
546
533
send2trash (os_path )
547
534
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 )
551
537
552
538
if os .path .isdir (os_path ):
553
539
# Don't permanently delete non-empty directories.
0 commit comments