Fix image urls.
[amitay/build-farm.git] / buildfarm / web / __init__.py
index f4e284bab50da745adef8b2032cefdbe34cab709..f58cb9a699cb98ee8a3066a93870706c4975cde8 100755 (executable)
@@ -41,6 +41,7 @@ from buildfarm import (
 from buildfarm.build import (
     LogFileMissing,
     NoSuchBuildError,
+    NoTestOutput,
     )
 
 import cgi
@@ -120,33 +121,39 @@ def html_build_status(status):
 
 
 def build_uri(myself, build):
-    params = {
-        "host": build.host,
-        "tree": build.tree,
-        "compiler": build.compiler,
-        "checksum": build.log_checksum(),
-        }
-    if build.revision:
-        params["revision"] = build.revision
-    return "%s?function=View+Build;%s" % (myself, ";".join(["%s=%s" % k for k in params.iteritems()]))
+    return "%s/build/%s" % (myself, build.log_checksum())
 
 
 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)
 
 
+def revision_uri(myself, revision, tree):
+    return "%s?function=diff;tree=%s;revision=%s" % (myself, tree, revision)
+
+
 def revision_link(myself, revision, tree):
     """return a link to a particular revision"""
     if revision is None:
         return "unknown"
-    return "<a href='%s?function=diff;tree=%s;revision=%s' title='View Diff for %s'>%s</a>" % (myself, tree, revision, revision, revision[:7])
+    return "<a href='%s' title='View Diff for %s'>%s</a>" % (
+        revision_uri(myself, revision, tree), revision, revision[:7])
 
 
 def subunit_to_buildfarm_result(subunit_result):
@@ -306,9 +313,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()
@@ -364,13 +371,6 @@ 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 render(self, output_type):
         raise NotImplementedError(self.render)
@@ -399,20 +399,13 @@ class ViewBuildPage(BuildFarmPage):
 
         yield "</tbody></table>\n"
 
-    def render(self, myself, tree, host, compiler, rev, checksum=None,
-            plain_logs=False):
+    def render(self, myself, build, plain_logs=False):
         """view one build in detail"""
 
         uname = None
         cflags = None
         config = None
-        try:
-            build = self.buildfarm.get_build(tree, host, compiler, rev,
-                checksum=checksum)
-        except NoSuchBuildError:
-            yield "No such build: %s on %s with %s, rev %r, checksum %r" % (
-                tree, host, compiler, rev, checksum)
-            return
+
         try:
             f = build.read_log()
             try:
@@ -443,40 +436,45 @@ class ViewBuildPage(BuildFarmPage):
         err = cgi.escape(err)
         yield '<h2>Host information:</h2>'
 
-        host_web_file = "../web/%s.html" % host
+        host_web_file = "../web/%s.html" % build.host
         if os.path.exists(host_web_file):
             yield util.FileLoad(host_web_file)
 
         yield "<table class='real'>\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, host, tree, compiler, host, self.buildfarm.hostdb[host].platform.encode("utf-8"))
+                (myself, build.host, build.tree, build.compiler, build.host, self.buildfarm.hostdb[build.host].platform.encode("utf-8"))
         if uname is not None:
             yield "<tr><td>Uname:</td><td>%s</td></tr>\n" % uname
-        yield "<tr><td>Tree:</td><td>%s</td></tr>\n" % self.tree_link(myself, tree)
-        yield "<tr><td>Build Revision:</td><td>%s</td></tr>\n" % revision_link(myself, build.revision, tree)
+        yield "<tr><td>Tree:</td><td>%s</td></tr>\n" % tree_link(myself, self.buildfarm.trees[build.tree])
+        yield "<tr><td>Build Revision:</td><td>%s</td></tr>\n" % revision_link(myself, build.revision, build.tree)
         yield "<tr><td>Build age:</td><td><div class='age'>%s</div></td></tr>\n" % self.red_age(build.age)
         yield "<tr><td>Status:</td><td>%s</td></tr>\n" % build_link(myself, build)
-        yield "<tr><td>Compiler:</td><td>%s</td></tr>\n" % compiler
+        yield "<tr><td>Compiler:</td><td>%s</td></tr>\n" % build.compiler
         if cflags is not None:
             yield "<tr><td>CFLAGS:</td><td>%s</td></tr>\n" % cflags
         if config is not None:
             yield "<tr><td>configure options:</td><td>%s</td></tr>\n" % config
         yield "</table>\n"
 
