options.py 7.71 KB
Newer Older
1
# Copyright (c) 2015-2016 Intel Corporation
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

# 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.

"""Stores global piglit options.

This is as close to a true global function as python gets. The only deal here
is that while you can mutate

"""

28 29 30
from __future__ import (
    absolute_import, division, print_function, unicode_literals
)
31 32 33 34
import collections
import os
import re

35 36
import six

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
__all__ = ['OPTIONS']

# pylint: disable=too-few-public-methods


_RETYPE = type(re.compile(''))


class _ReList(collections.MutableSequence):
    """A list-like container that only holds RegexObjects.

    This class behaves identically to a list, except that all objects are
    forced to be RegexObjects with a flag of re.IGNORECASE (2 if one inspects
    the object).

    If inputs do not match this object, they will be coerced to becoming such
    an object, or they assignment will fail.

    """
    def __init__(self, iterable=None):
        self._wrapped = []
        if iterable is not None:
            self.extend(iterable)

    @staticmethod
    def __compile(value):
        """Ensure that the object is properly compiled.

        If the object is not a RegexObject then compile it to one, setting the
        proper flag. If it is a RegexObject, and the flag is incorrect
        recompile it to have the proper flags. Otherwise return it.

        """
        if not isinstance(value, _RETYPE):
            return re.compile(value, re.IGNORECASE)
        elif value.flags != re.IGNORECASE:
            return re.compile(value.pattern, re.IGNORECASE)
        return value

    def __getitem__(self, index):
        return self._wrapped[index]

    def __setitem__(self, index, value):
        self._wrapped[index] = self.__compile(value)

    def __delitem__(self, index):
        del self._wrapped[index]

    def __len__(self):
        return len(self._wrapped)

    def insert(self, index, value):
        self._wrapped.insert(index, self.__compile(value))

    def __eq__(self, other):
        """Two ReList instances are the same if their wrapped list are equal."""
        if isinstance(other, _ReList):
            # There doesn't seem to be a better way to do this.
            return self._wrapped == other._wrapped  # pylint: disable=protected-access
        raise TypeError('Cannot compare _ReList and non-_ReList object')

    def __ne__(self, other):
        return not self == other

    def to_json(self):
        """Allow easy JSON serialization.

        This returns the pattern (the string or unicode used to create the re)
        of each re object in a list rather than the RegexObject itself. This is
        critical for JSON serialization, and thanks to the piglit_encoder this
        is all we need to serialize this class.

        """
        return [l.pattern for l in self]


113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
class _FilterReList(_ReList):
    """A version of ReList that handles group madness.

    Groups are printed with '/' as a separator, but internally something else
    may be used. This version replaces '/' with '.'.

    """
    def __setitem__(self, index, value):
        # Replace '/' with '.', this solves the problem of '/' not matching
        # grouptools.SEPARATOR, but without needing to import grouptools
        super(_FilterReList, self).__setitem__(index, value.replace('/', '.'))

    def insert(self, index, value):
        super(_FilterReList, self).insert(index, value.replace('/', '.'))


129 130 131 132 133 134 135 136 137
class _ReListDescriptor(object):
    """A Descriptor than ensures reassignment of _{in,ex}clude_filter is an
    _ReList

    Without this some behavior's can get very strange. This descriptor is
    mostly hit by testing code, but may be of use outside of testing at some
    point.

    """
138
    def __init__(self, name, type_=_ReList):
139
        self.__name = name
140
        self.__type = type_
141 142 143 144 145 146 147 148 149 150 151 152 153 154

    def __get__(self, instance, cls):
        try:
            return getattr(instance, self.__name)
        except AttributeError as e:
            new = _ReList()
            try:
                setattr(instance, self.__name, new)
            except Exception:
                raise e
            return new

    def __set__(self, instance, value):
        assert isinstance(value, (collections.Sequence, collections.Set))
155
        if isinstance(value, self.__type):
156 157
            setattr(instance, self.__name, value)
        else:
158
            setattr(instance, self.__name, self.__type(value))
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

    def __delete__(self, instance):
        raise NotImplementedError('Cannot delete {} from {}'.format(
            self.__name, instance.__class__))


class _Options(object):  # pylint: disable=too-many-instance-attributes
    """Contains all options for a piglit run.

    This is used as a sort of global state object, kinda like piglit.conf. This
    big difference here is that this object is largely generated internally,
    and is controlled mostly through command line options rather than through
    the configuration file.

    Options are as follows:
    concurrent -- True if concurrency is to be used
    execute -- False for dry run
    include_filter -- list of compiled regex which include exclusively tests
                      that match
    exclude_filter -- list of compiled regex which exclude tests that match
    valgrind -- True if valgrind is to be used
    dmesg -- True if dmesg checking is desired. This forces concurrency off
181
    monitored -- True if monitoring is desired. This forces concurrency off
182 183 184
    env -- environment variables set for each test before run

    """
185 186
    include_filter = _ReListDescriptor('_include_filter', type_=_FilterReList)
    exclude_filter = _ReListDescriptor('_exclude_filter', type_=_FilterReList)
187 188 189 190 191 192 193 194 195

    def __init__(self):
        self.concurrent = True
        self.execute = True
        self._include_filter = _ReList()
        self._exclude_filter = _ReList()
        self.exclude_tests = set()
        self.valgrind = False
        self.dmesg = False
196
        self.monitored = False
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
        self.sync = False

        # env is used to set some base environment variables that are not going
        # to change across runs, without sending them to os.environ which is
        # fickle and easy to break
        self.env = {
            'PIGLIT_SOURCE_DIR':
                os.environ.get(
                    'PIGLIT_SOURCE_DIR',
                    os.path.abspath(os.path.join(os.path.dirname(__file__),
                                                 '..')))
        }

    def clear(self):
        """Reinitialize all values to defaults."""
        self.__init__()

    def __iter__(self):
215
        for key, values in six.iteritems(self.__dict__):
216 217 218 219 220 221
            if not key.startswith('_'):
                yield key, values

        # Handle the attributes that have a descriptor separately
        yield 'include_filter', self.include_filter
        yield 'exclude_filter', self.exclude_filter
222 223 224


OPTIONS = _Options()