diff options
| author | Tarek Ziad? <tarek@ziade.org> | 2010-02-25 01:41:34 -0500 |
|---|---|---|
| committer | Tarek Ziad? <tarek@ziade.org> | 2010-02-25 01:41:34 -0500 |
| commit | b926293bd4b071eca4d2e16c7246845f7fd27a51 (patch) | |
| tree | 92699508311d994bac6eac6dd1fd48fc64bc6a5d /src/distutils2 | |
| parent | e313d42f46446c76a3e60267e9df29e51ff92722 (diff) | |
| download | disutils2-b926293bd4b071eca4d2e16c7246845f7fd27a51.tar.gz | |
VersionPredicate class is ready
Diffstat (limited to 'src/distutils2')
| -rw-r--r-- | src/distutils2/tests/test_version.py | 26 | ||||
| -rw-r--r-- | src/distutils2/version.py | 35 |
2 files changed, 55 insertions, 6 deletions
diff --git a/src/distutils2/tests/test_version.py b/src/distutils2/tests/test_version.py index 6a57979..2f0f54f 100644 --- a/src/distutils2/tests/test_version.py +++ b/src/distutils2/tests/test_version.py @@ -6,6 +6,7 @@ import os from distutils2.version import NormalizedVersion as V from distutils2.version import IrrationalVersionError from distutils2.version import suggest_normalized_version as suggest +from distutils2.version import VersionPredicate class VersionTestCase(unittest.TestCase): @@ -124,6 +125,31 @@ class VersionTestCase(unittest.TestCase): # they us "p1" "p2" for post releases self.assertEquals(suggest('1.4p1'), '1.4.post1') + def test_predicate(self): + # VersionPredicate knows how to parse stuff like: + # + # Project (>=version, ver2) + + predicates = ('zope.interface (>3.5.0)', + 'AnotherProject (3.4)', + 'OtherProject (<3.0)', + 'NoVersion', + 'Hey (>=2.5,<2.7)') + + for predicate in predicates: + v = VersionPredicate(predicate) + + assert VersionPredicate('Hey (>=2.5,<2.7)').match('2.6') + assert VersionPredicate('Ho').match('2.6') + assert not VersionPredicate('Hey (>=2.5,!=2.6,<2.7)').match('2.6') + assert VersionPredicate('Ho (<3.0)').match('2.6') + assert VersionPredicate('Ho (<3.0,!=2.5)').match('2.6.0') + assert not VersionPredicate('Ho (<3.0,!=2.6)').match('2.6.0') + + + # XXX need to silent the micro version in this case + #assert not VersionPredicate('Ho (<3.0,!=2.6)').match('2.6.3') + def test_suite(): #README = os.path.join(os.path.dirname(__file__), 'README.txt') #suite = [doctest.DocFileSuite(README), unittest.makeSuite(VersionTestCase)] diff --git a/src/distutils2/version.py b/src/distutils2/version.py index 3704260..da127dc 100644 --- a/src/distutils2/version.py +++ b/src/distutils2/version.py @@ -326,24 +326,47 @@ _VERSIONS = re.compile(r"^\s*\((.*)\)\s*$") _SPLIT_CMP = re.compile(r"^\s*(<=|>=|<|>|!=|==)\s*([^\s,]+)\s*$") def _split_predicate(predicate): - match = _SPLIT_CMP(predicate) + match = _SPLIT_CMP.match(predicate) if match is None: - raise ValueError('Bad package restriction syntax: %r' % version) - comp, version = res.groups() + # probably no op, we'll use "==" + comp, version = '==', predicate + else: + comp, version = match.groups() return comp, NormalizedVersion(version) class VersionPredicate(object): """Defines a predicate: ProjectName (>ver1,ver2, ..)""" + + _operators = {"<": lambda x, y: x < y, + ">": lambda x, y: x > y, + "<=": lambda x, y: x <= y, + ">=": lambda x, y: x >= y, + "==": lambda x, y: x == y, + "!=": lambda x, y: x != y} + def __init__(self, predicate): predicate = predicate.strip() match = _PREDICATE.match(predicate) if match is None: raise ValueError('Bad predicate "%s"' % predicate) + self.name, predicates = match.groups() predicates = predicates.strip() + predicates = _VERSIONS.match(predicates) if predicates is not None: - predicates = match.groups()[0] - self.versions = [_split_predicate(pred) - for pred in predicates.split(',')] + predicates = predicates.groups()[0] + self.predicates = [_split_predicate(pred.strip()) + for pred in predicates.split(',')] + else: + self.predicates = [] + + def match(self, version): + """Check if the provided version matches the predicates.""" + if isinstance(version, str): + version = NormalizedVersion(version) + for operator, predicate in self.predicates: + if not self._operators[operator](version, predicate): + return False + return True |
