Path: blob/main/Tools/build/verify_ensurepip_wheels.py
12 views
#! /usr/bin/env python312"""3Compare checksums for wheels in :mod:`ensurepip` against the Cheeseshop.45When GitHub Actions executes the script, output is formatted accordingly.6https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-a-notice-message7"""89import hashlib10import json11import os12import re13from pathlib import Path14from urllib.request import urlopen1516PACKAGE_NAMES = ("pip",)17ENSURE_PIP_ROOT = Path(__file__).parent.parent.parent / "Lib/ensurepip"18WHEEL_DIR = ENSURE_PIP_ROOT / "_bundled"19ENSURE_PIP_INIT_PY_TEXT = (ENSURE_PIP_ROOT / "__init__.py").read_text(encoding="utf-8")20GITHUB_ACTIONS = os.getenv("GITHUB_ACTIONS") == "true"212223def print_notice(file_path: str, message: str) -> None:24if GITHUB_ACTIONS:25message = f"::notice file={file_path}::{message}"26print(message, end="\n\n")272829def print_error(file_path: str, message: str) -> None:30if GITHUB_ACTIONS:31message = f"::error file={file_path}::{message}"32print(message, end="\n\n")333435def verify_wheel(package_name: str) -> bool:36# Find the package on disk37package_path = next(WHEEL_DIR.glob(f"{package_name}*.whl"), None)38if not package_path:39print_error("", f"Could not find a {package_name} wheel on disk.")40return False4142print(f"Verifying checksum for {package_path}.")4344# Find the version of the package used by ensurepip45package_version_match = re.search(46f'_{package_name.upper()}_VERSION = "([^"]+)', ENSURE_PIP_INIT_PY_TEXT47)48if not package_version_match:49print_error(50package_path,51f"No {package_name} version found in Lib/ensurepip/__init__.py.",52)53return False54package_version = package_version_match[1]5556# Get the SHA 256 digest from the Cheeseshop57try:58raw_text = urlopen(f"https://pypi.org/pypi/{package_name}/json").read()59except (OSError, ValueError):60print_error(package_path, f"Could not fetch JSON metadata for {package_name}.")61return False6263release_files = json.loads(raw_text)["releases"][package_version]64for release_info in release_files:65if package_path.name != release_info["filename"]:66continue67expected_digest = release_info["digests"].get("sha256", "")68break69else:70print_error(package_path, f"No digest for {package_name} found from PyPI.")71return False7273# Compute the SHA 256 digest of the wheel on disk74actual_digest = hashlib.sha256(package_path.read_bytes()).hexdigest()7576print(f"Expected digest: {expected_digest}")77print(f"Actual digest: {actual_digest}")7879if actual_digest != expected_digest:80print_error(81package_path, f"Failed to verify the checksum of the {package_name} wheel."82)83return False8485print_notice(86package_path,87f"Successfully verified the checksum of the {package_name} wheel.",88)89return True909192if __name__ == "__main__":93exit_status = 094for package_name in PACKAGE_NAMES:95if not verify_wheel(package_name):96exit_status = 197raise SystemExit(exit_status)9899100