From 7aca8482689b0eb91fc0b4ea1cbe89df69a67eef Mon Sep 17 00:00:00 2001 From: jbannon Date: Sun, 7 Aug 2022 06:00:28 +0000 Subject: [PATCH] [BACKEND] Do not write download archive file if no files changed --- src/ytdl_sub/cli/main.py | 13 ++++++++----- src/ytdl_sub/utils/file_handler.py | 9 +++++++++ .../ytdl_additions/enhanced_download_archive.py | 7 ++++--- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/ytdl_sub/cli/main.py b/src/ytdl_sub/cli/main.py index 52080fc2..9734b966 100644 --- a/src/ytdl_sub/cli/main.py +++ b/src/ytdl_sub/cli/main.py @@ -112,11 +112,14 @@ def main(): ) for subscription, transaction_log in transaction_logs: - logger.info( - "Downloads for %s:\n%s\n", - subscription.name, - transaction_log.to_output_message(subscription.output_directory), - ) + if transaction_log.is_empty: + logger.info("No files changed for %s", subscription.name) + else: + logger.info( + "Downloads for %s:\n%s\n", + subscription.name, + transaction_log.to_output_message(subscription.output_directory), + ) # Ran successfully, so we can delete the debug file Logger.cleanup(delete_debug_file=True) diff --git a/src/ytdl_sub/utils/file_handler.py b/src/ytdl_sub/utils/file_handler.py index 5e4a4e1f..c9531370 100644 --- a/src/ytdl_sub/utils/file_handler.py +++ b/src/ytdl_sub/utils/file_handler.py @@ -93,6 +93,15 @@ class FileHandlerTransactionLog: self.files_created: Dict[str, FileMetadata] = {} self.files_removed: Set[str] = set() + @property + def is_empty(self) -> bool: + """ + Returns + ------- + True if no transaction logs are recorded. False otherwise + """ + return len(self.files_created) == 0 and len(self.files_removed) == 0 + def log_created_file( self, file_name: str, file_metadata: Optional[FileMetadata] = None ) -> "FileHandlerTransactionLog": diff --git a/src/ytdl_sub/ytdl_additions/enhanced_download_archive.py b/src/ytdl_sub/ytdl_additions/enhanced_download_archive.py index 4776e734..b39cacd7 100644 --- a/src/ytdl_sub/ytdl_additions/enhanced_download_archive.py +++ b/src/ytdl_sub/ytdl_additions/enhanced_download_archive.py @@ -537,14 +537,15 @@ class EnhancedDownloadArchive: def save_download_mappings(self) -> "EnhancedDownloadArchive": """ - Saves the updated download mappings to the output directory + Saves the updated download mappings to the output directory if any files were changed. Returns ------- self """ - self._download_mapping.to_file(output_json_file=self._mapping_working_file_path) - self.save_file_to_output_directory(file_name=self._mapping_file_name) + if not self.get_file_handler_transaction_log().is_empty: + self._download_mapping.to_file(output_json_file=self._mapping_working_file_path) + self.save_file_to_output_directory(file_name=self._mapping_file_name) return self def save_file_to_output_directory(