Revert "Reuse DEADAGE rather than defining our own constant."
[build-farm.git] / buildfarm / web / __init__.py
index ffaeb60507f13ecf8e40313f64a55c740aa3e32e..7ba04c519079a5bbbb59914fb2b0ae92cbeb21ed 100755 (executable)
@@ -1,7 +1,7 @@
 #!/usr/bin/python
 # This CGI script presents the results of the build_farm build
 
-# Copyright (C) Jelmer Vernooij <jelmer@samba.org>     2010
+# Copyright (C) Jelmer Vernooij <jelmer@samba.org>     2010-2014
 # Copyright (C) Matthieu Patou <mat@matws.net>         2010-2012
 #
 # Based on the original web/build.pl:
@@ -33,6 +33,7 @@
 
 from collections import defaultdict
 import os
+import sys
 
 from buildfarm import (
     hostdb,
@@ -42,7 +43,6 @@ from buildfarm.build import (
     LogFileMissing,
     NoSuchBuildError,
     NoTestOutput,
-    BuildStatus,
     )
 
 import cgi
@@ -55,9 +55,14 @@ import time
 import wsgiref.util
 webdir = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "web"))
 
-GITWEB_BASE = "https://gitweb.samba.org"
+GITWEB_BASE = "//gitweb.samba.org"
 HISTORY_HORIZON = 1000
 
+# Maximum age of builds to consider when displaying summary page statistics.
+# Note that trees only get rebuilt when they change, so this value is
+# intentionally set to a high value to cope with trees that don't change often.
+SUMMARY_MAX_BUILD_AGE = (180 * 24 * 60 * 60)
+
 # this is automatically filled in
 deadhosts = []
 
@@ -126,7 +131,8 @@ def build_uri(myself, build):
 
 
 def build_link(myself, build):
-    return "<a href='%s'>%s</a>" % (build_uri(myself, build), html_build_status(build.status()))
+    return "<a href='%s'>%s</a>" % (
+        build_uri(myself, build), html_build_status(build.status()))
 
 
 def tree_uri(myself, tree):
@@ -135,7 +141,8 @@ def tree_uri(myself, tree):
 
 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)
+    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):
@@ -405,7 +412,7 @@ class BuildFarmPage(object):
 
 class ViewBuildPage(BuildFarmPage):
 
-    def show_oldrevs(self, myself, build, host, compiler, limit):
+    def show_oldrevs(self, myself, build, host, compiler, limit=None):
         """show the available old revisions, if any"""
 
         tree = build.tree
@@ -416,15 +423,11 @@ class ViewBuildPage(BuildFarmPage):
 
         yield "<h2>Older builds:</h2>\n"
 
-        yield "<table class='real'>\n"
+        yield "<table class='newtable'>\n"
         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
+        for old_build in old_builds[:limit]:
             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),
@@ -475,7 +478,7 @@ class ViewBuildPage(BuildFarmPage):
         if os.path.exists(host_web_file):
             yield util.FileLoad(host_web_file)
 
-        yield "<table class='real'>\n"
+        yield "<table class='newtable'>\n"
         yield "<tr><td>Host:</td><td><a href='%s?function=View+Host;host=%s;tree=%s;"\
               "compiler=%s#'>%s</a> - %s</td></tr>\n" %\
                 (myself, build.host, build.tree, build.compiler, build.host, self.buildfarm.hostdb[build.host].platform.encode("utf-8"))
@@ -534,7 +537,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='https://www.gnome.org'>Gnome Project</a>'s stock icons.</small></p>"
+            yield "<p><small>Some of the above icons derived from the <a href='//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;"\
@@ -598,7 +601,7 @@ class ViewRecentBuildsPage(BuildFarmPage):
 
         yield "<div id='recent-builds' class='build-section'>"
         yield "<h2>Recent builds of %s (%s branch %s)</h2>" % (tree, t.scm, t.branch)
-        yield "<table class='real'>"
+        yield "<table class='newtable'>"
         yield "<thead>"
         yield "<tr>"
         yield "<th><a href='%s;sortby=age' title='Sort by build age'>Age</a></th>" % sorturl
@@ -631,10 +634,10 @@ class ViewRecentBuildsPage(BuildFarmPage):
 class ViewHostPage(BuildFarmPage):
 
     def _render_build_list_header(self, host):
-        yield "<div class='host summary'>"
+        yield "<div>"
         yield "<a id='host' name='host'/>"
