(root)/
Python-3.12.0/
Tools/
build/
verify_ensurepip_wheels.py
       1  #!/usr/bin/env python3
       2  
       3  """
       4  Compare checksums for wheels in :mod:`ensurepip` against the Cheeseshop.
       5  
       6  When GitHub Actions executes the script, output is formatted accordingly.
       7  https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-a-notice-message
       8  """
       9  
      10  import hashlib
      11  import json
      12  import os
      13  import re
      14  from pathlib import Path
      15  from urllib.request import urlopen
      16  
      17  PACKAGE_NAMES = ("pip",)
      18  ENSURE_PIP_ROOT = Path(__file__).parent.parent.parent / "Lib/ensurepip"
      19  WHEEL_DIR = ENSURE_PIP_ROOT / "_bundled"
      20  ENSURE_PIP_INIT_PY_TEXT = (ENSURE_PIP_ROOT / "__init__.py").read_text(encoding="utf-8")
      21  GITHUB_ACTIONS = os.getenv("GITHUB_ACTIONS") == "true"
      22  
      23  
      24  def print_notice(file_path: str, message: str) -> None:
      25      if GITHUB_ACTIONS:
      26          message = f"::notice file={file_path}::{message}"
      27      print(message, end="\n\n")
      28  
      29  
      30  def print_error(file_path: str, message: str) -> None:
      31      if GITHUB_ACTIONS:
      32          message = f"::error file={file_path}::{message}"
      33      print(message, end="\n\n")
      34  
      35  
      36  def verify_wheel(package_name: str) -> bool:
      37      # Find the package on disk
      38      package_paths = list(WHEEL_DIR.glob(f"{package_name}*.whl"))
      39      if len(package_paths) != 1:
      40          if package_paths:
      41              for p in package_paths:
      42                  print_error(p, f"Found more than one wheel for package {package_name}.")
      43          else:
      44              print_error("", f"Could not find a {package_name} wheel on disk.")
      45          return False
      46  
      47      package_path = package_paths[0]
      48  
      49      print(f"Verifying checksum for {package_path}.")
      50  
      51      # Find the version of the package used by ensurepip
      52      package_version_match = re.search(
      53          f'_{package_name.upper()}_VERSION = "([^"]+)', ENSURE_PIP_INIT_PY_TEXT
      54      )
      55      if not package_version_match:
      56          print_error(
      57              package_path,
      58              f"No {package_name} version found in Lib/ensurepip/__init__.py.",
      59          )
      60          return False
      61      package_version = package_version_match[1]
      62  
      63      # Get the SHA 256 digest from the Cheeseshop
      64      try:
      65          raw_text = urlopen(f"https://pypi.org/pypi/{package_name}/json").read()
      66      except (OSError, ValueError):
      67          print_error(package_path, f"Could not fetch JSON metadata for {package_name}.")
      68          return False
      69  
      70      release_files = json.loads(raw_text)["releases"][package_version]
      71      for release_info in release_files:
      72          if package_path.name != release_info["filename"]:
      73              continue
      74          expected_digest = release_info["digests"].get("sha256", "")
      75          break
      76      else:
      77          print_error(package_path, f"No digest for {package_name} found from PyPI.")
      78          return False
      79  
      80      # Compute the SHA 256 digest of the wheel on disk
      81      actual_digest = hashlib.sha256(package_path.read_bytes()).hexdigest()
      82  
      83      print(f"Expected digest: {expected_digest}")
      84      print(f"Actual digest:   {actual_digest}")
      85  
      86      if actual_digest != expected_digest:
      87          print_error(
      88              package_path, f"Failed to verify the checksum of the {package_name} wheel."
      89          )
      90          return False
      91  
      92      print_notice(
      93          package_path,
      94          f"Successfully verified the checksum of the {package_name} wheel.",
      95      )
      96      return True
      97  
      98  
      99  if __name__ == "__main__":
     100      exit_status = 0
     101      for package_name in PACKAGE_NAMES:
     102          if not verify_wheel(package_name):
     103              exit_status = 1
     104      raise SystemExit(exit_status)