[PATCH 4/7] tests/functional: teach uncompress about zip files

Alex Bennée posted 7 patches 1 month, 1 week ago
Maintainers: Paolo Bonzini <pbonzini@redhat.com>, "Alex Bennée" <alex.bennee@linaro.org>, Thomas Huth <thuth@redhat.com>, "Philippe Mathieu-Daudé" <philmd@linaro.org>, Peter Maydell <peter.maydell@linaro.org>, "Daniel P. Berrangé" <berrange@redhat.com>
There is a newer version of this series
[PATCH 4/7] tests/functional: teach uncompress about zip files
Posted by Alex Bennée 1 month, 1 week ago
Yes people still use them.

Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
---
 tests/functional/qemu_test/uncompress.py | 25 ++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/tests/functional/qemu_test/uncompress.py b/tests/functional/qemu_test/uncompress.py
index 5bbdf8fe323..f1a0e583a5b 100644
--- a/tests/functional/qemu_test/uncompress.py
+++ b/tests/functional/qemu_test/uncompress.py
@@ -12,6 +12,7 @@
 import os
 import stat
 import shutil
+import zipfile
 from urllib.parse import urlparse
 from subprocess import run, CalledProcessError
 
@@ -58,6 +59,26 @@ def zstd_uncompress(zstd_path, output_path):
     os.chmod(output_path, stat.S_IRUSR | stat.S_IWUSR)
 
 
+def zip_uncompress(zip_path, output_path):
+    if os.path.exists(output_path):
+        return
+    with zipfile.ZipFile(zip_path, 'r') as zip_in:
+        try:
+            # Get the first file from the archive
+            names = zip_in.namelist()
+            if len(names) != 1:
+                raise Exception(
+                    f"Zip file {zip_path} should contain exactly one file, "
+                    f"but contains {len(names)}")
+            with zip_in.open(names[0], 'r') as archived_file:
+                with open(output_path, 'wb') as raw_out:
+                    shutil.copyfileobj(archived_file, raw_out)
+        except:
+            if os.path.exists(output_path):
+                os.remove(output_path)
+            raise
+
+
 def uncompress(compressed, uncompressed, format=None):
     '''
     @params compressed: filename, Asset, or file-like object to uncompress
@@ -81,6 +102,8 @@ def uncompress(compressed, uncompressed, format=None):
         gzip_uncompress(str(compressed), uncompressed)
     elif format == "zstd":
         zstd_uncompress(str(compressed), uncompressed)
+    elif format == "zip":
+        zip_uncompress(str(compressed), uncompressed)
     else:
         raise Exception(f"Unknown compression format {format}")
 
@@ -103,5 +126,7 @@ def guess_uncompress_format(compressed):
         return "gz"
     elif ext in [".zstd", ".zst"]:
         return 'zstd'
+    elif ext == ".zip":
+        return 'zip'
     else:
         raise Exception(f"Unknown compression format for {compressed}")
-- 
2.47.3


Re: [PATCH 4/7] tests/functional: teach uncompress about zip files
Posted by Thomas Huth 1 month, 1 week ago
On 06/03/2026 17.17, Alex Bennée wrote:
> Yes people still use them.
> 
> Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
> ---
>   tests/functional/qemu_test/uncompress.py | 25 ++++++++++++++++++++++++
>   1 file changed, 25 insertions(+)
> 
> diff --git a/tests/functional/qemu_test/uncompress.py b/tests/functional/qemu_test/uncompress.py
> index 5bbdf8fe323..f1a0e583a5b 100644
> --- a/tests/functional/qemu_test/uncompress.py
> +++ b/tests/functional/qemu_test/uncompress.py
> @@ -12,6 +12,7 @@
>   import os
>   import stat
>   import shutil
> +import zipfile
>   from urllib.parse import urlparse
>   from subprocess import run, CalledProcessError
>   
> @@ -58,6 +59,26 @@ def zstd_uncompress(zstd_path, output_path):
>       os.chmod(output_path, stat.S_IRUSR | stat.S_IWUSR)
>   
>   
> +def zip_uncompress(zip_path, output_path):
> +    if os.path.exists(output_path):
> +        return
> +    with zipfile.ZipFile(zip_path, 'r') as zip_in:
> +        try:
> +            # Get the first file from the archive
> +            names = zip_in.namelist()
> +            if len(names) != 1:
> +                raise Exception(
> +                    f"Zip file {zip_path} should contain exactly one file, "
> +                    f"but contains {len(names)}")
> +            with zip_in.open(names[0], 'r') as archived_file:
> +                with open(output_path, 'wb') as raw_out:
> +                    shutil.copyfileobj(archived_file, raw_out)
> +        except:
> +            if os.path.exists(output_path):
> +                os.remove(output_path)
> +            raise
> +
> +
>   def uncompress(compressed, uncompressed, format=None):
>       '''
>       @params compressed: filename, Asset, or file-like object to uncompress
> @@ -81,6 +102,8 @@ def uncompress(compressed, uncompressed, format=None):
>           gzip_uncompress(str(compressed), uncompressed)
>       elif format == "zstd":
>           zstd_uncompress(str(compressed), uncompressed)
> +    elif format == "zip":
> +        zip_uncompress(str(compressed), uncompressed)
>       else:
>           raise Exception(f"Unknown compression format {format}")

I think you should rather use archive_extract() instead of uncompress() for 
zip files, shouldn't you? ... and if I've got that right, support for zip is 
already included in there.

  Thomas


Re: [PATCH 4/7] tests/functional: teach uncompress about zip files
Posted by Pierrick Bouvier 1 month, 1 week ago
On 3/6/26 8:17 AM, Alex Bennée wrote:
> Yes people still use them.
> 
> Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
> ---
>   tests/functional/qemu_test/uncompress.py | 25 ++++++++++++++++++++++++
>   1 file changed, 25 insertions(+)
> 

Reviewed-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>