|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# |
| 3 | +# Visit https://aboutcode.org and https://github.com/aboutcode-org/univers for support and download. |
| 4 | + |
| 5 | +import re |
| 6 | + |
| 7 | + |
| 8 | +class IntdotVersion: |
| 9 | + """ |
| 10 | + intdot version. |
| 11 | +
|
| 12 | + The regex pattern for the intdot version is any number (>0) of integers separated by dots, followed by an arbitrary number of other characters, e.g., 1.2.3.5543, 123.234-prerelease, 1.2.3alpha |
| 13 | + """ |
| 14 | + |
| 15 | + VERSION_PATTERN = r"^(\d+(\.\d+)*)(.*)$" |
| 16 | + |
| 17 | + def __init__(self, version): |
| 18 | + if not self.is_valid(version): |
| 19 | + raise InvalidVersionError(version) |
| 20 | + |
| 21 | + version = str(version).strip() |
| 22 | + self.original = version |
| 23 | + |
| 24 | + def __eq__(self, other): |
| 25 | + return self.original == other.original |
| 26 | + |
| 27 | + def __lt__(self, other): |
| 28 | + return self.__cmp__(other) < 0 |
| 29 | + |
| 30 | + def __le__(self, other): |
| 31 | + return self.__cmp__(other) <= 0 |
| 32 | + |
| 33 | + def __gt__(self, other): |
| 34 | + return self.__cmp__(other) > 0 |
| 35 | + |
| 36 | + def __ge__(self, other): |
| 37 | + return self.__cmp__(other) >= 0 |
| 38 | + |
| 39 | + @classmethod |
| 40 | + def is_valid(cls, string): |
| 41 | + return re.compile(IntdotVersion.VERSION_PATTERN).match(string) |
| 42 | + |
| 43 | + def extract_numeric_labels(self, version): |
| 44 | + """ |
| 45 | + Check if the version matches the pattern; if it matches, extract the first group (identified by parentheses) which is the numeric part of the version |
| 46 | + """ |
| 47 | + match = re.match(IntdotVersion.VERSION_PATTERN, version) |
| 48 | + |
| 49 | + if match: |
| 50 | + version_labels = match.group(1) |
| 51 | + return version_labels |
| 52 | + else: |
| 53 | + raise InvalidVersionError(version) |
| 54 | + |
| 55 | + def __cmp__(self, other): |
| 56 | + """ |
| 57 | + Compare this version with ``other`` returning -1, 0, or 1 if the |
| 58 | + other version is larger, the same, or smaller than this |
| 59 | + one. |
| 60 | + """ |
| 61 | + if isinstance(other, str): |
| 62 | + other = IntdotVersion(other) |
| 63 | + |
| 64 | + if not isinstance(other, IntdotVersion): |
| 65 | + raise InvalidVersionError |
| 66 | + |
| 67 | + if self.original == other.original: |
| 68 | + return 0 |
| 69 | + |
| 70 | + lhlabels = self.extract_numeric_labels(self.original) |
| 71 | + rhlabels = self.extract_numeric_labels(other.original) |
| 72 | + |
| 73 | + if lhlabels == rhlabels: |
| 74 | + return 0 |
| 75 | + |
| 76 | + lhsize = len(lhlabels) |
| 77 | + rhsize = len(rhlabels) |
| 78 | + |
| 79 | + if lhsize > rhsize: |
| 80 | + limit = lhsize |
| 81 | + else: |
| 82 | + limit = rhsize |
| 83 | + |
| 84 | + limit -= 1 |
| 85 | + |
| 86 | + i = 0 |
| 87 | + |
| 88 | + while i <= limit: |
| 89 | + try: |
| 90 | + lhs = lhlabels[i] |
| 91 | + except IndexError: |
| 92 | + lhs = 0 |
| 93 | + |
| 94 | + try: |
| 95 | + rhs = rhlabels[i] |
| 96 | + except IndexError: |
| 97 | + rhs = 0 |
| 98 | + |
| 99 | + i += 1 |
| 100 | + |
| 101 | + if lhs == rhs: |
| 102 | + continue |
| 103 | + |
| 104 | + if int(lhs) > int(rhs): |
| 105 | + return 1 |
| 106 | + if int(lhs) < int(rhs): |
| 107 | + return -1 |
| 108 | + return 0 |
| 109 | + |
| 110 | + |
| 111 | +class InvalidVersionError(ValueError): |
| 112 | + pass |
0 commit comments