From 4a73bfc3b9c4507f270951757475e514cd666ead Mon Sep 17 00:00:00 2001 From: Betsy McPhail Date: Mon, 28 Jun 2021 10:39:10 -0400 Subject: Use Python's zipfile, if available (#24556) * Style fixes * Use Python's zipfile, if available The compression libs are optional in Python. Rely on python as a first attempt then fall back to `unzip` --- lib/spack/spack/util/compression.py | 24 +++++++++++++++++++++--- 1 file 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): -- cgit v1.2.3-60-g2f50