@@ -513,17 +513,6 @@ def delete_file(self, path):
513
513
if not self .exists (path ):
514
514
raise web .HTTPError (404 , four_o_four )
515
515
516
- def _check_trash (os_path ):
517
- if sys .platform in {"win32" , "darwin" }:
518
- return True
519
-
520
- # It's a bit more nuanced than this, but until we can better
521
- # distinguish errors from send2trash, assume that we can only trash
522
- # files on the same partition as the home directory.
523
- file_dev = os .stat (os_path ).st_dev
524
- home_dev = os .stat (os .path .expanduser ("~" )).st_dev
525
- return file_dev == home_dev
526
-
527
516
def is_non_empty_dir (os_path ):
528
517
if os .path .isdir (os_path ):
529
518
# A directory containing only leftover checkpoints is
@@ -539,20 +528,15 @@ def is_non_empty_dir(os_path):
539
528
# send2trash can really delete files on Windows, so disallow
540
529
# deleting non-empty files. See Github issue 3631.
541
530
raise web .HTTPError (400 , "Directory %s not empty" % os_path )
542
- if _check_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, the "look before you leap" approach is used.
546
- if not self .is_writable (path ):
547
- raise web .HTTPError (403 , "Permission denied: %s" % path )
548
- self .log .debug ("Sending %s to trash" , os_path )
531
+ # send2trash now supports deleting directories. see #1290
532
+ if not self .is_writable (path ):
533
+ raise web .HTTPError (403 , "Permission denied: %s" % path ) from None
534
+ self .log .debug ("Sending %s to trash" , os_path )
535
+ try :
549
536
send2trash (os_path )
550
- return
551
- else :
552
- self .log .warning (
553
- "Skipping trash for %s, on different device to home directory" ,
554
- os_path ,
555
- )
537
+ except OSError as e :
538
+ raise web .HTTPError (400 , "send2trash failed: %s" % e ) from e
539
+ return
556
540
557
541
if os .path .isdir (os_path ):
558
542
# Don't permanently delete non-empty directories.
@@ -967,17 +951,6 @@ async def delete_file(self, path):
967
951
if not os .path .exists (os_path ):
968
952
raise web .HTTPError (404 , "File or directory does not exist: %s" % os_path )
969
953
970
- async def _check_trash (os_path ):
971
- if sys .platform in {"win32" , "darwin" }:
972
- return True
973
-
974
- # It's a bit more nuanced than this, but until we can better
975
- # distinguish errors from send2trash, assume that we can only trash
976
- # files on the same partition as the home directory.
977
- file_dev = (await run_sync (os .stat , os_path )).st_dev
978
- home_dev = (await run_sync (os .stat , os .path .expanduser ("~" ))).st_dev
979
- return file_dev == home_dev
980
-
981
954
async def is_non_empty_dir (os_path ):
982
955
if os .path .isdir (os_path ):
983
956
# A directory containing only leftover checkpoints is
@@ -998,20 +971,15 @@ async def is_non_empty_dir(os_path):
998
971
# send2trash can really delete files on Windows, so disallow
999
972
# deleting non-empty files. See Github issue 3631.
1000
973
raise web .HTTPError (400 , "Directory %s not empty" % os_path )
1001
- if await _check_trash (os_path ):
1002
- # Looking at the code in send2trash, I don't think the errors it
1003
- # raises let us distinguish permission errors from other errors in
1004
- # code. So for now, the "look before you leap" approach is used.
1005
- if not self .is_writable (path ):
1006
- raise web .HTTPError (403 , "Permission denied: %s" % path )
1007
- self .log .debug ("Sending %s to trash" , os_path )
974
+ # send2trash now supports deleting directories. see #1290
975
+ if not self .is_writable (path ):
976
+ raise web .HTTPError (403 , "Permission denied: %s" % path ) from None
977
+ self .log .debug ("Sending %s to trash" , os_path )
978
+ try :
1008
979
send2trash (os_path )
1009
- return
1010
- else :
1011
- self .log .warning (
1012
- "Skipping trash for %s, on different device to home directory" ,
1013
- os_path ,
1014
- )
980
+ except OSError as e :
981
+ raise web .HTTPError (400 , "send2trash f`1ailed: %s" % e ) from e
982
+ return
1015
983
1016
984
if os .path .isdir (os_path ):
1017
985
# Don't permanently delete non-empty directories.
0 commit comments