-        yield "".join(self.show_oldrevs(myself, tree, host, compiler))
+        yield "".join(self.show_oldrevs(myself, build.tree, build.host, build.compiler))
 
         # check the head of the output for our magic string
         rev_var = ""
-        if rev:
-            rev_var = ";revision=%s" % rev
+        if build.revision:
+            rev_var = ";revision=%s" % build.revision
 
         yield "<div id='log'>"
 
+        yield "<p><a href='%s/+subunit'>Subunit output</a></p>" % build_uri(myself, build)
+        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,"\
-                  " unstyled view'>Plain View</a></p>" % (myself, host, tree, compiler, rev_var)
+                  " unstyled view'>Plain View</a></p>" % (myself, build.host, build.tree, build.compiler, rev_var)
 
             yield "<div id='actionList'>"
             # These can be pretty wide -- perhaps we need to
@@ -498,7 +496,7 @@ class ViewBuildPage(BuildFarmPage):
         else:
             yield "<p>Switch to the <a href='%s?function=View+Build;host=%s;tree=%s;"\
                   "compiler=%s%s' title='Switch to colourful, javascript-enabled, styled"\
-                  " view'>Enhanced View</a></p>" % (myself, host, tree, compiler, rev_var)
+                  " view'>Enhanced View</a></p>" % (myself, build.host, build.tree, build.compiler, rev_var)
             if err == "":
                 yield "<h2>No error log available</h2>"
             else:
@@ -592,7 +590,7 @@ class ViewHostPage(BuildFarmPage):
     def _render_build_html(self, myself, build):
         warnings = build.err_count()
         yield "<tr>"
-        yield "<td><span class='tree'>" + self.tree_link(myself, build.tree) +"</span>/" + build.compiler + "</td>"
+        yield "<td><span class='tree'>" + tree_link(myself, self.buildfarm.trees[build.tree]) +"</span>/" + build.compiler + "</td>"
         yield "<td>" + revision_link(myself, build.revision, build.tree) + "</td>"
         yield "<td><div class='age'>" + self.red_age(build.age) + "</div></td>"
         yield "<td><div class='status'>%s</div></td>" % build_link(myself, build)
@@ -719,7 +717,7 @@ class ViewSummaryPage(BuildFarmPage):
 
         for tree in sorted(self.buildfarm.trees.keys()):
             yield "<tr>"
-            yield "<td>%s</td>" % self.tree_link(myself, tree)
+            yield "<td>%s</td>" % tree_link(myself, self.buildfarm.trees[tree])
             yield "<td>%s</td>" % host_count[tree]
             yield "<td>%s</td>" % broken_count[tree]
             if panic_count[tree]:
@@ -882,6 +880,32 @@ class BuildFarmApp(object):
         yield "</div>\n"
         yield "</form>\n"
 
+    def html_page(self, form, lines):
+        yield "<html>\n"
+        yield "  <head>\n"
+        yield "    <title>samba.org build farm</title>\n"
+        yield "    <script language='javascript' src='/build_farm.js'></script>\n"
+        yield "    <meta name='keywords' contents='Samba SMB CIFS Build Farm'/>\n"
+        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 "  </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"))
+        yield "".join(lines)
+        yield util.FileLoad(os.path.join(webdir, "footer.html"))
+        yield "</body>"
+        yield "</html>"
+
     def __call__(self, environ, start_response):
         form = cgi.FieldStorage(fp=environ['wsgi.input'], environ=environ)
         fn_name = get_param(form, 'function') or ''
@@ -901,64 +925,105 @@ 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')])
 
-            yield "<html>\n"
-            yield "  <head>\n"
-            yield "    <title>samba.org build farm</title>\n"
-            yield "    <script language='javascript' src='/build_farm.js'></script>\n"
-            yield "    <meta name='keywords' contents='Samba SMB CIFS Build Farm'/>\n"
-            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 "  </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"))
+
             if fn_name == "View_Build":
                 plain_logs = (get_param(form, "plain") is not None and get_param(form, "plain").lower() in ("yes", "1", "on", "true", "y"))
                 revision = get_param(form, "revision")
                 checksum = get_param(form, "checksum")
