Skip to content

Log a warning if a cached file's length cannot be verified #3290

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

Merged
merged 1 commit into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 18 additions & 0 deletions astroquery/mast/tests/test_mast_remote.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst

import logging
from pathlib import Path
import numpy as np
import os
Expand Down Expand Up @@ -769,6 +770,23 @@ def test_observations_download_file_escaped(self, tmp_path):
f = fits.open(Path(tmp_path, filename))
f.close()

def test_observations_download_file_no_length(self, tmp_path, caplog):
# test that `download_file` correctly handles the case where the server
# does not return a Content-Length header for a cached file
# initial download
in_uri = "mast:HLA/url/cgi-bin/getdata.cgi?filename=hst_05206_01_wfpc2_f375n_wf_daophot_trm.cat"
filename = Path(in_uri).name
result = Observations.download_file(uri=in_uri, local_path=tmp_path)
assert result == ("COMPLETE", None, None)
assert Path(tmp_path, filename).exists()

# download again, should warn and re-download file
with caplog.at_level(logging.WARNING):
result = Observations.download_file(uri=in_uri, local_path=tmp_path)
assert "Could not verify length of cached file" in caplog.text
assert result == ("COMPLETE", None, None)
assert Path(tmp_path, filename).exists()

@pytest.mark.parametrize("test_data_uri, expected_cloud_uri", [
("mast:HST/product/u24r0102t_c1f.fits",
"s3://stpubdata/hst/public/u24r/u24r0102t/u24r0102t_c1f.fits"),
Expand Down
14 changes: 11 additions & 3 deletions astroquery/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,9 +487,17 @@
response.close()
return local_filepath
else:
# This case doesn't appear reachable under normal circumstances
# It is not covered by tests, and probably indicates a badly-behaved server
raise ValueError(f"Found cached file {local_filepath}. Could not verify length.")
# This is a special case where the server doesn't return a
# Content-Length header, but the file is already cached.
# One such case is dynamically generated files in the MAST Archive.
# In this case, we warn the user and re-download the file.
log.warning(f"Could not verify length of cached file {local_filepath}. "

Check warning on line 494 in astroquery/query.py

View check run for this annotation

Codecov / codecov/patch

astroquery/query.py#L494

Added line #L494 was not covered by tests
"Re-downloading the file.")
open_mode = 'wb'
response = self._session.request(method, url,

Check warning on line 497 in astroquery/query.py

View check run for this annotation

Codecov / codecov/patch

astroquery/query.py#L496-L497

Added lines #L496 - L497 were not covered by tests
timeout=timeout, stream=True,
auth=auth, **kwargs)
response.raise_for_status()

Check warning on line 500 in astroquery/query.py

View check run for this annotation

Codecov / codecov/patch

astroquery/query.py#L500

Added line #L500 was not covered by tests
else:
open_mode = 'wb'
if head_safe:
Expand Down
Loading