Extract common.UnzipToDir, invoke that from merge_target_files.py
This change adds another utility function to common.py: UnzipToDir, which is
generally useful. Refactor merge_target_files.py to use it, and also refactor
other uses in common.py to use it.
Test: ota_from_target_files.py, validate_target_files.py, test_common.py
Bug: 124464492
Change-Id: Ia571070bceb7d3c8002304836bdf688485bf0dd9
diff --git a/tools/releasetools/common.py b/tools/releasetools/common.py
index c685dd6..cfa81e1 100644
--- a/tools/releasetools/common.py
+++ b/tools/releasetools/common.py
@@ -768,30 +768,46 @@
shutil.copyfileobj(in_file, out_file)
+def UnzipToDir(filename, dirname, pattern=None):
+ """Unzips the archive to the given directory.
+
+ Args:
+ filename: The name of the zip file to unzip.
+
+ dirname: Where the unziped files will land.
+
+ pattern: Files to unzip from the archive. If omitted, will unzip the entire
+ archvie.
+ """
+
+ cmd = ["unzip", "-o", "-q", filename, "-d", dirname]
+ if pattern is not None:
+ cmd.extend(pattern)
+ RunAndCheckOutput(cmd)
+
+
def UnzipTemp(filename, pattern=None):
"""Unzips the given archive into a temporary directory and returns the name.
- If filename is of the form "foo.zip+bar.zip", unzip foo.zip into a temp dir,
- then unzip bar.zip into that_dir/BOOTABLE_IMAGES.
+ Args:
+ filename: If filename is of the form "foo.zip+bar.zip", unzip foo.zip into
+ a temp dir, then unzip bar.zip into that_dir/BOOTABLE_IMAGES.
+
+ pattern: Files to unzip from the archive. If omitted, will unzip the entire
+ archvie.
Returns:
The name of the temporary directory.
"""
- def unzip_to_dir(filename, dirname):
- cmd = ["unzip", "-o", "-q", filename, "-d", dirname]
- if pattern is not None:
- cmd.extend(pattern)
- RunAndCheckOutput(cmd)
-
tmp = MakeTempDir(prefix="targetfiles-")
m = re.match(r"^(.*[.]zip)\+(.*[.]zip)$", filename, re.IGNORECASE)
if m:
- unzip_to_dir(m.group(1), tmp)
- unzip_to_dir(m.group(2), os.path.join(tmp, "BOOTABLE_IMAGES"))
+ UnzipToDir(m.group(1), tmp, pattern)
+ UnzipToDir(m.group(2), os.path.join(tmp, "BOOTABLE_IMAGES"), pattern)
filename = m.group(1)
else:
- unzip_to_dir(filename, tmp)
+ UnzipToDir(filename, tmp, pattern)
return tmp
diff --git a/tools/releasetools/merge_target_files.py b/tools/releasetools/merge_target_files.py
index b2bb020..2645829 100755
--- a/tools/releasetools/merge_target_files.py
+++ b/tools/releasetools/merge_target_files.py
@@ -163,19 +163,13 @@
else:
filtered_extract_item_list.append(pattern)
- # Extract the filtered_extract_item_list from target_files into
- # target_files_temp_dir.
+ # Extract from target_files into target_files_temp_dir the
+ # filtered_extract_item_list.
- # TODO(b/124464492): Extend common.UnzipTemp() to handle this use case.
- command = [
- 'unzip',
- '-n',
- '-q',
- '-d', target_files_temp_dir,
- target_files
- ] + filtered_extract_item_list
-
- common.RunAndWait(command, verbose=True)
+ common.UnzipToDir(
+ target_files,
+ target_files_temp_dir,
+ filtered_extract_item_list)
def process_ab_partitions_txt(