Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check registry file hash before downloading #93

Merged
merged 15 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions src/lephare/data_retrieval.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,46 @@ def download_registry_from_github(url="", outfile=""):
Parameters
----------
url : str
The URL of the registry file. Defaults to a "data-registry.txt" file at
The URL of the registry file. Defaults to a "data_registry.txt" file at
DEFAULT_BASE_DATA_URL.
outfile : str
The path where the file will be saved. Defaults to DEFAULT_REGISTRY_FILE.

Notes
-----
We make the assumption that the hash file for the registry will be stored in
the same directory as the registry file, with the same name (sans extension)
plus "_hash.sha256".

Raises
------
Exception
If the file cannot be fetched from the URL.
"""
remote_registry_name = "data_registry.txt"

# Assign defaults if keywords left blank
if url == "":
url = urljoin(DEFAULT_BASE_DATA_URL, "data-registry.txt")
url = urljoin(DEFAULT_BASE_DATA_URL, remote_registry_name)
if outfile == "":
outfile = DEFAULT_REGISTRY_FILE

# Try to download the registry hash file, to see if we can skip downloading
# the actual registry file
if os.path.isfile(outfile):
local_registry_hash = pooch.file_hash(outfile, alg="sha256")
registry_hash_url = os.path.splitext(url)[0] + "_hash.sha256"
try:
response = requests.get(registry_hash_url, timeout=60)
if response.status_code == 200:
remote_registry_hash = response.text.strip()
if local_registry_hash == remote_registry_hash:
print(f"Local registry file is up to date: {outfile}")
return
except requests.exceptions.RequestException as e:
OliviaLynn marked this conversation as resolved.
Show resolved Hide resolved
print(f"Failed to fetch registry hash file: {e}")

# Download the registry file
response = requests.get(url, timeout=60)
if response.status_code == 200:
with open(outfile, "w", encoding="utf-8") as file:
Expand Down
17 changes: 17 additions & 0 deletions tests/lephare/test_data_retrieval.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,23 @@ def test_download_registry_from_github_success(mock_get):
assert file.read() == "file1\nfile2\nfile3"


@patch("os.path.isfile")
@patch("pooch.file_hash")
@patch("requests.get")
def test_download_registry_hash_already_matches(mock_get, mock_file_hash, mock_isfile):
mock_isfile.return_value = True
mock_file_hash.return_value = "registryhash123"
mock_get.return_value.status_code = 200
mock_get.return_value.text = "registryhash123"

with tempfile.TemporaryDirectory() as tmpdir:
outfile = os.path.join(tmpdir, "registry.txt")
download_registry_from_github(url="http://example.com/data_registry.txt", outfile=outfile)

assert mock_get.call_count == 1
assert mock_get.call_args[0][0] == "http://example.com/data_registry_hash.sha256"


@patch("requests.get")
def test_download_registry_from_github_failure(mock_get):
mock_get.return_value.status_code = 404
Expand Down
Loading