Skip to content

Commit 020702b

Browse files
authored
Merge pull request #3183 from zoghbi-a/heasarc-fix
fix sciserver downloads in heasarc (#3182)
2 parents 013261c + ffb26f6 commit 020702b

File tree

3 files changed

+40
-16
lines changed

3 files changed

+40
-16
lines changed

CHANGES.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ linelists.cdms
1414
- Add whole catalog retrieval, improve error messaging for unparseable lines,
1515
improve metadata catalog, and improve lookuptable behavior [#3173,#2901]
1616

17+
heasarc
18+
^^^^^^^
19+
20+
- Fix Heasarc.download_data for Sciserver. [#3183]
21+
1722
jplspec
1823
^^^^^^^
1924

@@ -69,7 +74,6 @@ Infrastructure, Utility and Other Changes and Additions
6974
-------------------------------------------------------
7075

7176

72-
7377
0.4.8 (2025-01-16)
7478
==================
7579

astroquery/heasarc/core.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -692,16 +692,17 @@ def _copy_sciserver(self, links, location='.'):
692692
Users should be using `~self.download_data` instead
693693
694694
"""
695-
if not (os.path.exists('/FTP/') and os.environ['HOME'].split('/')[-1] == 'idies'):
695+
if not os.path.exists('/FTP/'):
696696
raise FileNotFoundError(
697697
'No data archive found. This should be run on Sciserver '
698698
'with the data drive mounted.'
699699
)
700700

701-
if not os.path.exists(location):
702-
os.mkdir(location)
701+
# make sure the output folder exits
702+
os.makedirs(location, exist_ok=True)
703703

704704
for link in links['sciserver']:
705+
link = str(link)
705706
log.info(f'Copying to {link} from the data drive ...')
706707
if not os.path.exists(link):
707708
raise ValueError(
@@ -711,7 +712,8 @@ def _copy_sciserver(self, links, location='.'):
711712
'Heasarc Help desk'
712713
)
713714
if os.path.isdir(link):
714-
shutil.copytree(link, location)
715+
download_dir = os.path.basename(link.strip('/'))
716+
shutil.copytree(link, f'{location}/{download_dir}')
715717
else:
716718
shutil.copy(link, location)
717719

astroquery/heasarc/tests/test_heasarc.py

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

33
import os
4-
import shutil
54
import pytest
5+
import tempfile
66
from unittest.mock import patch, PropertyMock
77
from astropy.coordinates import SkyCoord
88
from astropy.table import Table
@@ -304,6 +304,24 @@ def test_download_data__missingcolumn(host):
304304
Heasarc.download_data(Table({"id": [1]}), host=host)
305305

306306

307+
def test_download_data__sciserver():
308+
with tempfile.TemporaryDirectory() as tmpdir:
309+
datadir = f'{tmpdir}/data'
310+
downloaddir = f'{tmpdir}/download'
311+
os.makedirs(datadir, exist_ok=True)
312+
with open(f'{datadir}/file.txt', 'w') as fp:
313+
fp.write('data')
314+
# include both a file and a directory
315+
tab = Table({'sciserver': [f'{tmpdir}/data/file.txt', f'{tmpdir}/data']})
316+
# The patch is to avoid the test that we are on sciserver
317+
with patch('os.path.exists') as exists:
318+
exists.return_value = True
319+
Heasarc.download_data(tab, host="sciserver", location=downloaddir)
320+
assert os.path.exists(f'{downloaddir}/file.txt')
321+
assert os.path.exists(f'{downloaddir}/data')
322+
assert os.path.exists(f'{downloaddir}/data/file.txt')
323+
324+
307325
def test_download_data__outside_sciserver():
308326
with pytest.raises(
309327
FileNotFoundError,
@@ -350,20 +368,20 @@ def test_s3_mock_basic(s3_mock):
350368
def test_s3_mock_file(s3_mock):
351369
links = Table({"aws": [f"s3://{s3_bucket}/{s3_key1}"]})
352370
Heasarc.enable_cloud(profile=False)
353-
Heasarc.download_data(links, host="aws", location=".")
354-
file = s3_key1.split("/")[-1]
355-
assert os.path.exists(file)
356-
os.remove(file)
371+
with tempfile.TemporaryDirectory() as tmpdir:
372+
Heasarc.download_data(links, host="aws", location=tmpdir)
373+
file = s3_key1.split("/")[-1]
374+
assert os.path.exists(f'{tmpdir}/{file}')
357375

358376

359377
@pytest.mark.filterwarnings("ignore::DeprecationWarning")
360378
@pytest.mark.skipif("not DO_AWS_S3")
361379
def test_s3_mock_directory(s3_mock):
362380
links = Table({"aws": [f"s3://{s3_bucket}/{s3_dir}"]})
363381
Heasarc.enable_cloud(profile=False)
364-
Heasarc.download_data(links, host="aws", location=".")
365-
assert os.path.exists("location")
366-
assert os.path.exists("location/file1.txt")
367-
assert os.path.exists("location/sub/file2.txt")
368-
assert os.path.exists("location/sub/sub2/file3.txt")
369-
shutil.rmtree("location")
382+
with tempfile.TemporaryDirectory() as tmpdir:
383+
Heasarc.download_data(links, host="aws", location=tmpdir)
384+
assert os.path.exists(f"{tmpdir}/location")
385+
assert os.path.exists(f"{tmpdir}/location/file1.txt")
386+
assert os.path.exists(f"{tmpdir}/location/sub/file2.txt")
387+
assert os.path.exists(f"{tmpdir}/location/sub/sub2/file3.txt")

0 commit comments

Comments
 (0)