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

fix sciserver downloads in heasarc (#3182) #3183

Merged
merged 2 commits into from
Jan 23, 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
6 changes: 5 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ linelists.cdms
- Add whole catalog retrieval, improve error messaging for unparseable lines,
improve metadata catalog, and improve lookuptable behavior [#3173,#2901]

heasarc
^^^^^^^

- Fix Heasarc.download_data for Sciserver. [#3183]

jplspec
^^^^^^^

Expand Down Expand Up @@ -69,7 +74,6 @@ Infrastructure, Utility and Other Changes and Additions
-------------------------------------------------------



0.4.8 (2025-01-16)
==================

Expand Down
10 changes: 6 additions & 4 deletions astroquery/heasarc/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,16 +692,17 @@ def _copy_sciserver(self, links, location='.'):
Users should be using `~self.download_data` instead
"""
if not (os.path.exists('/FTP/') and os.environ['HOME'].split('/')[-1] == 'idies'):
if not os.path.exists('/FTP/'):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not relevant to this PR, but slowly we should move along to use pathlib

raise FileNotFoundError(
'No data archive found. This should be run on Sciserver '
'with the data drive mounted.'
)

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

for link in links['sciserver']:
link = str(link)
log.info(f'Copying to {link} from the data drive ...')
if not os.path.exists(link):
raise ValueError(
Expand All @@ -711,7 +712,8 @@ def _copy_sciserver(self, links, location='.'):
'Heasarc Help desk'
)
if os.path.isdir(link):
shutil.copytree(link, location)
download_dir = os.path.basename(link.strip('/'))
shutil.copytree(link, f'{location}/{download_dir}')
else:
shutil.copy(link, location)

Expand Down
40 changes: 29 additions & 11 deletions astroquery/heasarc/tests/test_heasarc.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst

import os
import shutil
import pytest
import tempfile
from unittest.mock import patch, PropertyMock
from astropy.coordinates import SkyCoord
from astropy.table import Table
Expand Down Expand Up @@ -304,6 +304,24 @@ def test_download_data__missingcolumn(host):
Heasarc.download_data(Table({"id": [1]}), host=host)


def test_download_data__sciserver():
with tempfile.TemporaryDirectory() as tmpdir:
datadir = f'{tmpdir}/data'
downloaddir = f'{tmpdir}/download'
os.makedirs(datadir, exist_ok=True)
with open(f'{datadir}/file.txt', 'w') as fp:
fp.write('data')
# include both a file and a directory
tab = Table({'sciserver': [f'{tmpdir}/data/file.txt', f'{tmpdir}/data']})
# The patch is to avoid the test that we are on sciserver
with patch('os.path.exists') as exists:
exists.return_value = True
Heasarc.download_data(tab, host="sciserver", location=downloaddir)
assert os.path.exists(f'{downloaddir}/file.txt')
assert os.path.exists(f'{downloaddir}/data')
assert os.path.exists(f'{downloaddir}/data/file.txt')


def test_download_data__outside_sciserver():
with pytest.raises(
FileNotFoundError,
Expand Down Expand Up @@ -350,20 +368,20 @@ def test_s3_mock_basic(s3_mock):
def test_s3_mock_file(s3_mock):
links = Table({"aws": [f"s3://{s3_bucket}/{s3_key1}"]})
Heasarc.enable_cloud(profile=False)
Heasarc.download_data(links, host="aws", location=".")
file = s3_key1.split("/")[-1]
assert os.path.exists(file)
os.remove(file)
with tempfile.TemporaryDirectory() as tmpdir:
Heasarc.download_data(links, host="aws", location=tmpdir)
file = s3_key1.split("/")[-1]
assert os.path.exists(f'{tmpdir}/{file}')


@pytest.mark.filterwarnings("ignore::DeprecationWarning")
@pytest.mark.skipif("not DO_AWS_S3")
def test_s3_mock_directory(s3_mock):
links = Table({"aws": [f"s3://{s3_bucket}/{s3_dir}"]})
Heasarc.enable_cloud(profile=False)
Heasarc.download_data(links, host="aws", location=".")
assert os.path.exists("location")
assert os.path.exists("location/file1.txt")
assert os.path.exists("location/sub/file2.txt")
assert os.path.exists("location/sub/sub2/file3.txt")
shutil.rmtree("location")
with tempfile.TemporaryDirectory() as tmpdir:
Heasarc.download_data(links, host="aws", location=tmpdir)
assert os.path.exists(f"{tmpdir}/location")
assert os.path.exists(f"{tmpdir}/location/file1.txt")
assert os.path.exists(f"{tmpdir}/location/sub/file2.txt")
assert os.path.exists(f"{tmpdir}/location/sub/sub2/file3.txt")
Loading