efi: stub: Move the use_uart assignment immediately after exit_boot_services() call
[oweals/u-boot.git] / test / py / multiplexed_log.py
index c2a3b89536430fc0c911f83aae44a8783a1caef2..f23d5dec68cd3b8c17e83c09b4820d8d4900fb0f 100644 (file)
@@ -1,12 +1,12 @@
+# SPDX-License-Identifier: GPL-2.0
 # Copyright (c) 2015 Stephen Warren
 # Copyright (c) 2015-2016, NVIDIA CORPORATION. All rights reserved.
 # Copyright (c) 2015 Stephen Warren
 # Copyright (c) 2015-2016, NVIDIA CORPORATION. All rights reserved.
-#
-# SPDX-License-Identifier: GPL-2.0
 
 # Generate an HTML-formatted log file containing multiple streams of data,
 # each represented in a well-delineated/-structured fashion.
 
 import cgi
 
 # Generate an HTML-formatted log file containing multiple streams of data,
 # each represented in a well-delineated/-structured fashion.
 
 import cgi
+import datetime
 import os.path
 import shutil
 import subprocess
 import os.path
 import shutil
 import subprocess
@@ -101,6 +101,8 @@ class RunAndLog(object):
         self.logfile = logfile
         self.name = name
         self.chained_file = chained_file
         self.logfile = logfile
         self.name = name
         self.chained_file = chained_file
+        self.output = None
+        self.exit_status = None
 
     def close(self):
         """Clean up any resources managed by this object."""
 
     def close(self):
         """Clean up any resources managed by this object."""
@@ -109,6 +111,9 @@ class RunAndLog(object):
     def run(self, cmd, cwd=None, ignore_errors=False):
         """Run a command as a sub-process, and log the results.
 
     def run(self, cmd, cwd=None, ignore_errors=False):
         """Run a command as a sub-process, and log the results.
 
+        The output is available at self.output which can be useful if there is
+        an exception.
+
         Args:
             cmd: The command to execute.
             cwd: The directory to run the command in. Can be None to use the
         Args:
             cmd: The command to execute.
             cwd: The directory to run the command in. Can be None to use the
@@ -119,10 +124,10 @@ class RunAndLog(object):
                 raised if such problems occur.
 
         Returns:
                 raised if such problems occur.
 
         Returns:
-            Nothing.
+            The output as a string.
         """
 
         """
 
-        msg = "+" + " ".join(cmd) + "\n"
+        msg = '+' + ' '.join(cmd) + '\n'
         if self.chained_file:
             self.chained_file.write(msg)
         self.logfile.write(self, msg)
         if self.chained_file:
             self.chained_file.write(msg)
         self.logfile.write(self, msg)
@@ -159,8 +164,14 @@ class RunAndLog(object):
         self.logfile.write(self, output)
         if self.chained_file:
             self.chained_file.write(output)
         self.logfile.write(self, output)
         if self.chained_file:
             self.chained_file.write(output)
+        self.logfile.timestamp()
+
+        # Store the output so it can be accessed if we raise an exception.
+        self.output = output
+        self.exit_status = exit_status
         if exception:
             raise exception
         if exception:
             raise exception
+        return output
 
 class SectionCtxMgr(object):
     """A context manager for Python's "with" statement, which allows a certain
 
 class SectionCtxMgr(object):
     """A context manager for Python's "with" statement, which allows a certain
@@ -168,12 +179,13 @@ class SectionCtxMgr(object):
     Objects of this type should be created by factory functions in the Logfile
     class rather than directly."""
 
     Objects of this type should be created by factory functions in the Logfile
     class rather than directly."""
 
-    def __init__(self, log, marker):
+    def __init__(self, log, marker, anchor):
         """Initialize a new object.
 
         Args:
             log: The Logfile object to log to.
             marker: The name of the nested log section.
         """Initialize a new object.
 
         Args:
             log: The Logfile object to log to.
             marker: The name of the nested log section.
+            anchor: The anchor value to pass to start_section().
 
         Returns:
             Nothing.
 
         Returns:
             Nothing.
@@ -181,9 +193,10 @@ class SectionCtxMgr(object):
 
         self.log = log
         self.marker = marker
 
         self.log = log
         self.marker = marker
