Source

buildbot / bot2 / pypybuildbot / summary.py

Full commit
  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
import time, datetime, urlparse, urllib
import operator

import py
html = py.xml.html

from buildbot.status.web.base import HtmlResource
from buildbot.status.builder import FAILURE, EXCEPTION

def host_agnostic(url):
    parts = urlparse.urlsplit(url)
    return urlparse.urlunsplit(('','')+parts[2:])

def show_elapsed(secs):
    if secs < 5:
        return "%.02fs" % secs
    secs = int(round(secs))
    if secs < 60:
        return "%ds" % secs
    if secs < 5*60:
        return "%dm%d" % (secs/60, secs%60)
    mins = int(round(secs/60.))
    if mins < 60:
        return "%dm" % mins
    return "%dh%d" % (mins/60, mins%60)

class OutcomeSummary(object):
    def __init__(self, p, F, s, x):
        self.p = p # passed
        self.F = F # failed
        self.s = s # skipped
        self.x = x # xfailed

    def is_ok(self):
        return self.F == 0

    def to_tuple(self):
        return self.p, self.F, self.s, self.x

    def __str__(self):
        return '%d, %d F, %d s, %d x' % (self.p, self.F, self.s, self.x)

    def __add__(self, other):
        return self.__class__(self.p + other.p,
                              self.F + other.F,
                              self.s + other.s,
                              self.x + other.x)

class RevisionOutcomeSet(object):

    def __init__(self, rev, key=None, run_info=None):
        self.revision = rev
        self.key = key
        self._outcomes = {}
        self.failed = set()
        self.skipped = set()
        self._xfailed = 0
        self.longreprs = {}
        self._run_info = run_info

    def get_summary(self):
        return OutcomeSummary(self.numpassed,
                              len(self.failed),
                              len(self.skipped),
                              self.numxfailed)

    def populate_one(self, name, shortrepr, longrepr=None):
        if shortrepr == '!':
            namekey = [name, '']
        else:
            # pytest2 and pytest1 use different separators/test id
            # syntax support both here for now
            if '.py::' in name:
                namekey = name.split('::', 1)
            else:
                namekey = name.split(':', 1)
            if namekey[0].endswith('.py'):
                namekey[0] = namekey[0][:-3].replace('/', '.')
            if len(namekey) == 1:
                namekey.append('')
            namekey[1] = namekey[1].replace("::", ".")

        namekey = tuple(namekey)
        self._outcomes[namekey] = shortrepr
        if shortrepr.lower() == 's':
            self.skipped.add(namekey)
        elif shortrepr == '.':
            pass
        elif shortrepr == 'x':
            self._xfailed += 1
        else:
            self.failed.add(namekey)

        if longrepr:
            try:
                longrepr = longrepr.decode("utf-8")
            except UnicodeDecodeError:
                longrepr = longrepr.decode("latin-1")

            self.longreprs[namekey] = longrepr

    @property
    def numpassed(self):
        return (len(self._outcomes) - len(self.skipped) - len(self.failed)
                                    - self._xfailed)

    @property
    def numxfailed(self):
        return self._xfailed

    def populate(self, log):
        kind = None
        def add_one():
            if kind is not None:
                self.populate_one(name, kind, ''.join(longrepr))
        for line in log.readlines():
            first = line[0]
            if first == ' ':
                longrepr.append(line[1:])
                continue
            add_one()
            kind = first
            name = line[2:].rstrip()
            longrepr = []
        add_one()

    def get_outcome(self, namekey):
        return self._outcomes.get(namekey, ' ')

    def get_longrepr(self, namekey):
        return self.longreprs.get(namekey, '')

    def get_key_namekey(self, namekey):
        return (self.key, namekey)

    def get_run_infos(self):
        return {self.key: (self, self._run_info)}


