This commit is contained in:
bobokun 2024-10-13 16:12:37 -04:00
parent f19a0c149c
commit 8476b7846c
No known key found for this signature in database
GPG key ID: B73932169607D927
3 changed files with 22 additions and 8 deletions

View file

@ -1 +1 @@
4.1.12-develop2
4.1.12-develop3

View file

@ -29,7 +29,8 @@ class RemoveOrphaned:
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 @@ class RemoveOrphaned:
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 @@ class RemoveOrphaned:
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,
)

View file

@ -6,6 +6,7 @@ import os
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)