-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbbreport.py
executable file
·1445 lines (1204 loc) · 46.6 KB
/
bbreport.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import with_statement
import collections
import fnmatch
import gzip
import optparse
import os
import re
import shutil
import socket
import sqlite3
import sys
from contextlib import closing
from datetime import datetime
try:
import simplejson as json
except ImportError:
import json
try:
# Python 2.x
import urllib2
import urllib
import xmlrpclib
from ConfigParser import ConfigParser
except ImportError:
# Python 3.x
import urllib.request as urllib2
import urllib.parse as urllib
import xmlrpc.client as xmlrpclib
from configparser import ConfigParser
try:
# ANSI color support on Windows
import colorama
colorama.init()
except ImportError:
pass
__version__ = '0.1dev'
# Default number of builds
NUMBUILDS = 4
# The XMLRPC methods may give an error with larger requests
XMLRPC_LIMIT = 5
CACHE_BUILDS = 50
DEFAULT_BRANCHES = 'all'
DEFAULT_FAILURES = ''
DEFAULT_TIMEOUT = 4
MSG_MAXLENGTH = 60
MAX_FAILURES = 30
DEFAULT_OUTPUT = {}
BUILD_ID = 'revision'
ANSI_COLOR = ['black', 'red', 'green', 'yellow',
'blue', 'magenta', 'cyan', 'white']
baseurl = 'http://www.python.org/dev/buildbot/'
issuesurl = 'http://wiki.bbreport.googlecode.com/hg/KnownIssues.wiki'
# Configuration
basefile = os.path.splitext(__file__)[0]
conffile = basefile + '.conf'
# Database file
dbfile = basefile + '.cache'
# Generated JSON file (option --mode json)
jsonfile = basefile + '.json'
# Database connection
conn = None
# Count removed builds
removed_builds = 0
# Common statuses for Builds and Builders
S_BUILDING = 'building'
S_SUCCESS = 'success'
S_FAILURE = 'failure'
S_EXCEPTION = 'exception' # Build only (mapped to S_FAILURE)
S_UNSTABLE = 'unstable' # Builder only (intermittent failures)
S_OFFLINE = 'offline' # Builder only
S_MISSING = 'missing' # Builder only
BUILDER_STATUSES = (S_BUILDING, S_SUCCESS, S_UNSTABLE, S_FAILURE, S_OFFLINE)
# bytes/unicode helpers
b = lambda s: s.encode('utf-8')
u = lambda s: s.decode('utf-8')
# Regular expressions
RE_BUILD = re.compile(b('Build #(\d+)</h1>\r?\n?'
'<h2>Results:</h2>\r?\n?'
'<span class="([^"]+)">([^<]+)</span>'))
RE_BUILD_REVISION = re.compile(b('<li>Revision: (\d+)</li>'))
RE_FAILED = re.compile(b('(\d+) tests? failed:((?:\r?\n? +([^\r\n]+))+)'))
RE_TIMEOUT = re.compile(b('command timed out: (\d+) ([^,]+)'))
RE_STOP = re.compile(b('(process killed by .+)'))
RE_BBTEST = re.compile(b('make: \*\*\* \[buildbottest\] (.+)'))
RE_TEST = re.compile(b('(?:\[[^]]*\] )?(test_[^ <]+)(?:</span>|$)'))
# Buildbot errors
OSERRORS = (b('filesystem is full'),
b('No space left on device'),
b('Cannot allocate memory'))
# HTML pollution in the stdio log
HTMLNOISE = b('</span><span class="stdout">')
# Format output
SYMBOL = {S_SUCCESS: '_', S_FAILURE: '#', S_EXCEPTION: '?',
S_UNSTABLE: '?', S_BUILDING: '*', S_OFFLINE: '*'}
COLOR = {S_SUCCESS: 'green', S_FAILURE: 'red', S_EXCEPTION: 'yellow',
S_UNSTABLE: 'yellow', S_BUILDING: 'blue', S_OFFLINE: 'cyan'}
_escape_sequence = {}
# ~~ Compatibility with Python 2.5 ~~
if not hasattr(sqlite3.Connection, 'iterdump'):
try:
from pysqlite2 import dbapi2 as sqlite3
sqlite3.Connection.iterdump
except (ImportError, AttributeError):
sys.exit("*** Requires pysqlite 2.5.0 or Python >= 2.6")
try:
from collections import MutableMapping
except ImportError:
from UserDict import DictMixin as MutableMapping
try:
next
except NameError:
def next(iterator, default=None):
for item in iterator:
return item
return default
try:
out = getattr(__builtins__, 'print')
except AttributeError:
def out(*args, **kw):
sys.stdout.write(' '.join(str(arg) for arg in args) +
kw.get('end', '\n'))
# ~~ Helpers ~~
def exc():
return str(sys.exc_info()[1])
def prepare_output():
# Read the configuration and set the ANSI sequences to colorize the output
global cformat
default_fg = DEFAULT_OUTPUT.get('foreground', '').lower()
default_bg = DEFAULT_OUTPUT.get('background', '').lower()
_base = '\x1b[1;' if ('bold' in default_fg) else '\x1b['
fg_offset = 90 if ('bright' in default_fg) else 30
bg_offset = 100 if ('bright' in default_bg) else 40
fg_color = next((fg_offset + idx for (idx, color) in enumerate(ANSI_COLOR)
if color in default_fg), 39)
bg_color = next((bg_offset + idx for (idx, color) in enumerate(ANSI_COLOR)
if color in default_bg), 49)
for status, color in COLOR.items():
_escape_sequence[status] = ('%s%s;%sm%%s\x1b[%sm' %
(_base, fg_offset + ANSI_COLOR.index(color), bg_color, fg_color))
# Fallback to normal output, without color
with_color = DEFAULT_OUTPUT.get('color')
if (with_color is None and not sys.stdout.isatty() or
with_color in ('false', '0', 'off', 'no')):
cformat = _cformat_plain
def _cformat_plain(text, status, sep=' '):
# Straight output: statuses are represented with symbols
return sep.join((SYMBOL[status], str(text)))
def _cformat_color(text, status, sep=None):
# Colored output
return _escape_sequence[status] % text
def reset_terminal():
if cformat == _cformat_color:
# Reset terminal colors
out('\x1b[39;49;00m\r', end='')
cformat = _cformat_color
def trunc(tests, length):
# Join test names and truncate
text = ' ' + ' '.join(tests)
length -= len(text)
if length < 0:
text = text[:length - 3] + '...'
return text, length
def urlread(url):
# Return an empty string on IOError
try:
resource = urllib2.urlopen(url)
return resource.read()
except IOError:
return b('')
def parse_builder_name(name):
try:
# the branch name should always be the last part of the name
host, branch = name.rsplit(None, 1)
except ValueError:
host, branch = name, 'unknown'
if name.endswith('.dmg'):
# FIXME: fix buildbot names? :-)
branch = name[:-4]
return host, branch
# ~~ Builder and Build classes ~~
class Builder(object):
"""Represent a builder."""
saved = status = None
lastbuild = 0
def __init__(self, name):
self.name = name
self.host, self.branch = parse_builder_name(name)
self.url = baseurl + 'builders/' + urllib.quote(name)
self.builds = {}
self._load_builder()
if not self.saved:
self.save()
@classmethod
def query_all(cls):
"""Return the builders from the database, as a dict."""
if conn is None:
return {}
cur = conn.execute('SELECT builder FROM builders WHERE status '
'IS NULL OR status <> ?', (S_MISSING,))
return dict((name, cls(name)) for (name,) in cur.fetchall())
def get_builds(self, n, *builds):
"""Yield the last n builds.
Optionally, build tuples can be passed, for builds retrieved by XMLRPC.
It helps building the list faster, with less server queries.
"""
if builds:
# The list is not empty. Maybe the first build is missing.
if len(builds) < n:
last = Build(self.name, -1)
if last.num != builds[-1][1]:
self.add(last)
yield last
n -= 1
for build_info in reversed(builds):
build = Build(*build_info)
self.add(build)
yield build
if build.num == 0:
return
n -= len(builds)
offset = build.num - 1
else:
# The list is empty. Retrieve the builds by number (-1, -2, ...).
offset = -1
for i in range(n):
num = offset - i
build = Build(self.name, num)
if offset < 0 < build.num:
# use the real build numbers
offset = build.num + i
self.add(build)
yield build
# Reach the build #0? stop
if num == 0:
return
def get_saved_builds(self, n):
"""Retrieve the last n builds from the local cache."""
if conn is None:
return []
cur = conn.execute('SELECT build FROM builds WHERE builder = ? '
'ORDER BY build DESC LIMIT ?', (self.name, n))
builds = [Build(self.name, num) for (num,) in cur.fetchall()]
self.add(*builds)
return builds
def __eq__(self, other):
return str(self) == str(other)
def __str__(self):
return self.name
def _load_builder(self):
"""Populate the builder attributes from the local cache."""
if conn is None:
return
row = conn.execute('SELECT lastbuild, status FROM builders WHERE '
'builder = ? ', (self.name,)).fetchone()
if row is not None:
self.saved = True
(self.lastbuild, self.status) = row
if self.status == S_MISSING:
# Reset the builder status
self.set_status(None)
def add(self, *builds):
"""Add a build to this builder, and adjust lastbuild."""
last = self.lastbuild
for build in builds:
self.builds[build.num] = build
last = max(last, build.num)
if last > self.lastbuild:
self.lastbuild = last
self.remove_oldest()
self.save()
def set_status(self, status):
"""Set the builder status."""
self.status = status
self.save()
def remove_oldest(self):
global removed_builds
if conn is None:
return
if CACHE_BUILDS <= 0:
return
# Remove obsolete data
minbuild = self.lastbuild - CACHE_BUILDS
cur = conn.execute('DELETE FROM builds WHERE builder = ? AND '
'build < ?', (self.name, minbuild))
if cur.rowcount:
removed_builds += cur.rowcount
def save(self):
"""Insert or update the builder in the local cache."""
if conn is None:
return
if self.saved:
conn.execute('UPDATE builders SET lastbuild = ?, status = ? '
'WHERE builder = ?',
(self.lastbuild, self.status, self.name))
else:
conn.execute('INSERT INTO builders(builder, host, branch, '
'lastbuild, status) VALUES (?, ?, ?, ?, ?)',
(self.name, self.host, self.branch,
self.lastbuild, self.status))
self.saved = True
return True
class Build(object):
"""Represent a single build of a builder.
Build.result should be one of (S_SUCCESS, S_FAILURE, S_EXCEPTION).
If the result is not available, it defaults to S_BUILDING.
"""
_message = saved = result = None
revision = 0
def __init__(self, name, buildnum, *args):
self.builder = name
self.num = buildnum
self._url = '%s/builders/%s/builds/' % (baseurl, urllib.quote(name))
self._get_build(args)
self.failed_tests = []
if self.result not in (S_SUCCESS, S_BUILDING):
self._get_failures()
self.save()
@property
def id(self):
"""The build identifier."""
return getattr(self, BUILD_ID)
@property
def url(self):
"""The build URL."""
return self._url + str(self.num)
def _get_build(self, args):
# Load the build data from the cache, or online
if self.num is not None:
# Query the database
self.result = self._load_build()
if self.result:
return
if args:
# Use the XMLRPC response
assert len(args) == 7
revision, result = args[3:5]
if result in (S_EXCEPTION, S_FAILURE):
# Store the failure details
self._message = ' '.join(args[5])
if revision:
self.revision = int(revision)
self.result = result
if not self.result:
# Fallback to the web page
self.result = self._parse_build()
if self._message and self._message.startswith('failed svn'):
self.result = S_EXCEPTION
def _get_failures(self):
# Load the failures from the cache, or parse the stdio log
if self.saved and conn is not None:
cur = conn.execute('SELECT failed FROM failures WHERE '
'builder = ? AND build = ?',
(self.builder, self.num))
self.failed_tests = [test for (test,) in cur.fetchall()]
else:
if self._message is None or 'test' in self._message:
# Parse stdio on demand
self._parse_stdio()
def save(self):
"""Insert the build in the local cache."""
if conn is None or self.saved:
return
if self.result not in (S_SUCCESS, S_FAILURE, S_EXCEPTION):
return False
conn.execute('INSERT INTO builds(builder, build, revision, result, '
'message) VALUES (?, ?, ?, ?, ?)', (self.builder,
self.num, self.revision, self.result, self._message))
if self.failed_tests:
rows = ((self.builder, self.num, test)
for test in self.failed_tests)
conn.executemany('INSERT INTO failures(builder, build, failed) '
'VALUES (?, ?, ?)', rows)
self.saved = True
return True
def _load_build(self):
# Load revision, result and message from the local cache
result = None
if conn is not None and self.num >= 0:
row = conn.execute('SELECT revision, result, message FROM builds'
' WHERE builder = ? AND build = ?',
(self.builder, self.num)).fetchone()
if row is not None:
self.saved = True
(self.revision, result, self._message) = row
return result
def _parse_build(self):
# Retrieve num, result, revision and message from the server
build_page = urlread(self.url)
if not build_page:
return S_BUILDING
match = RE_BUILD.search(build_page)
if match:
self.num = int(match.group(1))
result = u(match.group(2))
self._message = u(match.group(3))
else:
result = S_BUILDING
match = RE_BUILD_REVISION.search(build_page)
if match:
self.revision = int(match.group(1))
self._load_build()
return result
def _parse_stdio(self):
# Lookup failures in the stdio log on the server
stdio = urlread(self.url + '/steps/test/logs/stdio')
stdio = stdio.replace(HTMLNOISE, b(''))
# Check if some test failed
fail = RE_FAILED.search(stdio)
if fail:
failed_count = int(fail.group(1))
failed_tests = u(fail.group(2).strip())
self.failed_tests = failed_tests.split()
assert len(self.failed_tests) == failed_count
lines = stdio.splitlines()
# Check if disk full or out of memory
for line in lines:
error = next((e for e in OSERRORS if e in line), None)
if error is None:
continue
self.result = S_EXCEPTION
self._message = u(error.lower())
break
else:
self._message = error = ''
if fail or error:
# If something is found, stop here
return
self._message = 'something crashed'
reversed_lines = reversed(lines)
for line in reversed_lines:
killed = RE_BBTEST.search(line) or RE_STOP.search(line)
if killed:
self._message = u(killed.group(1).strip().lower())
# Check previous line for a possible timeout
line = next(reversed_lines)
timeout = RE_TIMEOUT.search(line)
if timeout:
minutes = int(timeout.group(1)) // 60
# It is a test failure
self.result = S_FAILURE
self._message = 'hung for %d min' % minutes
# Move to previous line
line = next(reversed_lines)
failed = RE_TEST.match(line)
if failed:
# This is the last running test
self.failed_tests = [u(failed.group(1))]
break
else:
# No test failure: probably a buildbot error
self.result = S_EXCEPTION
def get_message(self, length=2048):
"""Return the build result including failed test as a string."""
if self.result in (S_SUCCESS, S_BUILDING):
return cformat(self.result, self.result)
msg = self._message
if self.failed_tests:
failed_tests, known = issues.match(self)
failed_count = len(failed_tests) + len(known)
if self.result == S_EXCEPTION and failed_count > 2:
# disk full or other buildbot error
msg += ' (%s failed)' % failed_count
else:
if not msg:
msg = '%s failed' % failed_count
msg += ':'
length -= len(msg)
if failed_tests:
(text, length) = trunc(failed_tests, length)
msg += cformat(text, S_FAILURE, sep='')
if known and not (failed_tests and length < 0):
msg += trunc(known, length)[0]
return SYMBOL[self.result] + ' ' + msg
# ~~ Issues ~~
class Rule(tuple):
"""Represent a matching rule for an issue."""
def __new__(cls, test='', message='', builder=''):
if not (test or message or builder):
raise TypeError('A Rule needs a test or a message '
'or a builder regex')
return tuple.__new__(cls, (test, message, builder))
def __init__(self, test, message, builder):
# Match the failed test exactly
if test and not test.endswith('$'):
test += '$'
self.test_re = re.compile(test)
self.message_re = re.compile(message)
self.builder_re = re.compile(builder)
def match(self, test, message, builder):
"""Check if the failure attributes match the issue criteria."""
return all((self.test_re.match(test),
self.message_re.match(message),
self.builder_re.match(builder)))
class MatchIssue(object):
"""Represent an issue from the issue tracker."""
def __init__(self, number, *rules):
self.number = number
self.rules = [Rule(*rule) for rule in rules]
self.events = {}
def __str__(self):
lines = []
out = lines.append
for rule in self.rules:
out('%s: %s:%s:%s' % ((self.number,) + rule))
indent = ' ' * (len(self.number) + 2)
for failure, builds in sorted(self.events.items()):
out(indent + ':'.join(failure) + ' ' +
cformat(' '.join(str(b.id) for b in builds), S_UNSTABLE))
return '\n'.join(lines)
def add(self, rule):
rule = Rule(*rule)
if rule not in self.rules:
self.rules.append(rule)
def match(self, build, *event):
"""Check if the failure attributes match any issue criteria."""
rv = any(rule.match(*event) for rule in self.rules)
if rv:
self.events.setdefault(event, []).append(build)
return rv
class Issues(dict, MutableMapping):
"""Ordered dictionary of issues from the issue tracker."""
def __init__(self, *args, **kw):
self.__keys = []
self._preload = []
self.new_events = {}
self.update(*args, **kw)
# By default do not record
self.__record = False
def __setitem__(self, key, value):
if key in self:
self[key].add(value)
else:
self.__keys.append(key)
dict.__setitem__(self, key, MatchIssue(key, value))
if self.__record:
conn.execute('INSERT INTO rules(issue, test, message, builder) '
'VALUES (?, ?, ?, ?)', (key,) + tuple(value))
def __iter__(self):
return iter(self.__keys)
try:
items = MutableMapping.iteritems
except AttributeError:
# Python 3
items = MutableMapping.items
def values(self):
"""Return the issues by number of events descending."""
return sorted(dict.values(self), key=lambda m: -len(m.events))
def clear(self, record=True):
del self.__keys[:]
self.new_events.clear()
dict.clear(self)
if conn is not None:
# Clear all entries before recording
conn.execute('DELETE FROM rules')
self.__record = record
def load(self, offline=False):
"""Populate the issues."""
if not offline:
page = urlread(issuesurl)
if page:
# Reset the table
self.clear()
else:
# If page is empty, use cache
offline = True
if offline:
# Load the cache first
self._load_from_cache()
if self._preload:
# Load local configuration
for issue, rule in self._preload:
self[issue] = rule
del self._preload[:]
if not offline:
# Load online issues
self._load_from_page(u(page))
def _load_from_cache(self):
"""Load the issues from the local cache."""
if conn is None:
return
cur = conn.execute('SELECT issue, test, message, builder FROM rules')
for row in cur.fetchall():
self[row[0]] = row[1:]
# Allow recording only if table is empty
self.__record = not cur.rowcount
def _load_from_page(self, page):
"""Retrieve the issues from the page."""
for line in page.splitlines():
if not line.startswith('||'):
continue
# Split table cells
cells = line.split('||')[1:-1]
if len(cells) < 4:
# Skip incomplete rules
continue
# Strip backquotes
rule = [cell.strip(' \t`') for cell in cells]
# Skip headers (bold formatted)
if rule[0][0] != '*':
self[rule[0]] = rule[1:4]
def match(self, build):
msg = build._message
builder = build.builder
known = []
new = []
new_events = self.new_events
for test in build.failed_tests:
event = (test, msg, builder)
issue = next((number for number, issue in self.items()
if issue.match(build, test, msg, builder)), False)
if issue:
test += '`%s' % issue
known.append(test)
else:
new.append(test)
new_events.setdefault(event, []).append(build)
return new, known
def new_failures(self, verbose=False):
lines = []
out = lines.append
new_failures = self.new_events
if new_failures:
count = len(new_failures)
if verbose or count <= MAX_FAILURES:
out('\n%s new test failure(s):' % count)
for failure, builds in sorted(new_failures.items()):
out(' ' + ':'.join(failure) + ' ' +
cformat(' '.join(str(b.id) for b in builds),
S_FAILURE))
else:
out(' and ' +
cformat('%s new test failures' % count, S_FAILURE))
return '\n'.join(lines)
def __str__(self):
return ('\n'.join(str(issue) for issue in self.values()) + '\n' +
self.new_failures(verbose=True))
# Instanciate a global Issues dictionary
issues = Issues()
# ~~ Output classes ~~
class AbstractOutput(object):
"""Base class for output."""
def __init__(self, options):
self.options = options
def add_builds(self, name, builds):
"""Add builds for a builder.
This method adds builds to the output object.
It can render a message after each addition.
Arguments:
- name: builder name (str)
- builds: list of Build objects
"""
pass
def display(self):
"""Display result.
This method is called once, after all builds have been added to
the output object. It renders the final message.
"""
pass
class BuilderOutput(AbstractOutput):
"""Default output."""
def __init__(self, options):
AbstractOutput.__init__(self, options)
self.counters = dict((s, 0) for s in BUILDER_STATUSES)
self.groups = dict((s, []) for s in BUILDER_STATUSES)
def print_builder(self, name, builds):
"""Print the builder result."""
quiet = self.options.quiet
count = {S_SUCCESS: 0, S_FAILURE: 0}
capsule = []
failed_builds = []
display_builds = []
for build in builds:
# Save horizontal space, printing only the last 3 digits
compact = (quiet or len(builds) > 6) and len(capsule) > 1
if build is None:
if len(capsule) < NUMBUILDS:
capsule.append(' ' * (5 if not compact else 3))
continue
result = build.result
if build.id:
id = '%5d' % build.id
id = id if not compact else id[-3:]
else:
id = ' *** ' if not compact else '***'
capsule.append(cformat(id, result, sep=''))
if result == S_BUILDING:
continue
elif result == S_SUCCESS:
count[S_SUCCESS] += 1
if self.options.verbose:
display_builds.append(build)
else:
count[S_FAILURE] += 1
failed_builds.append(build)
display_builds.append(build)
is_active = ((builds[0] and builds[0].revision) or
count[S_SUCCESS] > 0 or count[S_FAILURE] > 0)
if quiet > 1:
# Print only the colored buildbot names
if not is_active:
return S_OFFLINE
last_result = builds[0].result
if last_result in (S_SUCCESS, S_BUILDING):
return last_result
return S_FAILURE
if count[S_SUCCESS] == 0:
if is_active:
builder_status = S_FAILURE
else:
builder_status = S_OFFLINE
capsule = [cformat(' *** ', S_OFFLINE, sep='')] * 2
elif count[S_FAILURE] > 0:
builder_status = S_UNSTABLE
else:
builder_status = S_SUCCESS
out(cformat('%-26s' % name, builder_status), ', '.join(capsule),
end=' ')
if quiet and failed_builds:
# Print last failure or error.
out(failed_builds[0].get_message(MSG_MAXLENGTH))
else:
# Move to next line
out()
if not quiet:
for build in display_builds:
out('%4d %5d:' % (build.num, build.revision),
build.get_message())
return builder_status
def add_builds(self, name, builds):
builder_status = self.print_builder(name, builds)
if self.options.quiet > 1:
self.groups[builder_status].append(name)
self.counters[builder_status] += 1
def display(self):
totals = []
for status in BUILDER_STATUSES:
if self.counters[status]:
totals.append(cformat(self.counters[status], status, sep=':'))
# With -qq option
if self.options.quiet > 1:
self._group_by_status()
# Show the summary at the bottom
out('Totals:', ' + '.join(totals))
out(issues.new_failures())
def _group_by_status(self):
for status in BUILDER_STATUSES:
names = self.groups[status]
if not names:
continue
platforms = {}
for name in names:
try:
host, branch = name.rsplit(None, 1)
except ValueError:
host, branch = name, ''
platforms.setdefault(host, []).append(branch)
out(cformat(status.title() + ':', status))
for host, branches in sorted(platforms.items()):
out('\t' + cformat(host, status), ', '.join(branches))
class Branch(object):
"""Represent all results of a specific branch.
Used for the RevisionOutput.
"""
def __init__(self, name):
self.name = name
self.revisions = {}
self.last_revision = 0
class Revision(object):
"""Represent all results for a revision.
Used for the RevisionOutput.
"""
def __init__(self, number):
self.number = number
self.by_status = collections.defaultdict(list)
class RevisionOutput(AbstractOutput):
"""Alternative output by revision."""
def __init__(self, options):
AbstractOutput.__init__(self, options)
self.branches = {}
out("... retrieving build results")
def add_builds(self, name, builds):
host, branch_name = parse_builder_name(name)
for build in builds:
if build is None or build.revision == 0:
continue
try:
branch = self.branches[branch_name]
except KeyError:
branch = Branch(branch_name)
self.branches[branch.name] = branch
branch.last_revision = max(branch.last_revision, build.revision)
text = self.format_build(build)
if text is None:
continue
try:
revision = branch.revisions[build.revision]
except KeyError:
revision = Revision(build.revision)
branch.revisions[build.revision] = revision
revision.by_status[build.result].append(text)
# Filter revisions: remove success and building builds
# depending on verbose and quiet options
for branch in self.branches.values():
branch_items = list(branch.revisions.items())
for number, revision in branch_items:
results = list(revision.by_status.keys())
for result in results:
if not self.options.verbose and (result == S_BUILDING or
(result == S_SUCCESS and (self.options.quiet > 1 or
revision.number != branch.last_revision))):
del revision.by_status[result]
if not revision.by_status:
del branch.revisions[number]
def format_build(self, build):
msg = build.builder
length = 2048
if build.result not in (S_SUCCESS, S_BUILDING):
if build.result == S_EXCEPTION and (not self.options.verbose):
# Hide exceptions
return None
build_message = build._message
if build.failed_tests:
new_events, known = issues.match(build)
if new_events:
(text, length) = trunc(new_events, length)
msg += ':' + cformat(text, S_FAILURE, sep='')
elif self.options.quiet:
# Hide known failures
return None
if known:
msg += trunc(known, length)[0]
else:
msg += ': "%s"' % build_message
else:
msg = cformat(msg, build.result)