Skip to content

Commit 1ebd810

Browse files
authored
Merge pull request #3290 from snbianco/content-length-fix
Log a warning if a cached file's length cannot be verified
2 parents 6d87fc7 + 4ed16e4 commit 1ebd810

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

astroquery/mast/tests/test_mast_remote.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Licensed under a 3-clause BSD style license - see LICENSE.rst
22

3+
import logging
34
from pathlib import Path
45
import numpy as np
56
import os
@@ -769,6 +770,23 @@ def test_observations_download_file_escaped(self, tmp_path):
769770
f = fits.open(Path(tmp_path, filename))
770771
f.close()
771772

773+
def test_observations_download_file_no_length(self, tmp_path, caplog):
774+
# test that `download_file` correctly handles the case where the server
775+
# does not return a Content-Length header for a cached file
776+
# initial download
777+
in_uri = "mast:HLA/url/cgi-bin/getdata.cgi?filename=hst_05206_01_wfpc2_f375n_wf_daophot_trm.cat"
778+
filename = Path(in_uri).name
779+
result = Observations.download_file(uri=in_uri, local_path=tmp_path)
780+
assert result == ("COMPLETE", None, None)
781+
assert Path(tmp_path, filename).exists()
782+
783+
# download again, should warn and re-download file
784+
with caplog.at_level(logging.WARNING):
785+
result = Observations.download_file(uri=in_uri, local_path=tmp_path)
786+
assert "Could not verify length of cached file" in caplog.text
787+
assert result == ("COMPLETE", None, None)
788+
assert Path(tmp_path, filename).exists()
789+
772790
@pytest.mark.parametrize("test_data_uri, expected_cloud_uri", [
773791
("mast:HST/product/u24r0102t_c1f.fits",
774792
"s3://stpubdata/hst/public/u24r/u24r0102t/u24r0102t_c1f.fits"),

astroquery/query.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -487,9 +487,17 @@ def _download_file(self, url, local_filepath, timeout=None, auth=None,
487487
response.close()
488488
return local_filepath
489489
else:
490-
# This case doesn't appear reachable under normal circumstances
491-
# It is not covered by tests, and probably indicates a badly-behaved server
492-
raise ValueError(f"Found cached file {local_filepath}. Could not verify length.")
490+
# This is a special case where the server doesn't return a
491+
# Content-Length header, but the file is already cached.
492+
# One such case is dynamically generated files in the MAST Archive.
493+
# In this case, we warn the user and re-download the file.
494+
log.warning(f"Could not verify length of cached file {local_filepath}. "
495+
"Re-downloading the file.")
496+
open_mode = 'wb'
497+
response = self._session.request(method, url,
498+
timeout=timeout, stream=True,
499+
auth=auth, **kwargs)
500+
response.raise_for_status()
493501
else:
494502
open_mode = 'wb'
495503
if head_safe:

0 commit comments

Comments
 (0)