Skip to content

Commit ae011a1

Browse files
authored
Merge pull request #3108 from kirit93/fixes2760
Allowing non empty dirs to be deleted
2 parents b1e5f72 + b7e02a6 commit ae011a1

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

notebook/services/contents/filemanager.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -489,17 +489,8 @@ def delete_file(self, path):
489489
path = path.strip('/')
490490
os_path = self._get_os_path(path)
491491
rm = os.unlink
492-
if os.path.isdir(os_path):
493-
listing = os.listdir(os_path)
494-
# Don't delete non-empty directories.
495-
# A directory containing only leftover checkpoints is
496-
# considered empty.
497-
cp_dir = getattr(self.checkpoints, 'checkpoint_dir', None)
498-
for entry in listing:
499-
if entry != cp_dir:
500-
raise web.HTTPError(400, u'Directory %s not empty' % os_path)
501-
elif not os.path.isfile(os_path):
502-
raise web.HTTPError(404, u'File does not exist: %s' % os_path)
492+
if not os.path.exists(os_path):
493+
raise web.HTTPError(404, u'File or directory does not exist: %s' % os_path)
503494

504495
if self.delete_to_trash:
505496
self.log.debug("Sending %s to trash", os_path)
@@ -510,6 +501,14 @@ def delete_file(self, path):
510501
return
511502

512503
if os.path.isdir(os_path):
504+
listing = os.listdir(os_path)
505+
# Don't permanently delete non-empty directories.
506+
# A directory containing only leftover checkpoints is
507+
# considered empty.
508+
cp_dir = getattr(self.checkpoints, 'checkpoint_dir', None)
509+
for entry in listing:
510+
if entry != cp_dir:
511+
raise web.HTTPError(400, u'Directory %s not empty' % os_path)
513512
self.log.debug("Removing directory %s", os_path)
514513
with self.perm_to_403():
515514
shutil.rmtree(os_path)

notebook/services/contents/tests/test_contents_api.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -523,9 +523,11 @@ def test_delete_dirs(self):
523523
self.assertEqual(listing, [])
524524

525525
def test_delete_non_empty_dir(self):
526-
"""delete non-empty dir raises 400"""
527-
with assert_http_error(400):
528-
self.api.delete(u'å b')
526+
# Test that non empty directory can be deleted
527+
self.api.delete(u'å b')
528+
# Check if directory has actually been deleted
529+
with assert_http_error(404):
530+
self.api.list(u'å b')
529531

530532
def test_rename(self):
531533
resp = self.api.rename('foo/a.ipynb', 'foo/z.ipynb')

0 commit comments

Comments
 (0)