def generate_super_empty_image(target_dir, output_super_empty):
    """Generates super_empty image from target package.

  Args:
    target_dir: Path to the target file package which contains misc_info.txt for
      detailed information for super image.
    output_super_empty: If provided, copies a super_empty.img file from the
      target files package to this path.
  """
    # Create super_empty.img using the merged misc_info.txt.

    misc_info_txt = os.path.join(target_dir, 'META', 'misc_info.txt')

    use_dynamic_partitions = common.LoadDictionaryFromFile(misc_info_txt).get(
        'use_dynamic_partitions')

    if use_dynamic_partitions != 'true' and output_super_empty:
        raise ValueError(
            'Building super_empty.img requires use_dynamic_partitions=true.')
    elif use_dynamic_partitions == 'true':
        super_empty_img = os.path.join(target_dir, 'IMAGES', 'super_empty.img')
        build_super_image_args = [
            misc_info_txt,
            super_empty_img,
        ]
        build_super_image.main(build_super_image_args)

        # Copy super_empty.img to the user-provided output_super_empty location.
        if output_super_empty:
            shutil.copyfile(super_empty_img, output_super_empty)
Example #2
0
def merge_target_files(temp_dir, system_target_files, system_item_list,
                       system_misc_info_keys, other_target_files,
                       other_item_list, output_target_files, output_dir,
                       output_item_list, output_ota, output_img,
                       output_super_empty, rebuild_recovery):
    """Merge two target files packages together.

  This function takes system and other target files packages as input, performs
  various file extractions, special case processing, and finally creates a
  merged zip archive as output.

  Args:
    temp_dir: The name of a directory we use when we extract items from the
      input target files packages, and also a scratch directory that we use for
      temporary files.
    system_target_files: The name of the zip archive containing the system
      partial target files package.
    system_item_list: The list of items to extract from the partial system
      target files package as is, meaning these items will land in the output
      target files package exactly as they appear in the input partial system
      target files package.
    system_misc_info_keys: The list of keys to obtain from the system instance
      of META/misc_info.txt. The remaining keys from the other instance.
    other_target_files: The name of the zip archive containing the other partial
      target files package.
    other_item_list: The list of items to extract from the partial other target
      files package as is, meaning these items will land in the output target
      files package exactly as they appear in the input partial other target
      files package.
    output_target_files: The name of the output zip archive target files package
      created by merging system and other.
    output_dir: The destination directory for saving merged files.
    output_item_list: The list of items to copy into the output_dir.
    output_ota: The name of the output zip archive ota package.
    output_img: The name of the output zip archive img package.
    output_super_empty: If provided, creates a super_empty.img file from the
      merged target files package and saves it at this path.
    rebuild_recovery: If true, rebuild the recovery patch used by non-A/B
      devices and write it to the system image.
  """

    logger.info('starting: merge system %s and other %s into output %s',
                system_target_files, other_target_files, output_target_files)

    # Create directory names that we'll use when we extract files from system,
    # and other, and for zipping the final output.

    system_target_files_temp_dir = os.path.join(temp_dir, 'system')
    other_target_files_temp_dir = os.path.join(temp_dir, 'other')
    output_target_files_temp_dir = os.path.join(temp_dir, 'output')

    # Extract "as is" items from the input system partial target files package.
    # We extract them directly into the output temporary directory since the
    # items do not need special case processing.

    extract_items(target_files=system_target_files,
                  target_files_temp_dir=output_target_files_temp_dir,
                  extract_item_list=system_item_list)

    # Extract "as is" items from the input other partial target files package. We
    # extract them directly into the output temporary directory since the items
    # do not need special case processing.

    extract_items(target_files=other_target_files,
                  target_files_temp_dir=output_target_files_temp_dir,
                  extract_item_list=other_item_list)

    # Extract "special" items from the input system partial target files package.
    # We extract these items to different directory since they require special
    # processing before they will end up in the output directory.

    extract_items(target_files=system_target_files,
                  target_files_temp_dir=system_target_files_temp_dir,
                  extract_item_list=system_extract_special_item_list)

    # Extract "special" items from the input other partial target files package.
    # We extract these items to different directory since they require special
    # processing before they will end up in the output directory.

    extract_items(target_files=other_target_files,
                  target_files_temp_dir=other_target_files_temp_dir,
                  extract_item_list=other_extract_special_item_list)

    # Now that the temporary directories contain all the extracted files, perform
    # special case processing on any items that need it. After this function
    # completes successfully, all the files we need to create the output target
    # files package are in place.

    process_special_cases(
        system_target_files_temp_dir=system_target_files_temp_dir,
        other_target_files_temp_dir=other_target_files_temp_dir,
        output_target_files_temp_dir=output_target_files_temp_dir,
        system_misc_info_keys=system_misc_info_keys,
        rebuild_recovery=rebuild_recovery)

    # Regenerate IMAGES in the temporary directory.

    add_img_args = ['--verbose']
    if rebuild_recovery:
        add_img_args.append('--rebuild_recovery')
    add_img_args.append(output_target_files_temp_dir)

    add_img_to_target_files.main(add_img_args)

    # Create super_empty.img using the merged misc_info.txt.

    misc_info_txt = os.path.join(output_target_files_temp_dir, 'META',
                                 'misc_info.txt')

    def read_helper():
        with open(misc_info_txt) as f:
            return list(f.read().splitlines())

    use_dynamic_partitions = common.LoadDictionaryFromLines(
        read_helper()).get('use_dynamic_partitions')

    if use_dynamic_partitions != 'true' and output_super_empty:
        raise ValueError(
            'Building super_empty.img requires use_dynamic_partitions=true.')
    elif use_dynamic_partitions == 'true':
        super_empty_img = os.path.join(output_target_files_temp_dir, 'IMAGES',
                                       'super_empty.img')
        build_super_image_args = [
            misc_info_txt,
            super_empty_img,
        ]
        build_super_image.main(build_super_image_args)

        # Copy super_empty.img to the user-provided output_super_empty location.
        if output_super_empty:
            shutil.copyfile(super_empty_img, output_super_empty)

    # Create the IMG package from the merged target files (before zipping, in
    # order to avoid an unnecessary unzip and copy).

    if output_img:
        img_from_target_files_args = [
            output_target_files_temp_dir,
            output_img,
        ]
        img_from_target_files.main(img_from_target_files_args)

    # Finally, create the output target files zip archive and/or copy the
    # output items to the output target files directory.

    if output_dir:
        copy_items(output_target_files_temp_dir, output_dir, output_item_list)

    if not output_target_files:
        return

    output_zip = os.path.abspath(output_target_files)
    output_target_files_list = os.path.join(temp_dir, 'output.list')
    output_target_files_meta_dir = os.path.join(output_target_files_temp_dir,
                                                'META')

    find_command = [
        'find',
        output_target_files_meta_dir,
    ]
    find_process = common.Run(find_command,
                              stdout=subprocess.PIPE,
                              verbose=False)
    meta_content = common.RunAndCheckOutput(['sort'],
                                            stdin=find_process.stdout,
                                            verbose=False)

    find_command = [
        'find', output_target_files_temp_dir, '-path',
        output_target_files_meta_dir, '-prune', '-o', '-print'
    ]
    find_process = common.Run(find_command,
                              stdout=subprocess.PIPE,
                              verbose=False)
    other_content = common.RunAndCheckOutput(['sort'],
                                             stdin=find_process.stdout,
                                             verbose=False)

    with open(output_target_files_list, 'wb') as f:
        f.write(meta_content)
        f.write(other_content)

    command = [
        'soong_zip',
        '-d',
        '-o',
        output_zip,
        '-C',
        output_target_files_temp_dir,
        '-l',
        output_target_files_list,
    ]
    logger.info('creating %s', output_target_files)
    common.RunAndWait(command, verbose=True)

    # Create the OTA package from the merged target files package.

    if output_ota:
        ota_from_target_files_args = [
            output_zip,
            output_ota,
        ]
        ota_from_target_files.main(ota_from_target_files_args)
