blob: d7c18d1050fdd9ae2a0303e42da90f3be60d20eb [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 Gaynor72e37c42017-07-24 08:28:25 -04008import glob
Paul Kehrer8f1a2d52015-11-08 10:02:24 +09009import io
Alex Gaynor7a2b3582014-02-20 07:48:46 -080010import os
Alex Gaynorc7dd9de2017-05-20 14:37:40 -070011import subprocess
Alex Gaynora1dff052014-02-19 16:44:06 -080012import time
Alex Gaynor86201592014-02-19 14:01:06 -080013
Alex Gaynorc7dd9de2017-05-20 14:37:40 -070014import click
Paul Kehrere0e90a82015-11-08 09:38:24 +090015
Alex Gaynorc7dd9de2017-05-20 14:37:40 -070016from clint.textui.progress import Bar as ProgressBar
Alex Gaynor6bc3af72014-01-06 12:04:53 -080017
Alex Gaynor86201592014-02-19 14:01:06 -080018import requests
19
20
Paul Kehrer66e66952017-05-29 20:48:37 -050021JENKINS_URL = (
22 "https://ci.cryptography.io/job/cryptography-support-jobs/"
23 "job/wheel-builder"
24)
Alex Gaynora1dff052014-02-19 16:44:06 -080025
26
Alex Gaynorc7dd9de2017-05-20 14:37:40 -070027def run(*args, **kwargs):
Alex Gaynorc1f8e462017-08-04 13:45:11 -040028 print("[running] {0}".format(list(args)))
29 subprocess.check_call(list(args), **kwargs)
Alex Gaynorc7dd9de2017-05-20 14:37:40 -070030
31
Alex Gaynor9ca7f032014-09-26 23:30:31 -040032def wait_for_build_completed(session):
Paul Kehrerd755e662014-12-24 08:07:56 -060033 # Wait 20 seconds before actually checking if the build is complete, to
Alex Gaynord6b05982014-12-16 09:32:59 -080034 # ensure that it had time to really start.
Paul Kehrerd755e662014-12-24 08:07:56 -060035 time.sleep(20)
Alex Gaynora1dff052014-02-19 16:44:06 -080036 while True:
Alex Gaynor9ca7f032014-09-26 23:30:31 -040037 response = session.get(
Alex Gaynora1dff052014-02-19 16:44:06 -080038 "{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 Gaynor7a2b3582014-02-20 07:48:46 -080045 assert response.json()["result"] == "SUCCESS"
Alex Gaynora1dff052014-02-19 16:44:06 -080046 break
47 time.sleep(0.1)
Alex Gaynor6bc3af72014-01-06 12:04:53 -080048
Alex Gaynorc9e4c6a2014-02-19 14:29:37 -080049
Alex Gaynor9ca7f032014-09-26 23:30:31 -040050def download_artifacts(session):
51 response = session.get(
Alex Gaynor7a2b3582014-02-20 07:48:46 -080052 "{0}/lastBuild/api/json/".format(JENKINS_URL),
53 headers={
54 "Accept": "application/json"
55 }
56 )
57 response.raise_for_status()
Paul Kehrer1d08e512017-05-29 22:23:56 -050058 json_response = response.json()
59 assert not json_response["building"]
60 assert json_response["result"] == "SUCCESS"
Alex Gaynor6ab3f462014-02-20 09:07:53 -080061
62 paths = []
63
Paul Kehrer1d08e512017-05-29 22:23:56 -050064 for artifact in json_response["artifacts"]:
Alex Gaynor9ca7f032014-09-26 23:30:31 -040065 response = session.get(
Paul Kehrer1d08e512017-05-29 22:23:56 -050066 "{0}artifact/{1}".format(
67 json_response["url"], artifact["relativePath"]
68 ), stream=True
Alex Gaynor7a2b3582014-02-20 07:48:46 -080069 )
Paul Kehrer1d08e512017-05-29 22:23:56 -050070 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 Gaynor6ab3f462014-02-20 09:07:53 -080090 return paths
Alex Gaynor7a2b3582014-02-20 07:48:46 -080091
92
Alex Gaynorc7dd9de2017-05-20 14:37:40 -070093@click.command()
94@click.argument("version")
Alex Gaynor6bc3af72014-01-06 12:04:53 -080095def release(version):
96 """
97 ``version`` should be a string like '0.4' or '1.0'.
98 """
Alex Gaynorc7dd9de2017-05-20 14:37:40 -070099 run("git", "tag", "-s", version, "-m", "{0} release".format(version))
100 run("git", "push", "--tags")
Alex Gaynor6bc3af72014-01-06 12:04:53 -0800101
Alex Gaynorc7dd9de2017-05-20 14:37:40 -0700102 run("python", "setup.py", "sdist")
103 run("python", "setup.py", "sdist", "bdist_wheel", cwd="vectors/")
Alex Stapletona39a3192014-03-14 20:03:12 +0000104
Alex Gaynor72e37c42017-07-24 08:28:25 -0400105 packages = (
106 glob.glob("dist/cryptography-{0}*".format(version)) +
107 glob.glob("vectors/dist/cryptography_vectors-{0}*".format(version))
Alex Stapletona39a3192014-03-14 20:03:12 +0000108 )
Alex Gaynor72e37c42017-07-24 08:28:25 -0400109 run("twine", "upload", "-s", *packages)
Alex Gaynor86201592014-02-19 14:01:06 -0800110
Alex Gaynor9ca7f032014-09-26 23:30:31 -0400111 session = requests.Session()
112
Alex Gaynor6629fa52014-02-20 16:10:53 -0800113 token = getpass.getpass("Input the Jenkins token: ")
Paul Kehrer66e66952017-05-29 20:48:37 -0500114 response = session.get(
Paul Kehrer96ffd912017-07-24 02:44:19 +0300115 "{0}/buildWithParameters".format(JENKINS_URL),
Alex Gaynor86201592014-02-19 14:01:06 -0800116 params={
Paul Kehrer66e66952017-05-29 20:48:37 -0500117 "token": token,
Paul Kehrer01458732017-06-26 18:56:45 -1000118 "BUILD_VERSION": version,
Alex Gaynor86201592014-02-19 14:01:06 -0800119 "cause": "Building wheels for {0}".format(version)
120 }
121 )
Alex Gaynorc9e4c6a2014-02-19 14:29:37 -0800122 response.raise_for_status()
Alex Gaynor9ca7f032014-09-26 23:30:31 -0400123 wait_for_build_completed(session)
124 paths = download_artifacts(session)
Alex Gaynor49733502017-07-17 14:59:17 -0400125 run("twine", "upload", *paths)
Alex Gaynorc7dd9de2017-05-20 14:37:40 -0700126
127
128if __name__ == "__main__":
129 release()