def trim_typeshed_zip()

in scripts/download_typeshed.py [0:0]


def trim_typeshed_zip(zip_file: zipfile.ZipFile) -> TypeshedTrimmingResult:
    result_info = []

    stdlib_kept_count = 0
    stdlib_dropped_count = 0
    third_party_kept_count = 0
    third_party_dropped_count = 0
    third_party_package_count = 0
    for info in zip_file.infolist():
        parts = pathlib.Path(info.filename).parts

        if len(parts) <= 1:
            # Entry for the top-level directory
            result_info.append(info)
        elif parts[1] == "stdlib":
            # Python standard library
            if "@python2" in parts:
                if not info.is_dir():
                    stdlib_dropped_count += 1
            else:
                if not info.is_dir():
                    stdlib_kept_count += 1
                result_info.append(info)

        elif parts[1] == "stubs":
            # Third-party libraries
            if "@python2" in parts:
                if not info.is_dir():
                    third_party_dropped_count += 1
            else:
                if not info.is_dir():
                    third_party_kept_count += 1
                elif len(parts) == 3:
                    # Top-level directory of a third-party stub
                    third_party_package_count += 1
                result_info.append(info)

    return TypeshedTrimmingResult(
        entries=[
            FileEntry(
                # Other scripts expect the toplevel directory name to be
                # `typeshed-master`
                path=str(
                    pathlib.Path("typeshed-master").joinpath(
                        *pathlib.Path(info.filename).parts[1:]
                    )
                ),
                data=None if info.is_dir() else zip_file.read(info),
            )
            for info in result_info
        ],
        statistics=Statistics(
            stdlib=FileCount(kept=stdlib_kept_count, dropped=stdlib_dropped_count),
            third_party=FileCount(
                kept=third_party_kept_count, dropped=third_party_dropped_count
            ),
            third_party_package_count=third_party_package_count,
        ),
    )