Example #1
0
 def __new__(cls, spec):
     if isinstance(spec, cls):
         return spec
     self = object.__new__(cls)
     spec, _, oparts = spec.partition('(')
     self.spec = spec.strip()
     if oparts and oparts.strip()[-1] != ')':
         raise ValueError("Invalid MatchSpec: %s" % spec)
     parts = spec.split()
     self.strictness = len(parts)
     assert 1 <= self.strictness <= 3, repr(spec)
     self.name = parts[0]
     if self.strictness == 2:
         self.vspecs = VersionSpec(parts[1])
     elif self.strictness == 3:
         self.ver_build = tuple(parts[1:3])
     self.target = None
     self.optional = False
     self.negate = False
     if oparts:
         for opart in oparts.strip()[:-1].split(','):
             if opart == 'optional':
                 self.optional = True
             elif opart == 'negate':
                 self.negate = True
             elif opart.startswith('target='):
                 self.target = opart.split('=')[1].strip()
             else:
                 raise ValueError("Invalid MatchSpec: %s" % spec)
     return self
Example #2
0
 def test_local_identifier(self):
     """The separator for the local identifier should be either `.` or `+`"""
     # a valid versionstr should match itself
     versions = ("1.7.0" "1.7.0.post123" "1.7.0.post123.gabcdef9", "1.7.0.post123+gabcdef9")
     for version in versions:
         m = VersionSpec(version)
         self.assertTrue(m.match(version))
Example #3
0
 def test_match(self):
     for vspec, res in [
         ('1.7*', True),   ('1.7.1', True),    ('1.7.0', False),
         ('1.7', False),   ('1.5*', False),    ('>=1.5', True),
         ('!=1.5', True),  ('!=1.7.1', False), ('==1.7.1', True),
         ('==1.7', False), ('==1.7.2', False), ('==1.7.1.0', True),
         ]:
         m = VersionSpec(vspec)
         self.assertEqual(m.match('1.7.1'), res)
Example #4
0
 def test_local_identifier(self):
     """The separator for the local identifier should be either `.` or `+`"""
     # a valid versionstr should match itself
     versions = (
         '1.7.0'
         '1.7.0.post123'
         '1.7.0.post123.gabcdef9',
         '1.7.0.post123+gabcdef9',
     )
     for version in versions:
         m = VersionSpec(version)
         self.assertTrue(m.match(version))
Example #5
0
 def test_match(self):
     for vspec, res in [('1.7*', True), ('1.7.1', True), ('1.7.0', False),
                        ('1.7', False), ('1.5*', False), ('>=1.5', True),
                        ('!=1.5', True), ('!=1.7.1', False),
                        ('==1.7.1', True), ('==1.7', False),
                        ('==1.7.2', False), ('==1.7.1.0', True),
                        ('1.7*|1.8*', True), ('1.8*|1.9*', False),
                        ('>1.7,<1.8', True), ('>1.7.1,<1.8', False)]:
         m = VersionSpec(vspec)
         assert VersionSpec(m) is m
         assert str(m) == vspec
         assert repr(m) == "VersionSpec('%s')" % vspec
         self.assertEqual(m.match('1.7.1'), res)
