]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph-deploy.git/commitdiff
[RM-11115] make a class for normalized versions
authorAlfredo Deza <adeza@redhat.com>
Tue, 11 Aug 2015 18:19:52 +0000 (14:19 -0400)
committerAlfredo Deza <adeza@redhat.com>
Thu, 13 Aug 2015 12:31:52 +0000 (08:31 -0400)
Signed-off-by: Alfredo Deza <adeza@redhat.com>
ceph_deploy/util/versions.py [new file with mode: 0644]

diff --git a/ceph_deploy/util/versions.py b/ceph_deploy/util/versions.py
new file mode 100644 (file)
index 0000000..810eb38
--- /dev/null
@@ -0,0 +1,47 @@
+
+
+class NormalizedVersion(object):
+    """
+    A class to provide a clean interface for setting/retrieving distinct
+    version parts divided into major, minor, and patch (following convnetions
+    from semver (see http://semver.org/)
+
+    Since a lot of times version parts need to be compared, it provides for
+    `int` representations of their string counterparts, with some sanitization
+    processing.
+
+    Defaults to '0' or 0 (int) values when values are not set or parsing fails.
+    """
+
+    def __init__(self, raw_version):
+        self.raw_version = raw_version.strip()
+        self.major = '0'
+        self.minor = '0'
+        self.patch = '0'
+        self.garbage = ''
+        self.int_major = 0
+        self.int_minor = 0
+        self.int_patch = 0
+        self._version_map = {}
+        self._set_versions()
+
+    def _set_int_versions(self):
+        version_map = dict(
+            major=self.major,
+            minor=self.minor,
+            patch=self.patch,
+            garbage=self.garbage)
+
+        # safe int versions that remove non-numerical chars
+        # for example 'rc1' in a version like '1-rc1
+        for name, value in version_map.items():
+            if '-' in value:  # get rid of garbage like -dev1 or -rc1
+                value = value.split('-')[0]
+            value = float(''.join(c for c in value if c.isdigit()) or 0)
+            int_name = "int_%s" % name
+            setattr(self, int_name, value)
+
+    def _set_versions(self):
+        split_version = (self.raw_version.split('.') + ["0"]*4)[:4]
+        self.major, self.minor, self.patch, self.garbage = split_version
+        self._set_int_versions()