michael@0: # This Source Code Form is subject to the terms of the Mozilla Public michael@0: # License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: # file, You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: michael@0: import re michael@0: from distutils.version import LooseVersion michael@0: from mozpack.errors import errors michael@0: from collections import OrderedDict michael@0: michael@0: michael@0: class Flag(object): michael@0: ''' michael@0: Class for flags in manifest entries in the form: michael@0: "flag" (same as "flag=true") michael@0: "flag=yes|true|1" michael@0: "flag=no|false|0" michael@0: ''' michael@0: def __init__(self, name): michael@0: ''' michael@0: Initialize a Flag with the given name. michael@0: ''' michael@0: self.name = name michael@0: self.value = None michael@0: michael@0: def add_definition(self, definition): michael@0: ''' michael@0: Add a flag value definition. Replaces any previously set value. michael@0: ''' michael@0: if definition == self.name: michael@0: self.value = True michael@0: return michael@0: assert(definition.startswith(self.name)) michael@0: if definition[len(self.name)] != '=': michael@0: return errors.fatal('Malformed flag: %s' % definition) michael@0: value = definition[len(self.name) + 1:] michael@0: if value in ('yes', 'true', '1', 'no', 'false', '0'): michael@0: self.value = value michael@0: else: michael@0: return errors.fatal('Unknown value in: %s' % definition) michael@0: michael@0: def matches(self, value): michael@0: ''' michael@0: Return whether the flag value matches the given value. The values michael@0: are canonicalized for comparison. michael@0: ''' michael@0: if value in ('yes', 'true', '1', True): michael@0: return self.value in ('yes', 'true', '1', True) michael@0: if value in ('no', 'false', '0', False): michael@0: return self.value in ('no', 'false', '0', False, None) michael@0: raise RuntimeError('Invalid value: %s' % value) michael@0: michael@0: def __str__(self): michael@0: ''' michael@0: Serialize the flag value in the same form given to the last michael@0: add_definition() call. michael@0: ''' michael@0: if self.value is None: michael@0: return '' michael@0: if self.value is True: michael@0: return self.name michael@0: return '%s=%s' % (self.name, self.value) michael@0: michael@0: michael@0: class StringFlag(object): michael@0: ''' michael@0: Class for string flags in manifest entries in the form: michael@0: "flag=string" michael@0: "flag!=string" michael@0: ''' michael@0: def __init__(self, name): michael@0: ''' michael@0: Initialize a StringFlag with the given name. michael@0: ''' michael@0: self.name = name michael@0: self.values = [] michael@0: michael@0: def add_definition(self, definition): michael@0: ''' michael@0: Add a string flag definition. michael@0: ''' michael@0: assert(definition.startswith(self.name)) michael@0: value = definition[len(self.name):] michael@0: if value.startswith('='): michael@0: self.values.append(('==', value[1:])) michael@0: elif value.startswith('!='): michael@0: self.values.append(('!=', value[2:])) michael@0: else: michael@0: return errors.fatal('Malformed flag: %s' % definition) michael@0: michael@0: def matches(self, value): michael@0: ''' michael@0: Return whether one of the string flag definitions matches the given michael@0: value. michael@0: For example, michael@0: flag = StringFlag('foo') michael@0: flag.add_definition('foo!=bar') michael@0: flag.matches('bar') returns False michael@0: flag.matches('qux') returns True michael@0: flag = StringFlag('foo') michael@0: flag.add_definition('foo=bar') michael@0: flag.add_definition('foo=baz') michael@0: flag.matches('bar') returns True michael@0: flag.matches('baz') returns True michael@0: flag.matches('qux') returns False michael@0: ''' michael@0: if not self.values: michael@0: return True michael@0: for comparison, val in self.values: michael@0: if eval('value %s val' % comparison): michael@0: return True michael@0: return False michael@0: michael@0: def __str__(self): michael@0: ''' michael@0: Serialize the flag definitions in the same form given to each michael@0: add_definition() call. michael@0: ''' michael@0: res = [] michael@0: for comparison, val in self.values: michael@0: if comparison == '==': michael@0: res.append('%s=%s' % (self.name, val)) michael@0: else: michael@0: res.append('%s!=%s' % (self.name, val)) michael@0: return ' '.join(res) michael@0: michael@0: michael@0: class VersionFlag(object): michael@0: ''' michael@0: Class for version flags in manifest entries in the form: michael@0: "flag=version" michael@0: "flag<=version" michael@0: "flag=version" michael@0: "flag>version" michael@0: ''' michael@0: def __init__(self, name): michael@0: ''' michael@0: Initialize a VersionFlag with the given name. michael@0: ''' michael@0: self.name = name michael@0: self.values = [] michael@0: michael@0: def add_definition(self, definition): michael@0: ''' michael@0: Add a version flag definition. michael@0: ''' michael@0: assert(definition.startswith(self.name)) michael@0: value = definition[len(self.name):] michael@0: if value.startswith('='): michael@0: self.values.append(('==', LooseVersion(value[1:]))) michael@0: elif len(value) > 1 and value[0] in ['<', '>']: michael@0: if value[1] == '=': michael@0: if len(value) < 3: michael@0: return errors.fatal('Malformed flag: %s' % definition) michael@0: self.values.append((value[0:2], LooseVersion(value[2:]))) michael@0: else: michael@0: self.values.append((value[0], LooseVersion(value[1:]))) michael@0: else: michael@0: return errors.fatal('Malformed flag: %s' % definition) michael@0: michael@0: def matches(self, value): michael@0: ''' michael@0: Return whether one of the version flag definitions matches the given michael@0: value. michael@0: For example, michael@0: flag = VersionFlag('foo') michael@0: flag.add_definition('foo>=1.0') michael@0: flag.matches('1.0') returns True michael@0: flag.matches('1.1') returns True michael@0: flag.matches('0.9') returns False michael@0: flag = VersionFlag('foo') michael@0: flag.add_definition('foo>=1.0') michael@0: flag.add_definition('foo<0.5') michael@0: flag.matches('0.4') returns True michael@0: flag.matches('1.0') returns True michael@0: flag.matches('0.6') returns False michael@0: ''' michael@0: value = LooseVersion(value) michael@0: if not self.values: michael@0: return True michael@0: for comparison, val in self.values: michael@0: if eval('value %s val' % comparison): michael@0: return True michael@0: return False michael@0: michael@0: def __str__(self): michael@0: ''' michael@0: Serialize the flag definitions in the same form given to each michael@0: add_definition() call. michael@0: ''' michael@0: res = [] michael@0: for comparison, val in self.values: michael@0: if comparison == '==': michael@0: res.append('%s=%s' % (self.name, val)) michael@0: else: michael@0: res.append('%s%s%s' % (self.name, comparison, val)) michael@0: return ' '.join(res) michael@0: michael@0: michael@0: class Flags(OrderedDict): michael@0: ''' michael@0: Class to handle a set of flags definitions given on a single manifest michael@0: entry. michael@0: ''' michael@0: FLAGS = { michael@0: 'application': StringFlag, michael@0: 'appversion': VersionFlag, michael@0: 'platformversion': VersionFlag, michael@0: 'contentaccessible': Flag, michael@0: 'os': StringFlag, michael@0: 'osversion': VersionFlag, michael@0: 'abi': StringFlag, michael@0: 'platform': Flag, michael@0: 'xpcnativewrappers': Flag, michael@0: 'tablet': Flag, michael@0: } michael@0: RE = re.compile(r'([!<>=]+)') michael@0: michael@0: def __init__(self, *flags): michael@0: ''' michael@0: Initialize a set of flags given in string form. michael@0: flags = Flags('contentaccessible=yes', 'appversion>=3.5') michael@0: ''' michael@0: OrderedDict.__init__(self) michael@0: for f in flags: michael@0: name = self.RE.split(f) michael@0: name = name[0] michael@0: if not name in self.FLAGS: michael@0: errors.fatal('Unknown flag: %s' % name) michael@0: continue michael@0: if not name in self: michael@0: self[name] = self.FLAGS[name](name) michael@0: self[name].add_definition(f) michael@0: michael@0: def __str__(self): michael@0: ''' michael@0: Serialize the set of flags. michael@0: ''' michael@0: return ' '.join(str(self[k]) for k in self) michael@0: michael@0: def match(self, **filter): michael@0: ''' michael@0: Return whether the set of flags match the set of given filters. michael@0: flags = Flags('contentaccessible=yes', 'appversion>=3.5', michael@0: 'application=foo') michael@0: flags.match(application='foo') returns True michael@0: flags.match(application='foo', appversion='3.5') returns True michael@0: flags.match(application='foo', appversion='3.0') returns False michael@0: ''' michael@0: for name, value in filter.iteritems(): michael@0: if not name in self: michael@0: continue michael@0: if not self[name].matches(value): michael@0: return False michael@0: return True