blob: 15027d740ce0790fe1270bfc90981483e8c322d1 [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
Alex Gaynor7a2b3582014-02-20 07:48:46 -08008import os
Alex Gaynora1dff052014-02-19 16:44:06 -08009import time
Alex Gaynor86201592014-02-19 14:01:06 -080010
Paul Kehrere0e90a82015-11-08 09:38:24 +090011from clint.textui.progress import Bar as ProgressBar
12
Alex Gaynor6bc3af72014-01-06 12:04:53 -080013import invoke
14
Alex Gaynor86201592014-02-19 14:01:06 -080015import requests
16
17
Paul Kehrer47e5df92014-03-15 08:24:45 -040018JENKINS_URL = "https://jenkins.cryptography.io/job/cryptography-wheel-builder"
Alex Gaynora1dff052014-02-19 16:44:06 -080019
20
Alex Gaynor9ca7f032014-09-26 23:30:31 -040021def wait_for_build_completed(session):
Paul Kehrerd755e662014-12-24 08:07:56 -060022 # Wait 20 seconds before actually checking if the build is complete, to
Alex Gaynord6b05982014-12-16 09:32:59 -080023 # ensure that it had time to really start.
Paul Kehrerd755e662014-12-24 08:07:56 -060024 time.sleep(20)
Alex Gaynora1dff052014-02-19 16:44:06 -080025 while True:
Alex Gaynor9ca7f032014-09-26 23:30:31 -040026 response = session.get(
Alex Gaynora1dff052014-02-19 16:44:06 -080027 "{0}/lastBuild/api/json/".format(JENKINS_URL),
28 headers={
29 "Accept": "application/json",
30 }
31 )
32 response.raise_for_status()
33 if not response.json()["building"]:
Alex Gaynor7a2b3582014-02-20 07:48:46 -080034 assert response.json()["result"] == "SUCCESS"
Alex Gaynora1dff052014-02-19 16:44:06 -080035 break
36 time.sleep(0.1)
Alex Gaynor6bc3af72014-01-06 12:04:53 -080037
Alex Gaynorc9e4c6a2014-02-19 14:29:37 -080038
Alex Gaynor9ca7f032014-09-26 23:30:31 -040039def download_artifacts(session):
40 response = session.get(
Alex Gaynor7a2b3582014-02-20 07:48:46 -080041 "{0}/lastBuild/api/json/".format(JENKINS_URL),
42 headers={
43 "Accept": "application/json"
44 }
45 )
46 response.raise_for_status()
47 assert not response.json()["building"]
48 assert response.json()["result"] == "SUCCESS"
Alex Gaynor6ab3f462014-02-20 09:07:53 -080049
50 paths = []
51
Paul Kehreree1ca742015-05-28 23:23:26 -050052 last_build_number = response.json()["number"]
Alex Gaynor7a2b3582014-02-20 07:48:46 -080053 for run in response.json()["runs"]:
Paul Kehreree1ca742015-05-28 23:23:26 -050054 if run["number"] != last_build_number:
55 print(
56 "Skipping {0} as it is not from the latest build ({1})".format(
57 run["url"], last_build_number
58 )
59 )
60 continue
61
Alex Gaynor9ca7f032014-09-26 23:30:31 -040062 response = session.get(
Alex Gaynor7a2b3582014-02-20 07:48:46 -080063 run["url"] + "api/json/",
64 headers={
65 "Accept": "application/json",
66 }
67 )
68 response.raise_for_status()
69 for artifact in response.json()["artifacts"]:
Alex Gaynor9ca7f032014-09-26 23:30:31 -040070 response = session.get(
Paul Kehrere0e90a82015-11-08 09:38:24 +090071 "{0}artifact/{1}".format(run["url"], artifact["relativePath"]),
72 stream=True
Alex Gaynor7a2b3582014-02-20 07:48:46 -080073 )
Paul Kehrere0e90a82015-11-08 09:38:24 +090074 assert response.headers["content-length"]
75 print("Downloading {0}".format(artifact["fileName"]))
76 bar = ProgressBar(
77 expected_size=int(response.headers["content-length"]),
78 filled_char="="
79 )
80 content = b''
81 for data in response.iter_content(chunk_size=8192):
82 content += data
83 bar.show(len(content))
84 if bar.expected_size == len(content):
85 bar.done()
Alex Gaynor7a2b3582014-02-20 07:48:46 -080086 out_path = os.path.join(
87 os.path.dirname(__file__),
88 "dist",
89 artifact["fileName"],
90 )
91 with open(out_path, "wb") as f:
Paul Kehrere0e90a82015-11-08 09:38:24 +090092 f.write(content)
Alex Gaynor6ab3f462014-02-20 09:07:53 -080093 paths.append(out_path)
94 return paths
Alex Gaynor7a2b3582014-02-20 07:48:46 -080095
96
Alex Gaynor6bc3af72014-01-06 12:04:53 -080097@invoke.task
98def release(version):
99 """
100 ``version`` should be a string like '0.4' or '1.0'.
101 """
Paul Kehrerdd32e952014-07-09 21:14:24 -0500102 invoke.run("git tag -s {0} -m '{0} release'".format(version))
Alex Gaynor6b1235a2014-01-06 15:28:59 -0800103 invoke.run("git push --tags")
Alex Gaynor6bc3af72014-01-06 12:04:53 -0800104
Alex Gaynorfea893c2014-01-07 11:06:51 -0800105 invoke.run("python setup.py sdist")
Alex Stapleton3888a842014-03-24 23:05:53 +0000106 invoke.run("cd vectors/ && python setup.py sdist bdist_wheel")
Alex Stapletona39a3192014-03-14 20:03:12 +0000107
108 invoke.run(
109 "twine upload -s dist/cryptography-{0}* "
Alex Stapleton3888a842014-03-24 23:05:53 +0000110 "vectors/dist/cryptography_vectors-{0}*".format(version)
Alex Stapletona39a3192014-03-14 20:03:12 +0000111 )
Alex Gaynor86201592014-02-19 14:01:06 -0800112
Alex Gaynor9ca7f032014-09-26 23:30:31 -0400113 session = requests.Session()
114
Alex Gaynorb44158e2014-09-28 12:00:49 -0400115 # This tells the CDN to delete the cached response for the URL. We do this
116 # so that the Jenkins builders will see the new sdist immediately when they
117 # go to build the wheels.
Alex Gaynor0f74a2c2014-09-26 23:31:19 -0400118 response = session.request(
119 "PURGE", "https://pypi.python.org/simple/cryptography/"
120 )
Alex Gaynor9ca7f032014-09-26 23:30:31 -0400121 response.raise_for_status()
122
Paul Kehrerd1d6f0f2014-06-20 14:32:02 -0600123 username = getpass.getpass("Input the GitHub/Jenkins username: ")
Alex Gaynor6629fa52014-02-20 16:10:53 -0800124 token = getpass.getpass("Input the Jenkins token: ")
Alex Gaynor9ca7f032014-09-26 23:30:31 -0400125 response = session.post(
Alex Gaynora1dff052014-02-19 16:44:06 -0800126 "{0}/build".format(JENKINS_URL),
Paul Kehrerd1d6f0f2014-06-20 14:32:02 -0600127 auth=requests.auth.HTTPBasicAuth(
128 username, token
129 ),
Alex Gaynor86201592014-02-19 14:01:06 -0800130 params={
Alex Gaynor86201592014-02-19 14:01:06 -0800131 "cause": "Building wheels for {0}".format(version)
132 }
133 )
Alex Gaynorc9e4c6a2014-02-19 14:29:37 -0800134 response.raise_for_status()
Alex Gaynor9ca7f032014-09-26 23:30:31 -0400135 wait_for_build_completed(session)
136 paths = download_artifacts(session)
Alex Gaynor6ab3f462014-02-20 09:07:53 -0800137 invoke.run("twine upload {0}".format(" ".join(paths)))