diff --git a/VERSION b/VERSION index a82061a6..9ce6aa46 100755 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -4.1.12-develop2 +4.1.12-develop3 diff --git a/modules/core/remove_orphaned.py b/modules/core/remove_orphaned.py index a8179de1..b4fe7fb6 100644 --- a/modules/core/remove_orphaned.py +++ b/modules/core/remove_orphaned.py @@ -29,7 +29,8 @@ def rem_orphaned(self): logger.separator("Checking for Orphaned Files", space=False, border=False) torrent_files = [] orphaned_files = [] - excluded_orphan_files = [] + excluded_orphan_files = set() + exclude_patterns = [] root_files = self.executor.submit(util.get_root_files, self.root_dir, self.remote_dir, self.orphaned_dir) @@ -54,11 +55,13 @@ def rem_orphaned(self): exclude_pattern.replace(self.remote_dir, self.root_dir) for exclude_pattern in self.config.orphaned["exclude_patterns"] ] - excluded_orphan_files = [ - file for file in orphaned_files for exclude_pattern in exclude_patterns if fnmatch(file, exclude_pattern) - ] - orphaned_files = set(orphaned_files) - set(excluded_orphan_files) + for file in orphaned_files: + for exclude_pattern in exclude_patterns: + if fnmatch(file, exclude_pattern): + excluded_orphan_files.add(file) + + orphaned_files = orphaned_files - excluded_orphan_files # Check the threshold before deleting orphaned files max_orphaned_files_to_delete = self.config.orphaned.get("max_orphaned_files_to_delete") @@ -105,7 +108,9 @@ def rem_orphaned(self): orphaned_parent_path = set(self.executor.map(self.handle_orphaned_files, orphaned_files)) logger.print_line("Removing newly empty directories", self.config.loglevel) self.executor.map( - lambda directory: util.remove_empty_directories(directory, self.qbt.get_category_save_paths()), + lambda directory: util.remove_empty_directories( + directory, self.qbt.get_category_save_paths(), exclude_patterns + ), orphaned_parent_path, ) diff --git a/modules/util.py b/modules/util.py index d028e6dd..b32b084f 100755 --- a/modules/util.py +++ b/modules/util.py @@ -6,6 +6,7 @@ import shutil import signal import time +from fnmatch import fnmatch from pathlib import Path import requests @@ -486,7 +487,7 @@ def copy_files(src, dest): logger.error(ex) -def remove_empty_directories(pathlib_root_dir, excluded_paths=None): +def remove_empty_directories(pathlib_root_dir, excluded_paths=None, exclude_patterns=[]): """Remove empty directories recursively, optimized version.""" pathlib_root_dir = Path(pathlib_root_dir) if excluded_paths is not None: @@ -499,6 +500,14 @@ def remove_empty_directories(pathlib_root_dir, excluded_paths=None): if excluded_paths and root_path in excluded_paths: continue + exclude_pattern_match = False + for exclude_pattern in exclude_patterns: + if fnmatch(os.path.join(root, ""), exclude_pattern): + exclude_pattern_match = True + break + if exclude_pattern_match: + continue + # Attempt to remove the directory if it's empty try: os.rmdir(root)