Example #6
0
 def __new__(cls, spec, target=Ellipsis, optional=Ellipsis, normalize=False):
     if isinstance(spec, cls):
         if target is Ellipsis and optional is Ellipsis and not normalize:
             return spec
         target = spec.target if target is Ellipsis else target
         optional = spec.optional if optional is Ellipsis else optional
         spec = spec.spec
     self = object.__new__(cls)
     self.target = None if target is Ellipsis else target
     self.optional = False if optional is Ellipsis else bool(optional)
     spec, _, oparts = spec.partition('(')
     if oparts:
         if oparts.strip()[-1] != ')':
             raise ValueError("Invalid MatchSpec: %s" % spec)
         for opart in oparts.strip()[:-1].split(','):
             if opart == 'optional':
                 self.optional = True
             elif opart.startswith('target='):
                 self.target = opart.split('=')[1].strip()
             else:
                 raise ValueError("Invalid MatchSpec: %s" % spec)
     spec = self.spec = spec.strip()
     parts = spec.split()
     nparts = len(parts)
     assert 1 <= nparts <= 3, repr(spec)
     self.name = parts[0]
     if nparts == 1:
         self.match_fast = self._match_any
         self.strictness = 1
         return self
     self.strictness = 2
     vspec = VersionSpec(parts[1])
     if vspec.is_exact():
         if nparts > 2 and '*' not in parts[2]:
             self.version, self.build = parts[1:]
             self.match_fast = self._match_exact
             self.strictness = 3
             return self
         if normalize and not parts[1].endswith('*'):
             parts[1] += '*'
             vspec = VersionSpec(parts[1])
             self.spec = ' '.join(parts)
     self.version = vspec
     if nparts == 2:
         self.match_fast = self._match_version
     else:
         rx = r'^(?:%s)$' % parts[2].replace('*', r'.*')
         self.build = re.compile(rx)
         self.match_fast = self._match_full
     return self
Example #7
0
 def __new__(cls, spec, target=Ellipsis, optional=Ellipsis, normalize=False):
     if isinstance(spec, cls):
         if target is Ellipsis and optional is Ellipsis and not normalize:
             return spec
         target = spec.target if target is Ellipsis else target
         optional = spec.optional if optional is Ellipsis else optional
         spec = spec.spec
     self = object.__new__(cls)
     self.target = None if target is Ellipsis else target
     self.optional = False if optional is Ellipsis else bool(optional)
     spec, _, oparts = spec.partition('(')
     if oparts:
         if oparts.strip()[-1] != ')':
             raise ValueError("Invalid MatchSpec: %s" % spec)
         for opart in oparts.strip()[:-1].split(','):
             if opart == 'optional':
                 self.optional = True
             elif opart.startswith('target='):
                 self.target = opart.split('=')[1].strip()
             else:
                 raise ValueError("Invalid MatchSpec: %s" % spec)
     spec = self.spec = spec.strip()
     parts = spec.split()
     nparts = len(parts)
     assert 1 <= nparts <= 3, repr(spec)
     self.name = parts[0]
     if nparts == 1:
         self.match_fast = self._match_any
         self.strictness = 1
         return self
     self.strictness = 2
     vspec = VersionSpec(parts[1])
     if vspec.is_exact():
         if nparts > 2 and '*' not in parts[2]:
             self.version, self.build = parts[1:]
             self.match_fast = self._match_exact
             self.strictness = 3
             return self
         if normalize and not parts[1].endswith('*'):
             parts[1] += '*'
             vspec = VersionSpec(parts[1])
             self.spec = ' '.join(parts)
     self.version = vspec
     if nparts == 2:
         self.match_fast = self._match_version
     else:
         rx = r'^(?:%s)$' % parts[2].replace('*', r'.*')
         self.build = re.compile(rx)
         self.match_fast = self._match_full
     return self
Example #8
0
 def __new__(cls, spec, target=None, optional=None):
     if isinstance(spec, cls):
         return spec
     self = object.__new__(cls)
     spec, _, oparts = spec.partition('(')
     self.spec = spec.strip()
     if oparts and oparts.strip()[-1] != ')':
         raise ValueError("Invalid MatchSpec: %s" % spec)
     parts = spec.split()
     self.strictness = len(parts)
     assert 1 <= self.strictness <= 3, repr(spec)
     self.name = parts[0]
     if self.strictness == 2:
         self.vspecs = VersionSpec(parts[1])
     elif self.strictness == 3:
         self.ver_build = tuple(parts[1:3])
     self.target = target
     self.optional = optional
     if oparts:
         for opart in oparts.strip()[:-1].split(','):
             if opart == 'optional':
                 self.optional = True
             elif opart.startswith('target='):
                 self.target = opart.split('=')[1].strip()
             else:
                 raise ValueError("Invalid MatchSpec: %s" % spec)
     if self.optional is None:
         self.optional = False
     return self