-        yield "<h3>%s - %s</h3>" % (host.name, host.platform.encode("utf-8"))
-        yield "<table class='real'>"
+        yield "<h2>%s - %s</h2>" % (host.name, host.platform.encode("utf-8"))
+        yield "<table class='newtable'>"
         yield "<thead><tr><th>Target</th><th>Build<br/>Revision</th><th>Build<br />Age</th><th>Status<br />config/build<br />install/test</th><th>Warnings</th></tr></thead>"
         yield "<tbody>"
 
@@ -700,7 +703,7 @@ class ViewHostPage(BuildFarmPage):
 
         yield "<div class='build-section' id='dead-hosts'>"
         yield "<h2>Dead Hosts:</h2>"
-        yield "<table class='real'>"
+        yield "<table class='newtable'>"
         yield "<thead><tr><th>Host</th><th>OS</th><th>Min Age</th></tr></thead>"
         yield "<tbody>"
 
@@ -729,16 +732,15 @@ class ViewSummaryPage(BuildFarmPage):
         # output when we want
         broken_table = ""
 
-        builds = self.buildfarm.get_summary_builds()
+        builds = self.buildfarm.get_summary_builds(min_age=time.time() - SUMMARY_MAX_BUILD_AGE)
 
-        for build in builds:
-            host_count[build[0]]+=1
-            status = BuildStatus.__deserialize__(build[1])
+        for tree, status in builds:
+            host_count[tree]+=1
 
             if status.failed:
-                broken_count[build[0]]+=1
+                broken_count[tree]+=1
                 if "panic" in status.other_failures:
-                    panic_count[build[0]]+=1
+                    panic_count[tree]+=1
         return (host_count, broken_count, panic_count)
 
     def render_text(self, myself):
@@ -761,8 +763,8 @@ class ViewSummaryPage(BuildFarmPage):
 
         yield "<div id='build-counts' class='build-section'>"
         yield "<h2>Build counts:</h2>"
-        yield "<table class='real'>"
-        yield "<thead><tr><th>Tree</th><th>Total</th><th>Broken</th><th>Panic</th><th>Test coverage</th></tr></thead>"
+        yield "<table class='newtable'>"
+        yield "<thead><tr><th>Tree</th><th>Total</th><th>Broken</th><th>Panic</th><th>Test coverage</th><th></th></tr></thead>"
         yield "<tbody>"
 
         for tree in sorted(self.buildfarm.trees.keys()):
@@ -827,8 +829,8 @@ class HistoryPage(BuildFarmPage):
         <div class=\"diff\">
             <span class=\"html\"><a href=\"%s?function=diff;tree=%s;date=%s;%s\">show diffs</a></span>
         <br />
-            <span class=\"text\"><a href=\"%s?function=text_diff;tree=%s;date=%s;%s\">download diffs</a></span>
-            <br />
+            <span class=\"text\"><a href=\"%s?function=text_diff;tree=%s;date=%s;%s\">download diffs</a></span><br />
+            <span class=\"label\">Message:</span><br />
             <div class=\"history_log_message\">
                 <pre>%s</pre>
             </div>
@@ -860,8 +862,25 @@ class HistoryPage(BuildFarmPage):
         if builds:
             yield "<div class=\"builds\">\n"
             yield "<span class=\"label\">Builds: </span>\n"
+            yield "<table class=\"buildtable\">"
+            count=1
             for build in builds:
+                if count == 1:
+                    yield "<tr>"
+                yield "<td>"
                 yield "%s(%s) " % (build_link(myself, build), host_link(myself, build.host))
+                yield "</td>"
+                if count == 3:
+                    yield "</tr>"
+                    count = 1
+                else:
+                    count = count + 1
+            if count == 2:
+               yield "<td></td>"
+               yield "<td></td>"
+               yield "</tr>"
+               count = 1
+            yield "</table>"
             yield "</div>\n"
         yield "</div>\n"
 
@@ -883,19 +902,21 @@ class DiffPage(HistoryPage):
         changes = branch.changes_summary(revision)
         yield "".join(self.history_row_html(myself, entry, t, changes))
         diff = highlight(diff, DiffLexer(), HtmlFormatter())
-        yield "<pre>%s</pre>\n" % diff.encode("utf-8")
+        yield "<h2>Diff Result:</h2>"
+        yield "<pre>%s</pre>" % diff.encode("utf-8")
 
 
 class RecentCheckinsPage(HistoryPage):
 
-    limit = 40
+    limit = 10
 
