[Piglit] [PATCH 5/6] summary.py: Simplify the way HTMLIndex works

Dylan Baker baker.dylan.c at gmail.com
Mon Nov 4 08:53:46 PST 2013


This patch replaces home-rolled code with built-in python functions,
this results in cleaner, simpler, and faster code.

Signed-off-by: Dylan Baker <baker.dylan.c at gmail.com>
---
 framework/summary.py | 124 +++++++++++++++------------------------------------
 1 file changed, 36 insertions(+), 88 deletions(-)

diff --git a/framework/summary.py b/framework/summary.py
index f5f3c9b..4a6db43 100644
--- a/framework/summary.py
+++ b/framework/summary.py
@@ -46,54 +46,10 @@ class HTMLIndex(list):
     """
 
     def __init__(self, summary, page):
-        """
-        Steps through the list of groups and tests from all of the results and
+        """ Steps through the list of groups and tests from all of the results and
         generates a list of dicts that are passed to mako and turned into HTML
+        
         """
-
-        def returnList(open, close):
-            """
-            As HTMLIndex iterates through the groups and tests it uses this
-            function to determine which groups to close (and thus reduce the
-            depth of the next write) and which ones to open (thus increasing
-            the depth)
-
-            To that end one of two things happens, the path to the previous
-            group (close) and the next group (open) are equal, in that event we
-            don't want to open and close, becasue that will result in a
-            sawtooth pattern of a group with one test followed by the same
-            group with one test, over and over.  Instead we simply return two
-            empty lists, which will result in writing a long list of test
-            results.  The second option is that the paths are different, and
-            the function determines any commonality between the paths, and
-            returns the differences as close (the groups which are completly
-            written) and open (the new groups to write).
-            """
-            common = []
-
-            # Open and close are lists, representing the group hierarchy, open
-            # being the groups that need are soon to be written, and close
-            # representing the groups that have finished writing.
-            if open == close:
-                return [], []
-            else:
-                for i, j in itertools.izip_longest(open, close):
-                    if i != j:
-                        for k in common:
-                            open.remove(k)
-                            close.remove(k)
-                        return open, close
-                    else:
-                        common.append(i)
-
-        # set a starting depth of 1, 0 is used for 'all' so 1 is the
-        # next available group
-        depth = 1
-
-        # Current dir is a list representing the groups currently being
-        # written.
-        currentDir = []
-
         # Add the toplevel 'all' group
         self._newRow()
         self._groupRow("head", 0, 'all')
@@ -101,48 +57,40 @@ class HTMLIndex(list):
             self._groupResult(summary.fractions[each.name]['all'],
                               summary.status[each.name]['all'])
         self._endRow()
-
-        # Add the groups and tests to the out list
-        for key in sorted(page):
-
-            # Split the group names and test names, then determine
-            # which groups to close and which to open
-            openList = key.split('/')
-            test = openList.pop()
-            openList, closeList = returnList(openList, list(currentDir))
-
-            # Close any groups in the close list
-            # for each group closed, reduce the depth by one
-            for i in reversed(closeList):
-                currentDir.remove(i)
-                depth -= 1
-
-            # Open new groups
-            for localGroup in openList:
-                self._newRow()
-
-                # Add the left-most column: the name of the group
-                self._groupRow("head", depth, localGroup)
-
-                # Add the group that we just opened to the currentDir, which
-                # will then be used to add that group to the HTML list. If
-                # there is a KeyError (the group doesn't exist), use (0, 0)
-                # which will get skip. This sets the group coloring correctly
-                currentDir.append(localGroup)
-                for each in summary.results:
-                    # Decide which fields need to be updated
-                    self._groupResult(
-                        summary.fractions[each.name][path.join(*currentDir)],
-                        summary.status[each.name][path.join(*currentDir)])
-
-                # After each group increase the depth by one
-                depth += 1
-                self._endRow()
-
-            # Add the tests for the current group
+        
+        # 'all' gets the depth of 0, so start at 1
+        depth = 1
+        group = ""
+
+        # The following loop relies on alphabetical sorting of test names
+        for test in sorted(page):
+
+            # If the current group not the same as the last group call
+            # os.path.relpath on it. This gives ".." for each group to close
+            # (reducing the depth) and a name for each group to open
+            # (increasing the depth)
+            if path.dirname(test) != group:
+                for cur in path.relpath(path.dirname(test), group).split('/'):
+                    if cur == "..":
+                        depth -= 1
+                        group = path.dirname(group)
+                    else:
+                        group = path.join(group, cur)
+                        self._newRow()
+                        self._groupRow("head", depth, group)
+                        for each in summary.results:
+                            try:
+                                self._groupResult(
+                                    summary.fractions[each.name][group],
+                                    summary.status[each.name][group])
+                            except KeyError:
+                                self._groupResult((0, 0), 'skip')
+                        depth += 1
+                        self._endRow()
+
+            # Finally add a new row for the test itself, and add the left-most
+            # name column
             self._newRow()
-
-            # Add the left-most column: the name of the test
             self._testRow("group", depth, test)
 
             # Add the result from each test result to the HTML summary If there
@@ -150,7 +98,7 @@ class HTMLIndex(list):
             # return Not Run, with clas skip for highlighting
             for each in summary.results:
                 try:
-                    self._testResult(each.name, key, each.tests[key]['result'])
+                    self._testResult(each.name, test, each.tests[test]['result'])
                 except KeyError:
                     self.append({'type': 'other',
                                  'text': '<td class="skip">Not Run</td>'})
-- 
1.8.1.5



More information about the Piglit mailing list