ext_transform_feedback: document missing mode in usage
[piglit.git] / framework / grouptools.py
blobfb7e6c207675dab1584ece4c73f993381ff79e66
1 # coding=utf-8
2 # Copyright (c) 2014-2016, 2019 Intel Corporation
4 # Permission is hereby granted, free of charge, to any person obtaining a copy
5 # of this software and associated documentation files (the "Software"), to deal
6 # in the Software without restriction, including without limitation the rights
7 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 # copies of the Software, and to permit persons to whom the Software is
9 # furnished to do so, subject to the following conditions:
11 # The above copyright notice and this permission notice shall be included in
12 # all copies or substantial portions of the Software.
14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 # SOFTWARE.
22 """Module providing utility functions to work with piglit groups.
24 Instead of using posixpath (or the generic os.path) for working with tests this
25 module should be preferred.
27 Piglit groups look much like posix paths, they are '/' delimited with each
28 element representing a group, and the final element being the test name. Unlike
29 posix paths they may not start with a leading '/'.
31 """
33 __all__ = [
34 'SEPARATOR',
35 'commonprefix',
36 'format',
37 'from_path',
38 'groupname',
39 'join',
40 'split',
41 'splitname',
42 'testname',
45 SEPARATOR = '@'
48 def testname(group):
49 """Return the last element of a group name.
51 Provided the value 'group1|group2|test1' will provide 'test1', this
52 does not enforce any rules that the final element is a test name, and can
53 be used to shaved down groups.
55 Analogous to os.path.basename
57 """
58 return splitname(group)[1]
61 def groupname(group):
62 """Return all groups except the last.
64 Provided the value 'group1/group2/test1' will provide 'group1/group2', this
65 does not enforce any rules that the final element is a test name, and can
66 be used to shaved down groups.
68 Analogous to os.path.dirname
70 """
71 return splitname(group)[0]
74 def splitname(group):
75 """Split a group name, Returns tuple "(group, test)"."""
76 i = group.rfind(SEPARATOR) + 1
77 head, tail = group[:i], group[i:]
78 head = head.rstrip(SEPARATOR)
80 return head, tail
83 def commonprefix(args):
84 """Given a list of groups, returns the longest common leading component."""
85 if len(args) == 1:
86 return args[0]
87 elif any(e == '' for e in args):
88 return ''
90 common = []
92 for elems in zip(*[split(a) for a in args]):
93 iter_ = iter(elems)
94 first = next(iter_)
95 if all(first == r for r in iter_):
96 common.append(first)
97 else:
98 break
100 # Join needs at least one element to join
101 if common:
102 return join(*common)
103 else:
104 return ''
107 def join(first, *args):
108 """Join multiple groups together.
110 This function is implemented via string concatenation, while most
111 pythonistas would use list joining, because it is accepted as better. I
112 wrote a number of implementations and timed them with timeit. I found for
113 small number of joins (2-10) that str concatenation was quite a bit faster,
114 at around 100 elements list joining became faster. Since most of piglit's
115 use of join is for 2-10 elements I used string concatenation, which is
116 coincidentally very similar to the way posixpath.join is implemented.
119 # If first happens to be a non-existent value, walk through args until we
120 # find a real value and use that.
121 args = (a for a in args)
122 if not first:
123 for group in args:
124 if group:
125 first = group
126 break
128 for group in args:
129 # Only append things if the group is not empty, otherwise we'll get
130 # extra SEPARATORs where we don't want them
131 if group:
132 if not first.endswith(SEPARATOR):
133 first += SEPARATOR
134 first += group
136 return first
139 def split(group):
140 """Split the group into a list of elements.
142 If input is '' return an empty list
145 if group == '':
146 return []
147 return group.split(SEPARATOR)
150 def from_path(path):
151 """Create a group from a path.
153 This function takes a path, and creates a group out of it.
155 This safely handles both Windows and Unix style paths.
158 assert isinstance(path, str), 'Type must be str'
160 if '\\' in path:
161 path = path.replace('\\', SEPARATOR)
162 if '/' in path:
163 path = path.replace('/', SEPARATOR)
164 if '.' == path:
165 return ''
166 return path
169 def format(name):
170 """Format an internal name for printing.
172 It doesn't matter how the name is stored internally, presenting a
173 consistence, clean interface on the command line that doesn't contain any
174 ugly or problematic characters is important.
176 This replaces SEPARATOR with '/', which is what most devs are used to and
177 want to see.
180 assert isinstance(name, str)
181 return name.replace(SEPARATOR, '/')