summaryrefslogtreecommitdiffstats
path: root/python/mozbuild/mozpack/chrome/flags.py
blob: 6b096c862aaac5e02d9d7dacda42d9321d5e89cc (plain)
1
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
28
29
30
31
32
33
34
35
36
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import re
from collections import OrderedDict

import six
from packaging.version import Version

from mozpack.errors import errors


class Flag(object):
    """
    Class for flags in manifest entries in the form:
        "flag"   (same as "flag=true")
        "flag=yes|true|1"
        "flag=no|false|0"
    """

    def __init__(self, name):
        """
        Initialize a Flag with the given name.
        """
        self.name = name
        self.value = None

    def add_definition(self, definition):
        """
        Add a flag value definition. Replaces any previously set value.
        """
        if definition == self.name:
            self.value = True
            return
        assert definition.startswith(self.name)
        if definition[len(self.name)] != "=":
            return errors.fatal("Malformed flag: %s" % definition)
        value = definition[len(self.name) + 1 :]
        if value in ("yes", "true", "1", "no", "false", "0"):
            self.value = value
        else:
            return errors.fatal("Unknown value in: %s" % definition)

    def matches(self, value):
        """
        Return whether the flag value matches the given value. The values
        are canonicalized for comparison.
        """
        if value in ("yes", "true", "1", True):
            return self.value in ("yes", "true", "1", True)
        if value in ("no", "false", "0", False):
            return self.value in ("no", "false", "0", False, None)
        raise RuntimeError("Invalid value: %s" % value)

    def __str__(self):
        """
        Serialize the flag value in the same form given to the last
        add_definition() call.
        """
        if self.value is None:
            return ""
        if self.value is True:
            return self.name
        return "%s=%s" % (self.name, self.value)

    def __eq__(self, other):
        return str(self) == other


class StringFlag(object):
    """
    Class for string flags in manifest entries in the form:
        "flag=string"
        "flag!=string"
    """

    def __init__(self, name):
        """
        Initialize a StringFlag with the given name.
        """
        self.name = name
        self.values = []

    def add_definition(self, definition):
        """
        Add a string flag definition.
        """
        assert definition.startswith(self.name)
        value = definition[len(self.name) :]
        if value.startswith("="):
            self.values.append(("==", value[1:]))
        elif value.startswith("!="):
            self.values.append(("!=", value[2:]))
        else:
            return errors.fatal("Malformed flag: %s" % definition)

    def matches(self, value):
        """
        Return whether one of the string flag definitions matches the given
        value.
        For example,

            flag = StringFlag('foo')
            flag.add_definition('foo!=bar')
            flag.matches('bar') returns False
            flag.matches('qux') returns True
            flag = StringFlag('foo')
            flag.add_definition('foo=bar')
            flag.add_definition('foo=baz')
            flag.matches('bar') returns True
            flag.matches('baz') returns True
            flag.matches('qux') returns False
        """
        if not self.values:
            return True
        for comparison, val in self.values:
            if eval("value %s val" % comparison):
                return True
        return False

    def __str__(self):
        """
        Serialize the flag definitions in the same form given to each
        add_definition() call.
        """
        res = []
        for comparison, val in self.values:
            if comparison == "==":
                res.append("%s=%s" % (self.name, val))
            else:
                res.append("%s!=%s" % (self.name, val))
        return " ".join(res)

    def __eq__(self, other):
        return str(self) == other


class VersionFlag(object):
    """
    Class for version flags in manifest entries in the form:
        "flag=version"
        "flag<=version"
        "flag<version"
        "flag>=version"
        "flag>version"
    """

    def __init__(self, name):
        """
        Initialize a VersionFlag with the given name.
        """
        self.name = name
        self.values = []

    def add_definition(self, definition):
        """
        Add a version flag definition.
        """
        assert definition.startswith(self.name)
        value = definition[len(self.name) :]
        if value.startswith("="):
            self.values.append(("==", Version(value[1:])))
        elif len(value) > 1 and value[0] in ["<", ">"]:
            if value[1] == "=":
                if len(value) < 3:
                    return errors.fatal("Malformed flag: %s" % definition)
                self.values.append((value[0:2], Version(value[2:])))
            else:
                self.values.append((value[0], Version(value[1:])))
        else:
            return errors.fatal("Malformed flag: %s" % definition)

    def matches(self, value):
        """
        Return whether one of the version flag definitions matches the given
        value.
        For example,

            flag = VersionFlag('foo')
            flag.add_definition('foo>=1.0')
            flag.matches('1.0') returns True
            flag.matches('1.1') returns True
            flag.matches('0.9') returns False
            flag = VersionFlag('foo')
            flag.add_definition('foo>=1.0')
            flag.add_definition('foo<0.5')
            flag.matches('0.4') returns True
            flag.matches('1.0') returns True
            flag.matches('0.6') returns False
        """
        value = Version(value)
        if not self.values:
            return True
        for comparison, val in self.values:
            if eval("value %s val" % comparison):
                return True
        return False

    def __str__(self):
        """
        Serialize the flag definitions in the same form given to each
        add_definition() call.
        """
        res = []
        for comparison, val in self.values:
            if comparison == "==":
                res.append("%s=%s" % (self.name, val))
            else:
                res.append("%s%s%s" % (self.name, comparison, val))
        return " ".join(res)

    def __eq__(self, other):
        return str(self) == other


class Flags(OrderedDict):
    """
    Class to handle a set of flags definitions given on a single manifest
    entry.

    """

    FLAGS = {
        "application": StringFlag,
        "appversion": VersionFlag,
        "platformversion": VersionFlag,
        "contentaccessible": Flag,
        "os": StringFlag,
        "osversion": VersionFlag,
        "abi": StringFlag,
        "platform": Flag,
        "xpcnativewrappers": Flag,
        "tablet": Flag,
        "process": StringFlag,
        "backgroundtask": StringFlag,
    }
    RE = re.compile(r"([!<>=]+)")

    def __init__(self, *flags):
        """
        Initialize a set of flags given in string form.
           flags = Flags('contentaccessible=yes', 'appversion>=3.5')
        """
        OrderedDict.__init__(self)
        for f in flags:
            name = self.RE.split(f)
            name = name[0]
            if name not in self.FLAGS:
                errors.fatal("Unknown flag: %s" % name)
                continue
            if name not in self:
                self[name] = self.FLAGS[name](name)
            self[name].add_definition(f)

    def __str__(self):
        """
        Serialize the set of flags.
        """
        return " ".join(str(self[k]) for k in self)

    def match(self, **filter):
        """
        Return whether the set of flags match the set of given filters.
            flags = Flags('contentaccessible=yes', 'appversion>=3.5',
                          'application=foo')

            flags.match(application='foo') returns True
            flags.match(application='foo', appversion='3.5') returns True
            flags.match(application='foo', appversion='3.0') returns False

        """
        for name, value in six.iteritems(filter):
            if name not in self:
                continue
            if not self[name].matches(value):
                return False
        return True