summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBetsy McPhail <betsy.mcphail@kitware.com>2021-06-28 10:39:10 -0400
committerPeter Scheibel <scheibel1@llnl.gov>2022-03-17 09:01:01 -0700
commit4a73bfc3b9c4507f270951757475e514cd666ead (patch)
tree71ef842fbbad0d0252cfba9d53ed961f9df4f6ad
parentf8782c46d7a5d1caa4fbdbaba9c83bf6c0fcaf08 (diff)
downloadspack-4a73bfc3b9c4507f270951757475e514cd666ead.tar.gz
spack-4a73bfc3b9c4507f270951757475e514cd666ead.tar.bz2
spack-4a73bfc3b9c4507f270951757475e514cd666ead.tar.xz
spack-4a73bfc3b9c4507f270951757475e514cd666ead.zip
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`
-rw-r--r--lib/spack/spack/util/compression.py24
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):