changes to reviews
[build-farm.git] / buildfarm / web / __init__.py
index b2ef29b197f9c5431779a8ea299509035ea7a1dc..ead25730a7599cbe5d5be9a1dc19db37707999d5 100755 (executable)
@@ -2,7 +2,7 @@
 # This CGI script presents the results of the build_farm build
 
 # Copyright (C) Jelmer Vernooij <jelmer@samba.org>     2010
-# Copyright (C) Matthieu Patou <mat@matws.net>         2010
+# Copyright (C) Matthieu Patou <mat@matws.net>         2010-2012
 #
 # Based on the original web/build.pl:
 #
@@ -42,6 +42,7 @@ from buildfarm.build import (
     LogFileMissing,
     NoSuchBuildError,
     NoTestOutput,
+    BuildStatus,
     )
 
 import cgi
@@ -54,7 +55,7 @@ import time
 import wsgiref.util
 webdir = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "web"))
 
-GITWEB_BASE = "http://gitweb.samba.org"
+GITWEB_BASE = "https://gitweb.samba.org"
 HISTORY_HORIZON = 1000
 
 # this is automatically filled in
@@ -128,8 +129,17 @@ def build_link(myself, build):
     return "<a href='%s'>%s</a>" % (build_uri(myself, build), html_build_status(build.status()))
 
 
+def tree_uri(myself, tree):
+    return "%s/tree/%s" % (myself, tree.name)
+
+
+def tree_link(myself, tree):
+    """return a link to a particular tree"""
+    return "<a href='%s' title='View recent builds for %s'>%s:%s</a>" % (tree_uri(myself, tree), tree.name, tree.name, tree.branch)
+
+
 def host_uri(myself, host):
-    return "%s?function=View+Host;host=%s" % (myself, host)
+    return "%s/host/%s" % (myself, host)
 
 def host_link(myself, host):
     return "<a href='%s'>%s</a>" % (host_uri(myself, host), host)
@@ -158,6 +168,8 @@ def subunit_to_buildfarm_result(subunit_result):
         return "failed"
     elif subunit_result == "xfail":
         return "xfailed"
+    elif subunit_result == "uxsuccess":
+        return "uxpassed"
     else:
         return "unknown"
 
@@ -192,6 +204,10 @@ class LogPrettyPrinter(object):
         self.indice += 1
         return "".join(make_collapsible_html('test', m.group(1), '', self.indice, 'skipped'))
 
+    def _format_pretestsuite(self, m):
+        self.indice += 1
+        return m.group(1)+"".join(make_collapsible_html('pretest', 'Pretest infos', m.group(2), self.indice, 'ok'))+"\n"+m.group(3)
+
     def _format_testsuite(self, m):
         testName = m.group(1)
         content = m.group(2)
@@ -201,7 +217,11 @@ class LogPrettyPrinter(object):
         else:
             errorReason = ""
         self.indice += 1
-        return "".join(make_collapsible_html('test', testName, content+errorReason, self.indice, status))
+        backlink = ""
+        if m.group(3) in ("error", "failure"):
+            self.test_links.append([testName, 'lnk-test-%d' %self.indice])
+            backlink = "<p><a href='#shortcut2errors'>back to error list</a>"
+        return "".join(make_collapsible_html('test', testName, content+errorReason+backlink, self.indice, status))
 
     def _format_test(self, m):
         self.indice += 1
@@ -211,6 +231,7 @@ class LogPrettyPrinter(object):
         # do some pretty printing for the actions
         pattern = re.compile("(Running action\s+([\w\-]+)$(?:\s^.*$)*?\sACTION\ (PASSED|FAILED):\ ([\w\-]+)$)", re.M)
         log = pattern.sub(self._pretty_print, log)
+        buf = ""
 
         log = re.sub("""
               --==--==--==--==--==--==--==--==--==--==--.*?
@@ -222,17 +243,27 @@ class LogPrettyPrinter(object):
               ==========================================\s+
             """, self._format_stage, log)
 