Example #9
0
 def test_match(self):
     for vspec, res in [
         ('1.7*', True),
         ('1.7.1', True),
         ('1.7.0', False),
         ('1.7', False),
         ('1.5*', False),
         ('>=1.5', True),
         ('!=1.5', True),
         ('!=1.7.1', False),
         ('==1.7.1', True),
         ('==1.7', False),
         ('==1.7.2', False),
         ('==1.7.1.0', True),
     ]:
         m = VersionSpec(vspec)
         self.assertEqual(m.match('1.7.1'), res)
Example #10
0
 def test_match(self):
     for vspec, res in [
         ("1.7*", True),
         ("1.7.1", True),
         ("1.7.0", False),
         ("1.7", False),
         ("1.5*", False),
         (">=1.5", True),
         ("!=1.5", True),
         ("!=1.7.1", False),
         ("==1.7.1", True),
         ("==1.7", False),
         ("==1.7.2", False),
         ("==1.7.1.0", True),
     ]:
         m = VersionSpec(vspec)
         self.assertEqual(m.match("1.7.1"), res)
Example #11
0
 def test_match(self):
     for vspec, res in [
         ('1.7*', True),   ('1.7.1', True),    ('1.7.0', False),
         ('1.7', False),   ('1.5*', False),    ('>=1.5', True),
         ('!=1.5', True),  ('!=1.7.1', False), ('==1.7.1', True),
         ('==1.7', False), ('==1.7.2', False), ('==1.7.1.0', True),
         ('1.7*|1.8*', True), ('1.8*|1.9*', False),
         ('>1.7,<1.8', True), ('>1.7.1,<1.8', False),
         ('^1.7.1$', True), ('^1\.7\.1$', True), ('^1\.7\.[0-9]+$', True),
         ('^1\.8.*$', False), ('^1\.[5-8]\.1$', True), ('^[^1].*$', False),
         ('^[0-9+]+\.[0-9+]+\.[0-9]+$', True), ('^$', False),
         ('^.*$', True), ('1.7.*|^0.*$', True), ('1.6.*|^0.*$', False),
         ('1.6.*|^0.*$|1.7.1', True), ('^0.*$|1.7.1', True),
         ('1.6.*|^.*\.7\.1$|0.7.1', True),
         ]:
         m = VersionSpec(vspec)
         assert VersionSpec(m) is m
         assert str(m) == vspec
         assert repr(m) == "VersionSpec('%s')"%vspec
         self.assertEqual(m.match('1.7.1'), res)
Example #12
0
 def __new__(cls, spec, target=None, optional=False, parent=None):
     if isinstance(spec, cls):
         return spec
     self = _specs.get((spec,target,optional,parent))
     if self:
         return self
     self = object.__new__(cls)
     self.spec = spec
     parts = spec.split()
     self.strictness = len(parts)
     assert 1 <= self.strictness <= 3, repr(spec)
     self.name = parts[0]
     if self.strictness == 2:
         self.vspecs = VersionSpec(parts[1])
     elif self.strictness == 3:
         self.ver_build = tuple(parts[1:3])
     self.target = target
     self.optional = optional
     self.parent = parent
     return self
Example #13
0
 def test_match(self):
     for vspec, res in [
         ('1.7*', True),
         ('1.7.1', True),
         ('1.7.0', False),
         ('1.7', False),
         ('1.5*', False),
         ('>=1.5', True),
         ('!=1.5', True),
         ('!=1.7.1', False),
         ('==1.7.1', True),
         ('==1.7', False),
         ('==1.7.2', False),
         ('==1.7.1.0', True),
         ('1.7*|1.8*', True),
         ('1.8*|1.9*', False),
         ('>1.7,<1.8', True),
         ('>1.7.1,<1.8', False),
         ('^1.7.1$', True),
         ('^1\.7\.1$', True),
         ('^1\.7\.[0-9]+$', True),
         ('^1\.8.*$', False),
         ('^1\.[5-8]\.1$', True),
         ('^[^1].*$', False),
         ('^[0-9+]+\.[0-9+]+\.[0-9]+$', True),
         ('^$', False),
         ('^.*$', True),
         ('1.7.*|^0.*$', True),
         ('1.6.*|^0.*$', False),
         ('1.6.*|^0.*$|1.7.1', True),
         ('^0.*$|1.7.1', True),
         ('1.6.*|^.*\.7\.1$|0.7.1', True),
     ]:
         m = VersionSpec(vspec)
         assert VersionSpec(m) is m
         assert str(m) == vspec
         assert repr(m) == "VersionSpec('%s')" % vspec
         self.assertEqual(m.match('1.7.1'), res)