-                page = ViewBuildPage(self.buildfarm)
-                yield "".join(page.render(myself, tree, host, compiler, revision, checksum, plain_logs))
+                try:
+                    build = self.buildfarm.get_build(tree, host,
+                        compiler, revision, checksum=checksum)
+                except NoSuchBuildError:
+                    yield "No such build: %s on %s with %s, rev %r, checksum %r" % (
+                        tree, host, compiler, revision, checksum)
+                else:
+                    page = ViewBuildPage(self.buildfarm)
+                    plain_logs = (get_param(form, "plain") is not None and get_param(form, "plain").lower() in ("yes", "1", "on", "true", "y"))
+                    yield "".join(self.html_page(form, page.render(myself, build, plain_logs)))
             elif fn_name == "View_Host":
                 page = ViewHostPage(self.buildfarm)
-                yield "".join(page.render_html(myself, get_param(form, 'host')))
+                yield "".join(self.html_page(form, page.render_html(myself, get_param(form, 'host'))))
             elif fn_name == "Recent_Builds":
                 page = ViewRecentBuildsPage(self.buildfarm)
-                yield "".join(page.render(myself, get_param(form, "tree"), get_param(form, "sortby") or "age"))
+                yield "".join(self.html_page(form, page.render(myself, get_param(form, "tree"), get_param(form, "sortby") or "age")))
             elif fn_name == "Recent_Checkins":
                 # validate the tree
                 author = get_param(form, 'author')
                 page = RecentCheckinsPage(self.buildfarm)
-                yield "".join(page.render(myself, tree, author))
+                yield "".join(self.html_page(form, page.render(myself, tree, author)))
             elif fn_name == "diff":
                 revision = get_param(form, 'revision')
                 page = DiffPage(self.buildfarm)
-                yield "".join(page.render(myself, tree, revision))
-            elif os.getenv("PATH_INFO") not in (None, "", "/"):
-                paths = os.getenv("PATH_INFO").split('/')
-                if paths[1] == "recent":
-                    page = ViewRecentBuildsPage(self.buildfarm)
-                    yield "".join(page.render(myself, paths[2], get_param(form, 'sortby') or 'age'))
-                elif paths[1] == "host":
-                    page = ViewHostPage(self.buildfarm)
-                    yield "".join(page.render_html(myself, paths[2]))
+                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:
+                yield "Unknown function %s" % fn_name
+        else:
+            fn = wsgiref.util.shift_path_info(environ)
+            if fn == "tree":
+                start_response('200 OK', [
+                    ('Content-type', 'text/html; charset=utf-8')])
+                tree = wsgiref.util.shift_path_info(environ)
+                subfn = wsgiref.util.shift_path_info(environ)
+                if subfn in ("", None, "+recent"):
+                    page = ViewRecentBuildsPage(self.buildfarm)
+                    yield "".join(self.html_page(form, page.render(myself, tree, get_param(form, 'sortby') or 'age')))
+                else:
+                    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)
+                build = self.buildfarm.builds.get_by_checksum(build_checksum)
+                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 in ("", None):
+                    start_response('200 OK', [
+                        ('Content-type', 'text/html; charset=utf-8')])
+                    yield "".join(self.html_page(form, page.render(myself, build, False)))
+            elif fn in ("", None):
+                start_response('200 OK', [
+                    ('Content-type', 'text/html; charset=utf-8')])
                 page = ViewSummaryPage(self.buildfarm)
-                yield "".join(page.render_html(myself))
-            yield util.FileLoad(os.path.join(webdir, "footer.html"))
-            yield "</body>"
-            yield "</html>"
+                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__':
@@ -967,8 +1032,8 @@ if __name__ == '__main__':
     parser.add_option("--port", help="Port to listen on [localhost:8000]",
         default="localhost:8000", type=str)
     opts, args = parser.parse_args()
-    from buildfarm.sqldb import StormCachingBuildFarm
-    buildfarm = StormCachingBuildFarm()
+    from buildfarm import BuildFarm
+    buildfarm = BuildFarm()
     buildApp = BuildFarmApp(buildfarm)
     from wsgiref.simple_server import make_server
     import mimetypes