forked from AcademySoftwareFoundation/rez
-
Notifications
You must be signed in to change notification settings - Fork 0
/
release_util.py
144 lines (104 loc) · 3.65 KB
/
release_util.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
import argparse
from getpass import getpass
import pipes
import pprint
import subprocess
import sys
import requests
github_url = "https://{username}:{password}@api.github.com/repos/nerdvegas/rez/"
username = None
password = None
default_headers = {
"Content-Type": "application/json",
}
def get_url(endpoint=None):
global username, password
if username is None:
username = raw_input("Github username: ").strip()
password = getpass("Github password: ").strip()
url = github_url.format(username=username, password=password)
if endpoint:
url += endpoint
return url
def run_proc(*nargs):
cmd = ' '.join(map(pipes.quote, nargs))
print("Running: %s" % cmd)
proc = subprocess.Popen(nargs, stdout=subprocess.PIPE)
out, _ = proc.communicate()
if proc.returncode:
sys.exit(proc.returncode)
return out.strip()
def parse_topmost_changelog():
result = {}
body_lines = []
with open("CHANGELOG.md") as f:
for line in f.readlines():
parts = line.split()
# eg: ## [2.27.0](https://github.com/nerdvegas/rez/tree/2.27.0) (2019-01-24)
if parts and parts[0] == "##":
if result.get("version"):
result["body"] = ''.join(body_lines).strip()
return result
result["version"] = parts[1].split(']')[0].split('[')[-1]
result["name"] = result["version"] + ' ' + parts[-1]
elif result.get("version"):
body_lines.append(line)
def create_changelog_entry():
"""
Requires github_changelog_generator util:
https://github.com/github-changelog-generator/github-changelog-generator
"""
# get most recent tag - we only want to generate the new changelog up til there
changelog = parse_topmost_changelog()
tag_name = changelog["version"]
run_proc(
"github_changelog_generator",
"--since-tag=" + tag_name,
"--output=LATEST_CHANGELOG.md"
)
def create_release_notes():
# check that we're on master
branch = run_proc("git", "branch", "--contains").split()[-1]
if branch != "master":
print >> sys.stderr, "Must be run from master"
sys.exit(1)
# get current tag
latest_tag = run_proc("git", "describe", "--tags", "--abbrev=0")
# see if latest release already matches tag
response = requests.get(get_url("releases/latest"))
response.raise_for_status()
latest_release_tag = response.json()["tag_name"]
if latest_release_tag == latest_tag:
print >> sys.stderr, "Release for %s already exists" % latest_tag
sys.exit(1)
# parse latest release out of changelog
changelog = parse_topmost_changelog()
tag_name = changelog["version"]
if tag_name != latest_tag:
print >> sys.stderr, (
"Latest entry in changelog (%s) doesn't match latest tag (%s)"
% (tag_name, latest_tag)
)
sys.exit(1)
data = dict(
tag_name=tag_name,
name=changelog["name"],
body=changelog["body"]
)
print(pprint.pformat(data))
# create the release on github
response = requests.post(
get_url("releases"),
json=data,
headers=default_headers
)
response.raise_for_status()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
subparser = subparsers.add_parser("create-release-notes")
subparser.set_defaults(func=create_release_notes)
subparser = subparsers.add_parser("create-changelog-entry")
subparser.set_defaults(func=create_changelog_entry)
opts = parser.parse_args()
opts.func(opts)