Example #14
0
 def __new__(cls, spec, target=None, optional=False, negate=False):
     if isinstance(spec, cls):
         return spec
     self = object.__new__(cls)
     self.spec = spec
     parts = spec.split()
     self.strictness = len(parts)
     assert 1 <= self.strictness <= 3, repr(spec)
     self.name = parts[0]
     if self.strictness == 2:
         self.vspecs = VersionSpec(parts[1])
     elif self.strictness == 3:
         self.ver_build = tuple(parts[1:3])
     self.target = target
     self.optional = optional
     self.negate = negate
     return self
Example #15
0
 def __new__(cls, spec, target=None, optional=None):
     if isinstance(spec, cls):
         return spec
     self = object.__new__(cls)
     spec, _, oparts = spec.partition('(')
     self.spec = spec.strip()
     if oparts and oparts.strip()[-1] != ')':
         raise ValueError("Invalid MatchSpec: %s" % spec)
     parts = spec.split()
     self.strictness = len(parts)
     assert 1 <= self.strictness <= 3, repr(spec)
     self.name = parts[0]
     if self.strictness == 1:
         self.match_fast = self.match_fast_1
     else:
         self.version = VersionSpec(parts[1])
         if self.strictness == 2:
             self.match_fast = self.match_fast_2
         else:
             self.build = parts[2]
             if '*' in self.build:
                 rx = r'^(?:%s)$' % self.build.replace('*', r'.*')
                 self.regex = re.compile(rx)
                 self.match_fast = self.match_fast_4
             else:
                 self.match_fast = self.match_fast_3
     self.target = target
     self.optional = optional
     if oparts:
         for opart in oparts.strip()[:-1].split(','):
             if opart == 'optional':
                 self.optional = True
             elif opart.startswith('target='):
                 self.target = opart.split('=')[1].strip()
             else:
                 raise ValueError("Invalid MatchSpec: %s" % spec)
     if self.optional is None:
         self.optional = False
     return self
Example #16
0
class MatchSpec(object):

    def __new__(cls, spec, target=None, optional=False, parent=None):
        if isinstance(spec, cls):
            return spec
        self = _specs.get((spec,target,optional,parent))
        if self:
            return self
        self = object.__new__(cls)
        self.spec = spec
        parts = spec.split()
        self.strictness = len(parts)
        assert 1 <= self.strictness <= 3, repr(spec)
        self.name = parts[0]
        if self.strictness == 2:
            self.vspecs = VersionSpec(parts[1])
        elif self.strictness == 3:
            self.ver_build = tuple(parts[1:3])
        self.target = target
        self.optional = optional
        self.parent = parent
        return self

    def match_fast(self, version, build):
        if self.strictness == 1:
            return True
        elif self.strictness == 2:
            return self.vspecs.match(version)
        else:
            return bool((version, build) == self.ver_build)

    def match(self, info):
        if isinstance(info, string_types):
            name, version, build = info[:-8].rsplit('-',2)
        else:
            if isinstance(info, Package):
                info = info.info
            name = info.get('name')
            version = info.get('version')
            build = info.get('build')
        if name != self.name:
            return False
        return self.match_fast(version, build)

    def to_filename(self):
        if self.strictness == 3:
            return self.name + '-%s-%s.tar.bz2' % self.ver_build
        else:
            return None

    def __eq__(self, other):
        return type(other) is MatchSpec and self.spec == other.spec

    def __hash__(self):
        return hash(self.spec)

    def __repr__(self):
        res = 'MatchSpec(' + self.spec
        if self.target:
            res += ',target=' + self.target
        if self.optional:
            res += ',optional'
        return res + ')'

    def __str__(self):
        res = self.spec
        if res[-1] == '@' and self.strictness == 1:
            res = 'feature "%s"'%res[1:]
        if self.target or self.optional or self.parent:
            mods = []
            if self.target:
                mods.append('target='+self.target)
            if self.parent:
                mods.append('parent='+self.parent)
            if self.optional:
                mods.append('optional')
            res += ' (' + ','.join(mods) + ')'
        return res
