From baf8d70598f269651a34790d29dc970df880e158 Mon Sep 17 00:00:00 2001 From: cyberrumor Date: Wed, 13 Mar 2024 23:56:34 -0700 Subject: [PATCH] Ignore FileNotFoundError on delete or unlink --- ammo/mod_controller.py | 33 ++++++++++++++++++++++++++------- ammo/tool_controller.py | 15 ++++++++++++--- 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/ammo/mod_controller.py b/ammo/mod_controller.py index f85ea5c..e15b9c5 100755 --- a/ammo/mod_controller.py +++ b/ammo/mod_controller.py @@ -495,7 +495,10 @@ def _clean_game_dir(self): for file in filenames: full_path = d / file if full_path.is_symlink(): - full_path.unlink() + try: + full_path.unlink() + except FileNotFoundError: + pass self._remove_empty_dirs() @@ -707,7 +710,10 @@ def delete(self, component: DeleteEnum, index: Union[int, str]) -> None: self.deactivate(ComponentEnum.MOD, self.mods.index(mod)) for mod in visible_mods: self.mods.pop(self.mods.index(mod)) - shutil.rmtree(mod.location) + try: + shutil.rmtree(mod.location) + except FileNotFoundError: + pass deleted_mods += f"{mod.name}\n" self.commit() else: @@ -720,7 +726,10 @@ def delete(self, component: DeleteEnum, index: Union[int, str]) -> None: # Remove the mod from the controller then delete it. mod = self.mods.pop(index) - shutil.rmtree(mod.location) + try: + shutil.rmtree(mod.location) + except FileNotFoundError: + pass self.commit() case DeleteEnum.PLUGIN: @@ -757,8 +766,10 @@ def get_plugin_files(plugin): self.commit() self.plugins.pop(self.plugins.index(plugin)) for file in get_plugin_files(plugin): - if file.exists(): + try: file.unlink() + except FileNotFoundError: + pass deleted_plugins += f"{plugin.name}\n" self.refresh() self.commit() @@ -766,8 +777,10 @@ def get_plugin_files(plugin): try: plugin = self.plugins.pop(index) for file in get_plugin_files(plugin): - if file.exists(): + try: file.unlink() + except FileNotFoundError: + pass except IndexError as e: raise Warning(e) @@ -781,7 +794,10 @@ def get_plugin_files(plugin): download = self.downloads.pop( self.downloads.index(visible_download) ) - download.location.unlink() + try: + download.location.unlink() + except FileNotFoundError: + pass else: index = int(index) try: @@ -789,7 +805,10 @@ def get_plugin_files(plugin): except IndexError as e: # Demote IndexErrors raise Warning(e) - download.location.unlink() + try: + download.location.unlink() + except FileNotFoundError: + pass def install(self, index: Union[int, str]) -> None: """ diff --git a/ammo/tool_controller.py b/ammo/tool_controller.py index 08364ec..e8bd8e4 100755 --- a/ammo/tool_controller.py +++ b/ammo/tool_controller.py @@ -228,7 +228,10 @@ def delete(self, component: ToolEnum, index: Union[int, str]) -> None: self.deactivate(ComponentEnum.TOOL, self.tools.index(tool)) for tool in visible_tools: self.tools.pop(self.tools.index(tool)) - shutil.rmtree(tool) + try: + shutil.rmtree(tool) + except FileNotFoundError: + pass deleted_tools += f"{tool.name}\n" self.commit() else: @@ -239,7 +242,10 @@ def delete(self, component: ToolEnum, index: Union[int, str]) -> None: # Demote IndexErrors raise Warning(e) - shutil.rmtree(tool) + try: + shutil.rmtree(tool) + except FileNotFoundError: + pass case ToolEnum.DOWNLOAD: if index == "all": @@ -256,7 +262,10 @@ def delete(self, component: ToolEnum, index: Union[int, str]) -> None: except IndexError as e: # Demote IndexErrors raise Warning(e) - download.location.unlink() + try: + download.location.unlink() + except FileNotFoundError: + pass def install(self, index: Union[int, str]) -> None: """