Skip to content

Commit

Permalink
Remove redundant usage of sort_looseversions
Browse files Browse the repository at this point in the history
This now works directly.
Also fix the sort_looseversions function for the distutils version of LooseVersion
  • Loading branch information
Flamefire committed Aug 3, 2021
1 parent e44b66d commit c23493e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 29 deletions.
6 changes: 3 additions & 3 deletions easybuild/tools/docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
from easybuild.tools.config import build_option
from easybuild.tools.filetools import read_file
from easybuild.tools.modules import modules_tool
from easybuild.tools.py2vs3 import OrderedDict, ascii_lowercase, sort_looseversions
from easybuild.tools.py2vs3 import OrderedDict, ascii_lowercase
from easybuild.tools.toolchain.toolchain import DUMMY_TOOLCHAIN_NAME, SYSTEM_TOOLCHAIN_NAME, is_system_toolchain
from easybuild.tools.toolchain.utilities import search_toolchain
from easybuild.tools.utilities import INDENT_2SPACES, INDENT_4SPACES
Expand Down Expand Up @@ -663,7 +663,7 @@ def key_to_ref(name):
# sort pairs by version (and then by versionsuffix);
# we sort by LooseVersion to obtain chronological version ordering,
# but we also need to retain original string version for filtering-by-version done below
sorted_pairs = sort_looseversions((LooseVersion(v), vs, v) for v, vs in pairs)
sorted_pairs = sorted((LooseVersion(v), vs, v) for v, vs in pairs)

for _, vsuff, ver in sorted_pairs:
table_values[0].append('``%s``' % ver)
Expand Down Expand Up @@ -721,7 +721,7 @@ def list_software_txt(software, detailed=False):
# sort pairs by version (and then by versionsuffix);
# we sort by LooseVersion to obtain chronological version ordering,
# but we also need to retain original string version for filtering-by-version done below
sorted_pairs = sort_looseversions((LooseVersion(v), vs, v) for v, vs in pairs)
sorted_pairs = sorted((LooseVersion(v), vs, v) for v, vs in pairs)

for _, vsuff, ver in sorted_pairs:
tcs = [x['toolchain'] for x in software[key] if x['version'] == ver and x['versionsuffix'] == vsuff]
Expand Down
11 changes: 9 additions & 2 deletions easybuild/tools/py2vs3/py3.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@
# importlib only works with Python 3.4 & newer
from importlib import reload # noqa

try:
from distutils.version import LooseVersion
HAVE_DISTUTILS = True
except ImportError:
LooseVersion = None
HAVE_DISTUTILS = False

# string type that can be used in 'isinstance' calls
string_type = str

Expand Down Expand Up @@ -120,7 +127,7 @@ def simple_compare(x1, x2):
else:
return 0

if isinstance(v1, LooseVersion) and isinstance(v2, LooseVersion):
if HAVE_DISTUTILS and isinstance(v1, LooseVersion) and isinstance(v2, LooseVersion):
# implementation based on '14894.patch' patch file provided in https://bugs.python.org/issue14894
for ver1_part, ver2_part in zip_longest(v1.version, v2.version, fillvalue=''):
# use string comparison if version parts have different type
Expand All @@ -142,7 +149,7 @@ def simple_compare(x1, x2):


def sort_looseversions(looseversions):
"""Sort list of (values including) LooseVersion instances."""
"""Sort list of (values including) distutils.version.LooseVersion instances."""
# with Python 2, we can safely use 'sorted' on LooseVersion instances
# (but we can't in Python 3, see https://bugs.python.org/issue14894)
return sorted(looseversions, key=cmp_to_key(safe_cmp_looseversions))
63 changes: 39 additions & 24 deletions test/framework/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -6052,30 +6052,45 @@ def test_installdir(self):
self.assertTrue(eb.installdir.endswith('/software/Core/toy/0.0'))

def test_sort_looseversions(self):
"""Test sort_looseversions funuction."""
ver1 = LooseVersion('1.2.3')
ver2 = LooseVersion('4.5.6')
ver3 = LooseVersion('1.2.3dev')
ver4 = LooseVersion('system')
ver5 = LooseVersion('rc3')
ver6 = LooseVersion('v1802')

# some versions are included multiple times on purpose,
# to also test comparison between equal LooseVersion instances
input = [ver3, ver5, ver1, ver2, ver4, ver6, ver3, ver4, ver1]
expected = [ver1, ver1, ver3, ver3, ver2, ver5, ver4, ver4, ver6]
self.assertEqual(sort_looseversions(input), expected)

# also test on list of tuples consisting of a LooseVersion instance + a string
# (as in the list_software_* functions)
suff1 = ''
suff2 = '-foo'
suff3 = '-bar'
input = [(ver3, suff1), (ver5, suff3), (ver1, suff2), (ver2, suff3), (ver4, suff1),
(ver6, suff2), (ver3, suff3), (ver4, suff3), (ver1, suff1)]
expected = [(ver1, suff1), (ver1, suff2), (ver3, suff1), (ver3, suff3), (ver2, suff3),
(ver5, suff3), (ver4, suff1), (ver4, suff3), (ver6, suff2)]
self.assertEqual(sort_looseversions(input), expected)
"""Test sort_looseversions function."""
# Test twice: With the standard distutils LooseVersion (when available) and with our class
# Note that our class directly allows sorting but should also work with sort_loosversions
for use_distutils in (True, False):
if use_distutils:
try:
from distutils.version import LooseVersion as version_class
except ImportError:
continue
else:
version_class = LooseVersion

ver1 = version_class('1.2.3')
ver2 = version_class('4.5.6')
ver3 = version_class('1.2.3dev')
ver4 = version_class('system')
ver5 = version_class('rc3')
ver6 = version_class('v1802')

# some versions are included multiple times on purpose,
# to also test comparison between equal LooseVersion instances
input = [ver3, ver5, ver1, ver2, ver4, ver6, ver3, ver4, ver1]
expected = [ver1, ver1, ver3, ver3, ver2, ver5, ver4, ver4, ver6]
self.assertEqual(sort_looseversions(input), expected)
if not use_distutils:
self.assertEqual(sorted(input), expected)

# also test on list of tuples consisting of a LooseVersion instance + a string
# (as in the list_software_* functions)
suff1 = ''
suff2 = '-foo'
suff3 = '-bar'
input = [(ver3, suff1), (ver5, suff3), (ver1, suff2), (ver2, suff3), (ver4, suff1),
(ver6, suff2), (ver3, suff3), (ver4, suff3), (ver1, suff1)]
expected = [(ver1, suff1), (ver1, suff2), (ver3, suff1), (ver3, suff3), (ver2, suff3),
(ver5, suff3), (ver4, suff1), (ver4, suff3), (ver6, suff2)]
self.assertEqual(sort_looseversions(input), expected)
if not use_distutils:
self.assertEqual(sorted(input), expected)

def test_cuda_compute_capabilities(self):
"""Test --cuda-compute-capabilities configuration option."""
Expand Down

0 comments on commit c23493e

Please sign in to comment.