def merge_target_files(temp_dir, system_target_files, system_item_list,
                       system_misc_info_keys, other_target_files,
                       other_item_list, output_target_files, output_dir,
                       output_item_list, output_ota, output_img,
                       output_super_empty, rebuild_recovery):
  """Merge two target files packages together.

  This function takes system and other target files packages as input, performs
  various file extractions, special case processing, and finally creates a
  merged zip archive as output.

  Args:
    temp_dir: The name of a directory we use when we extract items from the
      input target files packages, and also a scratch directory that we use for
      temporary files.
    system_target_files: The name of the zip archive containing the system
      partial target files package.
    system_item_list: The list of items to extract from the partial system
      target files package as is, meaning these items will land in the output
      target files package exactly as they appear in the input partial system
      target files package.
    system_misc_info_keys: The list of keys to obtain from the system instance
      of META/misc_info.txt. The remaining keys from the other instance.
    other_target_files: The name of the zip archive containing the other partial
      target files package.
    other_item_list: The list of items to extract from the partial other target
      files package as is, meaning these items will land in the output target
      files package exactly as they appear in the input partial other target
      files package.
    output_target_files: The name of the output zip archive target files package
      created by merging system and other.
    output_dir: The destination directory for saving merged files.
    output_item_list: The list of items to copy into the output_dir.
    output_ota: The name of the output zip archive ota package.
    output_img: The name of the output zip archive img package.
    output_super_empty: If provided, creates a super_empty.img file from the
      merged target files package and saves it at this path.
    rebuild_recovery: If true, rebuild the recovery patch used by non-A/B
      devices and write it to the system image.
  """

  logger.info('starting: merge system %s and other %s into output %s',
              system_target_files, other_target_files, output_target_files)

  # Create directory names that we'll use when we extract files from system,
  # and other, and for zipping the final output.

  system_target_files_temp_dir = os.path.join(temp_dir, 'system')
  other_target_files_temp_dir = os.path.join(temp_dir, 'other')
  output_target_files_temp_dir = os.path.join(temp_dir, 'output')

  # Extract "as is" items from the input system partial target files package.
  # We extract them directly into the output temporary directory since the
  # items do not need special case processing.

  extract_items(
      target_files=system_target_files,
      target_files_temp_dir=output_target_files_temp_dir,
      extract_item_list=system_item_list)

  # Extract "as is" items from the input other partial target files package. We
  # extract them directly into the output temporary directory since the items
  # do not need special case processing.

  extract_items(
      target_files=other_target_files,
      target_files_temp_dir=output_target_files_temp_dir,
      extract_item_list=other_item_list)

  # Extract "special" items from the input system partial target files package.
  # We extract these items to different directory since they require special
  # processing before they will end up in the output directory.

  extract_items(
      target_files=system_target_files,
      target_files_temp_dir=system_target_files_temp_dir,
      extract_item_list=system_extract_special_item_list)

  # Extract "special" items from the input other partial target files package.
  # We extract these items to different directory since they require special
  # processing before they will end up in the output directory.

  extract_items(
      target_files=other_target_files,
      target_files_temp_dir=other_target_files_temp_dir,
      extract_item_list=other_extract_special_item_list)

  # Now that the temporary directories contain all the extracted files, perform
  # special case processing on any items that need it. After this function
  # completes successfully, all the files we need to create the output target
  # files package are in place.

  process_special_cases(
      system_target_files_temp_dir=system_target_files_temp_dir,
      other_target_files_temp_dir=other_target_files_temp_dir,
      output_target_files_temp_dir=output_target_files_temp_dir,
      system_misc_info_keys=system_misc_info_keys,
      rebuild_recovery=rebuild_recovery)

  # Regenerate IMAGES in the temporary directory.

  add_img_args = ['--verbose']
  if rebuild_recovery:
    add_img_args.append('--rebuild_recovery')
  add_img_args.append(output_target_files_temp_dir)

  add_img_to_target_files.main(add_img_args)

  # Create super_empty.img using the merged misc_info.txt.

  misc_info_txt = os.path.join(output_target_files_temp_dir, 'META',
                               'misc_info.txt')

  def read_helper():
    with open(misc_info_txt) as f:
      return list(f.read().splitlines())

  use_dynamic_partitions = common.LoadDictionaryFromLines(
      read_helper()).get('use_dynamic_partitions')

  if use_dynamic_partitions != 'true' and output_super_empty:
    raise ValueError(
        'Building super_empty.img requires use_dynamic_partitions=true.')
  elif use_dynamic_partitions == 'true':
    super_empty_img = os.path.join(output_target_files_temp_dir, 'IMAGES',
                                   'super_empty.img')
    build_super_image_args = [
        misc_info_txt,
        super_empty_img,
    ]
    build_super_image.main(build_super_image_args)

    # Copy super_empty.img to the user-provided output_super_empty location.
    if output_super_empty:
      shutil.copyfile(super_empty_img, output_super_empty)

  # Create the IMG package from the merged target files (before zipping, in
  # order to avoid an unnecessary unzip and copy).

  if output_img:
    img_from_target_files_args = [
        output_target_files_temp_dir,
        output_img,
    ]
    img_from_target_files.main(img_from_target_files_args)

  # Finally, create the output target files zip archive and/or copy the
  # output items to the output target files directory.

  if output_dir:
    copy_items(output_target_files_temp_dir, output_dir, output_item_list)

  if not output_target_files:
    return

  output_zip = os.path.abspath(output_target_files)
  output_target_files_list = os.path.join(temp_dir, 'output.list')
  output_target_files_meta_dir = os.path.join(output_target_files_temp_dir,
                                              'META')

  find_command = [
      'find',
      output_target_files_meta_dir,
  ]
  find_process = common.Run(find_command, stdout=subprocess.PIPE, verbose=False)
  meta_content = common.RunAndCheckOutput(['sort'],
                                          stdin=find_process.stdout,
                                          verbose=False)

  find_command = [
      'find', output_target_files_temp_dir, '-path',
      output_target_files_meta_dir, '-prune', '-o', '-print'
  ]
  find_process = common.Run(find_command, stdout=subprocess.PIPE, verbose=False)
  other_content = common.RunAndCheckOutput(['sort'],
                                           stdin=find_process.stdout,
                                           verbose=False)

  with open(output_target_files_list, 'wb') as f:
    f.write(meta_content)
    f.write(other_content)

  command = [
      'soong_zip',
      '-d',
      '-o',
      output_zip,
      '-C',
      output_target_files_temp_dir,
      '-l',
      output_target_files_list,
  ]
  logger.info('creating %s', output_target_files)
  common.RunAndWait(command, verbose=True)

  # Create the OTA package from the merged target files package.

  if output_ota:
    ota_from_target_files_args = [
        output_zip,
        output_ota,
    ]
    ota_from_target_files.main(ota_from_target_files_args)