mirror of
https://github.com/StuffAnThings/qbit_manage.git
synced 2025-10-12 06:47:18 +08:00
fixes #624
This commit is contained in:
parent
f19a0c149c
commit
8476b7846c
3 changed files with 22 additions and 8 deletions
2
VERSION
2
VERSION
|
@ -1 +1 @@
|
||||||
4.1.12-develop2
|
4.1.12-develop3
|
||||||
|
|
|
@ -29,7 +29,8 @@ class RemoveOrphaned:
|
||||||
logger.separator("Checking for Orphaned Files", space=False, border=False)
|
logger.separator("Checking for Orphaned Files", space=False, border=False)
|
||||||
torrent_files = []
|
torrent_files = []
|
||||||
orphaned_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)
|
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)
|
exclude_pattern.replace(self.remote_dir, self.root_dir)
|
||||||
for exclude_pattern in self.config.orphaned["exclude_patterns"]
|
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
|
# Check the threshold before deleting orphaned files
|
||||||
max_orphaned_files_to_delete = self.config.orphaned.get("max_orphaned_files_to_delete")
|
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))
|
orphaned_parent_path = set(self.executor.map(self.handle_orphaned_files, orphaned_files))
|
||||||
logger.print_line("Removing newly empty directories", self.config.loglevel)
|
logger.print_line("Removing newly empty directories", self.config.loglevel)
|
||||||
self.executor.map(
|
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,
|
orphaned_parent_path,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,7 @@ import os
|
||||||
import shutil
|
import shutil
|
||||||
import signal
|
import signal
|
||||||
import time
|
import time
|
||||||
|
from fnmatch import fnmatch
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
@ -486,7 +487,7 @@ def copy_files(src, dest):
|
||||||
logger.error(ex)
|
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."""
|
"""Remove empty directories recursively, optimized version."""
|
||||||
pathlib_root_dir = Path(pathlib_root_dir)
|
pathlib_root_dir = Path(pathlib_root_dir)
|
||||||
if excluded_paths is not None:
|
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:
|
if excluded_paths and root_path in excluded_paths:
|
||||||
continue
|
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
|
# Attempt to remove the directory if it's empty
|
||||||
try:
|
try:
|
||||||
os.rmdir(root)
|
os.rmdir(root)
|
||||||
|
|
Loading…
Add table
Reference in a new issue