+        pattern = re.compile("(Running action test).*$\s((?:^.*$\s)*?)^((?:skip-)?testsuite: )", re.M)
+        log = pattern.sub(self._format_pretestsuite, log)
+
         log = re.sub("skip-testsuite: ([\w\-=,_:\ /.&; \(\)]+).*?",
                 self._format_skip_testsuite, log)
 
+        self.test_links = []
         pattern = re.compile("^testsuite: (.+)$\s((?:^.*$\s)*?)testsuite-(\w+): .*?(?:(\[$\s(?:^.*$\s)*?^\]$)|$)", re.M)
         log = pattern.sub(self._format_testsuite, log)
         log = re.sub("""
               ^test: ([\w\-=,_:\ /.&; \(\)]+).*?
               (.*?)
-              (success|xfail|failure|skip): [\w\-=,_:\ /.&; \(\)]+( \[.*?\])?.*?
+              (success|xfail|failure|skip|uxsuccess): [\w\-=,_:\ /.&; \(\)]+( \[.*?\])?.*?
            """, self._format_test, log)
 
+        for tst in self.test_links:
+            buf = "%s\n<A href='#%s'>%s</A>" % (buf, tst[1], tst[0])
+
+        if not buf == "":
+            divhtml = "".join(make_collapsible_html('testlinks', 'Shortcut to failed tests', "<a name='shortcut2errors'></a>%s" % buf, self.indice, ""))+"\n"
+            log = re.sub("Running action\s+test", divhtml, log)
         return "<pre>%s</pre>" % log
 
 
