def extract_items(target_files, target_files_temp_dir, extract_item_list):
    """Extracts items from target files to temporary directory.

  This function extracts from the specified target files zip archive into the
  specified temporary directory, the items specified in the extract item list.

  Args:
    target_files: The target files zip archive from which to extract items.
    target_files_temp_dir: The temporary directory where the extracted items
      will land.
    extract_item_list: A list of items to extract.
  """

    logger.info('extracting from %s', target_files)

    # Filter the extract_item_list to remove any items that do not exist in the
    # zip file. Otherwise, the extraction step will fail.

    with zipfile.ZipFile(target_files,
                         allowZip64=True) as target_files_zipfile:
        target_files_namelist = target_files_zipfile.namelist()

    filtered_extract_item_list = []
    for pattern in extract_item_list:
        matching_namelist = fnmatch.filter(target_files_namelist, pattern)
        if not matching_namelist:
            logger.warning('no match for %s', pattern)
        else:
            filtered_extract_item_list.append(pattern)

    # Extract from target_files into target_files_temp_dir the
    # filtered_extract_item_list.

    common.UnzipToDir(target_files, target_files_temp_dir,
                      filtered_extract_item_list)
Пример #2
0
    def RepackApexPayload(self, payload_dir, payload_key, signing_args=None):
        """Rebuilds the apex file with the updated payload directory."""
        apex_dir = common.MakeTempDir()
        # Extract the apex file and reuse its meta files as repack parameters.
        common.UnzipToDir(self.apex_path, apex_dir)
        arguments_dict = {
            'manifest': os.path.join(apex_dir, 'apex_manifest.pb'),
            'build_info': os.path.join(apex_dir, 'apex_build_info.pb'),
            'key': payload_key,
        }
        for filename in arguments_dict.values():
            assert os.path.exists(filename), 'file {} not found'.format(
                filename)

        # The repack process will add back these files later in the payload image.
        for name in ['apex_manifest.pb', 'apex_manifest.json', 'lost+found']:
            path = os.path.join(payload_dir, name)
            if os.path.isfile(path):
                os.remove(path)
            elif os.path.isdir(path):
                shutil.rmtree(path)

        # TODO(xunchang) the signing process can be improved by using
        # '--unsigned_payload_only'. But we need to parse the vbmeta earlier for
        # the signing arguments, e.g. algorithm, salt, etc.
        payload_img = os.path.join(apex_dir, APEX_PAYLOAD_IMAGE)
        generate_image_cmd = [
            'apexer', '--force', '--payload_only', '--do_not_check_keyname',
            '--apexer_tool_path',
            os.getenv('PATH')
        ]
        for key, val in arguments_dict.items():
            generate_image_cmd.extend(['--' + key, val])

        # Add quote to the signing_args as we will pass
        # --signing_args "--signing_helper_with_files=%path" to apexer
        if signing_args:
            generate_image_cmd.extend(
                ['--signing_args', '"{}"'.format(signing_args)])

        # optional arguments for apex repacking
        manifest_json = os.path.join(apex_dir, 'apex_manifest.json')
        if os.path.exists(manifest_json):
            generate_image_cmd.extend(['--manifest_json', manifest_json])
        generate_image_cmd.extend([payload_dir, payload_img])
        if OPTIONS.verbose:
            generate_image_cmd.append('-v')
        common.RunAndCheckOutput(generate_image_cmd)

        # Add the payload image back to the apex file.
        common.ZipDelete(self.apex_path, APEX_PAYLOAD_IMAGE)
        with zipfile.ZipFile(self.apex_path, 'a',
                             allowZip64=True) as output_apex:
            common.ZipWrite(output_apex,
                            payload_img,
                            APEX_PAYLOAD_IMAGE,
                            compress_type=zipfile.ZIP_STORED)
        return self.apex_path
Пример #3
0
    def RepackApexPayload(self, payload_dir, payload_key, payload_public_key):
        """Rebuilds the apex file with the updated payload directory."""
        apex_dir = common.MakeTempDir()
        # Extract the apex file and reuse its meta files as repack parameters.
        common.UnzipToDir(self.apex_path, apex_dir)

        android_jar_path = common.OPTIONS.android_jar_path
        if not android_jar_path:
            android_jar_path = os.path.join(
                os.environ.get('ANDROID_BUILD_TOP', ''), 'prebuilts', 'sdk',
                'current', 'public', 'android.jar')
            logger.warning(
                'android_jar_path not found in options, falling back to'
                ' use %s', android_jar_path)

        arguments_dict = {
            'manifest': os.path.join(apex_dir, 'apex_manifest.pb'),
            'build_info': os.path.join(apex_dir, 'apex_build_info.pb'),
            'assets_dir': os.path.join(apex_dir, 'assets'),
            'android_jar_path': android_jar_path,
            'key': payload_key,
            'pubkey': payload_public_key,
        }
        for filename in arguments_dict.values():
            assert os.path.exists(filename), 'file {} not found'.format(
                filename)

        # The repack process will add back these files later in the payload image.
        for name in ['apex_manifest.pb', 'apex_manifest.json', 'lost+found']:
            path = os.path.join(payload_dir, name)
            if os.path.isfile(path):
                os.remove(path)
            elif os.path.isdir(path):
                shutil.rmtree(path)

        repacked_apex = common.MakeTempFile(suffix='.apex')
        repack_cmd = [
            'apexer', '--force', '--include_build_info',
            '--do_not_check_keyname', '--apexer_tool_path',
            os.getenv('PATH')
        ]
        for key, val in arguments_dict.items():
            repack_cmd.append('--' + key)
            repack_cmd.append(val)
        manifest_json = os.path.join(apex_dir, 'apex_manifest.json')
        if os.path.exists(manifest_json):
            repack_cmd.append('--manifest_json')
            repack_cmd.append(manifest_json)
        repack_cmd.append(payload_dir)
        repack_cmd.append(repacked_apex)
        common.RunAndCheckOutput(repack_cmd)

        return repacked_apex