#2 Python 3 compatibility
Merged 2 years ago by pmikova. Opened 2 years ago by lbalhar.
lbalhar/system-switch-java py3  into  master

file modified
+15 -25
@@ -144,28 +144,18 @@ 

      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:
@@ -195,12 +185,12 @@ 

          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)

+         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))
@@ -303,7 +293,7 @@ 

              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):

@@ -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 @@ 

          if path != file_name:

              raise AssertionError("Excepted path: '" + file_name + "' " +

                                   "Got path: '" + path + "'")

-         return io.BytesIO(contents)

+         return content_wrapper(contents)

      return stub_open

  

  
@@ -148,6 +154,7 @@ 

          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):
@@ -473,30 +480,23 @@ 

          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'])

With these changes, system-switch-java is compatible with Python 2 and 3. All tests are passing with both Pythons and also some basic scenarios described in this bugzilla work for me.

Let me know if I can help or clarify anything.

hi! This is great. Looks ok by me python-dummy, will let the guy who wished to make the pythont3 port to eyball it too.

Pull-Request has been merged by pmikova

2 years ago