-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-deployment-report.py
executable file
·352 lines (323 loc) · 12.1 KB
/
generate-deployment-report.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#!/usr/bin/python
import bzrlib.branch
import bzrlib.errors
from twisted.web.template import CDATA, flattenString, tags
import os
def flatten(t):
r =[]
def _(result):
r.append(result)
flattenString(None, t).addCallback(_)
return r[0]
class Component(object):
def __init__(self, name):
self.name = name
self.last_release = None
self.tip_revno = None
self.tip_revid = None
self.unreleased_revisions = None
def load_manifest(fname):
d = {}
from_line = None
for line in open(fname):
line = line.strip()
if line.startswith('# from '):
from_line = line[len('# from '):-1]
if from_line.startswith('EGG'):
from_line = None
if line.startswith('#'):
continue
package, version = line.split('==')
d[package.lower().strip()] = (version.strip(), from_line)
from_line = None
return d
def create_components_from_branches(branches=None):
if branches is None:
branches = os.listdir('/srv/lava/branches')
components = {}
for branch_name in branches:
component = Component(branch_name)
branch = bzrlib.branch.Branch.open(
os.path.join('/srv/lava/branches', branch_name))
branch.lock_read()
try:
branch_tags = branch.tags.get_tag_dict()
revno2release = {}
release2revno = {}
for tag, revid in branch_tags.iteritems():
if tag.startswith('release-'):
try:
revno = branch.revision_id_to_dotted_revno(revid)
except bzrlib.errors.NoSuchRevision:
pass
else:
if len(revno) > 1:
continue
revno2release[revno[0]] = tag[len('release-'):]
release2revno[tag[len('release-'):]] = revno[0]
if revno2release:
component.released_revno = max(revno2release)
component.last_release = revno2release[max(revno2release)]
else:
component.released_revno = None
component.release2revno = release2revno
component.revno2release = revno2release
revno, revid = branch.last_revision_info()
component.tip_revno = revno
component.tip_revid = revid
mainline_revs = []
unreleased_revs = []
while True:
rev = branch.repository.get_revision(revid)
if rev.message.strip() != 'post release bump':
if component.released_revno and revno > component.released_revno:
unreleased_revs.append((rev, revno))
mainline_revs.append((rev, revno))
if not rev.parent_ids:
break
revid = rev.parent_ids[0]
revno -= 1
component.unreleased_revisions = unreleased_revs
component.mainline_revs = mainline_revs
finally:
branch.unlock()
components[branch_name] = component
return components
def load_instances():
instances = {}
for instance_name in os.listdir(LAVA_INSTANCES):
manifest_path = os.path.join(
LAVA_INSTANCES, instance_name, 'code/current/manifest.txt')
if not os.path.exists(manifest_path):
continue
instances[instance_name] = load_manifest(manifest_path)
return instances
css = '''
body {
font-family: "Ubuntu", "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
}
td.version {
text-align: center;
}
.highlight {
font-weight: bold;
}
.clickable {
cursor: pointer;
}
.hidden {
display: none;
}
table.main {
border-collapse: collapse;
width: 80ex;
}
table.inner {
width: 100%;
}
table.inner > tbody > tr > td {
width: 4ex;
}
table.inner > tbody > tr > td+td {
width: auto;
}
table.main > thead > tr > th {
padding: 10px 8px;
border: 1px solid #ccc;
border-bottom: 2px solid black;
}
table.main > tbody > tr > td {
padding: 6px 8px;
border: 1px solid #ccc;
}
'''
js = '''
$(document).ready(function () {
$("td.clickable").click(
function (e) {
e.preventDefault();
$("#show-" + $(this).attr("id")).toggle();
});
});
'''
DOCTYPE = '''\
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
'''
def format_revlist(revlist, name=None):
table = tags.table(class_='inner')
if name:
table(tags.thead(tags.tr(tags.th(name, colspan="2"))))
revs = tags.tbody()
table(revs)
for rev, revno in revlist:
r = tags.tr()
r(tags.td(str(revno)))
r(tags.td(rev.message.splitlines()[0][:100],
style="text-align: left"))
revs(r)
return table
_id = 0
def get_id():
global _id
_id += 1
return 'id' + str(_id)
def make_html(components, instances):
table = tags.table(class_='main')
heading_row = tags.tr()
for heading in 'component', 'tip revno', 'unreleased revisions', 'latest release':
heading_row(tags.th(heading))
for instance_name in sorted(instances):
heading_row(tags.th(instance_name, class_="instance-name"))
table(tags.thead(heading_row))
tbody = tags.tbody()
for name, component in sorted(components.items()):
row = tags.tr(class_="component")
revs_between_ids = {}
extra_rows = []
def td(*args, **kwargs):
row(tags.td(*args, **kwargs))
td(name)
td(str(component.tip_revno), class_='version')
unreleased_count = len(component.unreleased_revisions)
if unreleased_count:
id_ = get_id()
td(
tags.a(str(unreleased_count), href='#', class_='highlight'),
class_='version clickable', id=id_)
sub_name = 'revs between %s (r%s) and tip (r%s)' % (
component.last_release, component.released_revno,
component.tip_revno)
extra_rows.append(
tags.tr(
tags.td(
format_revlist(component.unreleased_revisions, name=sub_name),
colspan=str(4 + len(instances))),
class_='hidden',
id="show-" + id_))
elif not component.last_release:
td(u'\N{EM DASH}', class_='version')
else:
td(str(unreleased_count), class_='version')
if component.last_release:
td(component.last_release, class_='version')
else:
td(u'???', class_='version')
for instance_name, instance in sorted(instances.items()):
ver, location = instance.get(name, (None, None))
if ver is None:
td(u'\N{EM DASH}', class_='version')
elif ver == component.last_release:
td(ver, class_='version')
elif ver in component.release2revno:
revno_low = component.release2revno[ver]
sub_name = 'revs between %s (r%s) and %s (r%s)' % (
ver, revno_low,
component.last_release, component.released_revno)
revlist = []
for rev, revno in component.mainline_revs:
if revno_low < revno < component.released_revno:
revlist.append((rev, revno))
if revlist:
id_ = get_id()
revs_between_ids[revno_low] = id_
extra_rows.append(
tags.tr(
tags.td(
format_revlist(revlist, name=sub_name),
colspan=str(4 + len(instances))),
class_='hidden branch-diff',
id="show-" + id_))
td(
tags.a(ver, href='#', class_='highlight'),
class_='version clickable', id=id_)
else:
td(tags.span(ver, class_='highlight'), class_='version')
elif location:
try:
branch = bzrlib.branch.Branch.open(location)
except bzrlib.errors.NoSuchBranch:
td(tags.span(ver, class_='highlight'), class_='version')
else:
branch.lock_read()
try:
# This utterly half-assed version of bzr missing
# doesn't take merges into account!
revno, revid = branch.last_revision_info()
ver = ver.split('dev')[0] + 'dev' + str(revno)
mainline_revids = dict(
(rev.revision_id, revno)
for rev, revno in component.mainline_revs)
in_branch_revs = []
while revid not in mainline_revids:
rev = branch.repository.get_revision(revid)
if rev.message != 'post release bump':
in_branch_revs.append((rev, revno))
revno -= 1
if not rev.parent_ids:
break
revid = rev.parent_ids[0]
tables = []
if in_branch_revs:
tables.append(
format_revlist(
in_branch_revs,
'in branch (with nick %s) but not tip' % branch.nick))
in_trunk_revs = []
lca_revno = revno
for rev, revno in component.mainline_revs:
if revno > lca_revno:
in_trunk_revs.append((rev, revno))
if in_trunk_revs:
tables.append(
format_revlist(
in_trunk_revs,
'in tip but not branch'))
if tables:
id_ = get_id()
td(
tags.a(ver, href='#', class_='highlight'),
class_='version clickable', id=id_)
extra_rows.append(
tags.tr(
tags.td(
tables,
colspan=str(4 + len(instances))),
class_='hidden branch-diff',
id="show-" + id_))
else:
if branch.last_revision() == component.tip_revno:
td(ver, class_='highlight version')
else:
td(ver, class_='version')
finally:
branch.unlock()
else:
td(tags.span(ver, class_='highlight'), class_='version')
tbody(row, *extra_rows)
table(tbody)
html = tags.html(
tags.head(
tags.title("Deployment report"),
tags.script(
src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js',
type='text/javascript'),
tags.script(
src='https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js',
type='text/javascript'),
tags.script(CDATA(js), type='text/javascript'),
tags.style(CDATA(css), type="text/css"),
),
tags.body(
tags.h1("Deployment report"),
table,
),
)
html(xmlns="http://www.w3.org/1999/xhtml")
return DOCTYPE + flatten(html)
LAVA_INSTANCES = '/srv/lava/instances'
if __name__ == '__main__':
components = create_components_from_branches()
instances = load_instances()
print make_html(components, instances)