class RevisionOutcomeSetCache(object):

    def __init__(self, cachesize=10):
        self._outcome_sets = {}
        self._lru = []
        self._hits = 0
        self._misses = 0
        self.cachesize = cachesize

    def clear(self): # for testing
        self._outcome_sets = {}
        self._lru = []

    def reset_counters(self):
        self._hits = 0
        self._misses = 0

    def stats(self):
        return "hits: %d, misses: %d" % (self._hits, self._misses)

    def _load_outcome_set(self, status, key):
        builderName, buildNumber = key
        builderStatus = status.getBuilder(builderName)
        build = builderStatus.getBuild(buildNumber)
        run_url = status.getURLForThing(build)

        rev = build.getProperty("got_revision")
        pytest_logs = []
        pytest_elapsed = 0
        with_logs = set()
        for step in build.getSteps():
            logs = dict((log.getName(), log) for log in step.getLogs())
            if 'pytestLog' in logs:
                with_logs.add(step)
                pytest_logs.append((step.getName(), logs['pytestLog']))
                ts = step.getTimes()
                if ts[0] is not None and ts[1] is not None:
                    pytest_elapsed += ts[1]-ts[0]

        run_info = {'URL': run_url, 'elapsed': pytest_elapsed or None,
                    'times': build.getTimes()}
        outcome_set = RevisionOutcomeSet(rev, key, run_info)
        someresult = False
        # "*-run" categories mean the build is not a test build!
        if builderStatus.category:
            someresult = builderStatus.category.endswith("-run")
        if pytest_logs:
            for stepName, resultLog in pytest_logs:
                if resultLog.hasContents():
                    someresult = True
                    outcome_set.populate(resultLog)

        failedtests = not not outcome_set.failed

        failure = None
        for step in build.getSteps():
            if step.getResults()[0] in (FAILURE, EXCEPTION):
                text = ' '.join(step.getText())
                if step in with_logs:
                    if failedtests and text.endswith('failed'):
                        continue
                failure = text
                break

        if not someresult or failure is not None:
            if failure:
                name = '"%s"' % failure # quote
            else:
                name = '<run>'
            outcome_set.populate_one(name, '!')

        return outcome_set

    def get(self, status, key):
        try:
            self._lru.remove(key)
        except ValueError:
            pass
        self._lru.append(key)
        try:
            outcome_set = self._outcome_sets[key]
            self._hits += 1
            return outcome_set
        except KeyError:
            pass
        self._misses += 1
        if len(self._lru) > self.cachesize:
            dead_key = self._lru.pop(0)
            self._outcome_sets.pop(dead_key, None)
        outcome_set = self._load_outcome_set(status, key)
        self._outcome_sets[key] = outcome_set
        return outcome_set

class GatherOutcomeSet(object):

    def __init__(self, map):
        self.map = map
        self._failed = None
        self._skipped = None
        self._numpassed = None
        self._numxfailed = None
        self.revision = map.values()[0].revision

    @property
    def failed(self):
        if self._failed is None:
            self._failed = set()
            for prefix, outcome in self.map.items():
                self._failed.update([(prefix,)+ namekey for namekey in
                                     outcome.failed])
        return self._failed

    @property
    def skipped(self):
        if self._skipped is None:
            self._skipped = set()
            for prefix, outcome in self.map.items():
                self._skipped.update([(prefix,) + namekey for namekey in
                                     outcome.skipped])
        return self._skipped

    @property
    def numpassed(self):
        if self._numpassed is None:
            numpassed = 0
            for  prefix, outcome in self.map.items():
                numpassed += outcome.numpassed
            self._numpassed = numpassed
        return self._numpassed

    @property
    def numxfailed(self):
        if self._numxfailed is None:
            numxfailed = 0
            for  prefix, outcome in self.map.items():
                numxfailed += outcome.numxfailed
            self._numxfailed = numxfailed
        return self._numxfailed

    def get_outcome(self, namekey):
        which = namekey[0]
        if which not in self.map:
            return ' '
        return self.map[which].get_outcome(namekey[1:])

    def get_longrepr(self, namekey):
        which = namekey[0]
        if which not in self.map:
            return ''
        return self.map[which].get_longrepr(namekey[1:])

    def get_key_namekey(self, namekey):
        return self.map[namekey[0]].get_key_namekey(namekey[1:])

    def get_run_infos(self):
        all = {}
        for outcome_set in self.map.itervalues():
            all.update(outcome_set.get_run_infos())
        return all

# ________________________________________________________________

N = 5

outcome_set_cache = RevisionOutcomeSetCache(32*(N+1))


def colsizes(namekeys):
    colsizes = None
    for keys in namekeys:
        if colsizes is None:
            colsizes = [0] * len(keys)
        colsizes = map(max, zip(map(len, keys), colsizes))

    return colsizes


