Alex Gaynor | 5951f46 | 2014-11-16 09:08:42 -0800 | [diff] [blame] | 1 | # This file is dual licensed under the terms of the Apache License, Version |
| 2 | # 2.0, and the BSD License. See the LICENSE file in the root of this repository |
| 3 | # for complete details. |
Terry Chia | a0a47cd | 2014-07-06 17:51:26 +0800 | [diff] [blame] | 4 | |
Alex Gaynor | 0e10f57 | 2014-01-06 13:17:31 -0800 | [diff] [blame] | 5 | from __future__ import absolute_import, division, print_function |
| 6 | |
Alex Gaynor | 8620159 | 2014-02-19 14:01:06 -0800 | [diff] [blame] | 7 | import getpass |
Alex Gaynor | 72e37c4 | 2017-07-24 08:28:25 -0400 | [diff] [blame] | 8 | import glob |
Paul Kehrer | 8f1a2d5 | 2015-11-08 10:02:24 +0900 | [diff] [blame] | 9 | import io |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame] | 10 | import json |
Alex Gaynor | 7a2b358 | 2014-02-20 07:48:46 -0800 | [diff] [blame] | 11 | import os |
Alex Gaynor | c7dd9de | 2017-05-20 14:37:40 -0700 | [diff] [blame] | 12 | import subprocess |
Alex Gaynor | a1dff05 | 2014-02-19 16:44:06 -0800 | [diff] [blame] | 13 | import time |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame] | 14 | import zipfile |
Alex Gaynor | 8620159 | 2014-02-19 14:01:06 -0800 | [diff] [blame] | 15 | |
Alex Gaynor | c7dd9de | 2017-05-20 14:37:40 -0700 | [diff] [blame] | 16 | import click |
Paul Kehrer | e0e90a8 | 2015-11-08 09:38:24 +0900 | [diff] [blame] | 17 | |
Alex Gaynor | 8620159 | 2014-02-19 14:01:06 -0800 | [diff] [blame] | 18 | import requests |
| 19 | |
| 20 | |
Alex Gaynor | c7dd9de | 2017-05-20 14:37:40 -0700 | [diff] [blame] | 21 | def run(*args, **kwargs): |
Alex Gaynor | c1f8e46 | 2017-08-04 13:45:11 -0400 | [diff] [blame] | 22 | print("[running] {0}".format(list(args))) |
| 23 | subprocess.check_call(list(args), **kwargs) |
Alex Gaynor | c7dd9de | 2017-05-20 14:37:40 -0700 | [diff] [blame] | 24 | |
| 25 | |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame] | 26 | def wait_for_build_complete_github_actions(session, token, run_url): |
Alex Gaynor | a1dff05 | 2014-02-19 16:44:06 -0800 | [diff] [blame] | 27 | while True: |
Alex Gaynor | 9ca7f03 | 2014-09-26 23:30:31 -0400 | [diff] [blame] | 28 | response = session.get( |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame] | 29 | run_url, |
Alex Gaynor | a1dff05 | 2014-02-19 16:44:06 -0800 | [diff] [blame] | 30 | headers={ |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame] | 31 | "Content-Type": "application/json", |
| 32 | "Authorization": "token {}".format(token), |
| 33 | }, |
Alex Gaynor | a1dff05 | 2014-02-19 16:44:06 -0800 | [diff] [blame] | 34 | ) |
| 35 | response.raise_for_status() |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame] | 36 | if response.json()["conclusion"] is not None: |
Alex Gaynor | a1dff05 | 2014-02-19 16:44:06 -0800 | [diff] [blame] | 37 | break |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame] | 38 | time.sleep(3) |
Alex Gaynor | 6bc3af7 | 2014-01-06 12:04:53 -0800 | [diff] [blame] | 39 | |
Alex Gaynor | c9e4c6a | 2014-02-19 14:29:37 -0800 | [diff] [blame] | 40 | |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame] | 41 | def download_artifacts_github_actions(session, token, run_url): |
Alex Gaynor | 9ca7f03 | 2014-09-26 23:30:31 -0400 | [diff] [blame] | 42 | response = session.get( |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame] | 43 | run_url, |
Alex Gaynor | 7a2b358 | 2014-02-20 07:48:46 -0800 | [diff] [blame] | 44 | headers={ |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame] | 45 | "Content-Type": "application/json", |
| 46 | "Authorization": "token {}".format(token), |
| 47 | }, |
Alex Gaynor | 7a2b358 | 2014-02-20 07:48:46 -0800 | [diff] [blame] | 48 | ) |
| 49 | response.raise_for_status() |
Alex Gaynor | 6ab3f46 | 2014-02-20 09:07:53 -0800 | [diff] [blame] | 50 | |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame] | 51 | response = session.get( |
| 52 | response.json()["artifacts_url"], |
| 53 | headers={ |
| 54 | "Content-Type": "application/json", |
| 55 | "Authorization": "token {}".format(token), |
| 56 | }, |
| 57 | ) |
| 58 | response.raise_for_status() |
Alex Gaynor | 6ab3f46 | 2014-02-20 09:07:53 -0800 | [diff] [blame] | 59 | paths = [] |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame] | 60 | for artifact in response.json()["artifacts"]: |
Alex Gaynor | 9ca7f03 | 2014-09-26 23:30:31 -0400 | [diff] [blame] | 61 | response = session.get( |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame] | 62 | artifact["archive_download_url"], |
| 63 | headers={ |
| 64 | "Content-Type": "application/json", |
| 65 | "Authorization": "token {}".format(token), |
| 66 | }, |
Alex Gaynor | 7a2b358 | 2014-02-20 07:48:46 -0800 | [diff] [blame] | 67 | ) |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame] | 68 | with zipfile.ZipFile(io.BytesIO(response.content)) as z: |
| 69 | for name in z.namelist(): |
| 70 | if not name.endswith(".whl"): |
| 71 | continue |
| 72 | p = z.open(name) |
| 73 | out_path = os.path.join( |
| 74 | os.path.dirname(__file__), |
| 75 | "dist", |
| 76 | os.path.basename(name), |
| 77 | ) |
| 78 | with open(out_path, "wb") as f: |
| 79 | f.write(p.read()) |
| 80 | paths.append(out_path) |
Alex Gaynor | 6ab3f46 | 2014-02-20 09:07:53 -0800 | [diff] [blame] | 81 | return paths |
Alex Gaynor | 7a2b358 | 2014-02-20 07:48:46 -0800 | [diff] [blame] | 82 | |
| 83 | |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame] | 84 | def build_github_actions_wheels(token, version): |
| 85 | session = requests.Session() |
| 86 | |
| 87 | response = session.post( |
| 88 | "https://api.github.com/repos/pyca/cryptography/actions/workflows/" |
| 89 | "wheel-builder.yml/dispatches", |
| 90 | headers={ |
| 91 | "Content-Type": "application/json", |
| 92 | "Accept": "application/vnd.github.v3+json", |
| 93 | "Authorization": "token {}".format(token), |
| 94 | }, |
| 95 | data=json.dumps({"ref": "master", "inputs": {"version": version}}), |
| 96 | ) |
| 97 | response.raise_for_status() |
| 98 | |
| 99 | # Give it a few seconds for the run to kick off. |
| 100 | time.sleep(5) |
| 101 | response = session.get( |
| 102 | ( |
| 103 | "https://api.github.com/repos/pyca/cryptography/actions/workflows/" |
| 104 | "wheel-builder.yml/runs?event=workflow_dispatch" |
| 105 | ), |
| 106 | headers={ |
| 107 | "Content-Type": "application/json", |
| 108 | "Authorization": "token {}".format(token), |
| 109 | }, |
| 110 | ) |
| 111 | response.raise_for_status() |
| 112 | run_url = response.json()["workflow_runs"][0]["url"] |
| 113 | wait_for_build_complete_github_actions(session, token, run_url) |
| 114 | return download_artifacts_github_actions(session, token, run_url) |
| 115 | |
| 116 | |
Alex Gaynor | c7dd9de | 2017-05-20 14:37:40 -0700 | [diff] [blame] | 117 | @click.command() |
| 118 | @click.argument("version") |
Alex Gaynor | 6bc3af7 | 2014-01-06 12:04:53 -0800 | [diff] [blame] | 119 | def release(version): |
| 120 | """ |
| 121 | ``version`` should be a string like '0.4' or '1.0'. |
| 122 | """ |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame] | 123 | github_token = getpass.getpass("Github person access token: ") |
| 124 | |
Alex Gaynor | c7dd9de | 2017-05-20 14:37:40 -0700 | [diff] [blame] | 125 | run("git", "tag", "-s", version, "-m", "{0} release".format(version)) |
| 126 | run("git", "push", "--tags") |
Alex Gaynor | 6bc3af7 | 2014-01-06 12:04:53 -0800 | [diff] [blame] | 127 | |
Alex Gaynor | c7dd9de | 2017-05-20 14:37:40 -0700 | [diff] [blame] | 128 | run("python", "setup.py", "sdist") |
| 129 | run("python", "setup.py", "sdist", "bdist_wheel", cwd="vectors/") |
Alex Stapleton | a39a319 | 2014-03-14 20:03:12 +0000 | [diff] [blame] | 130 | |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame] | 131 | packages = glob.glob("dist/cryptography-{0}*".format(version)) + glob.glob( |
| 132 | "vectors/dist/cryptography_vectors-{0}*".format(version) |
Alex Stapleton | a39a319 | 2014-03-14 20:03:12 +0000 | [diff] [blame] | 133 | ) |
Alex Gaynor | 72e37c4 | 2017-07-24 08:28:25 -0400 | [diff] [blame] | 134 | run("twine", "upload", "-s", *packages) |
Alex Gaynor | 8620159 | 2014-02-19 14:01:06 -0800 | [diff] [blame] | 135 | |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame] | 136 | github_actions_wheel_paths = build_github_actions_wheels( |
| 137 | github_token, version |
Alex Gaynor | 8620159 | 2014-02-19 14:01:06 -0800 | [diff] [blame] | 138 | ) |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame] | 139 | run("twine", "upload", *github_actions_wheel_paths) |
Alex Gaynor | c7dd9de | 2017-05-20 14:37:40 -0700 | [diff] [blame] | 140 | |
| 141 | |
| 142 | if __name__ == "__main__": |
| 143 | release() |