blob: 7e2c1d81a0aff317c60707f970e36190a78379b7 [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 Kehrer47e5df92014-03-15 08:24:45 -040020JENKINS_URL = "https://jenkins.cryptography.io/job/cryptography-wheel-builder"
Alex Gaynora1dff052014-02-19 16:44:06 -080021
22
Alex Gaynorc7dd9de2017-05-20 14:37:40 -070023def run(*args, **kwargs):
24 kwargs.setdefault("stderr", subprocess.STDOUT)
25 subprocess.check_output(list(args), **kwargs)
26
27
Alex Gaynor9ca7f032014-09-26 23:30:31 -040028def wait_for_build_completed(session):
Paul Kehrerd755e662014-12-24 08:07:56 -060029 # Wait 20 seconds before actually checking if the build is complete, to
Alex Gaynord6b05982014-12-16 09:32:59 -080030 # ensure that it had time to really start.
Paul Kehrerd755e662014-12-24 08:07:56 -060031 time.sleep(20)
Alex Gaynora1dff052014-02-19 16:44:06 -080032 while True:
Alex Gaynor9ca7f032014-09-26 23:30:31 -040033 response = session.get(
Alex Gaynora1dff052014-02-19 16:44:06 -080034 "{0}/lastBuild/api/json/".format(JENKINS_URL),
35 headers={
36 "Accept": "application/json",
37 }
38 )
39 response.raise_for_status()
40 if not response.json()["building"]:
Alex Gaynor7a2b3582014-02-20 07:48:46 -080041 assert response.json()["result"] == "SUCCESS"
Alex Gaynora1dff052014-02-19 16:44:06 -080042 break
43 time.sleep(0.1)
Alex Gaynor6bc3af72014-01-06 12:04:53 -080044
Alex Gaynorc9e4c6a2014-02-19 14:29:37 -080045
Alex Gaynor9ca7f032014-09-26 23:30:31 -040046def download_artifacts(session):
47 response = session.get(
Alex Gaynor7a2b3582014-02-20 07:48:46 -080048 "{0}/lastBuild/api/json/".format(JENKINS_URL),
49 headers={
50 "Accept": "application/json"
51 }
52 )
53 response.raise_for_status()
54 assert not response.json()["building"]
55 assert response.json()["result"] == "SUCCESS"
Alex Gaynor6ab3f462014-02-20 09:07:53 -080056
57 paths = []
58
Paul Kehreree1ca742015-05-28 23:23:26 -050059 last_build_number = response.json()["number"]
Alex Gaynor7a2b3582014-02-20 07:48:46 -080060 for run in response.json()["runs"]:
Paul Kehreree1ca742015-05-28 23:23:26 -050061 if run["number"] != last_build_number:
62 print(
63 "Skipping {0} as it is not from the latest build ({1})".format(
64 run["url"], last_build_number
65 )
66 )
67 continue
68
Alex Gaynor9ca7f032014-09-26 23:30:31 -040069 response = session.get(
Alex Gaynor7a2b3582014-02-20 07:48:46 -080070 run["url"] + "api/json/",
71 headers={
72 "Accept": "application/json",
73 }
74 )
75 response.raise_for_status()
76 for artifact in response.json()["artifacts"]:
Alex Gaynor9ca7f032014-09-26 23:30:31 -040077 response = session.get(
Paul Kehrere0e90a82015-11-08 09:38:24 +090078 "{0}artifact/{1}".format(run["url"], artifact["relativePath"]),
79 stream=True
Alex Gaynor7a2b3582014-02-20 07:48:46 -080080 )
Paul Kehrere0e90a82015-11-08 09:38:24 +090081 assert response.headers["content-length"]
82 print("Downloading {0}".format(artifact["fileName"]))
83 bar = ProgressBar(
84 expected_size=int(response.headers["content-length"]),
85 filled_char="="
86 )
Paul Kehrer8f1a2d52015-11-08 10:02:24 +090087 content = io.BytesIO()
Paul Kehrere0e90a82015-11-08 09:38:24 +090088 for data in response.iter_content(chunk_size=8192):
Paul Kehrer8f1a2d52015-11-08 10:02:24 +090089 content.write(data)
90 bar.show(content.tell())
Paul Kehrerff0d3752015-11-08 10:55:34 +090091 assert bar.expected_size == content.tell()
92 bar.done()
Alex Gaynor7a2b3582014-02-20 07:48:46 -080093 out_path = os.path.join(
94 os.path.dirname(__file__),
95 "dist",
96 artifact["fileName"],
97 )
98 with open(out_path, "wb") as f:
Paul Kehrer8f1a2d52015-11-08 10:02:24 +090099 f.write(content.getvalue())
Alex Gaynor6ab3f462014-02-20 09:07:53 -0800100 paths.append(out_path)
101 return paths
Alex Gaynor7a2b3582014-02-20 07:48:46 -0800102
103
Alex Gaynorc7dd9de2017-05-20 14:37:40 -0700104@click.command()
105@click.argument("version")
Alex Gaynor6bc3af72014-01-06 12:04:53 -0800106def release(version):
107 """
108 ``version`` should be a string like '0.4' or '1.0'.
109 """
Alex Gaynorc7dd9de2017-05-20 14:37:40 -0700110 run("git", "tag", "-s", version, "-m", "{0} release".format(version))
111 run("git", "push", "--tags")
Alex Gaynor6bc3af72014-01-06 12:04:53 -0800112
Alex Gaynorc7dd9de2017-05-20 14:37:40 -0700113 run("python", "setup.py", "sdist")
114 run("python", "setup.py", "sdist", "bdist_wheel", cwd="vectors/")
Alex Stapletona39a3192014-03-14 20:03:12 +0000115
Alex Gaynorc7dd9de2017-05-20 14:37:40 -0700116 run(
117 "twine", "upload", "-s", "dist/cryptography-{0}*".format(version),
118 "vectors/dist/cryptography_vectors-{0}*".format(version), shell=True
Alex Stapletona39a3192014-03-14 20:03:12 +0000119 )
Alex Gaynor86201592014-02-19 14:01:06 -0800120
Alex Gaynor9ca7f032014-09-26 23:30:31 -0400121 session = requests.Session()
122
Alex Gaynorb44158e2014-09-28 12:00:49 -0400123 # This tells the CDN to delete the cached response for the URL. We do this
124 # so that the Jenkins builders will see the new sdist immediately when they
125 # go to build the wheels.
Alex Gaynor0f74a2c2014-09-26 23:31:19 -0400126 response = session.request(
127 "PURGE", "https://pypi.python.org/simple/cryptography/"
128 )
Alex Gaynor9ca7f032014-09-26 23:30:31 -0400129 response.raise_for_status()
130
Paul Kehrerd1d6f0f2014-06-20 14:32:02 -0600131 username = getpass.getpass("Input the GitHub/Jenkins username: ")
Alex Gaynor6629fa52014-02-20 16:10:53 -0800132 token = getpass.getpass("Input the Jenkins token: ")
Alex Gaynor9ca7f032014-09-26 23:30:31 -0400133 response = session.post(
Alex Gaynora1dff052014-02-19 16:44:06 -0800134 "{0}/build".format(JENKINS_URL),
Paul Kehrerd1d6f0f2014-06-20 14:32:02 -0600135 auth=requests.auth.HTTPBasicAuth(
136 username, token
137 ),
Alex Gaynor86201592014-02-19 14:01:06 -0800138 params={
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()