class SummaryPage(object):
    SUCCESS_LINE = True

    def __init__(self, status):
        self.sections = []
        self.cur_cat_branch=None
        self.fixed_builder = False
        self.status = status

    def make_longrepr_url_for(self, outcome_set, namekey):
        cachekey, namekey = outcome_set.get_key_namekey(namekey)
        parms={
            'builder': cachekey[0],
            'build': cachekey[1],
            'mod': namekey[0],
            'testname': namekey[1]
            }
        qs = urllib.urlencode(parms)
        return "/summary/longrepr?" + qs

    def make_run_anchors_for(self, outcome_set):
        anchors = []
        infos = sorted(outcome_set.get_run_infos().items())
        minend = None
        maxend = None
        for cachekey, (run, info) in infos:
            builder = cachekey[0]
            anchors.append('  ')
            timing = ""
            if self.fixed_builder and info['elapsed'] is not None:
                timing = " in %s" % show_elapsed(info['elapsed'])
            if info['times'][1] is not None:
                day = time.localtime(info['times'][1])[:3]
                if minend is None:
                    minend = day
                else:
                    minend = min(minend, day)
                if maxend is None:
                    maxend = day
                else:
                    maxend = max(maxend, day)
            text = "%s [%d, %d F, %d s, %d x%s]" % (builder,
                                                    run.numpassed,
                                                    len(run.failed),
                                                    len(run.skipped),
                                                    run.numxfailed,
                                                    timing)
            anchors.append(html.a(text, href=host_agnostic(info['URL'])))
        if maxend is not None:
            mintxt = datetime.date(*minend).strftime("%d %b")
            maxtxt = datetime.date(*maxend).strftime("%d %b")
            if maxend == minend:
                anchors.append(' (%s)' % maxtxt)
            else:
                anchors.append(' (%s..%s)' % (mintxt, maxtxt))
        return anchors

    def _start_cat_branch(self, cat_branch, fine=False):
        category, branch = cat_branch
        branch = trunk_name(branch)
        category = category_name(category)

        self.cur_cat_branch = (category, branch)

        cat_anchor = html.a("{%s}" % category,
                            href="/summary?category=%s" % category,
                            class_="failSummary branch")

        branch_anchor = html.a(branch,
                               href="/summary?branch=%s" % branch,
                               class_="failSummary branch")
        if fine:
            extra = html.img(alt=":-)", src="success.png")
        else:
            extra = ""
        self.sections.append(html.h2(cat_anchor," ",branch_anchor, " ", extra))

    def _builder_anchor(self, builder):
        if self.fixed_builder:
            url = self.status.getURLForThing(self.status.getBuilder(builder))
            cls = "builder"
        else:
            url = "/summary?builder=%s" % builder
            cls = "builderquery"
        return html.a(builder, href=host_agnostic(url),
                      class_=' '.join(["failSummary", cls]))

    def _builder_num(self, outcome_set):
        return outcome_set.map.values()[0].key

    def _label(self, outcome_set):
        if self.fixed_builder:
            # (rev, buildNumber)
            buildNumber = self._builder_num(outcome_set)[1]
            return (outcome_set.revision, buildNumber)
        else:
            # rev
            return outcome_set.revision

    def _label_for_sorting(self, outcome_set):
        encodedrev = encode_rev_for_ordering(outcome_set.revision)
        if self.fixed_builder:
            # (rev, buildNumber)
            buildNumber = self._builder_num(outcome_set)[1]
            return (encodedrev, buildNumber)
        else:
            # rev
            return encodedrev

    def _label_anchor(self, outcome_set, revsize):
        rev = outcome_set.revision
        if self.fixed_builder:
            pick = "builder=%s&builds=%d" % self._builder_num(outcome_set)
        else:
            pick = "recentrev=%s" % rev
        category, branch = self.cur_cat_branch
        revtxt = str(rev)
        rev_anchor = html.a(revtxt, href="/summary?category=%s&branch=%s&%s" %
                            (category, branch, pick))
        rightalign = ' '*(revsize-len(revtxt))
        return [rev_anchor, rightalign]

    def add_section(self, cat_branch, outcome_sets):
        if not outcome_sets:
            return
        outcome_sets.sort(key=self._label_for_sorting)
        labels = [self._label(outcome_set) for outcome_set in outcome_sets]
        by_label = [(self._label(outcome_set), outcome_set)
                    for outcome_set in outcome_sets]
        revs = [outcome_set.revision for outcome_set in outcome_sets]

        _, last = by_label[-1]
        self._start_cat_branch(cat_branch, fine = not last.failed)

        lines = []
        revsize = max(map(len, revs))
        align = 2*len(labels)-1+revsize
        def bars():
            return ' |'*len(lines)
        for label, outcome_set in by_label:
            line = [bars(), ' '] + self._label_anchor(outcome_set, revsize)
            line.append((align-len(line[0]))*" ")
            line.append(self.make_run_anchors_for(outcome_set))
            line.append('\n')
            lines.append(line)
        lines.append([bars(), "\n"])

        a_num = len(self.sections)
        if self.SUCCESS_LINE:
            success = []
            for label, outcome_set in by_label:
                if outcome_set.failed:
                    symbol = html.a("-",
                        id="a%dc%d" % (a_num, 1<<len(success)),
                        href="javascript:togglestate(%d,%d)" % (
                                       a_num, 1<<len(success)),
                        class_="failSummary failed")
                else:
                    symbol = html.span("+", class_="failSummary success")
                success.append([" ", symbol])
            success.append("  success\n")
            lines.append(success)

        failed = set()
        exploded = set()
        for label, outcome_set in by_label:
            for failure in outcome_set.failed:
                letter = outcome_set.get_outcome(failure)
                if letter == '!':
                    exploded.add(failure)
                failed.add(failure)

        colwidths = colsizes(failed)

        def sorting(x):
            return (x not in exploded, x)

        for failure in sorted(failed, key=sorting):
            line = []
            combination = 0
            for i, (label, outcome_set) in enumerate(by_label):
                letter = outcome_set.get_outcome(failure)
                failed = letter.lower() not in ('s', '.', ' ')
                if failed:
                    combination |= 1 << i
                if outcome_set.get_longrepr(failure):
                    longrepr_url = self.make_longrepr_url_for(outcome_set,
                                                              failure)
                    extra = {}
                    if failed:
                        extra = {'class': "failSummary failed"}
                    line.append([" ",html.a(letter, href=longrepr_url,
                                            **extra)])
                else:
                    if failed:
                        line.append([" ",
                                     html.span(letter,
                                               class_="failSummary failed")])
                    else:
                        line.append(" %s" % letter)
            # builder
            builder_width = colwidths[0]
            builder = failure[0]
            spacing = ("  %-*s" % (builder_width, 'x'*len(builder))).split('x')
            spaceleft = spacing[0]
            spaceright = spacing[-1]
            builder_anchor = self._builder_anchor(builder)
            line.append([spaceleft, builder_anchor, spaceright])

            for width, key in zip(colwidths[1:], failure[1:]):
                line.append("  %-*s" % (width, key))
            line.append('\n')
            lines.append(html.span(line,
                                   class_="a%dc%d" % (a_num, combination)))

        section = html.pre(lines)
        self.sections.append(section)

    def add_no_revision_builds(self, status, no_revision_builds):
        if not no_revision_builds:
            return
        section = html.div(html.p("builds aborted without getting"
                                  " a revision:"))

        for build in no_revision_builds:
            builderName = build.getBuilder().getName()
            num = build.getNumber()
            descr = "%s #%d" % (builderName, num)
            url = status.getURLForThing(build)
            section.append(html.a(descr, href=host_agnostic(url)))
            section.append(html.br())
        self.sections.append(section)

    def add_comment(self, comm):
        self.sections.append(py.xml.raw("<!-- %s -->" % comm))

    def render(self):
        body_html = html.div(self.sections)
        return body_html.unicode()

