acloud: download remote image artifacts from Android Build.
Bug: 112879708
Test: atest acloud_test, m acloud && acloud create --local_instance or
acloud create --local_instance --branch master --build_target cf_x86_phone-userdebug --build_id 4972102 --log_file /tmp/acloud.log or
acloud create --local_instance --branch aosp_master --build_target aosp_cf_x86_phone-userdebug --build_id 5048089 --log_file /tmp/acloud.log
Change-Id: Ib3937807e533327ce288b546010c140d2e27228b
diff --git a/internal/lib/utils.py b/internal/lib/utils.py
index 2bdf8b0..ae334ac 100755
--- a/internal/lib/utils.py
+++ b/internal/lib/utils.py
@@ -30,7 +30,9 @@
import tempfile
import time
import uuid
+import zipfile
+from acloud import errors as root_errors
from acloud.internal import constants
from acloud.public import errors
@@ -363,6 +365,28 @@
raise errors.DriverError(
"rsa key is invalid: %s, error: %s" % (rsa, str(e)))
+def Decompress(sourcefile, dest=None):
+ """Decompress .zip or .tar.gz.
+
+ Args:
+ sourcefile: A string, a source file path to decompress.
+ dest: A string, a folder path as decompress destination.
+
+ Raises:
+ errors.UnsupportedCompressionFileType: Not supported extension.
+ """
+ logger.info("Start to decompress %s!", sourcefile)
+ dest_path = dest if dest else "."
+ if sourcefile.endswith(".tar.gz"):
+ with tarfile.open(sourcefile, "r:gz") as compressor:
+ compressor.extractall(dest_path)
+ elif sourcefile.endswith(".zip"):
+ with zipfile.ZipFile(sourcefile, 'r') as compressor:
+ compressor.extractall(dest_path)
+ else:
+ raise root_errors.UnsupportedCompressionFileType(
+ "Sorry, we could only support compression file type "
+ "for zip or tar.gz.")
# pylint: disable=old-style-class,no-init
class TextColors: