blob: ff3b03c633daa14920022c3115390e032a9b620c [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
Paul Kehrer8f1a2d52015-11-08 10:02:24 +09008import io
Alex Gaynor7a2b3582014-02-20 07:48:46 -08009import os
Alex Gaynorc7dd9de2017-05-20 14:37:40 -070010import subprocess
Alex Gaynora1dff052014-02-19 16:44:06 -080011import time
Alex Gaynor86201592014-02-19 14:01:06 -080012
Alex Gaynorc7dd9de2017-05-20 14:37:40 -070013import click
Paul Kehrere0e90a82015-11-08 09:38:24 +090014
Alex Gaynorc7dd9de2017-05-20 14:37:40 -070015from clint.textui.progress import Bar as ProgressBar
Alex Gaynor6bc3af72014-01-06 12:04:53 -080016
Alex Gaynor86201592014-02-19 14:01:06 -080017import requests
18
19
Paul Kehrer66e66952017-05-29 20:48:37 -050020JENKINS_URL = (
21 "https://ci.cryptography.io/job/cryptography-support-jobs/"
22 "job/wheel-builder"
23)
Alex Gaynora1dff052014-02-19 16:44:06 -080024
25
Alex Gaynorc7dd9de2017-05-20 14:37:40 -070026def run(*args, **kwargs):
27 kwargs.setdefault("stderr", subprocess.STDOUT)
28 subprocess.check_output(list(args), **kwargs)
29
30
Alex Gaynor9ca7f032014-09-26 23:30:31 -040031def wait_for_build_completed(session):
Paul Kehrerd755e662014-12-24 08:07:56 -060032 # Wait 20 seconds before actually checking if the build is complete, to
Alex Gaynord6b05982014-12-16 09:32:59 -080033 # ensure that it had time to really start.
Paul Kehrerd755e662014-12-24 08:07:56 -060034 time.sleep(20)
Alex Gaynora1dff052014-02-19 16:44:06 -080035 while True:
Alex Gaynor9ca7f032014-09-26 23:30:31 -040036 response = session.get(
Alex Gaynora1dff052014-02-19 16:44:06 -080037 "{0}/lastBuild/api/json/".format(JENKINS_URL),
38 headers={
39 "Accept": "application/json",
40 }
41 )
42 response.raise_for_status()
43 if not response.json()["building"]:
Alex Gaynor7a2b3582014-02-20 07:48:46 -080044 assert response.json()["result"] == "SUCCESS"
Alex Gaynora1dff052014-02-19 16:44:06 -080045 break
46 time.sleep(0.1)
Alex Gaynor6bc3af72014-01-06 12:04:53 -080047
Alex Gaynorc9e4c6a2014-02-19 14:29:37 -080048
Alex Gaynor9ca7f032014-09-26 23:30:31 -040049def download_artifacts(session):
50 response = session.get(
Alex Gaynor7a2b3582014-02-20 07:48:46 -080051 "{0}/lastBuild/api/json/".format(JENKINS_URL),
52 headers={
53 "Accept": "application/json"
54 }
55 )
56 response.raise_for_status()
57 assert not response.json()["building"]
58 assert response.json()["result"] == "SUCCESS"
Alex Gaynor6ab3f462014-02-20 09:07:53 -080059
60 paths = []
61
Paul Kehreree1ca742015-05-28 23:23:26 -050062 last_build_number = response.json()["number"]
Alex Gaynor7a2b3582014-02-20 07:48:46 -080063 for run in response.json()["runs"]:
Paul Kehreree1ca742015-05-28 23:23:26 -050064 if run["number"] != last_build_number:
65 print(
66 "Skipping {0} as it is not from the latest build ({1})".format(
67 run["url"], last_build_number
68 )
69 )
70 continue
71
Alex Gaynor9ca7f032014-09-26 23:30:31 -040072 response = session.get(
Alex Gaynor7a2b3582014-02-20 07:48:46 -080073 run["url"] + "api/json/",
74 headers={
75 "Accept": "application/json",
76 }
77 )
78 response.raise_for_status()
79 for artifact in response.json()["artifacts"]:
Alex Gaynor9ca7f032014-09-26 23:30:31 -040080 response = session.get(
Paul Kehrere0e90a82015-11-08 09:38:24 +090081 "{0}artifact/{1}".format(run["url"], artifact["relativePath"]),
82 stream=True
Alex Gaynor7a2b3582014-02-20 07:48:46 -080083 )
Paul Kehrere0e90a82015-11-08 09:38:24 +090084 assert response.headers["content-length"]
85 print("Downloading {0}".format(artifact["fileName"]))
86 bar = ProgressBar(
87 expected_size=int(response.headers["content-length"]),
88 filled_char="="
89 )
Paul Kehrer8f1a2d52015-11-08 10:02:24 +090090 content = io.BytesIO()
Paul Kehrere0e90a82015-11-08 09:38:24 +090091 for data in response.iter_content(chunk_size=8192):
Paul Kehrer8f1a2d52015-11-08 10:02:24 +090092 content.write(data)
93 bar.show(content.tell())
Paul Kehrerff0d3752015-11-08 10:55:34 +090094 assert bar.expected_size == content.tell()
95 bar.done()
Alex Gaynor7a2b3582014-02-20 07:48:46 -080096 out_path = os.path.join(
97 os.path.dirname(__file__),
98 "dist",
99 artifact["fileName"],
100 )
101 with open(out_path, "wb") as f:
Paul Kehrer8f1a2d52015-11-08 10:02:24 +0900102 f.write(content.getvalue())
Alex Gaynor6ab3f462014-02-20 09:07:53 -0800103 paths.append(out_path)
104 return paths
Alex Gaynor7a2b3582014-02-20 07:48:46 -0800105
106
Alex Gaynorc7dd9de2017-05-20 14:37:40 -0700107@click.command()
108@click.argument("version")
Alex Gaynor6bc3af72014-01-06 12:04:53 -0800109def release(version):
110 """
111 ``version`` should be a string like '0.4' or '1.0'.
112 """
Alex Gaynorc7dd9de2017-05-20 14:37:40 -0700113 run("git", "tag", "-s", version, "-m", "{0} release".format(version))
114 run("git", "push", "--tags")
Alex Gaynor6bc3af72014-01-06 12:04:53 -0800115
Alex Gaynorc7dd9de2017-05-20 14:37:40 -0700116 run("python", "setup.py", "sdist")
117 run("python", "setup.py", "sdist", "bdist_wheel", cwd="vectors/")
Alex Stapletona39a3192014-03-14 20:03:12 +0000118
Alex Gaynorc7dd9de2017-05-20 14:37:40 -0700119 run(
120 "twine", "upload", "-s", "dist/cryptography-{0}*".format(version),
121 "vectors/dist/cryptography_vectors-{0}*".format(version), shell=True
Alex Stapletona39a3192014-03-14 20:03:12 +0000122 )
Alex Gaynor86201592014-02-19 14:01:06 -0800123
Alex Gaynor9ca7f032014-09-26 23:30:31 -0400124 session = requests.Session()
125
Alex Gaynorb44158e2014-09-28 12:00:49 -0400126 # This tells the CDN to delete the cached response for the URL. We do this
127 # so that the Jenkins builders will see the new sdist immediately when they
128 # go to build the wheels.
Alex Gaynor0f74a2c2014-09-26 23:31:19 -0400129 response = session.request(
130 "PURGE", "https://pypi.python.org/simple/cryptography/"
131 )
Alex Gaynor9ca7f032014-09-26 23:30:31 -0400132 response.raise_for_status()
133
Alex Gaynor6629fa52014-02-20 16:10:53 -0800134 token = getpass.getpass("Input the Jenkins token: ")
Paul Kehrer66e66952017-05-29 20:48:37 -0500135 response = session.get(
Alex Gaynora1dff052014-02-19 16:44:06 -0800136 "{0}/build".format(JENKINS_URL),
Alex Gaynor86201592014-02-19 14:01:06 -0800137 params={
Paul Kehrer66e66952017-05-29 20:48:37 -0500138 "token": token,
Alex Gaynor86201592014-02-19 14:01:06 -0800139 "cause": "Building wheels for {0}".format(version)
140 }
141 )
Alex Gaynorc9e4c6a2014-02-19 14:29:37 -0800142 response.raise_for_status()
Alex Gaynor9ca7f032014-09-26 23:30:31 -0400143 wait_for_build_completed(session)
144 paths = download_artifacts(session)
Alex Gaynorc7dd9de2017-05-20 14:37:40 -0700145 run("twine", "upload", " ".join(paths))
146
147
148if __name__ == "__main__":
149 release()