Skip to content

Commit

Permalink
Merge pull request conda#2933 from kalefranz/versionspec-regex
Browse files Browse the repository at this point in the history
VersionSpec: expose full regex power
  • Loading branch information
kalefranz authored Jun 30, 2016
2 parents 751ea9c + f42c1c0 commit af87ef9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
17 changes: 17 additions & 0 deletions conda/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,15 @@ def __ge__(self, other):
# '<= 1.2' (space after operator), '<>1.2' (unknown operator),
# and '<=!1.2' (nonsensical operator).
version_relation_re = re.compile(r'(==|!=|<=|>=|<|>)(?![=<>!])(\S+)$')
regex_split_re = re.compile(r'(\^\S+?\$)')
regex_split_converter = {
'|': 'any',
',': 'all',
}
opdict = {'==': op.__eq__, '!=': op.__ne__, '<=': op.__le__,
'>=': op.__ge__, '<': op.__lt__, '>': op.__gt__}


class VersionSpec(object):
def exact_match_(self, vspec):
return self.spec == vspec
Expand All @@ -302,6 +308,17 @@ def __new__(cls, spec):
self.spec = spec
if isinstance(spec, tuple):
self.match = self.all_match_ if spec[0] == 'all' else self.any_match_
elif regex_split_re.match(spec):
m = regex_split_re.match(spec)
first = m.group()
operator = spec[m.end()] if len(spec) > m.end() else None
if operator is None:
self.spec = first
self.regex = re.compile(spec)
self.match = self.regex_match_
else:
return VersionSpec((regex_split_converter[operator],
tuple(VersionSpec(s) for s in (first, spec[m.end()+1:]))))
elif '|' in spec:
return VersionSpec(('any', tuple(VersionSpec(s) for s in spec.split('|'))))
elif ',' in spec:
Expand Down
8 changes: 7 additions & 1 deletion tests/test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,13 @@ def test_match(self):
('!=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.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
Expand Down

0 comments on commit af87ef9

Please sign in to comment.