-    def render(self, myself, tree, author=None):
+    def render(self, myself, tree, gitstart, author=None):
         t = self.buildfarm.trees[tree]
         interesting = list()
         authors = {"ALL": "ALL"}
         branch = t.get_branch()
         re_author = re.compile("^(.*) <(.*)>$")
+
         for entry in branch.log(limit=HISTORY_HORIZON):
             m = re_author.match(entry.author)
             authors[m.group(2)] = m.group(1)
@@ -905,46 +926,69 @@ class RecentCheckinsPage(HistoryPage):
         yield "<h2>Recent checkins for %s (%s branch %s)</h2>\n" % (
             tree, t.scm, t.branch)
         yield "<form method='GET'>"
+        yield "<div class='newform'>\n"
         yield "Select Author: "
         yield "".join(select(name="author", values=authors, default=author))
         yield "<input type='submit' name='sub_function' value='Refresh'/>"
         yield "<input type='hidden' name='tree' value='%s'/>" % tree
         yield "<input type='hidden' name='function', value='Recent Checkins'/>"
+        yield "</div>\n"
         yield "</form>"
 
-        for entry in interesting[:self.limit]:
+        gitstop = gitstart + self.limit
+
+        for entry in interesting[gitstart:gitstop]:
             changes = branch.changes_summary(entry.revision)
             yield "".join(self.history_row_html(myself, entry, t, changes))
         yield "\n"
 
+        yield "<form method='GET'>"
+        yield "<div class='newform'>\n"
+        if gitstart != 0:
+            yield "<button name='gitstart' type='submit' value=" + str(gitstart - self.limit) + " style='position:absolute;left:0px;'>Previous</button>"
+        if len(interesting) > gitstop:
+            yield "<button name='gitstart' type='submit' value=" + str(gitstop) + " style='position:absolute;right:0px;'>Next</button>"
+        yield "<input type='hidden' name='function', value='Recent Checkins'/>"
+        yield "<input type='hidden' name='gitcount' value='%s'/>" % gitstop
+        if author and author != "ALL":
+            yield "<input type='hidden' name='author' value='%s'/>" % author
+        yield "<input type='hidden' name='tree' value='%s'/>" % tree
+        yield "</div>\n"
+        yield "</form>"
+
 
 class BuildFarmApp(object):
 
     def __init__(self, buildfarm):
         self.buildfarm = buildfarm
 
-    def main_menu(self, tree, host, compiler):
+    def main_menu(self, tree, host, compiler, function):
         """main page"""
 
         yield "<form method='GET'>\n"
-        yield "<div id='build-menu'>\n"
+        yield "<div id='newbuildmenu'>\n"
         host_dict = {}
         for h in self.buildfarm.hostdb.hosts():
-            host_dict[h.name] = "%s -- %s" % (h.platform.encode("utf-8"), h.name)
+            host_dict[h.name] = "%s-%s" % (h.platform.encode("utf-8"), h.name)
         yield "".join(select("host", host_dict, default=host))
+        yield "<br/><br/>"
+
         tree_dict = {}
         for t in self.buildfarm.trees.values():
             tree_dict[t.name] = "%s:%s" % (t.name, t.branch)
         yield "".join(select("tree", tree_dict, default=tree))
+        yield "<br/><br/>"
         yield "".join(select("compiler", dict(zip(self.buildfarm.compilers, self.buildfarm.compilers)), default=compiler))
-        yield "<br/>\n"
-        yield "<input type='submit' name='function' value='View Build'/>\n"
-        yield "<input type='submit' name='function' value='View Host'/>\n"
-        yield "<input type='submit' name='function' value='Recent Checkins'/>\n"
-        yield "<input type='submit' name='function' value='Summary'/>\n"
-        yield "<input type='submit' name='function' value='Recent Builds'/>\n"
-        yield "</div>\n"
-        yield "</form>\n"
+        yield "<br/><br/>"
+        functions_dict = {
+            'View Build': 'View Build', 'Summary': 'Summary', 'View Host': 'View Host',
+            'Recent Builds': 'Recent Builds', 'Recent Checkins': 'Recent Checkins',
+            }
+        yield "".join(select("function", functions_dict, default=function))
+        yield "<br/><br/>"
+        yield "<br/><input type='submit' name='search' value='Go'/>"
+        yield "</div>"
+        yield "</form>"
 
     def html_page(self, form, lines):
         yield "<html>\n"
