Skip to content

Commit

Permalink
Add exception handling for file removal in fetch_remote_file function
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilipMay committed Dec 8, 2023
1 parent afb9930 commit cef3e5f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
8 changes: 7 additions & 1 deletion mltb2/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"""


import contextlib
import os
from typing import Optional

Expand Down Expand Up @@ -45,5 +46,10 @@ def fetch_remote_file(dirname, filename, url, sha256_checksum) -> str:
IOError: if the sha256 checksum is wrong
"""
remote = RemoteFileMetadata(filename=filename, url=url, checksum=sha256_checksum)
fetch_remote_file_path = _fetch_remote(remote, dirname=dirname)
try:
fetch_remote_file_path = _fetch_remote(remote, dirname=dirname)
except Exception:
with contextlib.suppress(FileNotFoundError):
os.remove(os.path.join(dirname, filename))
raise
return fetch_remote_file_path
2 changes: 2 additions & 0 deletions tests/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def test_fetch_remote_file(tmpdir):
sha256_checksum="8d834de97b095fbf4bf6075743827862be2c6c404594ae04606d9c56d8f1017b",
)
assert remote_file == os.path.join(tmpdir, filename)
assert os.path.exists(os.path.join(tmpdir, filename))


def test_fetch_remote_file_wrong_checksum(tmpdir):
Expand All @@ -29,6 +30,7 @@ def test_fetch_remote_file_wrong_checksum(tmpdir):
url="https://raw.githubusercontent.com/telekom/mltb2/main/LICENSE",
sha256_checksum="wrong",
)
assert not os.path.exists(os.path.join(tmpdir, filename))


def test_get_and_create_mltb2_data_dir(tmpdir):
Expand Down

0 comments on commit cef3e5f

Please sign in to comment.