Skip to content

Commit 8c90eb5

Browse files
wqj97qianjun.wqj
andauthored
send2trash now supports deleting from different filesystem type(#1290) (#1291)
Co-authored-by: qianjun.wqj <[email protected]>
1 parent 32df893 commit 8c90eb5

File tree

2 files changed

+17
-49
lines changed

2 files changed

+17
-49
lines changed

jupyter_server/services/contents/filemanager.py

Lines changed: 16 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -513,17 +513,6 @@ def delete_file(self, path):
513513
if not self.exists(path):
514514
raise web.HTTPError(404, four_o_four)
515515

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-
527516
def is_non_empty_dir(os_path):
528517
if os.path.isdir(os_path):
529518
# A directory containing only leftover checkpoints is
@@ -539,20 +528,15 @@ def is_non_empty_dir(os_path):
539528
# send2trash can really delete files on Windows, so disallow
540529
# deleting non-empty files. See Github issue 3631.
541530
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:
549536
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
556540

557541
if os.path.isdir(os_path):
558542
# Don't permanently delete non-empty directories.
@@ -967,17 +951,6 @@ async def delete_file(self, path):
967951
if not os.path.exists(os_path):
968952
raise web.HTTPError(404, "File or directory does not exist: %s" % os_path)
969953

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-
981954
async def is_non_empty_dir(os_path):
982955
if os.path.isdir(os_path):
983956
# A directory containing only leftover checkpoints is
@@ -998,20 +971,15 @@ async def is_non_empty_dir(os_path):
998971
# send2trash can really delete files on Windows, so disallow
999972
# deleting non-empty files. See Github issue 3631.
1000973
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:
1008979
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
1015983

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

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ dependencies = [
3939
"prometheus_client",
4040
"pywinpty;os_name=='nt'",
4141
"pyzmq>=24",
42-
"Send2Trash",
42+
"Send2Trash>=1.8.2",
4343
"terminado>=0.8.3",
4444
"tornado>=6.2.0",
4545
"traitlets>=5.6.0",

0 commit comments

Comments
 (0)