Fred Drake | 54398d6 | 2005-03-20 22:17:02 +0000 | [diff] [blame] | 1 | """Module for parsing and testing package version predicate strings. |
| 2 | """ |
| 3 | import re |
| 4 | import version |
| 5 | import operator |
| 6 | |
| 7 | re_validPackage = re.compile(r"(?i)^\s*([a-z_]\w*(?:\.[a-z_]\w*)*)(.*)") |
| 8 | # (package) (rest) |
| 9 | |
| 10 | re_paren = re.compile(r"^\s*\((.*)\)\s*$") # (list) inside of parentheses |
| 11 | re_splitComparison = re.compile(r"^\s*(<=|>=|<|>|!=|==)\s*([^\s,]+)\s*$") |
| 12 | # (comp) (version) |
| 13 | |
| 14 | def splitUp(pred): |
| 15 | """Parse a single version comparison. |
| 16 | Return (comparison string, StrictVersion) |
| 17 | """ |
| 18 | res = re_splitComparison.match(pred) |
| 19 | if not res: |
| 20 | raise ValueError, "Bad package restriction syntax: " + pred |
| 21 | comp, verStr = res.groups() |
| 22 | return (comp, version.StrictVersion(verStr)) |
| 23 | |
| 24 | compmap = {"<": operator.lt, "<=": operator.le, "==": operator.eq, |
| 25 | ">": operator.gt, ">=": operator.ge, "!=": operator.ne} |
| 26 | |
| 27 | class VersionPredicate: |
| 28 | """Parse and test package version predicates. |
| 29 | |
| 30 | >>> v = VersionPredicate("pyepat.abc (>1.0, <3333.3a1, !=1555.1b3)") |
| 31 | >>> print v |
| 32 | pyepat.abc (> 1.0, < 3333.3a1, != 1555.1b3) |
| 33 | >>> v.satisfied_by("1.1") |
| 34 | True |
| 35 | >>> v.satisfied_by("1.4") |
| 36 | True |
| 37 | >>> v.satisfied_by("1.0") |
| 38 | False |
| 39 | >>> v.satisfied_by("4444.4") |
| 40 | False |
| 41 | >>> v.satisfied_by("1555.1b3") |
| 42 | False |
| 43 | >>> v = VersionPredicate("pat( == 0.1 ) ") |
| 44 | >>> v.satisfied_by("0.1") |
| 45 | True |
| 46 | >>> v.satisfied_by("0.2") |
| 47 | False |
| 48 | >>> v = VersionPredicate("p1.p2.p3.p4(>=1.0, <=1.3a1, !=1.2zb3)") |
| 49 | Traceback (most recent call last): |
| 50 | ... |
| 51 | ValueError: invalid version number '1.2zb3' |
| 52 | |
| 53 | """ |
| 54 | |
| 55 | def __init__(self, versionPredicateStr): |
| 56 | """Parse a version predicate string. |
| 57 | """ |
| 58 | # Fields: |
| 59 | # name: package name |
| 60 | # pred: list of (comparison string, StrictVersion) |
| 61 | |
| 62 | versionPredicateStr = versionPredicateStr.strip() |
| 63 | if not versionPredicateStr: |
| 64 | raise ValueError, "Empty package restriction" |
| 65 | match = re_validPackage.match(versionPredicateStr) |
| 66 | if not match: |
| 67 | raise ValueError, "Bad package name in " + versionPredicateStr |
| 68 | self.name, paren = match.groups() |
| 69 | paren = paren.strip() |
| 70 | if paren: |
| 71 | match = re_paren.match(paren) |
| 72 | if not match: |
| 73 | raise ValueError, "Expected parenthesized list: " + paren |
| 74 | str = match.groups()[0] |
| 75 | self.pred = [splitUp(aPred) for aPred in str.split(",")] |
| 76 | if not self.pred: |
| 77 | raise ValueError("Empty Parenthesized list in %r" |
| 78 | % versionPredicateStr ) |
| 79 | else: |
| 80 | self.pred=[] |
| 81 | |
| 82 | def __str__(self): |
| 83 | if self.pred: |
| 84 | seq = [cond + " " + str(ver) for cond, ver in self.pred] |
| 85 | return self.name + " (" + ", ".join(seq) + ")" |
| 86 | else: |
| 87 | return self.name |
| 88 | |
| 89 | def satisfied_by(self, version): |
| 90 | """True if version is compatible with all the predicates in self. |
| 91 | The parameter version must be acceptable to the StrictVersion |
| 92 | constructor. It may be either a string or StrictVersion. |
| 93 | """ |
| 94 | for cond, ver in self.pred: |
| 95 | if not compmap[cond](version, ver): |
| 96 | return False |
| 97 | return True |
| 98 | |
| 99 | |
| 100 | def check_provision(value): |
| 101 | m = re.match("[a-zA-Z_]\w*(\.[a-zA-Z_]\w*)*(\s*\([^)]+\))?$", value) |
| 102 | if not m: |
| 103 | raise ValueError("illegal provides specification: %r" % value) |