1 | n/a | """Module for parsing and testing package version predicate strings. |
---|
2 | n/a | """ |
---|
3 | n/a | import re |
---|
4 | n/a | import distutils.version |
---|
5 | n/a | import operator |
---|
6 | n/a | |
---|
7 | n/a | |
---|
8 | n/a | re_validPackage = re.compile(r"(?i)^\s*([a-z_]\w*(?:\.[a-z_]\w*)*)(.*)", |
---|
9 | n/a | re.ASCII) |
---|
10 | n/a | # (package) (rest) |
---|
11 | n/a | |
---|
12 | n/a | re_paren = re.compile(r"^\s*\((.*)\)\s*$") # (list) inside of parentheses |
---|
13 | n/a | re_splitComparison = re.compile(r"^\s*(<=|>=|<|>|!=|==)\s*([^\s,]+)\s*$") |
---|
14 | n/a | # (comp) (version) |
---|
15 | n/a | |
---|
16 | n/a | |
---|
17 | n/a | def splitUp(pred): |
---|
18 | n/a | """Parse a single version comparison. |
---|
19 | n/a | |
---|
20 | n/a | Return (comparison string, StrictVersion) |
---|
21 | n/a | """ |
---|
22 | n/a | res = re_splitComparison.match(pred) |
---|
23 | n/a | if not res: |
---|
24 | n/a | raise ValueError("bad package restriction syntax: %r" % pred) |
---|
25 | n/a | comp, verStr = res.groups() |
---|
26 | n/a | return (comp, distutils.version.StrictVersion(verStr)) |
---|
27 | n/a | |
---|
28 | n/a | compmap = {"<": operator.lt, "<=": operator.le, "==": operator.eq, |
---|
29 | n/a | ">": operator.gt, ">=": operator.ge, "!=": operator.ne} |
---|
30 | n/a | |
---|
31 | n/a | class VersionPredicate: |
---|
32 | n/a | """Parse and test package version predicates. |
---|
33 | n/a | |
---|
34 | n/a | >>> v = VersionPredicate('pyepat.abc (>1.0, <3333.3a1, !=1555.1b3)') |
---|
35 | n/a | |
---|
36 | n/a | The `name` attribute provides the full dotted name that is given:: |
---|
37 | n/a | |
---|
38 | n/a | >>> v.name |
---|
39 | n/a | 'pyepat.abc' |
---|
40 | n/a | |
---|
41 | n/a | The str() of a `VersionPredicate` provides a normalized |
---|
42 | n/a | human-readable version of the expression:: |
---|
43 | n/a | |
---|
44 | n/a | >>> print(v) |
---|
45 | n/a | pyepat.abc (> 1.0, < 3333.3a1, != 1555.1b3) |
---|
46 | n/a | |
---|
47 | n/a | The `satisfied_by()` method can be used to determine with a given |
---|
48 | n/a | version number is included in the set described by the version |
---|
49 | n/a | restrictions:: |
---|
50 | n/a | |
---|
51 | n/a | >>> v.satisfied_by('1.1') |
---|
52 | n/a | True |
---|
53 | n/a | >>> v.satisfied_by('1.4') |
---|
54 | n/a | True |
---|
55 | n/a | >>> v.satisfied_by('1.0') |
---|
56 | n/a | False |
---|
57 | n/a | >>> v.satisfied_by('4444.4') |
---|
58 | n/a | False |
---|
59 | n/a | >>> v.satisfied_by('1555.1b3') |
---|
60 | n/a | False |
---|
61 | n/a | |
---|
62 | n/a | `VersionPredicate` is flexible in accepting extra whitespace:: |
---|
63 | n/a | |
---|
64 | n/a | >>> v = VersionPredicate(' pat( == 0.1 ) ') |
---|
65 | n/a | >>> v.name |
---|
66 | n/a | 'pat' |
---|
67 | n/a | >>> v.satisfied_by('0.1') |
---|
68 | n/a | True |
---|
69 | n/a | >>> v.satisfied_by('0.2') |
---|
70 | n/a | False |
---|
71 | n/a | |
---|
72 | n/a | If any version numbers passed in do not conform to the |
---|
73 | n/a | restrictions of `StrictVersion`, a `ValueError` is raised:: |
---|
74 | n/a | |
---|
75 | n/a | >>> v = VersionPredicate('p1.p2.p3.p4(>=1.0, <=1.3a1, !=1.2zb3)') |
---|
76 | n/a | Traceback (most recent call last): |
---|
77 | n/a | ... |
---|
78 | n/a | ValueError: invalid version number '1.2zb3' |
---|
79 | n/a | |
---|
80 | n/a | It the module or package name given does not conform to what's |
---|
81 | n/a | allowed as a legal module or package name, `ValueError` is |
---|
82 | n/a | raised:: |
---|
83 | n/a | |
---|
84 | n/a | >>> v = VersionPredicate('foo-bar') |
---|
85 | n/a | Traceback (most recent call last): |
---|
86 | n/a | ... |
---|
87 | n/a | ValueError: expected parenthesized list: '-bar' |
---|
88 | n/a | |
---|
89 | n/a | >>> v = VersionPredicate('foo bar (12.21)') |
---|
90 | n/a | Traceback (most recent call last): |
---|
91 | n/a | ... |
---|
92 | n/a | ValueError: expected parenthesized list: 'bar (12.21)' |
---|
93 | n/a | |
---|
94 | n/a | """ |
---|
95 | n/a | |
---|
96 | n/a | def __init__(self, versionPredicateStr): |
---|
97 | n/a | """Parse a version predicate string. |
---|
98 | n/a | """ |
---|
99 | n/a | # Fields: |
---|
100 | n/a | # name: package name |
---|
101 | n/a | # pred: list of (comparison string, StrictVersion) |
---|
102 | n/a | |
---|
103 | n/a | versionPredicateStr = versionPredicateStr.strip() |
---|
104 | n/a | if not versionPredicateStr: |
---|
105 | n/a | raise ValueError("empty package restriction") |
---|
106 | n/a | match = re_validPackage.match(versionPredicateStr) |
---|
107 | n/a | if not match: |
---|
108 | n/a | raise ValueError("bad package name in %r" % versionPredicateStr) |
---|
109 | n/a | self.name, paren = match.groups() |
---|
110 | n/a | paren = paren.strip() |
---|
111 | n/a | if paren: |
---|
112 | n/a | match = re_paren.match(paren) |
---|
113 | n/a | if not match: |
---|
114 | n/a | raise ValueError("expected parenthesized list: %r" % paren) |
---|
115 | n/a | str = match.groups()[0] |
---|
116 | n/a | self.pred = [splitUp(aPred) for aPred in str.split(",")] |
---|
117 | n/a | if not self.pred: |
---|
118 | n/a | raise ValueError("empty parenthesized list in %r" |
---|
119 | n/a | % versionPredicateStr) |
---|
120 | n/a | else: |
---|
121 | n/a | self.pred = [] |
---|
122 | n/a | |
---|
123 | n/a | def __str__(self): |
---|
124 | n/a | if self.pred: |
---|
125 | n/a | seq = [cond + " " + str(ver) for cond, ver in self.pred] |
---|
126 | n/a | return self.name + " (" + ", ".join(seq) + ")" |
---|
127 | n/a | else: |
---|
128 | n/a | return self.name |
---|
129 | n/a | |
---|
130 | n/a | def satisfied_by(self, version): |
---|
131 | n/a | """True if version is compatible with all the predicates in self. |
---|
132 | n/a | The parameter version must be acceptable to the StrictVersion |
---|
133 | n/a | constructor. It may be either a string or StrictVersion. |
---|
134 | n/a | """ |
---|
135 | n/a | for cond, ver in self.pred: |
---|
136 | n/a | if not compmap[cond](version, ver): |
---|
137 | n/a | return False |
---|
138 | n/a | return True |
---|
139 | n/a | |
---|
140 | n/a | |
---|
141 | n/a | _provision_rx = None |
---|
142 | n/a | |
---|
143 | n/a | def split_provision(value): |
---|
144 | n/a | """Return the name and optional version number of a provision. |
---|
145 | n/a | |
---|
146 | n/a | The version number, if given, will be returned as a `StrictVersion` |
---|
147 | n/a | instance, otherwise it will be `None`. |
---|
148 | n/a | |
---|
149 | n/a | >>> split_provision('mypkg') |
---|
150 | n/a | ('mypkg', None) |
---|
151 | n/a | >>> split_provision(' mypkg( 1.2 ) ') |
---|
152 | n/a | ('mypkg', StrictVersion ('1.2')) |
---|
153 | n/a | """ |
---|
154 | n/a | global _provision_rx |
---|
155 | n/a | if _provision_rx is None: |
---|
156 | n/a | _provision_rx = re.compile( |
---|
157 | n/a | r"([a-zA-Z_]\w*(?:\.[a-zA-Z_]\w*)*)(?:\s*\(\s*([^)\s]+)\s*\))?$", |
---|
158 | n/a | re.ASCII) |
---|
159 | n/a | value = value.strip() |
---|
160 | n/a | m = _provision_rx.match(value) |
---|
161 | n/a | if not m: |
---|
162 | n/a | raise ValueError("illegal provides specification: %r" % value) |
---|
163 | n/a | ver = m.group(2) or None |
---|
164 | n/a | if ver: |
---|
165 | n/a | ver = distutils.version.StrictVersion(ver) |
---|
166 | n/a | return m.group(1), ver |
---|