Merge "Extract common.UnzipToDir, invoke that from merge_target_files.py"

am: a2f536f9fa

Change-Id: I10fcfc3008d0699c92c53405d488c66571ef66ec
This commit is contained in:
Bill Peckham 2019-02-25 15:55:39 -08:00 committed by android-build-merger
commit 0ea998133c
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(