[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v2 24/31] tests/functional: add a generalized uncompress helper
From: |
Daniel P . Berrangé |
Subject: |
[PATCH v2 24/31] tests/functional: add a generalized uncompress helper |
Date: |
Wed, 11 Dec 2024 17:26:40 +0000 |
There are many types of compression that the tests deal with, and
it makes sense to have a single helper 'uncompress' that can deal
with all.
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
tests/functional/qemu_test/__init__.py | 1 +
tests/functional/qemu_test/uncompress.py | 47 ++++++++++++++++++++++++
2 files changed, 48 insertions(+)
diff --git a/tests/functional/qemu_test/__init__.py
b/tests/functional/qemu_test/__init__.py
index 665c482d13..3bd043e608 100644
--- a/tests/functional/qemu_test/__init__.py
+++ b/tests/functional/qemu_test/__init__.py
@@ -17,3 +17,4 @@
skipFlakyTest, skipUntrustedTest, skipBigDataTest, \
skipIfMissingImports
from .archive import archive_extract
+from .uncompress import uncompress
diff --git a/tests/functional/qemu_test/uncompress.py
b/tests/functional/qemu_test/uncompress.py
index 955170df65..6d02ded066 100644
--- a/tests/functional/qemu_test/uncompress.py
+++ b/tests/functional/qemu_test/uncompress.py
@@ -11,6 +11,9 @@
import lzma
import os
import shutil
+from urllib.parse import urlparse
+
+from .asset import Asset
def gzip_uncompress(gz_path, output_path):
@@ -34,3 +37,47 @@ def lzma_uncompress(xz_path, output_path):
except:
os.remove(output_path)
raise
+
+'''
+@params compressed: filename, Asset, or file-like object to uncompress
+@params uncompressed: filename to uncompress into
+@params format: optional compression format (gzip, lzma)
+
+Uncompresses @compressed into @uncompressed
+
+If @format is None, heuristics will be applied to guess the format
+from the filename or Asset URL. @format must be non-None if @uncompressed
+is a file-like object.
+
+Returns the fully qualified path to the uncompessed file
+'''
+def uncompress(compressed, uncompressed, format=None):
+ if format is None:
+ format = guess_uncompress_format(compressed)
+
+ if format == "xz":
+ lzma_uncompress(str(compressed), uncompressed)
+ elif format == "gz":
+ gzip_uncompress(str(compressed), uncompressed)
+ else:
+ raise Exception(f"Unknown compression format {format}")
+
+'''
+@params compressed: filename, Asset, or file-like object to guess
+
+Guess the format of @compressed, raising an exception if
+no format can be determined
+'''
+def guess_uncompress_format(compressed):
+ if type(compressed) == Asset:
+ compressed = urlparse(compressed.url).path
+ elif type(compressed) != str:
+ raise Exception(f"Unable to guess compression cformat for
{compressed}")
+
+ (name, ext) = os.path.splitext(compressed)
+ if ext == ".xz":
+ return "xz"
+ elif ext == ".gz":
+ return "gz"
+ else:
+ raise Exception(f"Unknown compression format for {compressed}")
--
2.46.0
- [PATCH v2 18/31] tests/functional: add common zip_extract helper, (continued)
- [PATCH v2 18/31] tests/functional: add common zip_extract helper, Daniel P . Berrangé, 2024/12/11
- [PATCH v2 19/31] tests/functional: add common deb_extract helper, Daniel P . Berrangé, 2024/12/11
- [PATCH v2 20/31] tests/functional: let cpio_extract accept filenames, Daniel P . Berrangé, 2024/12/11
- [PATCH v2 22/31] tests/functional: add 'archive_extract' to QemuBaseTest, Daniel P . Berrangé, 2024/12/11
- [PATCH v2 21/31] tests/functional: add a generalized archive_extract, Daniel P . Berrangé, 2024/12/11
- [PATCH v2 24/31] tests/functional: add a generalized uncompress helper,
Daniel P . Berrangé <=
- [PATCH v2 25/31] tests/functional: add 'uncompress' to QemuBaseTest, Daniel P . Berrangé, 2024/12/11
- [PATCH v2 23/31] tests/functional: convert tests to new archive_extract helper, Daniel P . Berrangé, 2024/12/11
- [PATCH v2 26/31] tests/functional: convert tests to new uncompress helper, Daniel P . Berrangé, 2024/12/11
- [PATCH v2 28/31] tests/functional: replace 'run_cmd' with subprocess helpers, Daniel P . Berrangé, 2024/12/11