@@ -955,22 +999,29 @@ 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='//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 "    <link rel='shortcut icon' href='//www.samba.org/samba/style/2010/grey/favicon.ico'/>"
+        yield "    <link rel='stylesheet' type='text/css' media='screen,projection' href='//www.samba.org/samba/style/2010/grey/screen.css'/>"
+        yield "    <link rel='stylesheet' type='text/css' media='print' href='//www.samba.org/samba/style/2010/grey/print.css'/> "
         yield "  </head>"
         yield "<body>"
 
-        yield util.FileLoad(os.path.join(webdir, "header2.html"))
-
         tree = get_param(form, "tree")
         host = get_param(form, "host")
         compiler = get_param(form, "compiler")
-        yield "".join(self.main_menu(tree, host, compiler))
-        yield util.FileLoad(os.path.join(webdir, "header3.html"))
+        function = get_param(form, "function")
+        yield "".join(self.main_menu(tree, host, compiler, function))
+        yield util.FileLoad(os.path.join(webdir, "bannernav1.html"))
+        yield util.SambaWebFileLoad(os.path.join(webdir, "samba-web"), "menu_think_samba_closed.html")
+        yield util.SambaWebFileLoad(os.path.join(webdir, "samba-web"), "menu_get_samba_closed.html")
+        yield util.SambaWebFileLoad(os.path.join(webdir, "samba-web"), "menu_learn_samba_closed.html")
+        yield util.SambaWebFileLoad(os.path.join(webdir, "samba-web"), "menu_talk_samba_closed.html")
+        yield util.SambaWebFileLoad(os.path.join(webdir, "samba-web"), "menu_hack_samba_closed.html")
+        yield util.SambaWebFileLoad(os.path.join(webdir, "samba-web"), "menu_contact_samba_closed.html")
+        yield util.FileLoad(os.path.join(webdir, "bannernav2.html"))
         yield "".join(lines)
-        yield util.FileLoad(os.path.join(webdir, "footer.html"))
-        yield "</body>"
-        yield "</html>"
+        yield util.SambaWebFileLoad(os.path.join(webdir, "samba-web"), "footer.html")
+        yield util.FileLoad(os.path.join(webdir, "closingtags.html"))
 
     def __call__(self, environ, start_response):
         form = cgi.FieldStorage(fp=environ['wsgi.input'], environ=environ)
@@ -1022,8 +1073,13 @@ class BuildFarmApp(object):
             elif fn_name == "Recent_Checkins":
                 # validate the tree
                 author = get_param(form, 'author')
+                gitstart = get_param(form, 'gitstart')
+                if gitstart is None:
+                    gitstart = 0
+                else:
+                    gitstart = int(gitstart)
                 page = RecentCheckinsPage(self.buildfarm)
-                yield "".join(self.html_page(form, page.render(myself, tree, author)))
+                yield "".join(self.html_page(form, page.render(myself, tree, gitstart, author)))
             elif fn_name == "diff":
                 revision = get_param(form, 'revision')
                 page = DiffPage(self.buildfarm)
@@ -1056,6 +1112,16 @@ class BuildFarmApp(object):
                     ('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 == "about":
+                start_response('200 OK', [
+                    ('Content-type', 'text/html; charset=utf-8')])
+                lines = util.FileLoad(os.path.join(webdir, "about.html"))
+                yield "".join(self.html_page(form, lines))
+            elif fn == "instructions":
+                start_response('200 OK', [
+                    ('Content-type', 'text/html; charset=utf-8')])
+                lines = util.FileLoad(os.path.join(webdir, "instructions.html"))
+                yield "".join(self.html_page(form, lines))
             elif fn == "build":
                 build_checksum = wsgiref.util.shift_path_info(environ)
                 try:
@@ -1124,6 +1190,8 @@ class BuildFarmApp(object):
 if __name__ == '__main__':
     import optparse
     parser = optparse.OptionParser("[options]")
+    parser.add_option("--debug-storm", help="Enable storm debugging",
+                      default=False, action='store_true')
     parser.add_option("--port", help="Port to listen on [localhost:8000]",
         default="localhost:8000", type=str)
     opts, args = parser.parse_args()
@@ -1151,6 +1219,9 @@ if __name__ == '__main__':
     except ValueError:
         address = "localhost"
         port = opts.port
+    if opts.debug_storm:
+        from storm.tracer import debug
+        debug(True, stream=sys.stdout)
     httpd = make_server(address, int(port), standaloneApp)
     print "Serving on %s:%d..." % (address, int(port))
     httpd.serve_forever()