From 435fb173d346fe39312d7cc163cb81dac6549b1d Mon Sep 17 00:00:00 2001
From: Lumir Balhar <lbalhar@redhat.com>
Date: Oct 14 2019 10:37:22 +0000
Subject: [PATCH 1/5] Re-type zip iterator to list to know its length


---

diff --git a/switch_java_functions.py b/switch_java_functions.py
index 11f7856..95a8322 100644
--- a/switch_java_functions.py
+++ b/switch_java_functions.py
@@ -195,7 +195,7 @@ def get_java_identifiers(get_alternatives_func=None):
         java_identifiers.append(java)
         if i == best:
             best_identifier_index = len(java_identifiers) - 1
-    identifiers_and_jdks = zip(java_identifiers, jdks)
+    identifiers_and_jdks = list(zip(java_identifiers, jdks))
     if len(identifiers_and_jdks) > 0:
         best_identifier = identifiers_and_jdks[best_identifier_index][0]
         identifiers_and_jdks = sorted(identifiers_and_jdks, cmp=version_sort_wrapper)

From 6ab10f2c9b2812462372d6e3573290c60f9e90bc Mon Sep 17 00:00:00 2001
From: Lumir Balhar <lbalhar@redhat.com>
Date: Oct 14 2019 10:38:04 +0000
Subject: [PATCH 2/5] Make sure that the line index will be always integer


---

diff --git a/switch_java_functions.py b/switch_java_functions.py
index 95a8322..30d0166 100644
--- a/switch_java_functions.py
+++ b/switch_java_functions.py
@@ -303,7 +303,7 @@ def _get_alternatives(alternative_file, exists):
             index = index + 1
             line = lines[index]
         # index points to blank line separating slaves from target.
-        slave_line_count = (index - first_slave_index) / 2
+        slave_line_count = (index - first_slave_index) // 2
         index = index + 1
         # index points to target.
         while index < len(lines):

From afcd19af619ba96b7c98deb50218bf3f4eb11cf2 Mon Sep 17 00:00:00 2001
From: Lumir Balhar <lbalhar@redhat.com>
Date: Oct 14 2019 10:42:43 +0000
Subject: [PATCH 3/5] Change the functions for comparisons to functions generating keys for sorting


---

diff --git a/switch_java_functions.py b/switch_java_functions.py
index 30d0166..1b8ccbf 100644
--- a/switch_java_functions.py
+++ b/switch_java_functions.py
@@ -144,28 +144,18 @@ def version_sort_split(i):
     a = re.split(regex, (i).lower())
     return a
 
-def version_sort(i, j):
-    a = version_sort_split(i)
-    b = version_sort_split(j)
-    la = len(a)
-    lb = len(b)
-    for x in range(0, min(la, lb) - 1):
-        aa = a[x]
-        bb = b[x]
+def version_sort_key(version):
+    result = []
+    parts = version_sort_split(version)
+    for part in parts:
         try:
-            na = int(aa)
-            nb = int(bb)
-            if na < nb: return  1
-            if na > nb: return -1
-        except Exception:
-            if aa < bb: return  1
-            if aa > bb: return -1
-    #return la-lb
-    #its disputable how to sort jdks wit NVR and without
-    return 0
-
-def version_sort_wrapper(i, j):
-    return version_sort(i[0],j[0])
+            result.append(int(part))
+        except ValueError:
+            result.append(part)
+    return result
+
+def version_sort_key_wrapper(i):
+    return version_sort_key(i[0])
 
 def get_java_identifiers(get_alternatives_func=None):
     if get_alternatives_func is None:
@@ -198,9 +188,9 @@ def get_java_identifiers(get_alternatives_func=None):
     identifiers_and_jdks = list(zip(java_identifiers, jdks))
     if len(identifiers_and_jdks) > 0:
         best_identifier = identifiers_and_jdks[best_identifier_index][0]
-        identifiers_and_jdks = sorted(identifiers_and_jdks, cmp=version_sort_wrapper)
+        identifiers_and_jdks = sorted(identifiers_and_jdks, key=version_sort_key_wrapper)
         initialize_alternatives_dictionaries(identifiers_and_jdks)
-        java_identifiers = sorted(java_identifiers, cmp=version_sort)
+        java_identifiers = sorted(java_identifiers, key=version_sort_key)
     if (ssj_debug):
         print(str(ALTERNATIVES))
         print(str(JAVA))
diff --git a/tests/test_switch_java_functions.py b/tests/test_switch_java_functions.py
index 9ef92db..aa8491f 100644
--- a/tests/test_switch_java_functions.py
+++ b/tests/test_switch_java_functions.py
@@ -473,30 +473,23 @@ class SimpleUsage(unittest.TestCase):
         self.assertEquals(a[7], 'openjdk')
         self.assertEquals(a[8], 'x686')
 
