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 | a1dff05 | 2014-02-19 16:44:06 -0800 | [diff] [blame] | 10 | import time |
Alex Gaynor | 8620159 | 2014-02-19 14:01:06 -0800 | [diff] [blame] | 11 | |
Paul Kehrer | e0e90a8 | 2015-11-08 09:38:24 +0900 | [diff] [blame] | 12 | from clint.textui.progress import Bar as ProgressBar |
| 13 | |
Alex Gaynor | 6bc3af7 | 2014-01-06 12:04:53 -0800 | [diff] [blame] | 14 | import invoke |
| 15 | |
Alex Gaynor | 8620159 | 2014-02-19 14:01:06 -0800 | [diff] [blame] | 16 | import requests |
| 17 | |
| 18 | |
Paul Kehrer | 47e5df9 | 2014-03-15 08:24:45 -0400 | [diff] [blame] | 19 | JENKINS_URL = "https://jenkins.cryptography.io/job/cryptography-wheel-builder" |
Alex Gaynor | a1dff05 | 2014-02-19 16:44:06 -0800 | [diff] [blame] | 20 | |
| 21 | |
Alex Gaynor | 9ca7f03 | 2014-09-26 23:30:31 -0400 | [diff] [blame] | 22 | def wait_for_build_completed(session): |
Paul Kehrer | d755e66 | 2014-12-24 08:07:56 -0600 | [diff] [blame] | 23 | # Wait 20 seconds before actually checking if the build is complete, to |
Alex Gaynor | d6b0598 | 2014-12-16 09:32:59 -0800 | [diff] [blame] | 24 | # ensure that it had time to really start. |
Paul Kehrer | d755e66 | 2014-12-24 08:07:56 -0600 | [diff] [blame] | 25 | time.sleep(20) |
Alex Gaynor | a1dff05 | 2014-02-19 16:44:06 -0800 | [diff] [blame] | 26 | while True: |
Alex Gaynor | 9ca7f03 | 2014-09-26 23:30:31 -0400 | [diff] [blame] | 27 | response = session.get( |
Alex Gaynor | a1dff05 | 2014-02-19 16:44:06 -0800 | [diff] [blame] | 28 | "{0}/lastBuild/api/json/".format(JENKINS_URL), |
| 29 | headers={ |
| 30 | "Accept": "application/json", |
| 31 | } |
| 32 | ) |
| 33 | response.raise_for_status() |
| 34 | if not response.json()["building"]: |
Alex Gaynor | 7a2b358 | 2014-02-20 07:48:46 -0800 | [diff] [blame] | 35 | assert response.json()["result"] == "SUCCESS" |
Alex Gaynor | a1dff05 | 2014-02-19 16:44:06 -0800 | [diff] [blame] | 36 | break |
| 37 | time.sleep(0.1) |
Alex Gaynor | 6bc3af7 | 2014-01-06 12:04:53 -0800 | [diff] [blame] | 38 | |
Alex Gaynor | c9e4c6a | 2014-02-19 14:29:37 -0800 | [diff] [blame] | 39 | |
Alex Gaynor | 9ca7f03 | 2014-09-26 23:30:31 -0400 | [diff] [blame] | 40 | def download_artifacts(session): |
| 41 | response = session.get( |
Alex Gaynor | 7a2b358 | 2014-02-20 07:48:46 -0800 | [diff] [blame] | 42 | "{0}/lastBuild/api/json/".format(JENKINS_URL), |
| 43 | headers={ |
| 44 | "Accept": "application/json" |
| 45 | } |
| 46 | ) |
| 47 | response.raise_for_status() |
| 48 | assert not response.json()["building"] |
| 49 | assert response.json()["result"] == "SUCCESS" |
Alex Gaynor | 6ab3f46 | 2014-02-20 09:07:53 -0800 | [diff] [blame] | 50 | |
| 51 | paths = [] |
| 52 | |
Paul Kehrer | ee1ca74 | 2015-05-28 23:23:26 -0500 | [diff] [blame] | 53 | last_build_number = response.json()["number"] |
Alex Gaynor | 7a2b358 | 2014-02-20 07:48:46 -0800 | [diff] [blame] | 54 | for run in response.json()["runs"]: |
Paul Kehrer | ee1ca74 | 2015-05-28 23:23:26 -0500 | [diff] [blame] | 55 | if run["number"] != last_build_number: |
| 56 | print( |
| 57 | "Skipping {0} as it is not from the latest build ({1})".format( |
| 58 | run["url"], last_build_number |
| 59 | ) |
| 60 | ) |
| 61 | continue |
| 62 | |
Alex Gaynor | 9ca7f03 | 2014-09-26 23:30:31 -0400 | [diff] [blame] | 63 | response = session.get( |
Alex Gaynor | 7a2b358 | 2014-02-20 07:48:46 -0800 | [diff] [blame] | 64 | run["url"] + "api/json/", |
| 65 | headers={ |
| 66 | "Accept": "application/json", |
| 67 | } |
| 68 | ) |
| 69 | response.raise_for_status() |
| 70 | for artifact in response.json()["artifacts"]: |
Alex Gaynor | 9ca7f03 | 2014-09-26 23:30:31 -0400 | [diff] [blame] | 71 | response = session.get( |
Paul Kehrer | e0e90a8 | 2015-11-08 09:38:24 +0900 | [diff] [blame] | 72 | "{0}artifact/{1}".format(run["url"], artifact["relativePath"]), |
| 73 | stream=True |
Alex Gaynor | 7a2b358 | 2014-02-20 07:48:46 -0800 | [diff] [blame] | 74 | ) |
Paul Kehrer | e0e90a8 | 2015-11-08 09:38:24 +0900 | [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 | ) |
Paul Kehrer | 8f1a2d5 | 2015-11-08 10:02:24 +0900 | [diff] [blame^] | 81 | content = io.BytesIO() |
Paul Kehrer | e0e90a8 | 2015-11-08 09:38:24 +0900 | [diff] [blame] | 82 | for data in response.iter_content(chunk_size=8192): |
Paul Kehrer | 8f1a2d5 | 2015-11-08 10:02:24 +0900 | [diff] [blame^] | 83 | content.write(data) |
| 84 | bar.show(content.tell()) |
| 85 | if bar.expected_size == content.tell(): |
Paul Kehrer | e0e90a8 | 2015-11-08 09:38:24 +0900 | [diff] [blame] | 86 | bar.done() |
Alex Gaynor | 7a2b358 | 2014-02-20 07:48:46 -0800 | [diff] [blame] | 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: |
Paul Kehrer | 8f1a2d5 | 2015-11-08 10:02:24 +0900 | [diff] [blame^] | 93 | f.write(content.getvalue()) |
Alex Gaynor | 6ab3f46 | 2014-02-20 09:07:53 -0800 | [diff] [blame] | 94 | paths.append(out_path) |
| 95 | return paths |
Alex Gaynor | 7a2b358 | 2014-02-20 07:48:46 -0800 | [diff] [blame] | 96 | |
| 97 | |
Alex Gaynor | 6bc3af7 | 2014-01-06 12:04:53 -0800 | [diff] [blame] | 98 | @invoke.task |
| 99 | def release(version): |
| 100 | """ |
| 101 | ``version`` should be a string like '0.4' or '1.0'. |
| 102 | """ |
Paul Kehrer | dd32e95 | 2014-07-09 21:14:24 -0500 | [diff] [blame] | 103 | invoke.run("git tag -s {0} -m '{0} release'".format(version)) |
Alex Gaynor | 6b1235a | 2014-01-06 15:28:59 -0800 | [diff] [blame] | 104 | invoke.run("git push --tags") |
Alex Gaynor | 6bc3af7 | 2014-01-06 12:04:53 -0800 | [diff] [blame] | 105 | |
Alex Gaynor | fea893c | 2014-01-07 11:06:51 -0800 | [diff] [blame] | 106 | invoke.run("python setup.py sdist") |
Alex Stapleton | 3888a84 | 2014-03-24 23:05:53 +0000 | [diff] [blame] | 107 | invoke.run("cd vectors/ && python setup.py sdist bdist_wheel") |
Alex Stapleton | a39a319 | 2014-03-14 20:03:12 +0000 | [diff] [blame] | 108 | |
| 109 | invoke.run( |
| 110 | "twine upload -s dist/cryptography-{0}* " |
Alex Stapleton | 3888a84 | 2014-03-24 23:05:53 +0000 | [diff] [blame] | 111 | "vectors/dist/cryptography_vectors-{0}*".format(version) |
Alex Stapleton | a39a319 | 2014-03-14 20:03:12 +0000 | [diff] [blame] | 112 | ) |
Alex Gaynor | 8620159 | 2014-02-19 14:01:06 -0800 | [diff] [blame] | 113 | |
Alex Gaynor | 9ca7f03 | 2014-09-26 23:30:31 -0400 | [diff] [blame] | 114 | session = requests.Session() |
| 115 | |
Alex Gaynor | b44158e | 2014-09-28 12:00:49 -0400 | [diff] [blame] | 116 | # 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 Gaynor | 0f74a2c | 2014-09-26 23:31:19 -0400 | [diff] [blame] | 119 | response = session.request( |
| 120 | "PURGE", "https://pypi.python.org/simple/cryptography/" |
| 121 | ) |
Alex Gaynor | 9ca7f03 | 2014-09-26 23:30:31 -0400 | [diff] [blame] | 122 | response.raise_for_status() |
| 123 | |
Paul Kehrer | d1d6f0f | 2014-06-20 14:32:02 -0600 | [diff] [blame] | 124 | username = getpass.getpass("Input the GitHub/Jenkins username: ") |
Alex Gaynor | 6629fa5 | 2014-02-20 16:10:53 -0800 | [diff] [blame] | 125 | token = getpass.getpass("Input the Jenkins token: ") |
Alex Gaynor | 9ca7f03 | 2014-09-26 23:30:31 -0400 | [diff] [blame] | 126 | response = session.post( |
Alex Gaynor | a1dff05 | 2014-02-19 16:44:06 -0800 | [diff] [blame] | 127 | "{0}/build".format(JENKINS_URL), |
Paul Kehrer | d1d6f0f | 2014-06-20 14:32:02 -0600 | [diff] [blame] | 128 | auth=requests.auth.HTTPBasicAuth( |
| 129 | username, token |
| 130 | ), |
Alex Gaynor | 8620159 | 2014-02-19 14:01:06 -0800 | [diff] [blame] | 131 | params={ |
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 | 6ab3f46 | 2014-02-20 09:07:53 -0800 | [diff] [blame] | 138 | invoke.run("twine upload {0}".format(" ".join(paths))) |