1 """Module for parsing and testing package version predicate strings.
4 import distutils
.version
8 re_validPackage
= re
.compile(r
"(?i)^\s*([a-z_]\w*(?:\.[a-z_]\w*)*)(.*)")
11 re_paren
= re
.compile(r
"^\s*\((.*)\)\s*$") # (list) inside of parentheses
12 re_splitComparison
= re
.compile(r
"^\s*(<=|>=|<|>|!=|==)\s*([^\s,]+)\s*$")
17 """Parse a single version comparison.
19 Return (comparison string, StrictVersion)
21 res
= re_splitComparison
.match(pred
)
23 raise ValueError("bad package restriction syntax: %r" % pred
)
24 comp
, verStr
= res
.groups()
25 return (comp
, distutils
.version
.StrictVersion(verStr
))
27 compmap
= {"<": operator
.lt
, "<=": operator
.le
, "==": operator
.eq
,
28 ">": operator
.gt
, ">=": operator
.ge
, "!=": operator
.ne
}
30 class VersionPredicate
:
31 """Parse and test package version predicates.
33 >>> v = VersionPredicate('pyepat.abc (>1.0, <3333.3a1, !=1555.1b3)')
35 The `name` attribute provides the full dotted name that is given::
40 The str() of a `VersionPredicate` provides a normalized
41 human-readable version of the expression::
44 pyepat.abc (> 1.0, < 3333.3a1, != 1555.1b3)
46 The `satisfied_by()` method can be used to determine with a given
47 version number is included in the set described by the version
50 >>> v.satisfied_by('1.1')
52 >>> v.satisfied_by('1.4')
54 >>> v.satisfied_by('1.0')
56 >>> v.satisfied_by('4444.4')
58 >>> v.satisfied_by('1555.1b3')
61 `VersionPredicate` is flexible in accepting extra whitespace::
63 >>> v = VersionPredicate(' pat( == 0.1 ) ')
66 >>> v.satisfied_by('0.1')
68 >>> v.satisfied_by('0.2')
71 If any version numbers passed in do not conform to the
72 restrictions of `StrictVersion`, a `ValueError` is raised::
74 >>> v = VersionPredicate('p1.p2.p3.p4(>=1.0, <=1.3a1, !=1.2zb3)')
75 Traceback (most recent call last):
77 ValueError: invalid version number '1.2zb3'
79 It the module or package name given does not conform to what's
80 allowed as a legal module or package name, `ValueError` is
83 >>> v = VersionPredicate('foo-bar')
84 Traceback (most recent call last):
86 ValueError: expected parenthesized list: '-bar'
88 >>> v = VersionPredicate('foo bar (12.21)')
89 Traceback (most recent call last):
91 ValueError: expected parenthesized list: 'bar (12.21)'
95 def __init__(self
, versionPredicateStr
):
96 """Parse a version predicate string.
100 # pred: list of (comparison string, StrictVersion)
102 versionPredicateStr
= versionPredicateStr
.strip()
103 if not versionPredicateStr
:
104 raise ValueError("empty package restriction")
105 match
= re_validPackage
.match(versionPredicateStr
)
107 raise ValueError("bad package name in %r" % versionPredicateStr
)
108 self
.name
, paren
= match
.groups()
109 paren
= paren
.strip()
111 match
= re_paren
.match(paren
)
113 raise ValueError("expected parenthesized list: %r" % paren
)
114 str = match
.groups()[0]
115 self
.pred
= [splitUp(aPred
) for aPred
in str.split(",")]
117 raise ValueError("empty parenthesized list in %r"
118 % versionPredicateStr
)
124 seq
= [cond
+ " " + str(ver
) for cond
, ver
in self
.pred
]
125 return self
.name
+ " (" + ", ".join(seq
) + ")"
129 def satisfied_by(self
, version
):
130 """True if version is compatible with all the predicates in self.
131 The parameter version must be acceptable to the StrictVersion
132 constructor. It may be either a string or StrictVersion.
134 for cond
, ver
in self
.pred
:
135 if not compmap
[cond
](version
, ver
):
142 def split_provision(value
):
143 """Return the name and optional version number of a provision.
145 The version number, if given, will be returned as a `StrictVersion`
146 instance, otherwise it will be `None`.
148 >>> split_provision('mypkg')
150 >>> split_provision(' mypkg( 1.2 ) ')
151 ('mypkg', StrictVersion ('1.2'))
154 if _provision_rx
is None:
155 _provision_rx
= re
.compile(
156 "([a-zA-Z_]\w*(?:\.[a-zA-Z_]\w*)*)(?:\s*\(\s*([^)\s]+)\s*\))?$")
157 value
= value
.strip()
158 m
= _provision_rx
.match(value
)
160 raise ValueError("illegal provides specification: %r" % value
)
161 ver
= m
.group(2) or None
163 ver
= distutils
.version
.StrictVersion(ver
)
164 return m
.group(1), ver