forked from AcademySoftwareFoundation/rez
-
Notifications
You must be signed in to change notification settings - Fork 0
/
release-rez.py
181 lines (132 loc) · 4.9 KB
/
release-rez.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
"""
Release a new version of rez.
Read RELEASE.md before using this utility.
"""
import argparse
import os
from getpass import getpass
from pipes import quote
import subprocess
import sys
try:
import requests
except ImportError:
sys.stderr.write("Requires python requests module.\n")
sys.exit(1)
source_path = os.path.dirname(os.path.realpath(__file__))
src_path = os.path.join(source_path, "src")
sys.path.insert(0, src_path)
from rez.utils._version import _rez_version
github_username = os.getenv("GITHUB_USERNAME")
github_password = os.getenv("GITHUB_PASSWORD")
verbose = False
def run_command(*nargs):
if verbose:
print("RUNNING: %s" % ' '.join(map(quote, nargs)))
proc = subprocess.Popen(nargs, stdout=subprocess.PIPE)
out, _ = proc.communicate()
if proc.returncode:
sys.stderr.write("Aborting, failed with exit code %d.\n" % proc.returncode)
sys.exit(proc.returncode)
return out.strip()
def get_github_url(endpoint=None):
global github_username, github_password
github_url = "https://{username}:{password}@api.github.com/repos/nerdvegas/rez/"
if github_username is None or github_password is None:
github_username = raw_input("Github username: ").strip()
github_password = getpass("Github password: ").strip()
url = github_url.format(username=github_username, password=github_password)
if endpoint:
url += endpoint
return url
def parse_topmost_changelog():
result = {}
body_lines = []
with open("CHANGELOG.md") as f:
for line in f.readlines():
parts = line.split()
# eg: ## 2.38.0 (2019-07-20)
if parts and parts[0] == "##":
if result.get("version"):
result["body"] = ''.join(body_lines).strip()
return result
result["version"] = parts[1]
result["name"] = result["version"] + ' '.join(parts[2:])
elif result.get("version"):
# GitHub seems to treat separate lines in the md as line breaks,
# rather than keeping them in the same paragraph as a typical
# md renderer would. So patch that up here.
#
br_chars = ('*', '-', '#', '[')
if body_lines and \
body_lines[-1].strip() and \
body_lines[-1][0] not in br_chars and \
line.strip() and \
line[0] not in br_chars:
# append to previous line
body_lines[-1] = body_lines[-1].rstrip() + ' ' + line
else:
body_lines.append(line)
# should never get here
assert False
def check_on_master():
branch = run_command("git", "branch", "--contains").split()[-1]
if branch != "master":
sys.stderr.write("Must be run from master.\n")
sys.exit(1)
def push_codebase():
run_command("git", "push")
def create_and_push_tag():
run_command("git", "tag", _rez_version)
run_command("git", "push", "origin", _rez_version)
def create_github_release_notes():
# check if latest release notes already match current version
response = requests.get(get_github_url("releases/latest"))
response.raise_for_status()
latest_release_tag = response.json()["tag_name"]
if latest_release_tag == _rez_version:
sys.stderr.write("Release for %s already exists.\n" % _rez_version)
sys.exit(1)
# parse latest release out of changelog
changelog = parse_topmost_changelog()
tag_name = changelog["version"]
if tag_name != _rez_version:
sys.stderr.write(
"Latest entry in changelog (%s) doesn't match current version (%s).\n"
% (tag_name, _rez_version)
)
sys.exit(1)
data = dict(
tag_name=_rez_version,
name=changelog["name"],
body=changelog["body"]
)
# create the release on github
response = requests.post(
get_github_url("releases"),
json=data,
headers={"Content-Type": "application/json"}
)
response.raise_for_status()
url = "https://github.com/nerdvegas/rez/releases/tag/" + _rez_version
print "Created release notes: " + url
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"-s", "--step", choices=("push", "tag", "release_notes"),
help="Just run one step of the release process")
parser.add_argument(
"-v", "--verbose", action="store_true",
help="verbose mode")
opts = parser.parse_args()
verbose = opts.verbose
print("Releasing rez-%s..." % _rez_version)
def doit(step):
return (opts.step is None) or (step == opts.step)
check_on_master()
if doit("push"):
push_codebase()
if doit("tag"):
create_and_push_tag()
if doit("release_notes"):
create_github_release_notes()