def download_cts_graphicsfuzz_tests(  # pylint: disable=too-many-locals;
        git_tool: Path, cookie: str,
        binaries: binaries_util.BinaryManager) -> None:
    work_dir = Path() / "temp" / ("cts_" + fuzz.get_random_name())

    latest_change = gerrit_util.get_latest_deqp_change(cookie)
    latest_change_number = latest_change["_number"]
    latest_change_details = gerrit_util.get_gerrit_change_details(
        change_number=latest_change_number, cookie=cookie)
    current_revision = latest_change_details["current_revision"]
    cts_archive_path = gerrit_util.download_gerrit_revision(
        output_path=work_dir / "cts.tgz",
        change_number=latest_change_number,
        revision=current_revision,
        download_type=gerrit_util.DownloadType.Archive,
        cookie=cookie,
    )

    cts_dir_name = "cts_temp"
    cts_out = util.extract_archive(cts_archive_path, work_dir / cts_dir_name)

    pending_graphicsfuzz_changes = gerrit_util.get_deqp_graphicsfuzz_pending_changes(
        cookie)

    for pending_change in pending_graphicsfuzz_changes:
        change_number = pending_change["_number"]
        change_details = gerrit_util.get_gerrit_change_details(
            change_number=change_number, cookie=cookie)
        current_revision = change_details["current_revision"]
        patch_zip = gerrit_util.download_gerrit_revision(
            output_path=work_dir / f"{change_number}.zip",
            change_number=change_number,
            revision=current_revision,
            download_type=gerrit_util.DownloadType.Patch,
            cookie=cookie,
        )
        util.extract_archive(patch_zip, work_dir)

    # Create a dummy git repo in the work directory, otherwise "git apply" can fail silently.
    # --unsafe-paths is possibly supposed to address this, but it doesn't seem to work if we
    # are already in a git repo.
    subprocess_util.run([str(git_tool), "init", "."],
                        verbose=True,
                        working_dir=work_dir)

    cmd = [str(git_tool), "apply"]

    patch_names = [p.name for p in work_dir.glob("*.diff")]

    cmd += patch_names

    # Use unix-style path for git.
    cmd += [
        "--verbose",
        "--unsafe-paths",
        f"--directory={cts_dir_name}",
        f"--include={cts_dir_name}/external/vulkancts/data/vulkan/amber/graphicsfuzz/*",
    ]

    subprocess_util.run(cmd, verbose=True, working_dir=work_dir)

    shader_dir = util.copy_dir(
        cts_out / "external" / "vulkancts" / "data" / "vulkan" / "amber" /
        "graphicsfuzz",
        Path() / "graphicsfuzz",
    )

    for amber_file in shader_dir.glob("*.amber"):
        amber_converter.extract_shaders(amber_file,
                                        output_dir=amber_file.parent,
                                        binaries=binaries)

        zip_files = [
            util.ZipEntry(f, Path(f.name))
            for f in sorted(shader_dir.glob(f"{amber_file.stem}.*"))
        ]

        util.create_zip(amber_file.with_suffix(".zip"), zip_files)
def download_cts_graphicsfuzz_tests(  # pylint: disable=too-many-locals;
    git_tool: Path,
    cookie: str,
    output_tests_dir: Path,
) -> Path:
    work_dir = Path() / "temp" / ("cts_" + fuzz.get_random_name())

    latest_change = gerrit_util.get_latest_deqp_change(cookie)
    latest_change_number = latest_change["_number"]
    latest_change_details = gerrit_util.get_gerrit_change_details(
        change_number=latest_change_number, cookie=cookie)
    current_revision = latest_change_details["current_revision"]
    cts_archive_path = gerrit_util.download_gerrit_revision(
        output_path=work_dir / "cts.tgz",
        change_number=latest_change_number,
        revision=current_revision,
        download_type=gerrit_util.DownloadType.Archive,
        cookie=cookie,
    )

    cts_dir_name = "cts_temp"
    cts_out = util.extract_archive(cts_archive_path, work_dir / cts_dir_name)

    pending_graphicsfuzz_changes = gerrit_util.get_deqp_graphicsfuzz_pending_changes(
        cookie)

    for pending_change in pending_graphicsfuzz_changes:
        change_number = pending_change["_number"]
        change_details = gerrit_util.get_gerrit_change_details(
            change_number=change_number, cookie=cookie)
        current_revision = change_details["current_revision"]
        patch_zip = gerrit_util.download_gerrit_revision(
            output_path=work_dir / f"{change_number}.zip",
            change_number=change_number,
            revision=current_revision,
            download_type=gerrit_util.DownloadType.Patch,
            cookie=cookie,
        )
        util.extract_archive(patch_zip, work_dir)

    # Create a dummy git repo in the work directory, otherwise "git apply" can fail silently.
    # --unsafe-paths is possibly supposed to address this, but it doesn't seem to work if we
    # are already in a git repo.
    subprocess_util.run([str(git_tool), "init", "."],
                        verbose=True,
                        working_dir=work_dir)

    cmd = [str(git_tool), "apply"]

    patch_names = [p.name for p in work_dir.glob("*.diff")]

    cmd += patch_names

    # Use unix-style path for git.
    cmd += [
        "--verbose",
        "--unsafe-paths",
        f"--directory={cts_dir_name}",
        f"--exclude={cts_dir_name}/external/vulkancts/data/vulkan/amber/graphicsfuzz/index.txt",
        f"--include={cts_dir_name}/external/vulkancts/data/vulkan/amber/graphicsfuzz/*",
    ]

    subprocess_util.run(cmd, verbose=True, working_dir=work_dir)

    util.copy_dir(
        cts_out / "external" / "vulkancts" / "data" / "vulkan" / "amber" /
        "graphicsfuzz",
        output_tests_dir,
    )

    # Sometimes dEQP contributors add non-GraphicsFuzz AmberScript files to the graphicsfuzz directory.
    # We remove these.
    bad_test_names = ["texel_offset.amber"]

    for bad_test_name in bad_test_names:
        bad_test = output_tests_dir / bad_test_name
        if bad_test.is_file():
            bad_test.unlink()

    return output_tests_dir