Skip to content

Commit

Permalink
Fix handling of maximum python version in pick_python_cmd
Browse files Browse the repository at this point in the history
When the minor version is unset, only the major version must be checked.
Otherwise patch versions etc. must be ignored.
This allows:
3.7.4 for "max_py_majver=3" and "max_py_majver=3 max_py_minver=7"
  • Loading branch information
Flamefire committed Oct 11, 2024
1 parent 60480fa commit a3c7b55
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
22 changes: 12 additions & 10 deletions easybuild/easyblocks/generic/pythonpackage.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def check_python_cmd(python_cmd):
log.debug(f"Python command '{python_cmd}' not available through $PATH")
return False

pyver = det_python_version(python_cmd)
pyver = LooseVersion(det_python_version(python_cmd))

if req_maj_ver is not None:
if req_min_ver is None:
Expand All @@ -119,26 +119,28 @@ def check_python_cmd(python_cmd):
req_majmin_ver = '%s.%s' % (req_maj_ver, req_min_ver)

# (strict) check for major version
maj_ver = pyver.split('.')[0]
if maj_ver != str(req_maj_ver):
maj_ver = pyver.version[0]
if maj_ver != req_maj_ver:
log.debug(f"Major Python version does not match: {maj_ver} vs {req_maj_ver}")
return False

# check for minimal minor version
if LooseVersion(pyver) < LooseVersion(req_majmin_ver):
if pyver < req_majmin_ver:
log.debug(f"Minimal requirement for minor Python version not satisfied: {pyver} vs {req_majmin_ver}")
return False

if max_py_majver is not None:
if max_py_minver is None:
max_majmin_ver = '%s.0' % max_py_majver
max_ver = int(max_py_majver)
tested_pyver = pyver.version[0]
else:
max_majmin_ver = '%s.%s' % (max_py_majver, max_py_minver)
max_ver = LooseVersion('%s.%s' % (max_py_majver, max_py_minver))
# Make sure we test only until the minor version, because 3.9.3 > 3.9 but we want to allow this
tested_pyver = '.'.join(str(v) for v in pyver.version[:2])

if LooseVersion(pyver) > LooseVersion(max_majmin_ver):
log.debug("Python version (%s) on the system is newer than the maximum supported "
"Python version specified in the easyconfig (%s)",
pyver, max_majmin_ver)
if tested_pyver > max_ver:
log.debug(f"Python version ({pyver}) on the system is newer than the maximum supported "
f"Python version specified in the easyconfig ({max_ver})")
return False

# all check passed
Expand Down
20 changes: 14 additions & 6 deletions test/easyblocks/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,20 @@ def test_pythonpackage_det_pylibdir(self):
def test_pythonpackage_pick_python_cmd(self):
"""Test pick_python_cmd function from pythonpackage.py."""
from easybuild.easyblocks.generic.pythonpackage import pick_python_cmd
self.assertTrue(pick_python_cmd() is not None)
self.assertTrue(pick_python_cmd(3) is not None)
self.assertTrue(pick_python_cmd(3, 6) is not None)
self.assertTrue(pick_python_cmd(123, 456) is None)
self.assertTrue(pick_python_cmd(2, 6, 123, 456) is not None)
self.assertTrue(pick_python_cmd(2, 6, 1, 1) is None)
self.assertIsNotNone(pick_python_cmd())
self.assertIsNotNone(pick_python_cmd(3))
self.assertIsNotNone(pick_python_cmd(3, 6))
self.assertIsNone(pick_python_cmd(123, 456))
self.assertIsNotNone(pick_python_cmd(2, 6, 123, 456))
self.assertIsNotNone(pick_python_cmd(2, 6, 2))
self.assertIsNone(pick_python_cmd(2, 6, 1, 1))
maj_ver, min_ver = sys.version_info[0:2]
self.assertIsNotNone(pick_python_cmd(maj_ver, min_ver))
self.assertIsNotNone(pick_python_cmd(maj_ver, min_ver, max_py_majver=maj_ver))
self.assertIsNotNone(pick_python_cmd(maj_ver, min_ver, max_py_majver=maj_ver, max_py_minver=min_ver))
self.assertIsNotNone(pick_python_cmd(maj_ver, min_ver, max_py_majver=maj_ver, max_py_minver=min_ver + 1))
self.assertIsNone(pick_python_cmd(maj_ver, min_ver, max_py_majver=maj_ver - 1))
self.assertIsNone(pick_python_cmd(maj_ver, min_ver, max_py_majver=maj_ver, max_py_minver=min_ver - 1))


def template_module_only_test(self, easyblock, name, version='1.3.2', extra_txt='', tmpdir=None):
Expand Down

0 comments on commit a3c7b55

Please sign in to comment.