Skip to content

Commit 815d2e5

Browse files
authored
Merge pull request #1613 from erwanp/fix_gzip_not_found
remove call to system-wide gunzip. Use python version instead.
2 parents fa9a34b + df3475a commit 815d2e5

File tree

2 files changed

+54
-27
lines changed

2 files changed

+54
-27
lines changed

astroquery/utils/system_tools.py

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
# Licensed under a 3-clause BSD style license - see LICENSE.rst
22
from __future__ import print_function
3-
import subprocess
43
import os
54

6-
75
# Import DEVNULL for py3 or py3
86
try:
97
from subprocess import DEVNULL
@@ -12,29 +10,6 @@
1210

1311
# Check availability of some system tools
1412
# Exceptions are raised if not found
15-
__is_gzip_found = False
16-
for test_cmd in (["gzip", "-V"], ["7z"]):
17-
try:
18-
subprocess.call(test_cmd, stdout=DEVNULL, stderr=DEVNULL)
19-
except OSError:
20-
pass
21-
else:
22-
__is_gzip_found = test_cmd[0]
23-
24-
25-
if __is_gzip_found == 'gzip':
26-
def _unzip_cmd(filename):
27-
return ["gzip", "-d", "{0}".format(filename)]
28-
elif __is_gzip_found == '7z':
29-
def _unzip_cmd(filename):
30-
return ["7z", "x",
31-
"{0}".format(filename),
32-
"-o{0}".format(os.path.split(filename)[0])]
33-
else:
34-
print("gzip was not found on your system! You should solve this issue for "
35-
"astroquery.eso to be at its best!\n"
36-
"On POSIX system: make sure gzip is installed and in your path!"
37-
"On Windows: same for 7-zip (http://www.7-zip.org)!")
3813

3914

4015
def gunzip(filename):
@@ -50,9 +25,17 @@ def gunzip(filename):
5025
Name of the decompressed file (or input filename if gzip is not
5126
available).
5227
"""
28+
import shutil
29+
import gzip
30+
31+
# system-wide 'gzip' was removed, Python gzip used instead.
32+
# See #1538 : https://github.com/astropy/astroquery/issues/1538
33+
5334
# ".fz" denotes RICE rather than gzip compression
54-
if __is_gzip_found and not filename.endswith('.fz'):
55-
subprocess.call(_unzip_cmd(filename), stdout=DEVNULL, stderr=DEVNULL)
35+
if not filename.endswith('.fz'):
36+
with gzip.open(filename, 'rb') as f_in:
37+
with open(filename.rsplit(".", 1)[0], 'wb') as f_out:
38+
shutil.copyfileobj(f_in, f_out)
5639
return filename.rsplit(".", 1)[0]
5740
else:
5841
return filename
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Licensed under a 3-clause BSD style license - see LICENSE.rst
2+
3+
try:
4+
import gzip
5+
6+
HAS_GZIP = True
7+
except ImportError:
8+
HAS_GZIP = False
9+
10+
import shutil
11+
import os
12+
from os.path import exists
13+
import tempfile
14+
15+
import pytest
16+
17+
from ..system_tools import gunzip
18+
19+
20+
@pytest.mark.skipif("not HAS_GZIP")
21+
def test_gunzip():
22+
23+
temp_dir = tempfile.mkdtemp()
24+
filename = temp_dir + os.sep + "test_gunzip.txt.gz"
25+
unziped_filename = filename.rsplit(".", 1)[0]
26+
27+
# First create a gzip file
28+
content = b"Bla"
29+
with gzip.open(filename, "wb") as f:
30+
f.write(content)
31+
32+
try:
33+
# Then test our gunzip command works and creates an unziped file
34+
gunzip(filename)
35+
assert exists(unziped_filename)
36+
37+
# Check content is the same
38+
with open(unziped_filename, "rb") as f:
39+
new_content = f.read()
40+
assert new_content == content
41+
42+
finally:
43+
# Clean
44+
shutil.rmtree(temp_dir)

0 commit comments

Comments
 (0)