@@ -304,9 +335,9 @@ def make_collapsible_html(type, title, output, id, status=""):
     :param title: the title to be displayed
     """
     if status.lower() in ("", "failed"):
-        icon = 'icon_hide_16.png'
+        icon = '/icon_hide_16.png'
     else:
-        icon = 'icon_unhide_16.png'
+        icon = '/icon_unhide_16.png'
 
     # trim leading and trailing whitespace
     output = output.strip()
@@ -314,7 +345,7 @@ def make_collapsible_html(type, title, output, id, status=""):
     # note that we may be inside a <pre>, so we don't put any extra whitespace
     # in this html
     yield "<div class='%s unit %s' id='%s-%s'>" % (type, status, type, id)
-    yield "<a href=\"javascript:handle('%s');\">" % id
+    yield "<a name='lnk-%s-%s' href=\"javascript:handle('%s');\">" % (type, id, id)
     yield "<img id='img-%s' name='img-%s' alt='%s' src='%s' />" % (id, id, status, icon)
     yield "<div class='%s title'>%s</div></a>" % (type, title)
     yield "<div class='%s status %s'>%s</div>" % (type, status, status)
@@ -362,13 +393,11 @@ class BuildFarmPage(object):
             return "<span class='old'>%s</span>" % util.dhm_time(age)
         return util.dhm_time(age)
 
-    def tree_link(self, myself, tree):
-        # return a link to a particular tree
-        branch = ""
-        if tree in self.buildfarm.trees:
-            branch = ":%s" % self.buildfarm.trees[tree].branch
-
-        return "<a href='%s?function=Recent+Builds;tree=%s' title='View recent builds for %s'>%s%s</a>" % (myself, tree, tree, tree, branch)
+    def tree_link(self, myself, treename):
+        try:
+            return tree_link(myself, self.buildfarm.trees[treename])
+        except KeyError:
+            return treename
 
     def render(self, output_type):
         raise NotImplementedError(self.render)
@@ -376,8 +405,10 @@ class BuildFarmPage(object):
 
 class ViewBuildPage(BuildFarmPage):
 
-    def show_oldrevs(self, myself, tree, host, compiler):
+    def show_oldrevs(self, myself, build, host, compiler, limit):
         """show the available old revisions, if any"""
+
+        tree = build.tree
         old_builds = self.buildfarm.builds.get_old_builds(tree, host, compiler)
 
         if not old_builds:
@@ -389,7 +420,11 @@ class ViewBuildPage(BuildFarmPage):
         yield "<thead><tr><th>Revision</th><th>Status</th><th>Age</th></tr></thead>\n"
         yield "<tbody>\n"
 
+        nb = 0
         for old_build in old_builds:
+            if limit >= 0 and nb >= limit:
+                break
+            nb = nb + 1
             yield "<tr><td>%s</td><td>%s</td><td>%s</td></tr>\n" % (
                 revision_link(myself, old_build.revision, tree),
                 build_link(myself, old_build),
@@ -397,7 +432,9 @@ class ViewBuildPage(BuildFarmPage):
 
         yield "</tbody></table>\n"
 
-    def render(self, myself, build, plain_logs=False):
+        yield "<p><a href='%s/limit/-1'>Show all previous build list</a>\n" % (build_uri(myself, build))
+
+    def render(self, myself, build, plain_logs=False, limit=10):
         """view one build in detail"""
 
         uname = None
@@ -455,7 +492,7 @@ class ViewBuildPage(BuildFarmPage):
             yield "<tr><td>configure options:</td><td>%s</td></tr>\n" % config
         yield "</table>\n"
 
-        yield "".join(self.show_oldrevs(myself, build.tree, build.host, build.compiler))
+        yield "".join(self.show_oldrevs(myself, build, build.host, build.compiler, limit))
 
         # check the head of the output for our magic string
         rev_var = ""
@@ -464,6 +501,19 @@ class ViewBuildPage(BuildFarmPage):
 
         yield "<div id='log'>"
 
+        yield "<p><a href='%s/+subunit'>Subunit output</a>" % build_uri(myself, build)
+        try:
+            previous_build = self.buildfarm.builds.get_previous_build(build.tree, build.host, build.compiler, build.revision)
+        except NoSuchBuildError:
+            pass
+        else:
+            yield ", <a href='%s/+subunit-diff/%s'>diff against previous</a>" % (
+                build_uri(myself, build), previous_build.log_checksum())
+        yield "</p>"
+        yield "<p><a href='%s/+stdout'>Standard output (as plain text)</a>, " % build_uri(myself, build)
+        yield "<a href='%s/+stderr'>Standard error (as plain text)</a>" % build_uri(myself, build)
+        yield "</p>"
+
         if not plain_logs:
             yield "<p>Switch to the <a href='%s?function=View+Build;host=%s;tree=%s"\
                   ";compiler=%s%s;plain=true' title='Switch to bland, non-javascript,"\
@@ -484,7 +534,7 @@ class ViewBuildPage(BuildFarmPage):
                 yield "<h2>Build log:</h2>\n"
                 yield print_log_pretty(log)
 
-            yield "<p><small>Some of the above icons derived from the <a href='http://www.gnome.org'>Gnome Project</a>'s stock icons.</small></p>"
+            yield "<p><small>Some of the above icons derived from the <a href='https://www.gnome.org'>Gnome Project</a>'s stock icons.</small></p>"
             yield "</div>"
         else:
             yield "<p>Switch to the <a href='%s?function=View+Build;host=%s;tree=%s;"\
@@ -511,6 +561,10 @@ class ViewRecentBuildsPage(BuildFarmPage):
         all_builds = []
 
         def build_platform(build):
+            host = self.buildfarm.hostdb[build.host]
+            return host.platform.encode("utf-8")
+
+        def build_platform_safe(build):
             try:
                 host = self.buildfarm.hostdb[build.host]
             except hostdb.NoSuchHost:
@@ -522,7 +576,7 @@ class ViewRecentBuildsPage(BuildFarmPage):
             "revision": lambda a, b: cmp(a.revision, b.revision),
             "age": lambda a, b: cmp(a.age, b.age),
             "host": lambda a, b: cmp(a.host, b.host),
-            "platform": lambda a, b: cmp(build_platform(a), build_platform(b)),
+            "platform": lambda a, b: cmp(build_platform_safe(a), build_platform_safe(b)),
             "compiler": lambda a, b: cmp(a.compiler, b.compiler),
             "status": lambda a, b: cmp(a.status(), b.status()),
             }
@@ -557,15 +611,19 @@ class ViewRecentBuildsPage(BuildFarmPage):
         yield "<tbody>"
 
         for build in all_builds:
-            yield "<tr>"
-            yield "<td>%s</td>" % util.dhm_time(build.age)
-            yield "<td>%s</td>" % revision_link(myself, build.revision, build.tree)
-            yield "<td>%s</td>" % build.tree
-            yield "<td>%s</td>" % build_platform(build)
-            yield "<td>%s</td>" % host_link(myself, build.host)
-            yield "<td>%s</td>" % build.compiler
-            yield "<td>%s</td>" % build_link(myself, build)
-            yield "</tr>"
+            try:
+                build_platform_name = build_platform(build)
+                yield "<tr>"
+                yield "<td>%s</td>" % util.dhm_time(build.age)
+                yield "<td>%s</td>" % revision_link(myself, build.revision, build.tree)
+                yield "<td>%s</td>" % build.tree
+                yield "<td>%s</td>" % build_platform_name
+                yield "<td>%s</td>" % host_link(myself, build.host)
+                yield "<td>%s</td>" % build.compiler
+                yield "<td>%s</td>" % build_link(myself, build)
+                yield "</tr>"
+            except hostdb.NoSuchHost:
+                pass
         yield "</tbody></table>"
         yield "</div>"
 
@@ -597,7 +655,6 @@ class ViewHostPage(BuildFarmPage):
             try:
                 host = self.buildfarm.hostdb[hostname]
             except hostdb.NoSuchHost:
-                deadhosts.append(hostname)
                 continue
             builds = list(self.buildfarm.get_host_builds(hostname))
             if len(builds) > 0:
@@ -653,7 +710,7 @@ class ViewHostPage(BuildFarmPage):
             try:
                 platform = self.buildfarm.hostdb[host].platform.encode("utf-8")
             except hostdb.NoSuchHost:
-                platform = "UNKNOWN"
+                continue
             yield "<tr><td>%s</td><td>%s</td><td>%s</td></tr>" %\
                     (host, platform, util.dhm_time(age))
 
@@ -672,16 +729,16 @@ class ViewSummaryPage(BuildFarmPage):
         # output when we want
         broken_table = ""
 
-        builds = self.buildfarm.get_last_builds()
+        builds = self.buildfarm.get_summary_builds()
 
-        for build in builds:
-            host_count[build.tree]+=1
-            status = build.status()
+        for tree, status_str in builds:
+            host_count[tree]+=1
+            status = BuildStatus.__deserialize__(status_str)
 
             if status.failed:
-                broken_count[build.tree]+=1
+                broken_count[tree]+=1
                 if "panic" in status.other_failures:
-                    panic_count[build.tree]+=1
+                    panic_count[tree]+=1
         return (host_count, broken_count, panic_count)
 
     def render_text(self, myself):
@@ -718,6 +775,7 @@ class ViewSummaryPage(BuildFarmPage):
             else:
                     yield "<td>"
             yield "%d</td>" % panic_count[tree]
+
             try:
                 lcov_status = self.buildfarm.lcov_status(tree)
             except NoSuchBuildError:
@@ -728,6 +786,17 @@ class ViewSummaryPage(BuildFarmPage):
                         self.buildfarm.LCOVHOST, tree, lcov_status)
                 else:
                     yield "<td></td>"
+
+            try:
+                unused_fns = self.buildfarm.unused_fns(tree)
+            except NoSuchBuildError:
+                yield "<td></td>"
+            else:
+                if unused_fns is not None:
+                    yield "<td><a href=\"/lcov/data/%s/%s/%s\">Unused Functions</a></td>" % (
+                        self.buildfarm.LCOVHOST, tree, unused_fns)
+                else:
+                    yield "<td></td>"
             yield "</tr>"
 
         yield "</tbody></table>"
@@ -800,7 +869,11 @@ class HistoryPage(BuildFarmPage):
 class DiffPage(HistoryPage):
 
     def render(self, myself, tree, revision):
-        t = self.buildfarm.trees[tree]
+        try:
+            t = self.buildfarm.trees[tree]
+        except KeyError:
+            yield "Unknown tree %s" % tree
+            return
         branch = t.get_branch()
         (entry, diff) = branch.diff(revision)
         # get information about the current diff
@@ -882,8 +955,8 @@ class BuildFarmApp(object):
         yield "    <meta name='description' contents='Home of the Samba Build Farm, the automated testing facility.'/>\n"
         yield "    <meta name='robots' contents='noindex'/>"
         yield "    <link rel='stylesheet' href='/build_farm.css' type='text/css' media='all'/>"
-        yield "    <link rel='stylesheet' href='http://master.samba.org/samba/style/common.css' type='text/css' media='all'/>"
-        yield "    <link rel='shortcut icon' href='http://www.samba.org/samba/images/favicon.ico'/>"
+        yield "    <link rel='stylesheet' href='//www.samba.org/samba/style/common.css' type='text/css' media='all'/>"
+        yield "    <link rel='shortcut icon' href='//www.samba.org/samba/images/favicon.ico'/>"
         yield "  </head>"
         yield "<body>"
 
@@ -918,7 +991,7 @@ class BuildFarmApp(object):
             start_response('200 OK', [('Content-type', 'text/plain')])
             page = ViewSummaryPage(self.buildfarm)
             yield "".join(page.render_text(myself))
-        else:
+        elif fn_name:
             start_response('200 OK', [
                 ('Content-type', 'text/html; charset=utf-8')])
 
@@ -955,33 +1028,97 @@ class BuildFarmApp(object):
                 revision = get_param(form, 'revision')
                 page = DiffPage(self.buildfarm)
                 yield "".join(self.html_page(form, page.render(myself, tree, revision)))
+            elif fn_name == "Summary":
+                page = ViewSummaryPage(self.buildfarm)
+                yield "".join(self.html_page(form, page.render_html(myself)))
             else:
-                fn = wsgiref.util.shift_path_info(environ)
-                if fn == "recent":
+                yield "Unknown function %s" % fn_name
+        else:
+            fn = wsgiref.util.shift_path_info(environ)
+            if fn == "tree":
+                tree = wsgiref.util.shift_path_info(environ)
+                subfn = wsgiref.util.shift_path_info(environ)
+                if subfn in ("", None, "+recent"):
+                    start_response('200 OK', [
+                        ('Content-type', 'text/html; charset=utf-8')])
                     page = ViewRecentBuildsPage(self.buildfarm)
-                    yield "".join(self.html_page(form, page.render(myself, wsgiref.util.shift_path_info(environ), get_param(form, 'sortby') or 'age')))
-                elif fn == "host":
-                    page = ViewHostPage(self.buildfarm)
-                    yield "".join(self.html_page(form, page.render_html(myself, wsgiref.util.shift_path_info(environ))))
-                elif fn == "build":
-                    build_checksum = wsgiref.util.shift_path_info(environ)
+                    yield "".join(self.html_page(form, page.render(myself, tree, get_param(form, 'sortby') or 'age')))
+                elif subfn == "+recent-ids":
+                    start_response('200 OK', [
+                        ('Content-type', 'text/plain; charset=utf-8')])
+                    yield "".join([x.log_checksum()+"\n" for x in self.buildfarm.get_tree_builds(tree) if x.has_log()])
+                else:
+                    start_response('200 OK', [
+                        ('Content-type', 'text/html; charset=utf-8')])
+                    yield "Unknown subfn %s" % subfn
+            elif fn == "host":
+                start_response('200 OK', [
+                    ('Content-type', 'text/html; charset=utf-8')])
+                page = ViewHostPage(self.buildfarm)
+                yield "".join(self.html_page(form, page.render_html(myself, wsgiref.util.shift_path_info(environ))))
+            elif fn == "build":
+                build_checksum = wsgiref.util.shift_path_info(environ)
+                try:
                     build = self.buildfarm.builds.get_by_checksum(build_checksum)
-                    page = ViewBuildPage(self.buildfarm)
-                    subfn = wsgiref.util.shift_path_info(environ)
-                    if subfn == "+plain":
-                        yield "".join(page.render(myself, build, True))
-                    elif subfn == "+subunit":
+                except NoSuchBuildError:
+                    start_response('404 Page Not Found', [
+                        ('Content-Type', 'text/html; charset=utf8')])
+                    yield "No build with checksum %s found" % build_checksum
+                    return
+                page = ViewBuildPage(self.buildfarm)
+                subfn = wsgiref.util.shift_path_info(environ)
+                if subfn == "+plain":
+                    start_response('200 OK', [
+                        ('Content-type', 'text/html; charset=utf-8')])
+                    yield "".join(page.render(myself, build, True))
+                elif subfn == "+subunit":
+                    start_response('200 OK', [
+                        ('Content-type', 'text/x-subunit; charset=utf-8'),
+                        ('Content-Disposition', 'attachment; filename="%s.%s.%s-%s.subunit"' % (build.tree, build.host, build.compiler, build.revision))])
+                    try:
+                        yield build.read_subunit().read()
+                    except NoTestOutput:
+                        yield "There was no test output"
+                elif subfn == "+stdout":
+                    start_response('200 OK', [
+                        ('Content-type', 'text/plain; charset=utf-8'),
+                        ('Content-Disposition', 'attachment; filename="%s.%s.%s-%s.log"' % (build.tree, build.host, build.compiler, build.revision))])
+                    yield build.read_log().read()
+                elif subfn == "+stderr":
+                    start_response('200 OK', [
+                        ('Content-type', 'text/plain; charset=utf-8'),
+                        ('Content-Disposition', 'attachment; filename="%s.%s.%s-%s.err"' % (build.tree, build.host, build.compiler, build.revision))])
+                    yield build.read_err().read()
+                elif subfn == "+subunit-diff":
+                    start_response('200 OK', [
+                        ('Content-type', 'text/plain; charset=utf-8')])
+                    subunit_this = build.read_subunit().readlines()
+                    other_build_checksum = wsgiref.util.shift_path_info(environ)
+                    other_build = self.buildfarm.builds.get_by_checksum(other_build_checksum)
+                    subunit_other = other_build.read_subunit().readlines()
+                    import difflib
+                    yield "".join(difflib.unified_diff(subunit_other, subunit_this))
+
+                elif subfn in ("", "limit", None):
+                    if subfn == "limit":
                         try:
-                            yield build.read_subunit().read()
-                        except NoTestOutput:
-                            yield "There was no test output"
-                    elif subfn == "":
-                        yield "".join(self.html_page(form, page.render(myself, build, False)))
-                elif fn == "":
-                    page = ViewSummaryPage(self.buildfarm)
-                    yield "".join(self.html_page(form, page.render_html(myself)))
-                else:
-                    yield "Unknown function %s" % fn
+                            limit = int(wsgiref.util.shift_path_info(environ))
+                        except:
+                            limit = 10
+                    else:
+                        limit = 10
+                    start_response('200 OK', [
+                        ('Content-type', 'text/html; charset=utf-8')])
+                    yield "".join(self.html_page(form, page.render(myself, build, False, limit)))
+            elif fn in ("", None):
+                start_response('200 OK', [
+                    ('Content-type', 'text/html; charset=utf-8')])
+                page = ViewSummaryPage(self.buildfarm)
+                yield "".join(self.html_page(form, page.render_html(myself)))
+            else:
+                start_response('404 Page Not Found', [
+                    ('Content-type', 'text/html; charset=utf-8')])
+                yield "Unknown function %s" % fn
 
 
 if __name__ == '__main__':