Path: blob/master/venv/Lib/site-packages/setuptools/_distutils/versionpredicate.py
811 views
"""Module for parsing and testing package version predicate strings.1"""2import re3import distutils.version4import operator567re_validPackage = re.compile(r"(?i)^\s*([a-z_]\w*(?:\.[a-z_]\w*)*)(.*)",8re.ASCII)9# (package) (rest)1011re_paren = re.compile(r"^\s*\((.*)\)\s*$") # (list) inside of parentheses12re_splitComparison = re.compile(r"^\s*(<=|>=|<|>|!=|==)\s*([^\s,]+)\s*$")13# (comp) (version)141516def splitUp(pred):17"""Parse a single version comparison.1819Return (comparison string, StrictVersion)20"""21res = re_splitComparison.match(pred)22if not res:23raise ValueError("bad package restriction syntax: %r" % pred)24comp, verStr = res.groups()25return (comp, distutils.version.StrictVersion(verStr))2627compmap = {"<": operator.lt, "<=": operator.le, "==": operator.eq,28">": operator.gt, ">=": operator.ge, "!=": operator.ne}2930class VersionPredicate:31"""Parse and test package version predicates.3233>>> v = VersionPredicate('pyepat.abc (>1.0, <3333.3a1, !=1555.1b3)')3435The `name` attribute provides the full dotted name that is given::3637>>> v.name38'pyepat.abc'3940The str() of a `VersionPredicate` provides a normalized41human-readable version of the expression::4243>>> print(v)44pyepat.abc (> 1.0, < 3333.3a1, != 1555.1b3)4546The `satisfied_by()` method can be used to determine with a given47version number is included in the set described by the version48restrictions::4950>>> v.satisfied_by('1.1')51True52>>> v.satisfied_by('1.4')53True54>>> v.satisfied_by('1.0')55False56>>> v.satisfied_by('4444.4')57False58>>> v.satisfied_by('1555.1b3')59False6061`VersionPredicate` is flexible in accepting extra whitespace::6263>>> v = VersionPredicate(' pat( == 0.1 ) ')64>>> v.name65'pat'66>>> v.satisfied_by('0.1')67True68>>> v.satisfied_by('0.2')69False7071If any version numbers passed in do not conform to the72restrictions of `StrictVersion`, a `ValueError` is raised::7374>>> v = VersionPredicate('p1.p2.p3.p4(>=1.0, <=1.3a1, !=1.2zb3)')75Traceback (most recent call last):76...77ValueError: invalid version number '1.2zb3'7879It the module or package name given does not conform to what's80allowed as a legal module or package name, `ValueError` is81raised::8283>>> v = VersionPredicate('foo-bar')84Traceback (most recent call last):85...86ValueError: expected parenthesized list: '-bar'8788>>> v = VersionPredicate('foo bar (12.21)')89Traceback (most recent call last):90...91ValueError: expected parenthesized list: 'bar (12.21)'9293"""9495def __init__(self, versionPredicateStr):96"""Parse a version predicate string.97"""98# Fields:99# name: package name100# pred: list of (comparison string, StrictVersion)101102versionPredicateStr = versionPredicateStr.strip()103if not versionPredicateStr:104raise ValueError("empty package restriction")105match = re_validPackage.match(versionPredicateStr)106if not match:107raise ValueError("bad package name in %r" % versionPredicateStr)108self.name, paren = match.groups()109paren = paren.strip()110if paren:111match = re_paren.match(paren)112if not match:113raise ValueError("expected parenthesized list: %r" % paren)114str = match.groups()[0]115self.pred = [splitUp(aPred) for aPred in str.split(",")]116if not self.pred:117raise ValueError("empty parenthesized list in %r"118% versionPredicateStr)119else:120self.pred = []121122def __str__(self):123if self.pred:124seq = [cond + " " + str(ver) for cond, ver in self.pred]125return self.name + " (" + ", ".join(seq) + ")"126else:127return self.name128129def satisfied_by(self, version):130"""True if version is compatible with all the predicates in self.131The parameter version must be acceptable to the StrictVersion132constructor. It may be either a string or StrictVersion.133"""134for cond, ver in self.pred:135if not compmap[cond](version, ver):136return False137return True138139140_provision_rx = None141142def split_provision(value):143"""Return the name and optional version number of a provision.144145The version number, if given, will be returned as a `StrictVersion`146instance, otherwise it will be `None`.147148>>> split_provision('mypkg')149('mypkg', None)150>>> split_provision(' mypkg( 1.2 ) ')151('mypkg', StrictVersion ('1.2'))152"""153global _provision_rx154if _provision_rx is None:155_provision_rx = re.compile(156r"([a-zA-Z_]\w*(?:\.[a-zA-Z_]\w*)*)(?:\s*\(\s*([^)\s]+)\s*\))?$",157re.ASCII)158value = value.strip()159m = _provision_rx.match(value)160if not m:161raise ValueError("illegal provides specification: %r" % value)162ver = m.group(2) or None163if ver:164ver = distutils.version.StrictVersion(ver)165return m.group(1), ver166167168