blob: 5f3f251d17bdc466842ab67a4534f701c6f8ff6c [file] [log] [blame]
Alex Gaynor5951f462014-11-16 09:08:42 -08001# 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 Chiaa0a47cd2014-07-06 17:51:26 +08004
Alex Gaynor0e10f572014-01-06 13:17:31 -08005from __future__ import absolute_import, division, print_function
6
Alex Gaynor86201592014-02-19 14:01:06 -08007import getpass
Alex Gaynor72e37c42017-07-24 08:28:25 -04008import glob
Paul Kehrer8f1a2d52015-11-08 10:02:24 +09009import io
Lucia Lic6ba99d2021-11-08 22:06:11 +080010import json
Alex Gaynor7a2b3582014-02-20 07:48:46 -080011import os
Alex Gaynorc7dd9de2017-05-20 14:37:40 -070012import subprocess
Alex Gaynora1dff052014-02-19 16:44:06 -080013import time
Lucia Lic6ba99d2021-11-08 22:06:11 +080014import zipfile
Alex Gaynor86201592014-02-19 14:01:06 -080015
Alex Gaynorc7dd9de2017-05-20 14:37:40 -070016import click
Paul Kehrere0e90a82015-11-08 09:38:24 +090017
Alex Gaynor86201592014-02-19 14:01:06 -080018import requests
19
20
Alex Gaynorc7dd9de2017-05-20 14:37:40 -070021def run(*args, **kwargs):
Alex Gaynorc1f8e462017-08-04 13:45:11 -040022 print("[running] {0}".format(list(args)))
23 subprocess.check_call(list(args), **kwargs)
Alex Gaynorc7dd9de2017-05-20 14:37:40 -070024
25
Lucia Lic6ba99d2021-11-08 22:06:11 +080026def wait_for_build_complete_github_actions(session, token, run_url):
Alex Gaynora1dff052014-02-19 16:44:06 -080027 while True:
Alex Gaynor9ca7f032014-09-26 23:30:31 -040028 response = session.get(
Lucia Lic6ba99d2021-11-08 22:06:11 +080029 run_url,
Alex Gaynora1dff052014-02-19 16:44:06 -080030 headers={
Lucia Lic6ba99d2021-11-08 22:06:11 +080031 "Content-Type": "application/json",
32 "Authorization": "token {}".format(token),
33 },
Alex Gaynora1dff052014-02-19 16:44:06 -080034 )
35 response.raise_for_status()
Lucia Lic6ba99d2021-11-08 22:06:11 +080036 if response.json()["conclusion"] is not None:
Alex Gaynora1dff052014-02-19 16:44:06 -080037 break
Lucia Lic6ba99d2021-11-08 22:06:11 +080038 time.sleep(3)
Alex Gaynor6bc3af72014-01-06 12:04:53 -080039
Alex Gaynorc9e4c6a2014-02-19 14:29:37 -080040
Lucia Lic6ba99d2021-11-08 22:06:11 +080041def download_artifacts_github_actions(session, token, run_url):
Alex Gaynor9ca7f032014-09-26 23:30:31 -040042 response = session.get(
Lucia Lic6ba99d2021-11-08 22:06:11 +080043 run_url,
Alex Gaynor7a2b3582014-02-20 07:48:46 -080044 headers={
Lucia Lic6ba99d2021-11-08 22:06:11 +080045 "Content-Type": "application/json",
46 "Authorization": "token {}".format(token),
47 },
Alex Gaynor7a2b3582014-02-20 07:48:46 -080048 )
49 response.raise_for_status()
Alex Gaynor6ab3f462014-02-20 09:07:53 -080050
Lucia Lic6ba99d2021-11-08 22:06:11 +080051 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 Gaynor6ab3f462014-02-20 09:07:53 -080059 paths = []
Lucia Lic6ba99d2021-11-08 22:06:11 +080060 for artifact in response.json()["artifacts"]:
Alex Gaynor9ca7f032014-09-26 23:30:31 -040061 response = session.get(
Lucia Lic6ba99d2021-11-08 22:06:11 +080062 artifact["archive_download_url"],
63 headers={
64 "Content-Type": "application/json",
65 "Authorization": "token {}".format(token),
66 },
Alex Gaynor7a2b3582014-02-20 07:48:46 -080067 )
Lucia Lic6ba99d2021-11-08 22:06:11 +080068 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 Gaynor6ab3f462014-02-20 09:07:53 -080081 return paths
Alex Gaynor7a2b3582014-02-20 07:48:46 -080082
83
Lucia Lic6ba99d2021-11-08 22:06:11 +080084def 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 Gaynorc7dd9de2017-05-20 14:37:40 -0700117@click.command()
118@click.argument("version")
Alex Gaynor6bc3af72014-01-06 12:04:53 -0800119def release(version):
120 """
121 ``version`` should be a string like '0.4' or '1.0'.
122 """
Lucia Lic6ba99d2021-11-08 22:06:11 +0800123 github_token = getpass.getpass("Github person access token: ")
124
Alex Gaynorc7dd9de2017-05-20 14:37:40 -0700125 run("git", "tag", "-s", version, "-m", "{0} release".format(version))
126 run("git", "push", "--tags")
Alex Gaynor6bc3af72014-01-06 12:04:53 -0800127
Alex Gaynorc7dd9de2017-05-20 14:37:40 -0700128 run("python", "setup.py", "sdist")
129 run("python", "setup.py", "sdist", "bdist_wheel", cwd="vectors/")
Alex Stapletona39a3192014-03-14 20:03:12 +0000130
Lucia Lic6ba99d2021-11-08 22:06:11 +0800131 packages = glob.glob("dist/cryptography-{0}*".format(version)) + glob.glob(
132 "vectors/dist/cryptography_vectors-{0}*".format(version)
Alex Stapletona39a3192014-03-14 20:03:12 +0000133 )
Alex Gaynor72e37c42017-07-24 08:28:25 -0400134 run("twine", "upload", "-s", *packages)
Alex Gaynor86201592014-02-19 14:01:06 -0800135
Lucia Lic6ba99d2021-11-08 22:06:11 +0800136 github_actions_wheel_paths = build_github_actions_wheels(
137 github_token, version
Alex Gaynor86201592014-02-19 14:01:06 -0800138 )
Lucia Lic6ba99d2021-11-08 22:06:11 +0800139 run("twine", "upload", *github_actions_wheel_paths)
Alex Gaynorc7dd9de2017-05-20 14:37:40 -0700140
141
142if __name__ == "__main__":
143 release()