Skip to content

Instantly share code, notes, and snippets.

@msukmanowsky
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msukmanowsky/9bb8b96906bbbe2daf72 to your computer and use it in GitHub Desktop.
Save msukmanowsky/9bb8b96906bbbe2daf72 to your computer and use it in GitHub Desktop.
Parse Apache Storm versions in Python and do easy comparisons on them. You could probably even import something from here https://github.com/pypa/pip/blob/19e29fc2e8e57a671e584726655bbb42c6e15eee/pip/_vendor/distlib/version.py and it'd work just fine but haven't tested.
import re
class InvalidVersionException(Exception): pass
class StormVersion(object):
VERSION_RE = re.compile(r"(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)"
"(?P<older_patch>\.\d+)?(?P<other>.*)")
RC_RE = re.compile(r"-rc(?P<release_candidate>\d+)", re.IGNORECASE)
def __init__(self, s):
self._string = s.strip()
m = StormVersion.VERSION_RE.match(self._string)
if not m:
raise InvalidVersionException("Invalid {} version number: {}"
.format(self.__class__.__name__, s))
g = m.groups()
self._comparison_parts = [int(x) for x in g[:3]]
self.major, self.minor, self.patch = self._comparison_parts
# Used for printing, we print RC separately
self._effective_version = list(self._comparison_parts)
# Strip off leading "." if we have a version, otherwise use 0
self.older_patch = int(g[3][1:]) if g[3] else 0
self._comparison_parts.append(self.older_patch)
if self.older_patch != 0:
self._effective_version.append(self.older_patch)
self.other = g[4]
m = StormVersion.RC_RE.match(self.other)
self.release_candidate = int(m.groups()[0]) if m else None
self._comparison_parts.append(self.release_candidate or 0)
def __repr__(self):
s = ("{} ({})"
.format(self.__class__.__name__,
".".join((str(x) for x in self._effective_version))))
if self.release_candidate is not None:
s = "{} RC-{}".format(s, self.release_candidate)
return s
def _check_compatible(self, other):
if type(self) is not type(other):
raise TypeError("Cannot compare {!r} and {!r}".format(self, other))
def __str__(self):
return self.__repr__()
def __eq__(self, other):
self._check_compatible(other)
return self._comparison_parts == other._comparison_parts
def __ne__(self, other):
return not self.__eq__(other)
def __lt__(self, other):
self._check_compatible(other)
return self._comparison_parts < other._comparison_parts
def __gt__(self, other):
self._check_compatible(other)
return self._comparison_parts > other._comparison_parts
def __le__(self, other):
self._check_compatible(other)
return self._comparison_parts <= other._comparison_parts
def __ge__(self, other):
self._check_compatible(other)
return self._comparison_parts >= other._comparison_parts
if __name__ == '__main__':
versions = (
"0.9.1",
"0.9.1-incubating", "0.9.0.1", "0.9.0", "0.9.0-rc3", "0.9.0-rc2",
"0.9.0-rc1", "0.8.3", "0.8.2", "0.8.1", "0.8.0", "0.7.4",
"0.7.3", "0.7.2", "0.7.1", "0.7.0", "0.6.2",
"0.6.1", "0.6.0", "0.5.4", "0.5.3", "0.5.2",
"0.5.1", "0.5.0"
)
from itertools import product
versions = sorted(versions)
print versions
for a, b in product(versions, reversed(versions)):
a = StormVersion(a)
b = StormVersion(b)
print "{!r} < {!r}: {!r}".format(a, b, a < b)
print "{!r} <= {!r}: {!r}".format(a, b, a <= b)
print "{!r} == {!r}: {!r}".format(a, b, a == b)
print "{!r} >= {!r}: {!r}".format(a, b, a >= b)
print "{!r} > {!r}: {!r}\n".format(a, b, a > b)
raw_input()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment