diff --git a/easybuild/easyblocks/generic/pythonpackage.py b/easybuild/easyblocks/generic/pythonpackage.py index b78868a2b4..8b651e44aa 100644 --- a/easybuild/easyblocks/generic/pythonpackage.py +++ b/easybuild/easyblocks/generic/pythonpackage.py @@ -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: @@ -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 diff --git a/test/easyblocks/module.py b/test/easyblocks/module.py index 7b9c6d612f..f35ff3cd7d 100644 --- a/test/easyblocks/module.py +++ b/test/easyblocks/module.py @@ -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):