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): |
Alex Gaynor | c1f8e46 | 2017-08-04 13:45:11 -0400 | [diff] [blame] | 28 | print("[running] {0}".format(list(args))) |
| 29 | subprocess.check_call(list(args), **kwargs) |
Alex Gaynor | c7dd9de | 2017-05-20 14:37:40 -0700 | [diff] [blame] | 30 | |
| 31 | |
Alex Gaynor | 9ca7f03 | 2014-09-26 23:30:31 -0400 | [diff] [blame] | 32 | def wait_for_build_completed(session): |
Paul Kehrer | d755e66 | 2014-12-24 08:07:56 -0600 | [diff] [blame] | 33 | # Wait 20 seconds before actually checking if the build is complete, to |
Alex Gaynor | d6b0598 | 2014-12-16 09:32:59 -0800 | [diff] [blame] | 34 | # ensure that it had time to really start. |
Paul Kehrer | d755e66 | 2014-12-24 08:07:56 -0600 | [diff] [blame] | 35 | time.sleep(20) |
Alex Gaynor | a1dff05 | 2014-02-19 16:44:06 -0800 | [diff] [blame] | 36 | while True: |
Alex Gaynor | 9ca7f03 | 2014-09-26 23:30:31 -0400 | [diff] [blame] | 37 | response = session.get( |
Alex Gaynor | a1dff05 | 2014-02-19 16:44:06 -0800 | [diff] [blame] | 38 | "{0}/lastBuild/api/json/".format(JENKINS_URL), |
| 39 | headers={ |
| 40 | "Accept": "application/json", |
| 41 | } |
| 42 | ) |
| 43 | response.raise_for_status() |
| 44 | if not response.json()["building"]: |
Alex Gaynor | 7a2b358 | 2014-02-20 07:48:46 -0800 | [diff] [blame] | 45 | assert response.json()["result"] == "SUCCESS" |
Alex Gaynor | a1dff05 | 2014-02-19 16:44:06 -0800 | [diff] [blame] | 46 | break |
| 47 | time.sleep(0.1) |
Alex Gaynor | 6bc3af7 | 2014-01-06 12:04:53 -0800 | [diff] [blame] | 48 | |
Alex Gaynor | c9e4c6a | 2014-02-19 14:29:37 -0800 | [diff] [blame] | 49 | |
Alex Gaynor | 9ca7f03 | 2014-09-26 23:30:31 -0400 | [diff] [blame] | 50 | def download_artifacts(session): |
| 51 | response = session.get( |
Alex Gaynor | 7a2b358 | 2014-02-20 07:48:46 -0800 | [diff] [blame] | 52 | "{0}/lastBuild/api/json/".format(JENKINS_URL), |
| 53 | headers={ |
| 54 | "Accept": "application/json" |
| 55 | } |
| 56 | ) |
| 57 | response.raise_for_status() |
Paul Kehrer | 1d08e51 | 2017-05-29 22:23:56 -0500 | [diff] [blame] | 58 | json_response = response.json() |
| 59 | assert not json_response["building"] |
| 60 | assert json_response["result"] == "SUCCESS" |
Alex Gaynor | 6ab3f46 | 2014-02-20 09:07:53 -0800 | [diff] [blame] | 61 | |
| 62 | paths = [] |
| 63 | |
Paul Kehrer | 1d08e51 | 2017-05-29 22:23:56 -0500 | [diff] [blame] | 64 | for artifact in json_response["artifacts"]: |
Alex Gaynor | 9ca7f03 | 2014-09-26 23:30:31 -0400 | [diff] [blame] | 65 | response = session.get( |
Paul Kehrer | 1d08e51 | 2017-05-29 22:23:56 -0500 | [diff] [blame] | 66 | "{0}artifact/{1}".format( |
| 67 | json_response["url"], artifact["relativePath"] |
| 68 | ), stream=True |
Alex Gaynor | 7a2b358 | 2014-02-20 07:48:46 -0800 | [diff] [blame] | 69 | ) |
Paul Kehrer | 1d08e51 | 2017-05-29 22:23:56 -0500 | [diff] [blame] | 70 | assert response.headers["content-length"] |
| 71 | print("Downloading {0}".format(artifact["fileName"])) |
| 72 | bar = ProgressBar( |
| 73 | expected_size=int(response.headers["content-length"]), |
| 74 | filled_char="=" |
| 75 | ) |
| 76 | content = io.BytesIO() |
| 77 | for data in response.iter_content(chunk_size=8192): |
| 78 | content.write(data) |
| 79 | bar.show(content.tell()) |
| 80 | assert bar.expected_size == content.tell() |
| 81 | bar.done() |
| 82 | out_path = os.path.join( |
| 83 | os.path.dirname(__file__), |
| 84 | "dist", |
| 85 | artifact["fileName"], |
| 86 | ) |
| 87 | with open(out_path, "wb") as f: |
| 88 | f.write(content.getvalue()) |
| 89 | paths.append(out_path) |
Alex Gaynor | 6ab3f46 | 2014-02-20 09:07:53 -0800 | [diff] [blame] | 90 | return paths |
Alex Gaynor | 7a2b358 | 2014-02-20 07:48:46 -0800 | [diff] [blame] | 91 | |
| 92 | |
Alex Gaynor | c7dd9de | 2017-05-20 14:37:40 -0700 | [diff] [blame] | 93 | @click.command() |
| 94 | @click.argument("version") |
Alex Gaynor | 6bc3af7 | 2014-01-06 12:04:53 -0800 | [diff] [blame] | 95 | def release(version): |
| 96 | """ |
| 97 | ``version`` should be a string like '0.4' or '1.0'. |
| 98 | """ |
Alex Gaynor | c7dd9de | 2017-05-20 14:37:40 -0700 | [diff] [blame] | 99 | run("git", "tag", "-s", version, "-m", "{0} release".format(version)) |
| 100 | run("git", "push", "--tags") |
Alex Gaynor | 6bc3af7 | 2014-01-06 12:04:53 -0800 | [diff] [blame] | 101 | |
Alex Gaynor | c7dd9de | 2017-05-20 14:37:40 -0700 | [diff] [blame] | 102 | run("python", "setup.py", "sdist") |
| 103 | run("python", "setup.py", "sdist", "bdist_wheel", cwd="vectors/") |
Alex Stapleton | a39a319 | 2014-03-14 20:03:12 +0000 | [diff] [blame] | 104 | |
Alex Gaynor | 72e37c4 | 2017-07-24 08:28:25 -0400 | [diff] [blame] | 105 | packages = ( |
| 106 | glob.glob("dist/cryptography-{0}*".format(version)) + |
| 107 | glob.glob("vectors/dist/cryptography_vectors-{0}*".format(version)) |
Alex Stapleton | a39a319 | 2014-03-14 20:03:12 +0000 | [diff] [blame] | 108 | ) |
Alex Gaynor | 72e37c4 | 2017-07-24 08:28:25 -0400 | [diff] [blame] | 109 | run("twine", "upload", "-s", *packages) |
Alex Gaynor | 8620159 | 2014-02-19 14:01:06 -0800 | [diff] [blame] | 110 | |
Alex Gaynor | 9ca7f03 | 2014-09-26 23:30:31 -0400 | [diff] [blame] | 111 | session = requests.Session() |
| 112 | |
Alex Gaynor | 6629fa5 | 2014-02-20 16:10:53 -0800 | [diff] [blame] | 113 | token = getpass.getpass("Input the Jenkins token: ") |
Paul Kehrer | 66e6695 | 2017-05-29 20:48:37 -0500 | [diff] [blame] | 114 | response = session.get( |
Paul Kehrer | 96ffd91 | 2017-07-24 02:44:19 +0300 | [diff] [blame] | 115 | "{0}/buildWithParameters".format(JENKINS_URL), |
Alex Gaynor | 8620159 | 2014-02-19 14:01:06 -0800 | [diff] [blame] | 116 | params={ |
Paul Kehrer | 66e6695 | 2017-05-29 20:48:37 -0500 | [diff] [blame] | 117 | "token": token, |
Paul Kehrer | 0145873 | 2017-06-26 18:56:45 -1000 | [diff] [blame] | 118 | "BUILD_VERSION": version, |
Alex Gaynor | 8620159 | 2014-02-19 14:01:06 -0800 | [diff] [blame] | 119 | "cause": "Building wheels for {0}".format(version) |
| 120 | } |
| 121 | ) |
Alex Gaynor | c9e4c6a | 2014-02-19 14:29:37 -0800 | [diff] [blame] | 122 | response.raise_for_status() |
Alex Gaynor | 9ca7f03 | 2014-09-26 23:30:31 -0400 | [diff] [blame] | 123 | wait_for_build_completed(session) |
| 124 | paths = download_artifacts(session) |
Alex Gaynor | 4973350 | 2017-07-17 14:59:17 -0400 | [diff] [blame] | 125 | run("twine", "upload", *paths) |
Alex Gaynor | c7dd9de | 2017-05-20 14:37:40 -0700 | [diff] [blame] | 126 | |
| 127 | |
| 128 | if __name__ == "__main__": |
| 129 | release() |