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 |
Alex Gaynor | 72e37c4 | 2017-07-24 08:28:25 -0400 | [diff] [blame^] | 8 | import glob |
Paul Kehrer | 8f1a2d5 | 2015-11-08 10:02:24 +0900 | [diff] [blame] | 9 | import io |
Alex Gaynor | 7a2b358 | 2014-02-20 07:48:46 -0800 | [diff] [blame] | 10 | import os |
Alex Gaynor | c7dd9de | 2017-05-20 14:37:40 -0700 | [diff] [blame] | 11 | import subprocess |
Alex Gaynor | a1dff05 | 2014-02-19 16:44:06 -0800 | [diff] [blame] | 12 | import time |
Alex Gaynor | 8620159 | 2014-02-19 14:01:06 -0800 | [diff] [blame] | 13 | |
Alex Gaynor | c7dd9de | 2017-05-20 14:37:40 -0700 | [diff] [blame] | 14 | import click |
Paul Kehrer | e0e90a8 | 2015-11-08 09:38:24 +0900 | [diff] [blame] | 15 | |
Alex Gaynor | c7dd9de | 2017-05-20 14:37:40 -0700 | [diff] [blame] | 16 | from clint.textui.progress import Bar as ProgressBar |
Alex Gaynor | 6bc3af7 | 2014-01-06 12:04:53 -0800 | [diff] [blame] | 17 | |
Alex Gaynor | 8620159 | 2014-02-19 14:01:06 -0800 | [diff] [blame] | 18 | import requests |
| 19 | |
| 20 | |
Paul Kehrer | 66e6695 | 2017-05-29 20:48:37 -0500 | [diff] [blame] | 21 | JENKINS_URL = ( |
| 22 | "https://ci.cryptography.io/job/cryptography-support-jobs/" |
| 23 | "job/wheel-builder" |
| 24 | ) |
Alex Gaynor | a1dff05 | 2014-02-19 16:44:06 -0800 | [diff] [blame] | 25 | |
| 26 | |
Alex Gaynor | c7dd9de | 2017-05-20 14:37:40 -0700 | [diff] [blame] | 27 | def run(*args, **kwargs): |
| 28 | kwargs.setdefault("stderr", subprocess.STDOUT) |
Alex Gaynor | eadebec | 2017-06-04 15:51:09 -0400 | [diff] [blame] | 29 | try: |
| 30 | subprocess.check_output(list(args), **kwargs) |
| 31 | except subprocess.CalledProcessError as e: |
Alex Gaynor | 3ff51d4 | 2017-06-04 16:38:55 -0400 | [diff] [blame] | 32 | # Reraise this with a different type so that str(e) is something with |
Alex Gaynor | eadebec | 2017-06-04 15:51:09 -0400 | [diff] [blame] | 33 | # stdout in it. |
| 34 | raise Exception(e.cmd, e.returncode, e.output) |
Alex Gaynor | c7dd9de | 2017-05-20 14:37:40 -0700 | [diff] [blame] | 35 | |
| 36 | |
Alex Gaynor | 9ca7f03 | 2014-09-26 23:30:31 -0400 | [diff] [blame] | 37 | def wait_for_build_completed(session): |
Paul Kehrer | d755e66 | 2014-12-24 08:07:56 -0600 | [diff] [blame] | 38 | # Wait 20 seconds before actually checking if the build is complete, to |
Alex Gaynor | d6b0598 | 2014-12-16 09:32:59 -0800 | [diff] [blame] | 39 | # ensure that it had time to really start. |
Paul Kehrer | d755e66 | 2014-12-24 08:07:56 -0600 | [diff] [blame] | 40 | time.sleep(20) |
Alex Gaynor | a1dff05 | 2014-02-19 16:44:06 -0800 | [diff] [blame] | 41 | while True: |
Alex Gaynor | 9ca7f03 | 2014-09-26 23:30:31 -0400 | [diff] [blame] | 42 | response = session.get( |
Alex Gaynor | a1dff05 | 2014-02-19 16:44:06 -0800 | [diff] [blame] | 43 | "{0}/lastBuild/api/json/".format(JENKINS_URL), |
| 44 | headers={ |
| 45 | "Accept": "application/json", |
| 46 | } |
| 47 | ) |
| 48 | response.raise_for_status() |
| 49 | if not response.json()["building"]: |
Alex Gaynor | 7a2b358 | 2014-02-20 07:48:46 -0800 | [diff] [blame] | 50 | assert response.json()["result"] == "SUCCESS" |
Alex Gaynor | a1dff05 | 2014-02-19 16:44:06 -0800 | [diff] [blame] | 51 | break |
| 52 | time.sleep(0.1) |
Alex Gaynor | 6bc3af7 | 2014-01-06 12:04:53 -0800 | [diff] [blame] | 53 | |
Alex Gaynor | c9e4c6a | 2014-02-19 14:29:37 -0800 | [diff] [blame] | 54 | |
Alex Gaynor | 9ca7f03 | 2014-09-26 23:30:31 -0400 | [diff] [blame] | 55 | def download_artifacts(session): |
| 56 | response = session.get( |
Alex Gaynor | 7a2b358 | 2014-02-20 07:48:46 -0800 | [diff] [blame] | 57 | "{0}/lastBuild/api/json/".format(JENKINS_URL), |
| 58 | headers={ |
| 59 | "Accept": "application/json" |
| 60 | } |
| 61 | ) |
| 62 | response.raise_for_status() |
Paul Kehrer | 1d08e51 | 2017-05-29 22:23:56 -0500 | [diff] [blame] | 63 | json_response = response.json() |
| 64 | assert not json_response["building"] |
| 65 | assert json_response["result"] == "SUCCESS" |
Alex Gaynor | 6ab3f46 | 2014-02-20 09:07:53 -0800 | [diff] [blame] | 66 | |
| 67 | paths = [] |
| 68 | |
Paul Kehrer | 1d08e51 | 2017-05-29 22:23:56 -0500 | [diff] [blame] | 69 | for artifact in json_response["artifacts"]: |
Alex Gaynor | 9ca7f03 | 2014-09-26 23:30:31 -0400 | [diff] [blame] | 70 | response = session.get( |
Paul Kehrer | 1d08e51 | 2017-05-29 22:23:56 -0500 | [diff] [blame] | 71 | "{0}artifact/{1}".format( |
| 72 | json_response["url"], artifact["relativePath"] |
| 73 | ), stream=True |
Alex Gaynor | 7a2b358 | 2014-02-20 07:48:46 -0800 | [diff] [blame] | 74 | ) |
Paul Kehrer | 1d08e51 | 2017-05-29 22:23:56 -0500 | [diff] [blame] | 75 | assert response.headers["content-length"] |
| 76 | print("Downloading {0}".format(artifact["fileName"])) |
| 77 | bar = ProgressBar( |
| 78 | expected_size=int(response.headers["content-length"]), |
| 79 | filled_char="=" |
| 80 | ) |
| 81 | content = io.BytesIO() |
| 82 | for data in response.iter_content(chunk_size=8192): |
| 83 | content.write(data) |
| 84 | bar.show(content.tell()) |
| 85 | assert bar.expected_size == content.tell() |
| 86 | bar.done() |
| 87 | out_path = os.path.join( |
| 88 | os.path.dirname(__file__), |
| 89 | "dist", |
| 90 | artifact["fileName"], |
| 91 | ) |
| 92 | with open(out_path, "wb") as f: |
| 93 | f.write(content.getvalue()) |
| 94 | paths.append(out_path) |
Alex Gaynor | 6ab3f46 | 2014-02-20 09:07:53 -0800 | [diff] [blame] | 95 | return paths |
Alex Gaynor | 7a2b358 | 2014-02-20 07:48:46 -0800 | [diff] [blame] | 96 | |
| 97 | |
Alex Gaynor | c7dd9de | 2017-05-20 14:37:40 -0700 | [diff] [blame] | 98 | @click.command() |
| 99 | @click.argument("version") |
Alex Gaynor | 6bc3af7 | 2014-01-06 12:04:53 -0800 | [diff] [blame] | 100 | def release(version): |
| 101 | """ |
| 102 | ``version`` should be a string like '0.4' or '1.0'. |
| 103 | """ |
Alex Gaynor | c7dd9de | 2017-05-20 14:37:40 -0700 | [diff] [blame] | 104 | run("git", "tag", "-s", version, "-m", "{0} release".format(version)) |
| 105 | run("git", "push", "--tags") |
Alex Gaynor | 6bc3af7 | 2014-01-06 12:04:53 -0800 | [diff] [blame] | 106 | |
Alex Gaynor | c7dd9de | 2017-05-20 14:37:40 -0700 | [diff] [blame] | 107 | run("python", "setup.py", "sdist") |
| 108 | run("python", "setup.py", "sdist", "bdist_wheel", cwd="vectors/") |
Alex Stapleton | a39a319 | 2014-03-14 20:03:12 +0000 | [diff] [blame] | 109 | |
Alex Gaynor | 72e37c4 | 2017-07-24 08:28:25 -0400 | [diff] [blame^] | 110 | packages = ( |
| 111 | glob.glob("dist/cryptography-{0}*".format(version)) + |
| 112 | glob.glob("vectors/dist/cryptography_vectors-{0}*".format(version)) |
Alex Stapleton | a39a319 | 2014-03-14 20:03:12 +0000 | [diff] [blame] | 113 | ) |
Alex Gaynor | 72e37c4 | 2017-07-24 08:28:25 -0400 | [diff] [blame^] | 114 | run("twine", "upload", "-s", *packages) |
Alex Gaynor | 8620159 | 2014-02-19 14:01:06 -0800 | [diff] [blame] | 115 | |
Alex Gaynor | 9ca7f03 | 2014-09-26 23:30:31 -0400 | [diff] [blame] | 116 | session = requests.Session() |
| 117 | |
Alex Gaynor | b44158e | 2014-09-28 12:00:49 -0400 | [diff] [blame] | 118 | # This tells the CDN to delete the cached response for the URL. We do this |
| 119 | # so that the Jenkins builders will see the new sdist immediately when they |
| 120 | # go to build the wheels. |
Alex Gaynor | 0f74a2c | 2014-09-26 23:31:19 -0400 | [diff] [blame] | 121 | response = session.request( |
| 122 | "PURGE", "https://pypi.python.org/simple/cryptography/" |
| 123 | ) |
Alex Gaynor | 9ca7f03 | 2014-09-26 23:30:31 -0400 | [diff] [blame] | 124 | response.raise_for_status() |
| 125 | |
Alex Gaynor | 6629fa5 | 2014-02-20 16:10:53 -0800 | [diff] [blame] | 126 | token = getpass.getpass("Input the Jenkins token: ") |
Paul Kehrer | 66e6695 | 2017-05-29 20:48:37 -0500 | [diff] [blame] | 127 | response = session.get( |
Paul Kehrer | 96ffd91 | 2017-07-24 02:44:19 +0300 | [diff] [blame] | 128 | "{0}/buildWithParameters".format(JENKINS_URL), |
Alex Gaynor | 8620159 | 2014-02-19 14:01:06 -0800 | [diff] [blame] | 129 | params={ |
Paul Kehrer | 66e6695 | 2017-05-29 20:48:37 -0500 | [diff] [blame] | 130 | "token": token, |
Paul Kehrer | 0145873 | 2017-06-26 18:56:45 -1000 | [diff] [blame] | 131 | "BUILD_VERSION": version, |
Alex Gaynor | 8620159 | 2014-02-19 14:01:06 -0800 | [diff] [blame] | 132 | "cause": "Building wheels for {0}".format(version) |
| 133 | } |
| 134 | ) |
Alex Gaynor | c9e4c6a | 2014-02-19 14:29:37 -0800 | [diff] [blame] | 135 | response.raise_for_status() |
Alex Gaynor | 9ca7f03 | 2014-09-26 23:30:31 -0400 | [diff] [blame] | 136 | wait_for_build_completed(session) |
| 137 | paths = download_artifacts(session) |
Alex Gaynor | 4973350 | 2017-07-17 14:59:17 -0400 | [diff] [blame] | 138 | run("twine", "upload", *paths) |
Alex Gaynor | c7dd9de | 2017-05-20 14:37:40 -0700 | [diff] [blame] | 139 | |
| 140 | |
| 141 | if __name__ == "__main__": |
| 142 | release() |