class LongRepr(HtmlResource):

    def get_namekey(self, request):
        mod = request.args.get('mod', [])
        if not mod:
            mod = None
        else:
            mod = mod[0]

        testname = request.args.get('testname', [])
        if testname:
            testname = testname[0]
        else:
            testname = ''

        return (mod, testname)

    def getTitle(self, request):
        mod, testname = self.get_namekey(request)
        if mod is None:
            return "no such test"
        return "%s %s" % (mod, testname)

    def body(self, request):
        t0 = time.time()
        outcome_set_cache.reset_counters()

        builder = request.args.get('builder', [])
        build = request.args.get('build', [])
        if not builder or not build:
            return "no such build"
        builderName = builder[0]
        buildNumber = int(build[0])

        outcome_set = outcome_set_cache.get(self.getStatus(request),
                                            (builderName,
                                             buildNumber))

        namekey = self.get_namekey(request)

        longrepr = outcome_set.get_longrepr(namekey)

        return html.div([html.pre(longrepr),
                         py.xml.raw("<!-- %s -->" % outcome_set_cache.stats())
                         ]).unicode()

def getProp(obj, name, default=None):
    try:
        return obj.getProperty(name)
    except KeyError:
        return default

def make_test(lst):
    if lst is None:
        return lambda v: True
    else:
        membs = set(lst)
        return lambda v: v in membs

