[Piglit] [PATCH 4/4] grouptools.py: Add a module specifically for working with group strings

Dylan Baker baker.dylan.c at gmail.com
Mon Nov 3 17:05:06 PST 2014


This module is largely just posixpath (sometimes the functions are
renamed), with a couple of unique functions, which are basically
wrappers around posixpath functions with some special exceptions. Having
and using the module presents the advantage of being able to change the
implementation, including how the groups are manipulated. It also is
clearer than working with posixpath directly.

Signed-off-by: Dylan Baker <dylanx.c.baker at intel.com>
---
 framework/grouptools.py             | 73 +++++++++++++++++++++++++++++++++++++
 framework/summary.py                | 27 +++++---------
 framework/tests/grouptools_tests.py | 51 ++++++++++++++++++++++++++
 3 files changed, 134 insertions(+), 17 deletions(-)
 create mode 100644 framework/grouptools.py
 create mode 100644 framework/tests/grouptools_tests.py

diff --git a/framework/grouptools.py b/framework/grouptools.py
new file mode 100644
index 0000000..615eeda
--- /dev/null
+++ b/framework/grouptools.py
@@ -0,0 +1,73 @@
+# Copyright (c) 2014 Intel Corporation
+
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+# SOFTWARE.
+
+"""Module providing utility functions to work with piglit groups.
+
+Piglit groups look much like posix paths, they are '/' delimited with each
+element representing a group, and the final element being the test name. Unlike
+posix paths they may not start with a leading '/'.
+
+"""
+
+import posixpath
+
+__all__ = [
+    'join',
+    'commonprefix',
+    'relgroup',
+    'split',
+    'split_test',
+    'group',
+    'test',
+]
+
+# pylint: disable=invalid-name
+join = posixpath.join
+commonprefix = posixpath.commonprefix
+split_test = posixpath.split
+group = posixpath.dirname
+test = posixpath.basename
+# pylint: enable=invalid-name
+
+
+def relgroup(large, small):
+    """Find the relationship between two groups.
+
+    This allows the comparison of two groups, and returns a string. If start
+    start is longer than the group then '' is returned.
+
+    """
+    if len(small) > len(large):
+        return ''
+    elif small == '' and large == '':
+        return ''
+    else:
+        return posixpath.relpath(large, small)
+
+
+def split(group_):
+    """Split the group into a list of elements.
+
+    If input is '', return an empty list
+
+    """
+    if group_ == '':
+        return []
+    return group_.split('/')
diff --git a/framework/summary.py b/framework/summary.py
index 64b0f7b..00febc0 100644
--- a/framework/summary.py
+++ b/framework/summary.py
@@ -22,8 +22,6 @@
 from __future__ import print_function
 import os
 import os.path as path
-import posixpath
-import itertools
 import shutil
 import collections
 import tempfile
@@ -38,6 +36,7 @@ from mako.template import Template
 # the module
 import framework.status as so
 import framework.results
+import framework.grouptools as grouptools
 
 
 __all__ = [
@@ -121,7 +120,7 @@ def _test_result(href, group, text, exclude):
         if css in exclude:
             href = None
         else:
-            href = normalize_href(posixpath.join(group, href + '.html'))
+            href = normalize_href(grouptools.join(group, href + '.html'))
 
     return template.render(css=css, text=text, href=href)
 
@@ -136,7 +135,7 @@ def html_generator(summary, page, exclude):
     yield '<td />'
     for each in summary.results:
         href = normalize_href(
-            posixpath.join(escape_pathname(each.name), "index.html"))
+            grouptools.join(escape_pathname(each.name), "index.html"))
         yield ('<td class="head"><b>{0}</b><br />'
                '(<a href="{1}">info</a>)'
                '</td>'.format(each.name, href))
@@ -161,24 +160,18 @@ def html_generator(summary, page, exclude):
         assert '\\' not in key, '\\ is not allowed in test names'
 
         previous = current
-        current, test = posixpath.split(key)
+        current, test = grouptools.split_test(key)
         if current != previous:
-            common = posixpath.commonprefix([current, previous])
+            common = grouptools.commonprefix([current, previous])
 
             # close any elements of the previous group that are not shared with
             # the previous group.
-            # On the first run posixpath.relpath will have a value error
-            # because previous is ''.
-            try:
-                for _ in posixpath.relpath(previous, common).split('/'):
-                    depth -= 1
-            except ValueError as e:
-                if e.message != 'no path specified':
-                    raise e
+            for _ in grouptools.split(grouptools.relgroup(previous, common)):
+                depth -= 1
 
             # Open new groups
-            work = common.split('/')
-            for group in posixpath.relpath(current, common).split('/'):
+            work = grouptools.split(common)
+            for group in grouptools.split(grouptools.relgroup(current, common)):
                 work.append(group)
                 yield new_row
 
@@ -187,7 +180,7 @@ def html_generator(summary, page, exclude):
                 for each in summary.results:
                     yield _group_result(
                         summary.status[each.name][path.join(*work)],
-                        summary.fractions[each.name][path.join(*work)])
+                        summary.fractions[each.name][grouptools.join(*work)])
 
                 # After each group increase the depth by one
                 depth += 1
diff --git a/framework/tests/grouptools_tests.py b/framework/tests/grouptools_tests.py
new file mode 100644
index 0000000..b2fac9b
--- /dev/null
+++ b/framework/tests/grouptools_tests.py
@@ -0,0 +1,51 @@
+# Copyright (c) 2014 Intel Corporation
+
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+# SOFTWARE.
+
+"""Module with tests for grouptools."""
+
+import nose.tools as nt
+
+import framework.grouptools as grouptools
+
+
+def test_relgroup():
+    """grouptools.relgroup returns the difference between two groups."""
+    nt.assert_equal(grouptools.relgroup('foo/bar/oink', 'foo/bar'), 'oink')
+
+
+def test_relgroup_small():
+    """grouptools.relgroup returns '' if the base group is larger than other.
+    """
+    nt.assert_equal(grouptools.relgroup('foo/bar', 'foo/bar/oink'), '')
+
+
+def test_relgroup_blanks():
+    """grouptools.relgroup handles '' for both inputs."""
+    nt.assert_equal(grouptools.relgroup('', ''), '')
+
+
+def test_split_blank():
+    """grouptools.split return [] if input is ''."""
+    nt.assert_equal(grouptools.split(''), [])
+
+
+def test_split():
+    """grouptools.split works."""
+    nt.assert_equal(grouptools.split('foo/bar/oink'), ['foo', 'bar', 'oink'])
-- 
2.1.3



More information about the Piglit mailing list