-    def test_version_sort(self):
-        a=funcs.version_sort("1.7.0-openjdk-1.el7.x86_64", "1.7.0-openjdk-1.el7.x86_64")
-        self.assertEquals(a, 0)
-        a=funcs.version_sort("1.7.0-openjdk-01.el7.x86_64", "1.7.0-openjdk-1.el7.x86_64")
-        self.assertEquals(a, 0)
-        a=funcs.version_sort("1.7.0-openjdk-1.el7.x86_64", "1.007.0-openjdk-00001.el7.x86_64")
-        self.assertEquals(a, 0)
-        a=funcs.version_sort("1.7.0-openjdk-2.el7.x86_64", "1.7.0-openjdk-1.el7.x86_64")
-        self.assertEquals(a, -1)
-        a=funcs.version_sort("1.7.0-openjdk-2.el7.x86_64", "1.7.0-openjdk-3.el7.x86_64")
-        self.assertEquals(a, 1)
-        a=funcs.version_sort("1.7.0-a-0.el7.x86_64", "1.7.0-b-0.el7.x86_64")
-        self.assertEquals(a, 1)
-        a=funcs.version_sort("1.7.0-az-0.el7.x86_64", "1.7.0-ax-0.el7.x86_64")
-        self.assertEquals(a, -1)
-        a=funcs.version_sort("1.7.0-openjdk-1.7.0.51-2.4.5.5.fc21.i386", "1.7.0-openjdk")
-        self.assertEquals(a, 0) # see comment in version_sort
-        a=funcs.version_sort("1.7.0-oracle", "1.7.0-openjdk.x86_64")
-        self.assertEquals(a, -1)
-
-
-
-
-
-
-
-
+    def test_version_sort_key(self):
+        # Test function itself
+        a=funcs.version_sort_key("1.7.0-openjdk-1.el7.x86_64")
+        self.assertEquals(a, [1, 7, 0, 'openjdk', 1, 7, 0, 'openjdk', 1, 'el7', 'x86_64'])
+        a=funcs.version_sort_key("1.7.0-openjdk-01.el7.x86_64")
+        self.assertEquals(a, [1, 7, 0, 'openjdk', 1, 7, 0, 'openjdk', 1, 'el7', 'x86_64'])
+        a=funcs.version_sort_key("1.7.0-openjdk-00001.el7.x86_64")
+        self.assertEquals(a, [1, 7, 0, 'openjdk', 1, 7, 0, 'openjdk', 1, 'el7', 'x86_64'])
+
+        # Test sorting using this function
+        a=sorted(["1.7.0-openjdk-2.el7.x86_64", "1.7.0-openjdk-1.el7.x86_64"], key=funcs.version_sort_key)
+        self.assertEquals(a, ['1.7.0-openjdk-1.el7.x86_64', '1.7.0-openjdk-2.el7.x86_64'])
+        a=sorted(["1.7.0-openjdk-2.el7.x86_64", "1.7.0-openjdk-3.el7.x86_64"], key=funcs.version_sort_key)
+        self.assertEquals(a, ['1.7.0-openjdk-2.el7.x86_64', '1.7.0-openjdk-3.el7.x86_64'])
+        a=sorted(["1.7.0-a-0.el7.x86_64", "1.7.0-b-0.el7.x86_64"], key=funcs.version_sort_key)
+        self.assertEquals(a, ['1.7.0-a-0.el7.x86_64', '1.7.0-b-0.el7.x86_64'])
+        a=sorted(["1.7.0-az-0.el7.x86_64", "1.7.0-ax-0.el7.x86_64"], key=funcs.version_sort_key)
+        self.assertEquals(a, ['1.7.0-ax-0.el7.x86_64', '1.7.0-az-0.el7.x86_64'])
+        a=sorted(["1.7.0-oracle", "1.7.0-openjdk.x86_64"], key=funcs.version_sort_key)
+        self.assertEquals(a, ['1.7.0-openjdk.x86_64', '1.7.0-oracle'])

From c7dffe085e63f472c418031625ffaf6c31c2dcc0 Mon Sep 17 00:00:00 2001
From: Lumir Balhar <lbalhar@redhat.com>
Date: Oct 14 2019 10:42:43 +0000
Subject: [PATCH 4/5] Use different content wrapper for different Python version


---

diff --git a/tests/test_switch_java_functions.py b/tests/test_switch_java_functions.py
index aa8491f..2bbc37e 100644
--- a/tests/test_switch_java_functions.py
+++ b/tests/test_switch_java_functions.py
@@ -2,11 +2,17 @@
 
 '''Unit test for switch_java_functions'''
 
-import io
 import unittest
+import sys
 
 import switch_java_functions as funcs
 
+PY2 = sys.version_info[0] == 2
+if PY2:
+    from io import BytesIO as content_wrapper
+else:
+    from io import StringIO as content_wrapper
+
 SET_ALTERNATIVES = '/usr/sbin/alternatives --set '
 
 def file_always_exists(path):
@@ -22,7 +28,7 @@ def create_stub_open_for(file_name, contents):
         if path != file_name:
             raise AssertionError("Excepted path: '" + file_name + "' " +
                                  "Got path: '" + path + "'")
-        return io.BytesIO(contents)
+        return content_wrapper(contents)
     return stub_open
 
 

From 5491b9c040ab5f46fb2e0ac2432d6029b2926025 Mon Sep 17 00:00:00 2001
From: Lumir Balhar <lbalhar@redhat.com>
Date: Oct 14 2019 10:42:43 +0000
Subject: [PATCH 5/5] Initialize also JAVADOCZIP dictionary to None


---

diff --git a/tests/test_switch_java_functions.py b/tests/test_switch_java_functions.py
index 2bbc37e..41f3806 100644
--- a/tests/test_switch_java_functions.py
+++ b/tests/test_switch_java_functions.py
@@ -154,6 +154,7 @@ class SimpleUsage(unittest.TestCase):
         funcs.SDK[java] = None
         funcs.PLUGIN[java] = None
         funcs.JAVADOCDIR[java] = None
+        funcs.JAVADOCZIP[java] = None
 
     @unittest.skip('implement this test')
     def test_get_java_identifiers(self):

