blob: b380179d9c7f8780f8e3924afd4e12cc47a20f2a [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)
Alex Gaynoreadebec2017-06-04 15:51:09 -040028 try:
29 subprocess.check_output(list(args), **kwargs)
30 except subprocess.CalledProcessError as e:
Alex Gaynor3ff51d42017-06-04 16:38:55 -040031 # Reraise this with a different type so that str(e) is something with
Alex Gaynoreadebec2017-06-04 15:51:09 -040032 # stdout in it.
33 raise Exception(e.cmd, e.returncode, e.output)
Alex Gaynorc7dd9de2017-05-20 14:37:40 -070034
35
Alex Gaynor9ca7f032014-09-26 23:30:31 -040036def wait_for_build_completed(session):
Paul Kehrerd755e662014-12-24 08:07:56 -060037 # Wait 20 seconds before actually checking if the build is complete, to
Alex Gaynord6b05982014-12-16 09:32:59 -080038 # ensure that it had time to really start.
Paul Kehrerd755e662014-12-24 08:07:56 -060039 time.sleep(20)
Alex Gaynora1dff052014-02-19 16:44:06 -080040 while True:
Alex Gaynor9ca7f032014-09-26 23:30:31 -040041 response = session.get(
Alex Gaynora1dff052014-02-19 16:44:06 -080042 "{0}/lastBuild/api/json/".format(JENKINS_URL),
43 headers={
44 "Accept": "application/json",
45 }
46 )
47 response.raise_for_status()
48 if not response.json()["building"]:
Alex Gaynor7a2b3582014-02-20 07:48:46 -080049 assert response.json()["result"] == "SUCCESS"
Alex Gaynora1dff052014-02-19 16:44:06 -080050 break
51 time.sleep(0.1)
Alex Gaynor6bc3af72014-01-06 12:04:53 -080052
Alex Gaynorc9e4c6a2014-02-19 14:29:37 -080053
Alex Gaynor9ca7f032014-09-26 23:30:31 -040054def download_artifacts(session):
55 response = session.get(
Alex Gaynor7a2b3582014-02-20 07:48:46 -080056 "{0}/lastBuild/api/json/".format(JENKINS_URL),
57 headers={
58 "Accept": "application/json"
59 }
60 )
61 response.raise_for_status()
Paul Kehrer1d08e512017-05-29 22:23:56 -050062 json_response = response.json()
63 assert not json_response["building"]
64 assert json_response["result"] == "SUCCESS"
Alex Gaynor6ab3f462014-02-20 09:07:53 -080065
66 paths = []
67
Paul Kehrer1d08e512017-05-29 22:23:56 -050068 for artifact in json_response["artifacts"]:
Alex Gaynor9ca7f032014-09-26 23:30:31 -040069 response = session.get(
Paul Kehrer1d08e512017-05-29 22:23:56 -050070 "{0}artifact/{1}".format(
71 json_response["url"], artifact["relativePath"]
72 ), stream=True
Alex Gaynor7a2b3582014-02-20 07:48:46 -080073 )
Paul Kehrer1d08e512017-05-29 22:23:56 -050074 assert response.headers["content-length"]
75 print("Downloading {0}".format(artifact["fileName"]))
76 bar = ProgressBar(
77 expected_size=int(response.headers["content-length"]),
78 filled_char="="
79 )
80 content = io.BytesIO()
81 for data in response.iter_content(chunk_size=8192):
82 content.write(data)
83 bar.show(content.tell())
84 assert bar.expected_size == content.tell()
85 bar.done()
86 out_path = os.path.join(
87 os.path.dirname(__file__),
88 "dist",
89 artifact["fileName"],
90 )
91 with open(out_path, "wb") as f:
92 f.write(content.getvalue())
93 paths.append(out_path)
Alex Gaynor6ab3f462014-02-20 09:07:53 -080094 return paths
Alex Gaynor7a2b3582014-02-20 07:48:46 -080095
96
Alex Gaynorc7dd9de2017-05-20 14:37:40 -070097@click.command()
98@click.argument("version")
Alex Gaynor6bc3af72014-01-06 12:04:53 -080099def release(version):
100 """
101 ``version`` should be a string like '0.4' or '1.0'.
102 """
Alex Gaynorc7dd9de2017-05-20 14:37:40 -0700103 run("git", "tag", "-s", version, "-m", "{0} release".format(version))
104 run("git", "push", "--tags")
Alex Gaynor6bc3af72014-01-06 12:04:53 -0800105
Alex Gaynorc7dd9de2017-05-20 14:37:40 -0700106 run("python", "setup.py", "sdist")
107 run("python", "setup.py", "sdist", "bdist_wheel", cwd="vectors/")
Alex Stapletona39a3192014-03-14 20:03:12 +0000108
Alex Gaynorc7dd9de2017-05-20 14:37:40 -0700109 run(
110 "twine", "upload", "-s", "dist/cryptography-{0}*".format(version),
111 "vectors/dist/cryptography_vectors-{0}*".format(version), shell=True
Alex Stapletona39a3192014-03-14 20:03:12 +0000112 )
Alex Gaynor86201592014-02-19 14:01:06 -0800113
Alex Gaynor9ca7f032014-09-26 23:30:31 -0400114 session = requests.Session()
115
Alex Gaynorb44158e2014-09-28 12:00:49 -0400116 # This tells the CDN to delete the cached response for the URL. We do this
117 # so that the Jenkins builders will see the new sdist immediately when they
118 # go to build the wheels.
Alex Gaynor0f74a2c2014-09-26 23:31:19 -0400119 response = session.request(
120 "PURGE", "https://pypi.python.org/simple/cryptography/"
121 )
Alex Gaynor9ca7f032014-09-26 23:30:31 -0400122 response.raise_for_status()
123
Alex Gaynor6629fa52014-02-20 16:10:53 -0800124 token = getpass.getpass("Input the Jenkins token: ")
Paul Kehrer66e66952017-05-29 20:48:37 -0500125 response = session.get(
Paul Kehrer96ffd912017-07-24 02:44:19 +0300126 "{0}/buildWithParameters".format(JENKINS_URL),
Alex Gaynor86201592014-02-19 14:01:06 -0800127 params={
Paul Kehrer66e66952017-05-29 20:48:37 -0500128 "token": token,
Paul Kehrer01458732017-06-26 18:56:45 -1000129 "BUILD_VERSION": version,
Alex Gaynor86201592014-02-19 14:01:06 -0800130 "cause": "Building wheels for {0}".format(version)
131 }
132 )
Alex Gaynorc9e4c6a2014-02-19 14:29:37 -0800133 response.raise_for_status()
Alex Gaynor9ca7f032014-09-26 23:30:31 -0400134 wait_for_build_completed(session)
135 paths = download_artifacts(session)
Alex Gaynor49733502017-07-17 14:59:17 -0400136 run("twine", "upload", *paths)
Alex Gaynorc7dd9de2017-05-20 14:37:40 -0700137
138
139if __name__ == "__main__":
140 release()