blob: e7a0080b802d3a22d4271e5a333b7409960a3e4d [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()
Paul Kehrer1d08e512017-05-29 22:23:56 -050057 json_response = response.json()
58 assert not json_response["building"]
59 assert json_response["result"] == "SUCCESS"
Alex Gaynor6ab3f462014-02-20 09:07:53 -080060
61 paths = []
62
Paul Kehrer1d08e512017-05-29 22:23:56 -050063 for artifact in json_response["artifacts"]:
Alex Gaynor9ca7f032014-09-26 23:30:31 -040064 response = session.get(
Paul Kehrer1d08e512017-05-29 22:23:56 -050065 "{0}artifact/{1}".format(
66 json_response["url"], artifact["relativePath"]
67 ), stream=True
Alex Gaynor7a2b3582014-02-20 07:48:46 -080068 )
Paul Kehrer1d08e512017-05-29 22:23:56 -050069 assert response.headers["content-length"]
70 print("Downloading {0}".format(artifact["fileName"]))
71 bar = ProgressBar(
72 expected_size=int(response.headers["content-length"]),
73 filled_char="="
74 )
75 content = io.BytesIO()
76 for data in response.iter_content(chunk_size=8192):
77 content.write(data)
78 bar.show(content.tell())
79 assert bar.expected_size == content.tell()
80 bar.done()
81 out_path = os.path.join(
82 os.path.dirname(__file__),
83 "dist",
84 artifact["fileName"],
85 )
86 with open(out_path, "wb") as f:
87 f.write(content.getvalue())
88 paths.append(out_path)
Alex Gaynor6ab3f462014-02-20 09:07:53 -080089 return paths
Alex Gaynor7a2b3582014-02-20 07:48:46 -080090
91
Alex Gaynorc7dd9de2017-05-20 14:37:40 -070092@click.command()
93@click.argument("version")
Alex Gaynor6bc3af72014-01-06 12:04:53 -080094def release(version):
95 """
96 ``version`` should be a string like '0.4' or '1.0'.
97 """
Alex Gaynorc7dd9de2017-05-20 14:37:40 -070098 run("git", "tag", "-s", version, "-m", "{0} release".format(version))
99 run("git", "push", "--tags")
Alex Gaynor6bc3af72014-01-06 12:04:53 -0800100
Alex Gaynorc7dd9de2017-05-20 14:37:40 -0700101 run("python", "setup.py", "sdist")
102 run("python", "setup.py", "sdist", "bdist_wheel", cwd="vectors/")
Alex Stapletona39a3192014-03-14 20:03:12 +0000103
Alex Gaynorc7dd9de2017-05-20 14:37:40 -0700104 run(
105 "twine", "upload", "-s", "dist/cryptography-{0}*".format(version),
106 "vectors/dist/cryptography_vectors-{0}*".format(version), shell=True
Alex Stapletona39a3192014-03-14 20:03:12 +0000107 )
Alex Gaynor86201592014-02-19 14:01:06 -0800108
Alex Gaynor9ca7f032014-09-26 23:30:31 -0400109 session = requests.Session()
110
Alex Gaynorb44158e2014-09-28 12:00:49 -0400111 # This tells the CDN to delete the cached response for the URL. We do this
112 # so that the Jenkins builders will see the new sdist immediately when they
113 # go to build the wheels.
Alex Gaynor0f74a2c2014-09-26 23:31:19 -0400114 response = session.request(
115 "PURGE", "https://pypi.python.org/simple/cryptography/"
116 )
Alex Gaynor9ca7f032014-09-26 23:30:31 -0400117 response.raise_for_status()
118
Alex Gaynor6629fa52014-02-20 16:10:53 -0800119 token = getpass.getpass("Input the Jenkins token: ")
Paul Kehrer66e66952017-05-29 20:48:37 -0500120 response = session.get(
Alex Gaynora1dff052014-02-19 16:44:06 -0800121 "{0}/build".format(JENKINS_URL),
Alex Gaynor86201592014-02-19 14:01:06 -0800122 params={
Paul Kehrer66e66952017-05-29 20:48:37 -0500123 "token": token,
Alex Gaynor86201592014-02-19 14:01:06 -0800124 "cause": "Building wheels for {0}".format(version)
125 }
126 )
Alex Gaynorc9e4c6a2014-02-19 14:29:37 -0800127 response.raise_for_status()
Alex Gaynor9ca7f032014-09-26 23:30:31 -0400128 wait_for_build_completed(session)
129 paths = download_artifacts(session)
Alex Gaynorc7dd9de2017-05-20 14:37:40 -0700130 run("twine", "upload", " ".join(paths))
131
132
133if __name__ == "__main__":
134 release()