From e24e71be6aec4e83b2d7a9023068cab377132bbe Mon Sep 17 00:00:00 2001 From: "John W. Parent" <45471568+johnwparent@users.noreply.github.com> Date: Fri, 13 May 2022 16:38:05 -0400 Subject: Preserve Permissions on .zip extraction (#30407) #24556 merged in support for Python's .zip file support via ZipFile. However as per #30200 ZipFile does not preserve file permissions of the extracted contents. This PR returns to using the `unzip` executable on non-Windows systems (as was the case before #24556) and now uses `tar` on Windows to extract .zip files. --- lib/spack/spack/util/compression.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/spack/spack/util/compression.py b/lib/spack/spack/util/compression.py index 60b43e34ce..a5b7db4ec2 100644 --- a/lib/spack/spack/util/compression.py +++ b/lib/spack/spack/util/compression.py @@ -5,6 +5,7 @@ import os import re +import sys from itertools import product from spack.util.executable import which @@ -18,6 +19,8 @@ NOTAR_EXTS = ["zip", "tgz", "tbz", "tbz2", "txz"] ALLOWED_ARCHIVE_TYPES = [".".join(ext) for ext in product( PRE_EXTS, EXTS)] + PRE_EXTS + EXTS + NOTAR_EXTS +is_windows = sys.platform == 'win32' + def allowed_archive(path): return any(path.endswith(t) for t in ALLOWED_ARCHIVE_TYPES) @@ -48,15 +51,14 @@ def _unzip(archive_file): 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 + exe = 'unzip' + arg = '-q' + if is_windows: + exe = 'tar' + arg = '-xf' + unzip = which(exe, required=True) + unzip.add_default_arg(arg) + unzip(archive_file) def decompressor_for(path, extension=None): -- cgit v1.2.3-70-g09d2