| 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 |
|
| 18 |
This script is intended to be run from a directory containing the artifacts |
| 19 |
that have been uploaded and only the artifacts that have been uploaded. We use |
| 20 |
PyPI's JSON API to verify that the proper filenames and checksums are present. |
| 21 |
|
| 22 |
Note that PyPI may take several minutes to update its metadata. Don't have a |
| 23 |
heart attack immediately. |
| 24 |
|
| 25 |
This sanity check is a good first step, but ideally, we would automate the |
| 26 |
entire release process. |
| 27 |
""" |
| 28 |
|
| 29 |
import argparse |
| 30 |
import collections |
| 31 |
import hashlib |
| 32 |
import os |
| 33 |
import sys |
| 34 |
|
| 35 |
import 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 |
] |
| 48 |
|
| 49 |
Artifact = collections.namedtuple("Artifact", ("filename", "checksum")) |
| 50 |
|
| 51 |
|
| 52 |
def _get_md5_checksum(filename): |
| 53 |
"""Calculate the md5sum for a file.""" |
| 54 |
hash_md5 = hashlib.md5() |
| 55 |
with open(filename, 'rb') as f: |
| 56 |
for chunk in iter(lambda: f.read(4096), b""): |
| 57 |
hash_md5.update(chunk) |
| 58 |
return hash_md5.hexdigest() |
| 59 |
|
| 60 |
|
| 61 |
def _get_local_artifacts(): |
| 62 |
"""Get a set of artifacts representing all files in the cwd.""" |
| 63 |
return set( |
| 64 |
Artifact(f, _get_md5_checksum(f)) for f in os.listdir(os.getcwd())) |
| 65 |
|
| 66 |
|
| 67 |
def _get_remote_artifacts_for_package(package, version): |
| 68 |
"""Get a list of artifacts based on PyPi's json metadata. |
| 69 |
|
| 70 |
Note that this data will not updated immediately after upload. In my |
| 71 |
experience, it has taken a minute on average to be fresh. |
| 72 |
""" |
| 73 |
artifacts = set() |
| 74 |
payload_resp = requests.get("https://pypi.org/pypi/{}/{}/json".format( |
| 75 |
package, version)) |
| 76 |
payload_resp.raise_for_status() |
| 77 |
payload = payload_resp.json() |
| 78 |
for download_info in payload['urls']: |
| 79 |
artifacts.add( |
| 80 |
Artifact(download_info['filename'], download_info['md5_digest'])) |
| 81 |
return artifacts |
| 82 |
|
| 83 |
|
| 84 |
def _get_remote_artifacts_for_packages(packages, version): |
| 85 |
artifacts = set() |
| 86 |
for package in packages: |
| 87 |
artifacts |= _get_remote_artifacts_for_package(package, version) |
| 88 |
return artifacts |
| 89 |
|
| 90 |
|
| 91 |
def _verify_release(version, packages): |
| 92 |
"""Compare the local artifacts to the packages uploaded to PyPI.""" |
| 93 |
local_artifacts = _get_local_artifacts() |
| 94 |
remote_artifacts = _get_remote_artifacts_for_packages(packages, version) |
| 95 |
if local_artifacts != remote_artifacts: |
| 96 |
local_but_not_remote = local_artifacts - remote_artifacts |
| 97 |
remote_but_not_local = remote_artifacts - local_artifacts |
| 98 |
if local_but_not_remote: |
| 99 |
print("The following artifacts exist locally but not remotely.") |
| 100 |
for artifact in local_but_not_remote: |
| 101 |
print(artifact) |
| 102 |
if remote_but_not_local: |
| 103 |
print("The following artifacts exist remotely but not locally.") |
| 104 |
for artifact in remote_but_not_local: |
| 105 |
print(artifact) |
| 106 |
sys.exit(1) |
| 107 |
print("Release verified successfully.") |
| 108 |
|
| 109 |
|
| 110 |
if __name__ == "__main__": |
| 111 |
parser = argparse.ArgumentParser( |
| 112 |
"Verify a release. Run this from a directory containing only the" |
| 113 |
"artifacts to be uploaded. Note that PyPI may take several minutes" |
| 114 |
"after the upload to reflect the proper metadata.") |
| 115 |
parser.add_argument("version") |
| 116 |
parser.add_argument("packages", |
| 117 |
nargs='*', |
| 118 |
type=str, |
| 119 |
default=_DEFAULT_PACKAGES) |
| 120 |
args = parser.parse_args() |
| 121 |
_verify_release(args.version, args.packages) |