diff options
-rw-r--r-- | lib/spack/spack/util/compression.py | 24 |
1 files changed, 21 insertions, 3 deletions
diff --git a/lib/spack/spack/util/compression.py b/lib/spack/spack/util/compression.py index 421b0f66f2..44d19bd00c 100644 --- a/lib/spack/spack/util/compression.py +++ b/lib/spack/spack/util/compression.py @@ -39,13 +39,31 @@ def _gunzip(archive_file): f_out.write(f_in.read()) +def _unzip(archive_file): + """Try to use Python's zipfile, but extract in the current working + directory instead of in-place. + + If unavailable, try unzip + + Args: + archive_file (str): absolute path of the file to be decompressed + """ + try: + from zipfile import ZipFile + destination_abspath = os.getcwd() + with ZipFile(archive_file, 'r') as zf: + zf.extractall(destination_abspath) + except ImportError: + unzip = which('unzip', required=True) + unzip.add_default_arg('-q') + return unzip + + def decompressor_for(path, extension=None): """Get the appropriate decompressor for a path.""" if ((extension and re.match(r'\.?zip$', extension)) or path.endswith('.zip')): - unzip = which('unzip', required=True) - unzip.add_default_arg('-q') - return unzip + return _unzip if extension and re.match(r'gz', extension): return _gunzip if extension and re.match(r'bz2', extension): |