+        self.anchor = anchor
 
     def __enter__(self):
 
     def __enter__(self):
-        self.log.start_section(self.marker)
+        self.anchor = self.log.start_section(self.marker, self.anchor)
 
     def __exit__(self, extype, value, traceback):
         self.log.end_section(self.marker)
 
     def __exit__(self, extype, value, traceback):
         self.log.end_section(self.marker)
@@ -202,19 +215,83 @@ class Logfile(object):
             Nothing.
         """
 
             Nothing.
         """
 
-        self.f = open(fn, "wt")
+        self.f = open(fn, 'wt')
         self.last_stream = None
         self.blocks = []
         self.cur_evt = 1
         self.last_stream = None
         self.blocks = []
         self.cur_evt = 1
-        shutil.copy(mod_dir + "/multiplexed_log.css", os.path.dirname(fn))
-        self.f.write("""\
+        self.anchor = 0
+        self.timestamp_start = self._get_time()
+        self.timestamp_prev = self.timestamp_start
+        self.timestamp_blocks = []
+        self.seen_warning = False
+
+        shutil.copy(mod_dir + '/multiplexed_log.css', os.path.dirname(fn))
+        self.f.write('''\
 <html>
 <head>
 <link rel="stylesheet" type="text/css" href="multiplexed_log.css">
 <html>
 <head>
 <link rel="stylesheet" type="text/css" href="multiplexed_log.css">
+<script src="http://code.jquery.com/jquery.min.js"></script>
+<script>
+$(document).ready(function () {
+    // Copy status report HTML to start of log for easy access
+    sts = $(".block#status_report")[0].outerHTML;
+    $("tt").prepend(sts);
+
+    // Add expand/contract buttons to all block headers
+    btns = "<span class=\\\"block-expand hidden\\\">[+] </span>" +
+        "<span class=\\\"block-contract\\\">[-] </span>";
+    $(".block-header").prepend(btns);
+
+    // Pre-contract all blocks which passed, leaving only problem cases
+    // expanded, to highlight issues the user should look at.
+    // Only top-level blocks (sections) should have any status
+    passed_bcs = $(".block-content:has(.status-pass)");
+    // Some blocks might have multiple status entries (e.g. the status
+    // report), so take care not to hide blocks with partial success.
+    passed_bcs = passed_bcs.not(":has(.status-fail)");
+    passed_bcs = passed_bcs.not(":has(.status-xfail)");
+    passed_bcs = passed_bcs.not(":has(.status-xpass)");
+    passed_bcs = passed_bcs.not(":has(.status-skipped)");
+    passed_bcs = passed_bcs.not(":has(.status-warning)");
+    // Hide the passed blocks
+    passed_bcs.addClass("hidden");
+    // Flip the expand/contract button hiding for those blocks.
+    bhs = passed_bcs.parent().children(".block-header")
+    bhs.children(".block-expand").removeClass("hidden");
+    bhs.children(".block-contract").addClass("hidden");
+
+    // Add click handler to block headers.
+    // The handler expands/contracts the block.
+    $(".block-header").on("click", function (e) {
+        var header = $(this);
+        var content = header.next(".block-content");
+        var expanded = !content.hasClass("hidden");
+        if (expanded) {
+            content.addClass("hidden");
+            header.children(".block-expand").first().removeClass("hidden");
+            header.children(".block-contract").first().addClass("hidden");
+        } else {
+            header.children(".block-contract").first().removeClass("hidden");
+            header.children(".block-expand").first().addClass("hidden");
+            content.removeClass("hidden");
+        }
+    });
+
+    // When clicking on a link, expand the target block
+    $("a").on("click", function (e) {
+        var block = $($(this).attr("href"));
+        var header = block.children(".block-header");
+        var content = block.children(".block-content").first();
+        header.children(".block-contract").first().removeClass("hidden");
+        header.children(".block-expand").first().addClass("hidden");
+        content.removeClass("hidden");
+    });
+});
+</script>
 </head>
 <body>
 <tt>
 </head>
 <body>
 <tt>
-""")
+''')
 
     def close(self):
         """Close the log file.
 
     def close(self):
         """Close the log file.
@@ -228,17 +305,17 @@ class Logfile(object):
             Nothing.
         """
 
             Nothing.
         """
 
-        self.f.write("""\
+        self.f.write('''\
 </tt>
 </body>
 </html>
 </tt>
 </body>
 </html>
