Skip to content

Commit

Permalink
Ignore FileNotFoundError on delete or unlink
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberrumor committed Mar 14, 2024
1 parent 1528737 commit baf8d70
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
33 changes: 26 additions & 7 deletions ammo/mod_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -757,17 +766,21 @@ 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()
else:
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)

Expand All @@ -781,15 +794,21 @@ 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:
download = self.downloads.pop(index)
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:
"""
Expand Down
15 changes: 12 additions & 3 deletions ammo/tool_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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":
Expand 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:
"""
Expand Down

0 comments on commit baf8d70

Please sign in to comment.