Example #17
0
class MatchSpec(object):
    def __new__(cls, spec, target=None, optional=None):
        if isinstance(spec, cls):
            return spec
        self = object.__new__(cls)
        spec, _, oparts = spec.partition('(')
        self.spec = spec.strip()
        if oparts and oparts.strip()[-1] != ')':
            raise ValueError("Invalid MatchSpec: %s" % spec)
        parts = spec.split()
        self.strictness = len(parts)
        assert 1 <= self.strictness <= 3, repr(spec)
        self.name = parts[0]
        if self.strictness == 2:
            self.vspecs = VersionSpec(parts[1])
        elif self.strictness == 3:
            self.ver_build = tuple(parts[1:3])
        self.target = target
        self.optional = optional
        if oparts:
            for opart in oparts.strip()[:-1].split(','):
                if opart == 'optional':
                    self.optional = True
                elif opart.startswith('target='):
                    self.target = opart.split('=')[1].strip()
                else:
                    raise ValueError("Invalid MatchSpec: %s" % spec)
        if self.optional is None:
            self.optional = False
        return self

    def match_fast(self, version, build):
        if self.strictness == 1:
            return True
        elif self.strictness == 2:
            return self.vspecs.match(version)
        else:
            return bool((version, build) == self.ver_build)

    def match(self, info):
        if isinstance(info, string_types):
            name, version, build = info[:-8].rsplit('-', 2)
        else:
            name = info.get('name')
            version = info.get('version')
            build = info.get('build')
        if name != self.name:
            return False
        return self.match_fast(version, build)

    def to_filename(self):
        if self.strictness == 3 and not self.optional:
            return self.name + '-%s-%s.tar.bz2' % self.ver_build
        else:
            return None

    def __eq__(self, other):
        return (type(other) is MatchSpec and
                (self.spec, self.optional, self.target) ==
                (other.spec, other.optional, other.target))

    def __hash__(self):
        return hash(self.spec)

    def __repr__(self):
        return "MatchSpec('%s')" % self.__str__()

    def __str__(self):
        res = self.spec
        if self.optional or self.target:
            args = []
            if self.optional:
                args.append('optional')
            if self.target:
                args.append('target='+self.target)
            res = '%s (%s)' % (res, ','.join(args))
        return res
Example #18
0
class MatchSpec(object):
    def __new__(cls, spec, target=None, optional=False, negate=False):
        if isinstance(spec, cls):
            return spec
        self = object.__new__(cls)
        self.spec = spec
        parts = spec.split()
        self.strictness = len(parts)
        assert 1 <= self.strictness <= 3, repr(spec)
        self.name = parts[0]
        if self.strictness == 2:
            self.vspecs = VersionSpec(parts[1])
        elif self.strictness == 3:
            self.ver_build = tuple(parts[1:3])
        self.target = target
        self.optional = optional
        self.negate = negate
        return self

    def match_fast(self, version, build):
        if self.strictness == 1:
            res = True
        elif self.strictness == 2:
            res = self.vspecs.match(version)
        else:
            res = bool((version, build) == self.ver_build)
        return res != self.negate

    def match(self, info):
        if isinstance(info, string_types):
            name, version, build = info[:-8].rsplit('-', 2)
        else:
            name = info.get('name')
            version = info.get('version')
            build = info.get('build')
        if name != self.name:
            return False
        return self.match_fast(version, build)

    def to_filename(self):
        if self.strictness == 3 and not self.optional and not self.negate:
            return self.name + '-%s-%s.tar.bz2' % self.ver_build
        else:
            return None

    def __eq__(self, other):
        return type(other) is MatchSpec and self.spec == other.spec

    def __hash__(self):
        return hash((self.spec, self.negate))

    def __repr__(self):
        res = 'MatchSpec(' + repr(self.spec)
        if self.target:
            res += ',target=' + repr(self.target)
        if self.optional:
            res += ',optional=True'
        if self.negate:
            res += ',negate=True'
        return res + ')'

    def __str__(self):
        res = self.spec
        if self.target or self.optional:
            mods = []
            if self.target:
                mods.append('target='+str(self.target))
            if self.optional:
                mods.append('optional')
            if self.negate:
                mods.append('negate')
            res += ' (' + ', '.join(mods) + ')'
        return res
