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
This commit is contained in:
Bill Peckham
2019-02-22 10:57:43 -08:00
parent dd4e2e59aa
commit 8ff3fbdd08
2 changed files with 33 additions and 23 deletions

View File

@@ -768,30 +768,46 @@ def Gunzip(in_filename, out_filename):
shutil.copyfileobj(in_file, out_file) 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): def UnzipTemp(filename, pattern=None):
"""Unzips the given archive into a temporary directory and returns the name. """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, Args:
then unzip bar.zip into that_dir/BOOTABLE_IMAGES. 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: Returns:
The name of the temporary directory. 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-") tmp = MakeTempDir(prefix="targetfiles-")
m = re.match(r"^(.*[.]zip)\+(.*[.]zip)$", filename, re.IGNORECASE) m = re.match(r"^(.*[.]zip)\+(.*[.]zip)$", filename, re.IGNORECASE)
if m: if m:
unzip_to_dir(m.group(1), tmp) UnzipToDir(m.group(1), tmp, pattern)
unzip_to_dir(m.group(2), os.path.join(tmp, "BOOTABLE_IMAGES")) UnzipToDir(m.group(2), os.path.join(tmp, "BOOTABLE_IMAGES"), pattern)
filename = m.group(1) filename = m.group(1)
else: else:
unzip_to_dir(filename, tmp) UnzipToDir(filename, tmp, pattern)
return tmp return tmp

View File

@@ -163,19 +163,13 @@ def extract_items(target_files, target_files_temp_dir, extract_item_list):
else: else:
filtered_extract_item_list.append(pattern) filtered_extract_item_list.append(pattern)
# Extract the filtered_extract_item_list from target_files into # Extract from target_files into target_files_temp_dir the
# target_files_temp_dir. # filtered_extract_item_list.
# TODO(b/124464492): Extend common.UnzipTemp() to handle this use case. common.UnzipToDir(
command = [ target_files,
'unzip', target_files_temp_dir,
'-n', filtered_extract_item_list)
'-q',
'-d', target_files_temp_dir,
target_files
] + filtered_extract_item_list
common.RunAndWait(command, verbose=True)
def process_ab_partitions_txt( def process_ab_partitions_txt(