You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by al...@apache.org on 2015/05/26 19:27:01 UTC

ambari git commit: AMBARI-10968. ambari_server/utils.py should be able to parse version with postfix text (Di Li via alejandro)

Repository: ambari
Updated Branches:
  refs/heads/trunk 84d774ce1 -> d636d70d7


AMBARI-10968. ambari_server/utils.py should be able to parse version with postfix text (Di Li via alejandro)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/d636d70d
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/d636d70d
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/d636d70d

Branch: refs/heads/trunk
Commit: d636d70d7a448b87b1443afb78dfec89b2f5d752
Parents: 84d774c
Author: Alejandro Fernandez <af...@hortonworks.com>
Authored: Tue May 26 10:26:54 2015 -0700
Committer: Alejandro Fernandez <af...@hortonworks.com>
Committed: Tue May 26 10:26:54 2015 -0700

----------------------------------------------------------------------
 .../src/main/python/ambari_server/utils.py       | 19 +++++++++++++++++++
 ambari-server/src/test/python/TestUtils.py       | 16 ++++++++++++++++
 2 files changed, 35 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/d636d70d/ambari-server/src/main/python/ambari_server/utils.py
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/python/ambari_server/utils.py b/ambari-server/src/main/python/ambari_server/utils.py
index f09a95c..2afcd1b 100644
--- a/ambari-server/src/main/python/ambari_server/utils.py
+++ b/ambari-server/src/main/python/ambari_server/utils.py
@@ -248,7 +248,26 @@ def get_postgre_running_status():
 
 
 def compare_versions(version1, version2):
+  """Compare two versions by digits. Ignore any alphanumeric characters after - and _ postfix.
+  Return 1 if version1 is newer than version2
+  Return -1 if version1 is older than version2
+  Return 0 if two versions are the same
+  """
   def normalize(v):
+    v = str(v)
+    v = re.sub(r'^\D+', '', v)
+    v = re.sub(r'\D+$', '', v)
+    v = v.strip(".-_")
+    pos_under = v.find("_")
+    pos_dash = v.find("-")
+    if pos_under > 0 and pos_dash < 0:
+      pos = pos_under
+    elif pos_under < 0 and pos_dash > 0:
+      pos = pos_dash
+    else:
+      pos = min(pos_under, pos_dash)
+    if pos > 0:
+      v = v[0:pos]
     return [int(x) for x in re.sub(r'(\.0+)*$', '', v).split(".")]
   return cmp(normalize(version1), normalize(version2))
   pass

http://git-wip-us.apache.org/repos/asf/ambari/blob/d636d70d/ambari-server/src/test/python/TestUtils.py
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/python/TestUtils.py b/ambari-server/src/test/python/TestUtils.py
index bbd40bf..06e0db4 100644
--- a/ambari-server/src/test/python/TestUtils.py
+++ b/ambari-server/src/test/python/TestUtils.py
@@ -226,3 +226,19 @@ class TestUtils(TestCase):
     # now supply keyword args to override env params
     formatted_message = formatter.format(message, envfoo="foobar", envbar="foobarbaz", foo="foo3", bar="bar3")
     self.assertEquals("foo3 bar3 foobar foobarbaz", formatted_message)
+
+  def test_compare_versions(self):
+    self.assertEquals(utils.compare_versions("1.7.0", "2.0.0"), -1)
+    self.assertEquals(utils.compare_versions("2.0.0", "2.0.0"), 0)
+    self.assertEquals(utils.compare_versions("2.1.0", "2.0.0"), 1)
+
+    self.assertEquals(utils.compare_versions("1.7.0_abc", "2.0.0-abc"), -1)
+    self.assertEquals(utils.compare_versions("2.0.0.abc", "2.0.0_abc"), 0)
+    self.assertEquals(utils.compare_versions("2.1.0-abc", "2.0.0.abc"), 1)
+
+    self.assertEquals(utils.compare_versions("2.1.0-1","2.0.0-2"),1)
+    self.assertEquals(utils.compare_versions("2.0.0_1","2.0.0-2"),0)
+    self.assertEquals(utils.compare_versions("2.0.0-1","2.0.0-2"),0)
+    self.assertEquals(utils.compare_versions("2.0.0_1","2.0.0_2"),0)
+    self.assertEquals(utils.compare_versions("2.0.0-abc","2.0.0_abc"),0)
+