Example #19
0
class MatchSpec(object):
    def __new__(cls,
                spec,
                target=None,
                optional=False,
                negate=False,
                parent=None):
        if isinstance(spec, cls):
            return spec
        self = object.__new__(cls)
        self.spec = spec
        parts = spec.split()
        self.strictness = len(parts)
        assert 1 <= self.strictness <= 3, repr(spec)
        self.name = parts[0]
        if self.strictness == 2:
            self.vspecs = VersionSpec(parts[1])
        elif self.strictness == 3:
            self.ver_build = tuple(parts[1:3])
        self.target = target
        self.optional = optional
        self.negate = negate
        self.parent = parent
        return self

    def match_fast(self, version, build):
        if self.strictness == 1:
            res = True
        elif self.strictness == 2:
            res = self.vspecs.match(version)
        else:
            res = bool((version, build) == self.ver_build)
        return res != self.negate

    def match(self, info):
        if isinstance(info, string_types):
            name, version, build = info[:-8].rsplit('-', 2)
        else:
            name = info.get('name')
            version = info.get('version')
            build = info.get('build')
        if name != self.name:
            return False
        return self.match_fast(version, build)

    def to_filename(self):
        if self.strictness == 3 and not self.optional and not self.negate and not self.parent:
            return self.name + '-%s-%s.tar.bz2' % self.ver_build
        else:
            return None

    def __eq__(self, other):
        return type(other) is MatchSpec and self.spec == other.spec

    def __hash__(self):
        return hash((self.spec, self.negate))

    def __repr__(self):
        res = 'MatchSpec(' + repr(self.spec)
        if self.target:
            res += ',target=' + repr(self.target)
        if self.optional:
            res += ',optional=True'
        if self.negate:
            res += ',negate=True'
        return res + ')'

    def __str__(self):
        res = self.spec
        if self.target or self.optional or self.parent:
            mods = []
            if self.target:
                mods.append('target=' + str(self.target))
            if self.parent:
                mods.append('parent=' + str(self.parent))
            if self.optional:
                mods.append('optional')
            if self.negate:
                mods.append('negate')
            res += ' (' + ', '.join(mods) + ')'
        return res
