Alex Gaynor | 5951f46 | 2014-11-16 09:08:42 -0800 | [diff] [blame] | 1 | # 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 Chia | a0a47cd | 2014-07-06 17:51:26 +0800 | [diff] [blame] | 4 | |
Alex Gaynor | 0e10f57 | 2014-01-06 13:17:31 -0800 | [diff] [blame] | 5 | from __future__ import absolute_import, division, print_function |
| 6 | |
Alex Gaynor | 8620159 | 2014-02-19 14:01:06 -0800 | [diff] [blame] | 7 | import getpass |
Paul Kehrer | 8f1a2d5 | 2015-11-08 10:02:24 +0900 | [diff] [blame] | 8 | import io |
Alex Gaynor | 7a2b358 | 2014-02-20 07:48:46 -0800 | [diff] [blame] | 9 | import os |
Alex Gaynor | c7dd9de | 2017-05-20 14:37:40 -0700 | [diff] [blame] | 10 | import subprocess |
Alex Gaynor | a1dff05 | 2014-02-19 16:44:06 -0800 | [diff] [blame] | 11 | import time |
Alex Gaynor | 8620159 | 2014-02-19 14:01:06 -0800 | [diff] [blame] | 12 | |
Alex Gaynor | c7dd9de | 2017-05-20 14:37:40 -0700 | [diff] [blame] | 13 | import click |
Paul Kehrer | e0e90a8 | 2015-11-08 09:38:24 +0900 | [diff] [blame] | 14 | |
Alex Gaynor | c7dd9de | 2017-05-20 14:37:40 -0700 | [diff] [blame] | 15 | from clint.textui.progress import Bar as ProgressBar |
Alex Gaynor | 6bc3af7 | 2014-01-06 12:04:53 -0800 | [diff] [blame] | 16 | |
Alex Gaynor | 8620159 | 2014-02-19 14:01:06 -0800 | [diff] [blame] | 17 | import requests |
| 18 | |
| 19 | |
Paul Kehrer | 66e6695 | 2017-05-29 20:48:37 -0500 | [diff] [blame] | 20 | JENKINS_URL = ( |
| 21 | "https://ci.cryptography.io/job/cryptography-support-jobs/" |
| 22 | "job/wheel-builder" |
| 23 | ) |
Alex Gaynor | a1dff05 | 2014-02-19 16:44:06 -0800 | [diff] [blame] | 24 | |
| 25 | |
Alex Gaynor | c7dd9de | 2017-05-20 14:37:40 -0700 | [diff] [blame] | 26 | def run(*args, **kwargs): |
| 27 | kwargs.setdefault("stderr", subprocess.STDOUT) |
| 28 | subprocess.check_output(list(args), **kwargs) |
| 29 | |
| 30 | |
Alex Gaynor | 9ca7f03 | 2014-09-26 23:30:31 -0400 | [diff] [blame] | 31 | def wait_for_build_completed(session): |
Paul Kehrer | d755e66 | 2014-12-24 08:07:56 -0600 | [diff] [blame] | 32 | # Wait 20 seconds before actually checking if the build is complete, to |
Alex Gaynor | d6b0598 | 2014-12-16 09:32:59 -0800 | [diff] [blame] | 33 | # ensure that it had time to really start. |
Paul Kehrer | d755e66 | 2014-12-24 08:07:56 -0600 | [diff] [blame] | 34 | time.sleep(20) |
Alex Gaynor | a1dff05 | 2014-02-19 16:44:06 -0800 | [diff] [blame] | 35 | while True: |
Alex Gaynor | 9ca7f03 | 2014-09-26 23:30:31 -0400 | [diff] [blame] | 36 | response = session.get( |
Alex Gaynor | a1dff05 | 2014-02-19 16:44:06 -0800 | [diff] [blame] | 37 | "{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 Gaynor | 7a2b358 | 2014-02-20 07:48:46 -0800 | [diff] [blame] | 44 | assert response.json()["result"] == "SUCCESS" |
Alex Gaynor | a1dff05 | 2014-02-19 16:44:06 -0800 | [diff] [blame] | 45 | break |
| 46 | time.sleep(0.1) |
Alex Gaynor | 6bc3af7 | 2014-01-06 12:04:53 -0800 | [diff] [blame] | 47 | |
Alex Gaynor | c9e4c6a | 2014-02-19 14:29:37 -0800 | [diff] [blame] | 48 | |
Alex Gaynor | 9ca7f03 | 2014-09-26 23:30:31 -0400 | [diff] [blame] | 49 | def download_artifacts(session): |
| 50 | response = session.get( |
Alex Gaynor | 7a2b358 | 2014-02-20 07:48:46 -0800 | [diff] [blame] | 51 | "{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 Gaynor | 6ab3f46 | 2014-02-20 09:07:53 -0800 | [diff] [blame] | 59 | |
| 60 | paths = [] |
| 61 | |
Paul Kehrer | ee1ca74 | 2015-05-28 23:23:26 -0500 | [diff] [blame] | 62 | last_build_number = response.json()["number"] |
Alex Gaynor | 7a2b358 | 2014-02-20 07:48:46 -0800 | [diff] [blame] | 63 | for run in response.json()["runs"]: |
Paul Kehrer | ee1ca74 | 2015-05-28 23:23:26 -0500 | [diff] [blame] | 64 | 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 Gaynor | 9ca7f03 | 2014-09-26 23:30:31 -0400 | [diff] [blame] | 72 | response = session.get( |
Alex Gaynor | 7a2b358 | 2014-02-20 07:48:46 -0800 | [diff] [blame] | 73 | 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 Gaynor | 9ca7f03 | 2014-09-26 23:30:31 -0400 | [diff] [blame] | 80 | response = session.get( |
Paul Kehrer | e0e90a8 | 2015-11-08 09:38:24 +0900 | [diff] [blame] | 81 | "{0}artifact/{1}".format(run["url"], artifact["relativePath"]), |
| 82 | stream=True |
Alex Gaynor | 7a2b358 | 2014-02-20 07:48:46 -0800 | [diff] [blame] | 83 | ) |
Paul Kehrer | e0e90a8 | 2015-11-08 09:38:24 +0900 | [diff] [blame] | 84 | 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 Kehrer | 8f1a2d5 | 2015-11-08 10:02:24 +0900 | [diff] [blame] | 90 | content = io.BytesIO() |
Paul Kehrer | e0e90a8 | 2015-11-08 09:38:24 +0900 | [diff] [blame] | 91 | for data in response.iter_content(chunk_size=8192): |
Paul Kehrer | 8f1a2d5 | 2015-11-08 10:02:24 +0900 | [diff] [blame] | 92 | content.write(data) |
| 93 | bar.show(content.tell()) |
Paul Kehrer | ff0d375 | 2015-11-08 10:55:34 +0900 | [diff] [blame] | 94 | assert bar.expected_size == content.tell() |
| 95 | bar.done() |
Alex Gaynor | 7a2b358 | 2014-02-20 07:48:46 -0800 | [diff] [blame] | 96 | 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 Kehrer | 8f1a2d5 | 2015-11-08 10:02:24 +0900 | [diff] [blame] | 102 | f.write(content.getvalue()) |
Alex Gaynor | 6ab3f46 | 2014-02-20 09:07:53 -0800 | [diff] [blame] | 103 | paths.append(out_path) |
| 104 | return paths |
Alex Gaynor | 7a2b358 | 2014-02-20 07:48:46 -0800 | [diff] [blame] | 105 | |
| 106 | |
Alex Gaynor | c7dd9de | 2017-05-20 14:37:40 -0700 | [diff] [blame] | 107 | @click.command() |
| 108 | @click.argument("version") |
Alex Gaynor | 6bc3af7 | 2014-01-06 12:04:53 -0800 | [diff] [blame] | 109 | def release(version): |
| 110 | """ |
| 111 | ``version`` should be a string like '0.4' or '1.0'. |
| 112 | """ |
Alex Gaynor | c7dd9de | 2017-05-20 14:37:40 -0700 | [diff] [blame] | 113 | run("git", "tag", "-s", version, "-m", "{0} release".format(version)) |
| 114 | run("git", "push", "--tags") |
Alex Gaynor | 6bc3af7 | 2014-01-06 12:04:53 -0800 | [diff] [blame] | 115 | |
Alex Gaynor | c7dd9de | 2017-05-20 14:37:40 -0700 | [diff] [blame] | 116 | run("python", "setup.py", "sdist") |
| 117 | run("python", "setup.py", "sdist", "bdist_wheel", cwd="vectors/") |
Alex Stapleton | a39a319 | 2014-03-14 20:03:12 +0000 | [diff] [blame] | 118 | |
Alex Gaynor | c7dd9de | 2017-05-20 14:37:40 -0700 | [diff] [blame] | 119 | run( |
| 120 | "twine", "upload", "-s", "dist/cryptography-{0}*".format(version), |
| 121 | "vectors/dist/cryptography_vectors-{0}*".format(version), shell=True |
Alex Stapleton | a39a319 | 2014-03-14 20:03:12 +0000 | [diff] [blame] | 122 | ) |
Alex Gaynor | 8620159 | 2014-02-19 14:01:06 -0800 | [diff] [blame] | 123 | |
Alex Gaynor | 9ca7f03 | 2014-09-26 23:30:31 -0400 | [diff] [blame] | 124 | session = requests.Session() |
| 125 | |
Alex Gaynor | b44158e | 2014-09-28 12:00:49 -0400 | [diff] [blame] | 126 | # 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 Gaynor | 0f74a2c | 2014-09-26 23:31:19 -0400 | [diff] [blame] | 129 | response = session.request( |
| 130 | "PURGE", "https://pypi.python.org/simple/cryptography/" |
| 131 | ) |
Alex Gaynor | 9ca7f03 | 2014-09-26 23:30:31 -0400 | [diff] [blame] | 132 | response.raise_for_status() |
| 133 | |
Alex Gaynor | 6629fa5 | 2014-02-20 16:10:53 -0800 | [diff] [blame] | 134 | token = getpass.getpass("Input the Jenkins token: ") |
Paul Kehrer | 66e6695 | 2017-05-29 20:48:37 -0500 | [diff] [blame] | 135 | response = session.get( |
Alex Gaynor | a1dff05 | 2014-02-19 16:44:06 -0800 | [diff] [blame] | 136 | "{0}/build".format(JENKINS_URL), |
Alex Gaynor | 8620159 | 2014-02-19 14:01:06 -0800 | [diff] [blame] | 137 | params={ |
Paul Kehrer | 66e6695 | 2017-05-29 20:48:37 -0500 | [diff] [blame] | 138 | "token": token, |
Alex Gaynor | 8620159 | 2014-02-19 14:01:06 -0800 | [diff] [blame] | 139 | "cause": "Building wheels for {0}".format(version) |
| 140 | } |
| 141 | ) |
Alex Gaynor | c9e4c6a | 2014-02-19 14:29:37 -0800 | [diff] [blame] | 142 | response.raise_for_status() |
Alex Gaynor | 9ca7f03 | 2014-09-26 23:30:31 -0400 | [diff] [blame] | 143 | wait_for_build_completed(session) |
| 144 | paths = download_artifacts(session) |
Alex Gaynor | c7dd9de | 2017-05-20 14:37:40 -0700 | [diff] [blame] | 145 | run("twine", "upload", " ".join(paths)) |
| 146 | |
| 147 | |
| 148 | if __name__ == "__main__": |
| 149 | release() |