1#!/usr/bin/env python3 2 3# Copyright 2019 gRPC authors. 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16"""Verifies that all gRPC Python artifacts have been successfully published. 17 18This script is intended to be run from a directory containing the artifacts 19that have been uploaded and only the artifacts that have been uploaded. We use 20PyPI's JSON API to verify that the proper filenames and checksums are present. 21 22Note that PyPI may take several minutes to update its metadata. Don't have a 23heart attack immediately. 24 25This sanity check is a good first step, but ideally, we would automate the 26entire release process. 27""" 28 29import argparse 30import collections 31import hashlib 32import os 33import sys 34 35import requests 36 37_DEFAULT_PACKAGES = [ 38 "grpcio", 39 "grpcio-tools", 40 "grpcio-status", 41 "grpcio-health-checking", 42 "grpcio-reflection", 43 "grpcio-channelz", 44 "grpcio-testing", 45 "grpcio-admin", 46 "grpcio-csds", 47 "grpcio-observability", 48 "grpcio-csm-observability", 49 "xds-protos", 50] 51 52Artifact = collections.namedtuple("Artifact", ("filename", "checksum")) 53 54 55def _get_md5_checksum(filename): 56 """Calculate the md5sum for a file.""" 57 hash_md5 = hashlib.md5() 58 with open(filename, "rb") as f: 59 for chunk in iter(lambda: f.read(4096), b""): 60 hash_md5.update(chunk) 61 return hash_md5.hexdigest() 62 63 64def _get_local_artifacts(): 65 """Get a set of artifacts representing all files in the cwd.""" 66 return set( 67 Artifact(f, _get_md5_checksum(f)) for f in os.listdir(os.getcwd()) 68 ) 69 70 71def _get_remote_artifacts_for_package(package, version): 72 """Get a list of artifacts based on PyPi's json metadata. 73 74 Note that this data will not updated immediately after upload. In my 75 experience, it has taken a minute on average to be fresh. 76 """ 77 artifacts = set() 78 payload_resp = requests.get( 79 "https://pypi.org/pypi/{}/{}/json".format(package, version) 80 ) 81 payload_resp.raise_for_status() 82 payload = payload_resp.json() 83 for download_info in payload["urls"]: 84 artifacts.add( 85 Artifact(download_info["filename"], download_info["md5_digest"]) 86 ) 87 return artifacts 88 89 90def _get_remote_artifacts_for_packages(packages, version): 91 artifacts = set() 92 for package in packages: 93 artifacts |= _get_remote_artifacts_for_package(package, version) 94 return artifacts 95 96 97def _verify_release(version, packages): 98 """Compare the local artifacts to the packages uploaded to PyPI.""" 99 local_artifacts = _get_local_artifacts() 100 remote_artifacts = _get_remote_artifacts_for_packages(packages, version) 101 if local_artifacts != remote_artifacts: 102 local_but_not_remote = local_artifacts - remote_artifacts 103 remote_but_not_local = remote_artifacts - local_artifacts 104 if local_but_not_remote: 105 print("The following artifacts exist locally but not remotely.") 106 for artifact in local_but_not_remote: 107 print(artifact) 108 if remote_but_not_local: 109 print("The following artifacts exist remotely but not locally.") 110 for artifact in remote_but_not_local: 111 print(artifact) 112 sys.exit(1) 113 print("Release verified successfully.") 114 115 116if __name__ == "__main__": 117 parser = argparse.ArgumentParser( 118 "Verify a release. Run this from a directory containing only the" 119 "artifacts to be uploaded. Note that PyPI may take several minutes" 120 "after the upload to reflect the proper metadata." 121 ) 122 parser.add_argument("version") 123 parser.add_argument( 124 "packages", nargs="*", type=str, default=_DEFAULT_PACKAGES 125 ) 126 args = parser.parse_args() 127 _verify_release(args.version, args.packages) 128