-""")
+''')
         self.f.close()
 
     # The set of characters that should be represented as hexadecimal codes in
     # the log file.
         self.f.close()
 
     # The set of characters that should be represented as hexadecimal codes in
     # the log file.
-    _nonprint = ("%" + "".join(chr(c) for c in range(0, 32) if c not in (9, 10)) +
-                 "".join(chr(c) for c in range(127, 256)))
+    _nonprint = ('%' + ''.join(chr(c) for c in range(0, 32) if c not in (9, 10)) +
+                 ''.join(chr(c) for c in range(127, 256)))
 
     def _escape(self, data):
         """Render data format suitable for inclusion in an HTML document.
 
     def _escape(self, data):
         """Render data format suitable for inclusion in an HTML document.
@@ -253,8 +330,8 @@ class Logfile(object):
             An escaped version of the data.
         """
 
             An escaped version of the data.
         """
 
-        data = data.replace(chr(13), "")
-        data = "".join((c in self._nonprint) and ("%%%02x" % ord(c)) or
+        data = data.replace(chr(13), '')
+        data = ''.join((c in self._nonprint) and ('%%%02x' % ord(c)) or
                        c for c in data)
         data = cgi.escape(data)
         return data
                        c for c in data)
         data = cgi.escape(data)
         return data
@@ -272,46 +349,63 @@ class Logfile(object):
         self.cur_evt += 1
         if not self.last_stream:
             return
         self.cur_evt += 1
         if not self.last_stream:
             return
-        self.f.write("</pre>\n")
-        self.f.write("<div class=\"stream-trailer\" id=\"" +
-                     self.last_stream.name + "\">End stream: " +
-                     self.last_stream.name + "</div>\n")
-        self.f.write("</div>\n")
+        self.f.write('</pre>\n')
+        self.f.write('<div class="stream-trailer block-trailer">End stream: ' +
+                     self.last_stream.name + '</div>\n')
+        self.f.write('</div>\n')
+        self.f.write('</div>\n')
         self.last_stream = None
 
         self.last_stream = None
 
-    def _note(self, note_type, msg):
+    def _note(self, note_type, msg, anchor=None):
         """Write a note or one-off message to the log file.
 
         Args:
             note_type: The type of note. This must be a value supported by the
                 accompanying multiplexed_log.css.
             msg: The note/message to log.
         """Write a note or one-off message to the log file.
 
         Args:
             note_type: The type of note. This must be a value supported by the
                 accompanying multiplexed_log.css.
             msg: The note/message to log.
+            anchor: Optional internal link target.
 
         Returns:
             Nothing.
         """
 
         self._terminate_stream()
 
         Returns:
             Nothing.
         """
 
         self._terminate_stream()
-        self.f.write("<div class=\"" + note_type + "\">\n<pre>")
+        self.f.write('<div class="' + note_type + '">\n')
+        self.f.write('<pre>')
+        if anchor:
+            self.f.write('<a href="#%s">' % anchor)
         self.f.write(self._escape(msg))
         self.f.write(self._escape(msg))
-        self.f.write("\n</pre></div>\n")
+        if anchor:
+            self.f.write('</a>')
+        self.f.write('\n</pre>\n')
+        self.f.write('</div>\n')
 
 
-    def start_section(self, marker):
+    def start_section(self, marker, anchor=None):
         """Begin a new nested section in the log file.
 
         Args:
             marker: The name of the section that is starting.
         """Begin a new nested section in the log file.
 
         Args:
             marker: The name of the section that is starting.
+            anchor: The value to use for the anchor. If None, a unique value
+              will be calculated and used
 
         Returns:
 
         Returns:
-            Nothing.
+            Name of the HTML anchor emitted before section.
         """
 
         self._terminate_stream()
         self.blocks.append(marker)
         """
 
         self._terminate_stream()
         self.blocks.append(marker)
-        blk_path = "/".join(self.blocks)
-        self.f.write("<div class=\"section\" id=\"" + blk_path + "\">\n")
-        self.f.write("<div class=\"section-header\" id=\"" + blk_path +
-                     "\">Section: " + blk_path + "</div>\n")
+        self.timestamp_blocks.append(self._get_time())
+        if not anchor:
+            self.anchor += 1
+            anchor = str(self.anchor)
+        blk_path = '/'.join(self.blocks)
+        self.f.write('<div class="section block" id="' + anchor + '">\n')
+        self.f.write('<div class="section-header block-header">Section: ' +
+                     blk_path + '</div>\n')
+        self.f.write('<div class="section-content block-content">\n')
+        self.timestamp()
+
+        return anchor
 
     def end_section(self, marker):
         """Terminate the current nested section in the log file.
 
     def end_section(self, marker):
         """Terminate the current nested section in the log file.
@@ -327,16 +421,22 @@ class Logfile(object):
         """
 
         if (not self.blocks) or (marker != self.blocks[-1]):
         """
 
         if (not self.blocks) or (marker != self.blocks[-1]):
-            raise Exception("Block nesting mismatch: \"%s\" \"%s\"" %
-                            (marker, "/".join(self.blocks)))
+            raise Exception('Block nesting mismatch: "%s" "%s"' %
+                            (marker, '/'.join(self.blocks)))
         self._terminate_stream()
         self._terminate_stream()
-        blk_path = "/".join(self.blocks)
-        self.f.write("<div class=\"section-trailer\" id=\"section-trailer-" +
-                     blk_path + "\">End section: " + blk_path + "</div>\n")
-        self.f.write("</div>\n")
+        timestamp_now = self._get_time()
+        timestamp_section_start = self.timestamp_blocks.pop()
+        delta_section = timestamp_now - timestamp_section_start
+        self._note("timestamp",
+            "TIME: SINCE-SECTION: " + str(delta_section))
+        blk_path = '/'.join(self.blocks)
+        self.f.write('<div class="section-trailer block-trailer">' +
+                     'End section: ' + blk_path + '</div>\n')
+        self.f.write('</div>\n')
+        self.f.write('</div>\n')
         self.blocks.pop()
 
         self.blocks.pop()
 
-    def section(self, marker):
+    def section(self, marker, anchor=None):
         """Create a temporary section in the log file.
 
         This function creates a context manager for Python's "with" statement,
         """Create a temporary section in the log file.
 
         This function creates a context manager for Python's "with" statement,
@@ -349,12 +449,13 @@ class Logfile(object):
 
         Args:
             marker: The name of the nested section.
 
         Args:
             marker: The name of the nested section.
+            anchor: The anchor value to pass to start_section().
 
         Returns:
             A context manager object.
         """
 
 
         Returns:
             A context manager object.
         """
 
-        return SectionCtxMgr(self, marker)
+        return SectionCtxMgr(self, marker, anchor)
 
     def error(self, msg):
         """Write an error note to the log file.
 
     def error(self, msg):
         """Write an error note to the log file.
@@ -378,8 +479,23 @@ class Logfile(object):
             Nothing.
         """
 
             Nothing.
         """
 
+        self.seen_warning = True
         self._note("warning", msg)
 
         self._note("warning", msg)
 
+    def get_and_reset_warning(self):
+        """Get and reset the log warning flag.
+
+        Args:
+            None
+
+        Returns:
+            Whether a warning was seen since the last call.
+        """
+
+        ret = self.seen_warning
+        self.seen_warning = False
+        return ret
+
     def info(self, msg):
         """Write an informational note to the log file.
 
     def info(self, msg):
         """Write an informational note to the log file.
 
@@ -404,41 +520,108 @@ class Logfile(object):
 
         self._note("action", msg)
 
 
         self._note("action", msg)
 
-    def status_pass(self, msg):
+    def _get_time(self):
+        return datetime.datetime.now()
+
+    def timestamp(self):
+        """Write a timestamp to the log file.
+
+        Args:
+            None
+
+        Returns:
+            Nothing.
+        """
+
+        timestamp_now = self._get_time()
+        delta_prev = timestamp_now - self.timestamp_prev
+        delta_start = timestamp_now - self.timestamp_start
+        self.timestamp_prev = timestamp_now
+
+        self._note("timestamp",
+            "TIME: NOW: " + timestamp_now.strftime("%Y/%m/%d %H:%M:%S.%f"))
+        self._note("timestamp",
+            "TIME: SINCE-PREV: " + str(delta_prev))
+        self._note("timestamp",
+            "TIME: SINCE-START: " + str(delta_start))
+
+    def status_pass(self, msg, anchor=None):
+        """Write a note to the log file describing test(s) which passed.
+
+        Args:
+            msg: A message describing the passed test(s).
+            anchor: Optional internal link target.
+
+        Returns:
+            Nothing.
+        """
+
+        self._note("status-pass", msg, anchor)
+
+    def status_warning(self, msg, anchor=None):
         """Write a note to the log file describing test(s) which passed.
 
         Args:
         """Write a note to the log file describing test(s) which passed.
 
         Args:
-            msg: A message describing passed test(s).
+            msg: A message describing the passed test(s).
+            anchor: Optional internal link target.
 
         Returns:
             Nothing.
         """
 
 
         Returns:
             Nothing.
         """
 
-        self._note("status-pass", msg)
+        self._note("status-warning", msg, anchor)
 
 
-    def status_skipped(self, msg):
+    def status_skipped(self, msg, anchor=None):
         """Write a note to the log file describing skipped test(s).
 
         Args:
         """Write a note to the log file describing skipped test(s).
 
         Args:
-            msg: A message describing passed test(s).
+            msg: A message describing the skipped test(s).
+            anchor: Optional internal link target.
+
+        Returns:
+            Nothing.
+        """
+
+        self._note("status-skipped", msg, anchor)
+
+    def status_xfail(self, msg, anchor=None):
+        """Write a note to the log file describing xfailed test(s).
+
+        Args:
+            msg: A message describing the xfailed test(s).
+            anchor: Optional internal link target.
+
+        Returns:
+            Nothing.
+        """
+
+        self._note("status-xfail", msg, anchor)
+
+    def status_xpass(self, msg, anchor=None):
+        """Write a note to the log file describing xpassed test(s).
+
+        Args:
+            msg: A message describing the xpassed test(s).
+            anchor: Optional internal link target.
 
         Returns:
             Nothing.
         """
 
 
         Returns:
             Nothing.
         """
 
-        self._note("status-skipped", msg)
+        self._note("status-xpass", msg, anchor)
 
 
-    def status_fail(self, msg):
+    def status_fail(self, msg, anchor=None):
         """Write a note to the log file describing failed test(s).
 
         Args:
         """Write a note to the log file describing failed test(s).
 
         Args:
-            msg: A message describing passed test(s).
+            msg: A message describing the failed test(s).
+            anchor: Optional internal link target.
 
         Returns:
             Nothing.
         """
 
 
         Returns:
             Nothing.
         """
 
-        self._note("status-fail", msg)
+        self._note("status-fail", msg, anchor)
 
     def get_stream(self, name, chained_file=None):
         """Create an object to log a single stream's data into the log file.
 
     def get_stream(self, name, chained_file=None):
         """Create an object to log a single stream's data into the log file.
@@ -495,15 +678,16 @@ class Logfile(object):
 
         if stream != self.last_stream:
             self._terminate_stream()
 
         if stream != self.last_stream:
             self._terminate_stream()
-            self.f.write("<div class=\"stream\" id=\"%s\">\n" % stream.name)
-            self.f.write("<div class=\"stream-header\" id=\"" + stream.name +
-                         "\">Stream: " + stream.name + "</div>\n")
-            self.f.write("<pre>")
+            self.f.write('<div class="stream block">\n')
+            self.f.write('<div class="stream-header block-header">Stream: ' +
+                         stream.name + '</div>\n')
+            self.f.write('<div class="stream-content block-content">\n')
+            self.f.write('<pre>')
         if implicit:
         if implicit:
-            self.f.write("<span class=\"implicit\">")
+            self.f.write('<span class="implicit">')
         self.f.write(self._escape(data))
         if implicit:
         self.f.write(self._escape(data))
         if implicit:
-            self.f.write("</span>")
+            self.f.write('</span>')
         self.last_stream = stream
 
     def flush(self):
         self.last_stream = stream
 
     def flush(self):