Example #20
0
class MatchSpec(object):
    def __new__(cls, spec, target=None, optional=None):
        if isinstance(spec, cls):
            return spec
        self = object.__new__(cls)
        spec, _, oparts = spec.partition('(')
        self.spec = spec.strip()
        if oparts and oparts.strip()[-1] != ')':
            raise ValueError("Invalid MatchSpec: %s" % spec)
        parts = spec.split()
        self.strictness = len(parts)
        assert 1 <= self.strictness <= 3, repr(spec)
        self.name = parts[0]
        if self.strictness == 1:
            self.match_fast = self.match_fast_1
        else:
            self.version = VersionSpec(parts[1])
            if self.strictness == 2:
                self.match_fast = self.match_fast_2
            else:
                self.build = parts[2]
                if '*' in self.build:
                    rx = r'^(?:%s)$' % self.build.replace('*', r'.*')
                    self.regex = re.compile(rx)
                    self.match_fast = self.match_fast_4
                else:
                    self.match_fast = self.match_fast_3
        self.target = target
        self.optional = optional
        if oparts:
            for opart in oparts.strip()[:-1].split(','):
                if opart == 'optional':
                    self.optional = True
                elif opart.startswith('target='):
                    self.target = opart.split('=')[1].strip()
                else:
                    raise ValueError("Invalid MatchSpec: %s" % spec)
        if self.optional is None:
            self.optional = False
        return self

    def match_fast_1(self, verison, build):
        return True

    def match_fast_2(self, version, build):
        return self.version.match(version)

    def match_fast_3(self, version, build):
        return build == self.build and self.version.match(version)

    def match_fast_4(self, version, build):
        return self.regex.match(build) and self.version.match(version)

    def match(self, info):
        if type(info) is dict:
            name = info.get('name')
            version = info.get('version')
            build = info.get('build')
        else:
            name, version, build = info[:-8].rsplit('-', 2)
        if name != self.name:
            return False
        return self.match_fast(version, build)

    def to_filename(self):
        if self.strictness == 3 and not self.optional and '*' not in self.build:
            return self.name + '-%s-%s.tar.bz2' % (self.version.spec, self.build)
        else:
            return None

    def __lt__(self, other):
        return (type(other) is MatchSpec and
                (self.spec, self.optional, self.target) <
                (other.spec, other.optional, other.target))

    def __eq__(self, other):
        return (type(other) is MatchSpec and
                (self.spec, self.optional, self.target) ==
                (other.spec, other.optional, other.target))

    def __hash__(self):
        return hash(self.spec)

    def __repr__(self):
        return "MatchSpec('%s')" % self.__str__()

    def __str__(self):
        res = self.spec
        if self.optional or self.target:
            args = []
            if self.optional:
                args.append('optional')
            if self.target:
                args.append('target='+self.target)
            res = '%s (%s)' % (res, ','.join(args))
        return res
Example #21
0
class MatchSpec(object):
    def __new__(cls, spec):
        if isinstance(spec, cls):
            return spec
        self = object.__new__(cls)
        spec, _, oparts = spec.partition('(')
        self.spec = spec.strip()
        if oparts and oparts.strip()[-1] != ')':
            raise ValueError("Invalid MatchSpec: %s" % spec)
        parts = spec.split()
        self.strictness = len(parts)
        assert 1 <= self.strictness <= 3, repr(spec)
        self.name = parts[0]
        if self.strictness == 2:
            self.vspecs = VersionSpec(parts[1])
        elif self.strictness == 3:
            self.ver_build = tuple(parts[1:3])
        self.target = None
        self.optional = False
        self.negate = False
        if oparts:
            for opart in oparts.strip()[:-1].split(','):
                if opart == 'optional':
                    self.optional = True
                elif opart == 'negate':
                    self.negate = True
                elif opart.startswith('target='):
                    self.target = opart.split('=')[1].strip()
                else:
                    raise ValueError("Invalid MatchSpec: %s" % spec)
        return self

    def match_fast(self, version, build):
        if self.strictness == 1:
            res = True
        elif self.strictness == 2:
            res = self.vspecs.match(version)
        else:
            res = bool((version, build) == self.ver_build)
        return res != self.negate

    def match(self, info):
        if isinstance(info, string_types):
            name, version, build = info[:-8].rsplit('-', 2)
        else:
            name = info.get('name')
            version = info.get('version')
            build = info.get('build')
        if name != self.name:
            return False
        return self.match_fast(version, build)

    def to_filename(self):
        if self.strictness == 3 and not self.optional and not self.negate:
            return self.name + '-%s-%s.tar.bz2' % self.ver_build
        else:
            return None

    def __eq__(self, other):
        return (type(other) is MatchSpec
                and (self.spec, self.optional, self.negate, self.target)
                == (other.spec, other.optional, other.negate, other.target))

    def __hash__(self):
        return hash((self.spec, self.negate))

    def __repr__(self):
        return "MatchSpec('%s')" % self.__str__()

    def __str__(self):
        res = self.spec
        if self.optional or self.negate or self.target:
            args = []
            if self.optional:
                args.append('optional')
            if self.negate:
                args.append('negate')
            if self.target:
                args.append('target=' + self.target)
            res = '%s (%s)' % (res, ','.join(args))
        return res