def make_subst(v1, v2):
    def subst(v):
        if v == v1:
            return v2
        return v
    return subst

trunk_name = make_subst(None, "<trunk>")
trunk_value = make_subst("<trunk>", None)
category_name = make_subst(None, '-')
nocat_value = make_subst("-", None)

def safe_int(v):
    try:
        return int(v)
    except ValueError:
        return None

def encode_rev_for_ordering(rev):
    # subversion: just an integer
    if isinstance(rev, int) or rev.isdigit():
        return (1, int(rev))
    # mercurial: "integer:globalid"
    if ':' in rev and rev[:rev.index(':')].isdigit():
        i = rev.index(':')
        return (2, int(rev[:i]), rev)
    # unknown
    return (0, rev)

class Summary(HtmlResource):

    def __init__(self, categories=[], branch_order_prefixes=[]):
        HtmlResource.__init__(self)
        self.putChild('longrepr', LongRepr())
        self._defaultBranchCache = {}
        self.categories = categories
        self.branch_order_prefixes = branch_order_prefixes

    def content(self, req, context):
        body = self.body(req)
        context['content'] = body
        template = req.site.buildbot_service.templates.get_template(
            "summary.html")
        return template.render(**context)

    def getTitle(self, request):
        status = self.getStatus(request)
        return "%s: summaries of last %d revisions" % (status.getProjectName(),
                                                       N)

    @staticmethod
    def _prune_runs(runs, cutnum):
        keys = runs.keys()
        #
        def revkey(rev):
            if isinstance(rev, tuple):
                buildNumber, rev = rev
            else:
                buildNumber = None
            return (buildNumber, encode_rev_for_ordering(rev))
        #
        keys.sort(key=revkey)
        if len(runs) > cutnum:
            for rev in keys[:-cutnum]:
                del runs[rev]

    def _fish_defaultBranch(self, status, builderName):
        try:
            return self._defaultBranchCache[builderName]
        except KeyError:
            pass
        builder = status.botmaster.builders[builderName]
        branch = None
        for _, kw in builder.buildFactory.steps:
            if 'defaultBranch' in kw:
                if kw.get('explicitBranch'):
                    branch = kw['defaultBranch']
                break
        self._defaultBranchCache[builderName] = branch
        return branch

    def _get_branch(self, status, build):
        branch = getProp(build, 'explicitBranch')
        # fish
        if branch is None:
            builderName = build.getBuilder().getName()
            branch = self._fish_defaultBranch(status, builderName)
            branch = branch or getProp(build, 'branch')
        return branch

    def _now(self):
        return time.time()

    def _age(self, build):
        start, _ = build.getTimes()
        return (self._now()-start)/(60*60*24) # in days

    def recentRuns(self, status, only_recentrevs=None, only_branches=None,
                                 only_builder=None, only_builds=None,
                                 only_categories=None):
        test_rev = make_test(only_recentrevs)
        test_branch = make_test(only_branches)
        test_builder = make_test(only_builder)
        fixed_builder = bool(only_builder)
        prune_old = not (only_builds or only_recentrevs or
                         only_builder or only_branches)

        cat_branches = {}

        for builderName in status.getBuilderNames(only_categories):
            if not test_builder(builderName):
                continue
            builderStatus = status.getBuilder(builderName)
            if only_builds:
                def builditer():
                    for num in only_builds:
                        b = builderStatus.getBuild(num)
                        if b is not None:
                            yield b
                builditer = builditer()
            else:
                builditer = builderStatus.generateFinishedBuilds(num_builds=5*N)

            for build in builditer:
                if prune_old and self._age(build) > 7:
                    continue
                branch = self._get_branch(status, build)
                if not test_branch(branch):
                    continue
                got_rev = getProp(build, 'got_revision', None)
                if not test_rev(got_rev):
                    continue

                cat_branch = (builderStatus.category, branch)

                runs, no_revision_builds = cat_branches.setdefault(cat_branch,
                                                               ({}, []))

                if got_rev is None:
                    if self._age(build) <= 7:
                        no_revision_builds.append(build)
                else:
                    rev = got_rev
                    buildNumber = build.getNumber()
                    if fixed_builder:
                        builds = runs.setdefault((buildNumber, rev), {})
                    else:
                        builds = runs.setdefault(rev, {})
                        # pick the most recent or ?

                    if builderName not in builds:
                        builds[builderName] = build.getNumber()

        for cat_branch, (runs, no_revision_builds) in cat_branches.items():
            self._prune_runs(runs, N)
            for label, runBuilds in runs.iteritems():
                for builderName, buildNumber in runBuilds.items():
                    key = (builderName, buildNumber)
                    outcome_set = outcome_set_cache.get(status, key)
                    runBuilds[builderName] = outcome_set

        return cat_branches

    @staticmethod
    def _parse_builds(build_select):
        builds = set()
        for sel in build_select:
            for onesel in sel.split(','):
                build = safe_int(onesel)
                if build is not None:
                    builds.add(build)
                    continue
                build_start_end = onesel.split('-')
                if len(build_start_end) == 2:
                    build_start = safe_int(build_start_end[0])
                    build_end = safe_int(build_start_end[1])
                    if (build_start is not None and build_end is not None):
                        builds.update(range(build_start, build_end+1))
        return builds

    def _cat_branch_key(self, (category, branch)):
        branch_key = (0,)
        if branch is not None:
            for j, prefix in enumerate(self.branch_order_prefixes):
                if branch.startswith(prefix):
                    branch_key = (j+1, branch)
                    break
            else:
                branch_key = (len(self.branch_order_prefixes)+1, branch)
        for i, catprefix in enumerate(self.categories):
            if category.startswith(catprefix):
                # kill '-' to make 'linux32' sort before 'linux-armel'
                category = category.replace('-', '')
                break
        else:
            i = len(self.categories)
        cat_key = (i, category)
        return cat_key + branch_key

    def body(self, request):
        t0 = time.time()
        outcome_set_cache.reset_counters()

        status = self.getStatus(request)

        page = SummaryPage(status)
        #page.sections.append(repr(request.args))

        only_branches = request.args.get('branch', None)
        only_recentrevs = request.args.get('recentrev', None)
        if only_branches is not None:
            only_branches = map(trunk_value, only_branches)
        only_builder = request.args.get('builder', None)
        only_builds = None
        if only_builder is not None:
            only_builder = only_builder[-1:] # pick exactly one
            page.fixed_builder = True
            build_select = request.args.get('builds', None)
            if build_select is not None:
                only_builds = self._parse_builds(build_select)
        only_categories = request.args.get('category', None)
        if only_categories is not None:
            only_categories = map(nocat_value, only_categories)

        cat_branches = self.recentRuns(status,
                                   only_recentrevs = only_recentrevs,
                                   only_branches = only_branches,
                                   only_builder = only_builder,
                                   only_builds = only_builds,
                                   only_categories = only_categories
                                   )

        sorting = sorted(cat_branches.iterkeys(), key=self._cat_branch_key)
        for cat_branch in sorting:
            runs, no_revision_builds = cat_branches[cat_branch]
            outcome_sets = []
            for label, by_build in runs.items():
                outcome_sets.append(GatherOutcomeSet(by_build))
            page.add_section(cat_branch, outcome_sets)
            page.add_no_revision_builds(status, no_revision_builds)

        t1 = time.time()
        total_time = time.time()-t0
        page.add_comment('t=%.2f; %s' % (total_time,
                                         outcome_set_cache.stats()))

        if request.args:
            trunk_vs_any_text = "filter nothing"
            trunk_vs_any_query = ""
        else:
            trunk_vs_any_text = "all <trunk>"
            trunk_vs_any_query = "?branch=<trunk>"

        trunk_vs_any_anchor = html.a(trunk_vs_any_text,
                                     href="/summary%s" %
                                     trunk_vs_any_query,
                                     class_="failSummary trunkVsAny")
        trunk_vs_any = html.div(trunk_vs_any_anchor,
                                style="position: absolute; right: 5%;")
        return trunk_vs_any.unicode() + page.render()