Cleaner empty dir cleanup

This commit is contained in:
Ruud
2014-06-03 22:49:12 +02:00
parent 4b236c6ed6
commit ab2b2cfe6e
+12 -24
View File
@@ -143,34 +143,22 @@ class Plugin(object):
def deleteEmptyFolder(self, folder, show_error = True, only_clean = None): def deleteEmptyFolder(self, folder, show_error = True, only_clean = None):
folder = sp(folder) folder = sp(folder)
allowed_dirs = [] for item in os.listdir(folder):
if only_clean: full_folder = os.path.join(folder, item)
for item in os.listdir(folder):
full_path = os.path.join(folder, item)
if item in only_clean and os.path.isdir(full_path):
allowed_dirs.append(full_path)
for root, dirs, files in os.walk(folder): if not only_clean or (item in only_clean and os.path.isdir(full_folder)):
for dir_name in dirs: for root, dirs, files in os.walk(full_folder):
full_path = os.path.join(root, dir_name)
if only_clean: for dir_name in dirs:
allow = False full_path = os.path.join(root, dir_name)
for allowed_dir in allowed_dirs:
if allowed_dir in full_path:
allow = True
break
if not allow: if len(os.listdir(full_path)) == 0:
continue try:
os.rmdir(full_path)
if len(os.listdir(full_path)) == 0: except:
try: if show_error:
os.rmdir(full_path) log.error('Couldn\'t remove empty directory %s: %s', (full_path, traceback.format_exc()))
except:
if show_error:
log.error('Couldn\'t remove empty directory %s: %s', (full_path, traceback.format_exc()))
try: try:
os.rmdir(folder) os.rmdir(folder)