Make remove_orphaned even faster

This is not an addiction I swear
This commit is contained in:
buthed010203 2023-05-23 10:12:51 -04:00 committed by GitHub
parent be742694d1
commit 2fd5701922
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -28,31 +28,24 @@ class RemoveOrphaned:
self.stats = 0 self.stats = 0
logger.separator("Checking for Orphaned Files", space=False, border=False) logger.separator("Checking for Orphaned Files", space=False, border=False)
torrent_files = [] torrent_files = []
root_files = []
orphaned_files = [] orphaned_files = []
excluded_orphan_files = [] excluded_orphan_files = []
root_files = util.get_root_files(self.remote_dir, self.root_dir, self.orphaned_dir) root_files = self.executor.submit(util.get_root_files, self.remote_dir, self.root_dir, self.orphaned_dir)
# Get an updated list of torrents # Get an updated list of torrents
logger.print_line("Locating orphan files", self.config.loglevel) logger.print_line("Locating orphan files", self.config.loglevel)
torrent_list = self.qbt.get_torrents({"sort": "added_on"}) torrent_list = self.qbt.get_torrents({"sort": "added_on"})
torrent_files_and_save_path = []
for torrent in torrent_list:
torrent_files = []
for torrent_files_dict in torrent.files:
torrent_files.append(torrent_files_dict.name)
torrent_files_and_save_path.append((torrent_files, torrent.save_path))
torrent_files.extend( torrent_files.extend(
[ [
fullpath fullpath
for fullpathlist in self.executor.map(get_full_path_of_torrent_files, torrent_files_and_save_path) for fullpathlist in self.executor.map(self.get_full_path_of_torrent_files, torrent_list)
for fullpath in fullpathlist for fullpath in fullpathlist
if fullpath not in torrent_files
] ]
) )
orphaned_files = set(root_files) - set(torrent_files) orphaned_files = set(root_files.result()) - set(torrent_files)
if self.config.orphaned["exclude_patterns"]: if self.config.orphaned["exclude_patterns"]:
logger.print_line("Processing orphan exclude patterns") logger.print_line("Processing orphan exclude patterns")
@ -93,6 +86,7 @@ class RemoveOrphaned:
orphaned_parent_path = set(self.executor.map(self.move_orphan, orphaned_files)) orphaned_parent_path = set(self.executor.map(self.move_orphan, 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(lambda dir: util.remove_empty_directories(dir, "**/*"), orphaned_parent_path) self.executor.map(lambda dir: util.remove_empty_directories(dir, "**/*"), orphaned_parent_path)
else: else:
logger.print_line("No Orphaned Files found.", self.config.loglevel) logger.print_line("No Orphaned Files found.", self.config.loglevel)
@ -102,13 +96,14 @@ class RemoveOrphaned:
util.move_files(src, dest, True) util.move_files(src, dest, True)
return os.path.dirname(file).replace(self.root_dir, self.remote_dir) return os.path.dirname(file).replace(self.root_dir, self.remote_dir)
def get_full_path_of_torrent_files(self, torrent):
torrent_files = map(lambda dict: dict.name, torrent.files)
save_path = torrent.save_path
def get_full_path_of_torrent_files(torrent_files_and_save_path): fullpath_torrent_files = []
torrent_files, save_path = torrent_files_and_save_path for file in torrent_files:
fullpath_torrent_files = [] fullpath = os.path.join(save_path, file)
for file in torrent_files: # Replace fullpath with \\ if qbm is running in docker (linux) but qbt is on windows
fullpath = os.path.join(save_path, file) fullpath = fullpath.replace(r"/", "\\") if ":\\" in fullpath else fullpath
# Replace fullpath with \\ if qbm is running in docker (linux) but qbt is on windows fullpath_torrent_files.append(fullpath)
fullpath = fullpath.replace(r"/", "\\") if ":\\" in fullpath else fullpath return fullpath_torrent_files
fullpath_torrent_files.append(fullpath)
return fullpath_torrent_files