Permalink
Browse files

Change delete_empty_folders add ek.ek, keep_dir, ignore_items

Add ek.ek
Add keep_dir, don't delete show.location during postprocessing
Add ignore_items : treat folder empty when it only contains
files, folders in ignore_items
  • Loading branch information...
1 parent 45f1628 commit a2d6e2f57007c93191b692eebc53a0282d12403d Patrick Vos committed Sep 19, 2012
Showing with 29 additions and 18 deletions.
  1. +24 −12 sickbeard/helpers.py
  2. +5 −6 sickbeard/postProcessor.py
View
@@ -501,24 +501,36 @@ def rename_ep_file(cur_path, new_path):
return True
-def delete_empty_folders(check_empty_dir):
+def delete_empty_folders(check_empty_dir, keep_dir=None):
"""
Walks backwards up the path and deletes any empty folders found.
-
+
check_empty_dir: The path to clean (absolute path to a folder)
+ keep_dir: Clean until this path is reached
"""
-
- logger.log(u"Trying to clean any empty folders under "+check_empty_dir)
-
+
+ # treat check_empty_dir as empty when it only contains these items
+ ignore_items = []
+
+ logger.log(u"Trying to clean any empty folders under " + check_empty_dir)
+
# as long as the folder exists and doesn't contain any files, delete it
- while os.path.isdir(check_empty_dir) and not os.listdir(check_empty_dir):
- logger.log(u"Deleting empty folder: "+check_empty_dir)
- try:
- os.rmdir(check_empty_dir)
- except (WindowsError, OSError), e:
- logger.log(u"Unable to delete "+check_empty_dir+": "+repr(e)+" / "+str(e), logger.WARNING)
+ while ek.ek(os.path.isdir, check_empty_dir) and check_empty_dir != keep_dir:
+
+ check_files = ek.ek(os.listdir, check_empty_dir)
+
+ if not check_files or (len(check_files) <= len(ignore_items) and all([check_file in ignore_items for check_file in check_files])):
+ # directory is empty or contains only ignore_items
+ try:
+ logger.log(u"Deleting empty folder: " + check_empty_dir)
+ # need shutil.rmtree when ignore_items is really implemented
+ ek.ek(os.rmdir, check_empty_dir)
+ except (WindowsError, OSError), e:
+ logger.log(u"Unable to delete " + check_empty_dir + ": " + repr(e) + " / " + str(e), logger.WARNING)
+ break
+ check_empty_dir = ek.ek(os.path.dirname, check_empty_dir)
+ else:
break
- check_empty_dir = os.path.dirname(check_empty_dir)
def chmodAsParent(childPath):
View
@@ -202,11 +202,7 @@ def _delete(self, file_path, associated_files=False):
ek.ek(os.remove, cur_file)
# do the library update for synoindex
notifiers.synoindex_notifier.deleteFile(cur_file)
-
-
- # clean up any left over folders
- helpers.delete_empty_folders(ek.ek(os.path.dirname, file_path))
-
+
def _combined_file_operation (self, file_path, new_path, new_base_name, associated_files=False, action=None):
"""
Performs a generic operation (move or copy) on a file. Can rename the file as well as change its location,
@@ -760,9 +756,12 @@ def process(self):
for cur_ep in [ep_obj] + ep_obj.relatedEps:
try:
self._delete(cur_ep.location, associated_files=True)
+ # clean up any left over folders
+ if cur_ep.location:
+ helpers.delete_empty_folders(ek.ek(os.path.dirname, cur_ep.location), keep_dir=ep_obj.show._location)
except OSError, IOError:
raise exceptions.PostProcessingFailed("Unable to delete the existing files")
-
+
# if the show directory doesn't exist then make it if allowed
if not ek.ek(os.path.isdir, ep_obj.show._location) and sickbeard.CREATE_MISSING_SHOW_DIRS:
self._log(u"Show directory doesn't exist, creating it", logger.DEBUG)

0 comments on commit a2d6e2f

Please sign in to comment.