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

remove call to system-wide gunzip. Use python version instead. #1613

Merged
merged 8 commits into from
Apr 16, 2020
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
37 changes: 10 additions & 27 deletions astroquery/utils/system_tools.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst
from __future__ import print_function
import subprocess
import os


# Import DEVNULL for py3 or py3
try:
from subprocess import DEVNULL
Expand All @@ -12,29 +10,6 @@

# Check availability of some system tools
# Exceptions are raised if not found
__is_gzip_found = False
for test_cmd in (["gzip", "-V"], ["7z"]):
try:
subprocess.call(test_cmd, stdout=DEVNULL, stderr=DEVNULL)
except OSError:
pass
else:
__is_gzip_found = test_cmd[0]


if __is_gzip_found == 'gzip':
def _unzip_cmd(filename):
return ["gzip", "-d", "{0}".format(filename)]
elif __is_gzip_found == '7z':
def _unzip_cmd(filename):
return ["7z", "x",
"{0}".format(filename),
"-o{0}".format(os.path.split(filename)[0])]
else:
print("gzip was not found on your system! You should solve this issue for "
"astroquery.eso to be at its best!\n"
"On POSIX system: make sure gzip is installed and in your path!"
"On Windows: same for 7-zip (http://www.7-zip.org)!")


def gunzip(filename):
Expand All @@ -50,9 +25,17 @@ def gunzip(filename):
Name of the decompressed file (or input filename if gzip is not
available).
"""
import shutil
import gzip

# system-wide 'gzip' was removed, Python gzip used instead.
# See #1538 : https://github.com/astropy/astroquery/issues/1538

# ".fz" denotes RICE rather than gzip compression
if __is_gzip_found and not filename.endswith('.fz'):
subprocess.call(_unzip_cmd(filename), stdout=DEVNULL, stderr=DEVNULL)
if not filename.endswith('.fz'):
with gzip.open(filename, 'rb') as f_in:
with open(filename.rsplit(".", 1)[0], 'wb') as f_out:
shutil.copyfileobj(f_in, f_out)
return filename.rsplit(".", 1)[0]
else:
return filename
Expand Down
44 changes: 44 additions & 0 deletions astroquery/utils/tests/test_system_tools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst

try:
import gzip

HAS_GZIP = True
except ImportError:
HAS_GZIP = False

import shutil
import os
from os.path import exists
import tempfile

import pytest
Copy link
Member

Choose a reason for hiding this comment

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

unused import.


from ..system_tools import gunzip


@pytest.mark.skipif("not HAS_GZIP")
def test_gunzip():

temp_dir = tempfile.mkdtemp()
filename = temp_dir + os.sep + "test_gunzip.txt.gz"
unziped_filename = filename.rsplit(".", 1)[0]

# First create a gzip file
content = b"Bla"
with gzip.open(filename, "wb") as f:
f.write(content)

try:
# Then test our gunzip command works and creates an unziped file
gunzip(filename)
assert exists(unziped_filename)

# Check content is the same
with open(unziped_filename, "rb") as f:
new_content = f.read()
assert new_content == content

finally:
# Clean
shutil.rmtree(temp_dir)