Skip to content

Commit

Permalink
fixes #624
Browse files Browse the repository at this point in the history
  • Loading branch information
bobokun committed Oct 13, 2024
1 parent f19a0c1 commit 8476b78
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.1.12-develop2
4.1.12-develop3
17 changes: 11 additions & 6 deletions modules/core/remove_orphaned.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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")
Expand Down Expand Up @@ -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,
)

Expand Down
11 changes: 10 additions & 1 deletion modules/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import shutil
import signal
import time
from fnmatch import fnmatch
from pathlib import Path

import requests
Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand Down

0 comments on commit 8476b78

Please sign in to comment.