Skip to content

Commit

Permalink
Merge pull request #3108 from kirit93/fixes2760
Browse files Browse the repository at this point in the history
Allowing non empty dirs to be deleted
  • Loading branch information
takluyver authored Dec 5, 2017
2 parents b1e5f72 + b7e02a6 commit ae011a1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
21 changes: 10 additions & 11 deletions notebook/services/contents/filemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,17 +489,8 @@ def delete_file(self, path):
path = path.strip('/')
os_path = self._get_os_path(path)
rm = os.unlink
if os.path.isdir(os_path):
listing = os.listdir(os_path)
# Don't delete non-empty directories.
# A directory containing only leftover checkpoints is
# considered empty.
cp_dir = getattr(self.checkpoints, 'checkpoint_dir', None)
for entry in listing:
if entry != cp_dir:
raise web.HTTPError(400, u'Directory %s not empty' % os_path)
elif not os.path.isfile(os_path):
raise web.HTTPError(404, u'File does not exist: %s' % os_path)
if not os.path.exists(os_path):
raise web.HTTPError(404, u'File or directory does not exist: %s' % os_path)

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

if os.path.isdir(os_path):
listing = os.listdir(os_path)
# Don't permanently delete non-empty directories.
# A directory containing only leftover checkpoints is
# considered empty.
cp_dir = getattr(self.checkpoints, 'checkpoint_dir', None)
for entry in listing:
if entry != cp_dir:
raise web.HTTPError(400, u'Directory %s not empty' % os_path)
self.log.debug("Removing directory %s", os_path)
with self.perm_to_403():
shutil.rmtree(os_path)
Expand Down
8 changes: 5 additions & 3 deletions notebook/services/contents/tests/test_contents_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,9 +523,11 @@ def test_delete_dirs(self):
self.assertEqual(listing, [])

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

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

0 comments on commit ae011a1

Please sign in to comment.