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
|
#!/usr/bin/env python
from __future__ import print_function
import os
import math
from collections import OrderedDict
try:
from github3 import login
from github3 import GitHubError
except ImportError:
print('github3.py required.')
print('pip install https+git://github.com/sigmavirus24/github3.py')
exit(1)
try:
import pypandoc
except ImportError:
print('pypandoc required.')
print('conda install -c https://conda.anaconda.org/janschulz pypandoc')
exit(1)
header = """
# Release Notes
"""
title = """
## {name}
"""
message = """
### {title}
*{date}*
{body}
"""
def explode_newlines(s):
"""Normalize newlines and double them up for MD->RST conversion.
"""
tmp = ''
for line in s.splitlines():
line = line.replace('\r\n', '\n')
line = line + '\n\n'
tmp += line
return tmp
def transform_headings(s):
"""Trust no one.
Header levels less than or equal to 3 are up-converted.
1 = 4
2 = 4
3 = 5
4 = 6
5 = 6
6 = 6
Oh well, sucks right?
"""
delim = '#'
headings = OrderedDict(
h6=delim * 6,
h5=delim * 5,
h4=delim * 4,
h3=delim * 3,
h2=delim * 2,
h1=delim * 1
)
output = ''
for line in s.splitlines():
length = len(line)
stop = len(headings.keys()) + 1
if length < stop:
stop = length
block = line[:stop]
depth = 0
for i, ch in enumerate(block, 1):
if ch != '#': break
depth = i
if depth == 0 or depth > 3:
output += explode_newlines(line)
continue
#print('length={:<10d} stop={:<10d} depth={:<10d} block={:>20s}'.format(length, stop, depth, block))
current = headings['h'+str(depth)]
try:
required = headings['h'+str(math.ceil(depth + depth / depth + 1))]
except KeyError:
required = headings['h'+str(len(headings) - 1)]
if depth > 0 and depth <= 3:
print("Heading levels 1, 2, and 3 are allocated by this script...")
print("Offending line: '{0}'".format(line))
print("Automatically converted to: '{0}'".format(required))
print("(FIX YOUR RELEASE NOTES)\n\n")
#print('Transformed "{0}" -> "{1}"'.format(current, required))
line = line.replace(current, required)
output += explode_newlines(line)
return output
def read_token(filename):
"""For the sake of security, paste your github auth token in a file and reference it using this function
Expected file format (one-liner):
GITHUB_USER_AUTH_TOKEN_HERE
"""
with open(filename, 'r') as f:
# Obtain the first line in the token file
token = f.readline().rstrip()
if not token:
return None
for i, ch in enumerate(token):
if not ch.isalnum():
raise ValueError('Invalid token: Non-alphanumeric character detected (Value: "{0}", ASCII: {1}, Index: {2}) '.format(ch, ord(ch), i))
return token
def pull_release_notes(tmpfile, outfile):
with open(tmpfile, 'w+') as mdout:
# Write header (main title)
print(header, file=mdout)
# Sort repositories inline alphabetically, because otherwise its seemingly random order
for repository in sorted(list(org.repositories()), key=lambda k: k.as_dict()['name']):
repo = repository.as_dict()
repo_name = repo['name'].upper()
# Ignore repositories without release notes
releases = list(repository.releases())
if not releases:
continue
# Write repository title
print(title.format(name=repo_name), file=mdout)
for note in repository.releases():
# RST is EXTREMELY particular about newlines.
body = explode_newlines(note.body)
body = transform_headings(note.body)
# Write release notes structure
print(message.format(title=note.name,
date=note.published_at,
body=body), file=mdout)
if __name__ == '__main__':
owner = 'spacetelescope'
tmpfile = 'release_notes.md'
outfile = 'release_notes.rst'
gh = login(token=read_token('TOKEN'))
org = gh.organization(owner)
try:
pull_release_notes(tmpfile, outfile)
except GitHubError as e:
print(e)
exit(1)
if os.path.exists(outfile):
os.unlink(outfile)
if os.path.exists(tmpfile):
pypandoc.convert(source=tmpfile,
to='rst',
outputfile=outfile)
os.unlink(tmpfile)
|