Skip to content

Commit 653a28e

Browse files
committed
simplify code for determining the PYTHONPAH module entries
The check whether the path(s) have already been added is redundant as that is done in the module_generator class. The whole code with all the checks is superflous if there are no python paths to add. To avoid excessive indentation return early with an empty list in that case and same for Python installations for consistency.
1 parent eeb290f commit 653a28e

File tree

1 file changed

+26
-29
lines changed

1 file changed

+26
-29
lines changed

easybuild/framework/easyblock.py

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,43 +1399,40 @@ def make_module_pythonpath(self):
13991399
Add lines for module file to update $PYTHONPATH or $EBPYTHONPREFIXES,
14001400
if they aren't already present and the standard lib/python*/site-packages subdirectory exists
14011401
"""
1402-
lines = []
1403-
if not os.path.isfile(os.path.join(self.installdir, 'bin', 'python')): # only needed when not a python install
1404-
python_subdir_pattern = os.path.join(self.installdir, 'lib', 'python*', 'site-packages')
1405-
candidate_paths = (os.path.relpath(path, self.installdir) for path in glob.glob(python_subdir_pattern))
1406-
python_paths = [path for path in candidate_paths if re.match(r'lib/python\d+\.\d+/site-packages', path)]
1402+
if os.path.isfile(os.path.join(self.installdir, 'bin', 'python')): # only needed when not a python install
1403+
return []
14071404

1408-
# determine whether Python is a runtime dependency;
1409-
# if so, we assume it was installed with EasyBuild, and hence is aware of $EBPYTHONPREFIXES
1410-
runtime_deps = [dep['name'] for dep in self.cfg.dependencies(runtime_only=True)]
1405+
python_subdir_pattern = os.path.join(self.installdir, 'lib', 'python*', 'site-packages')
1406+
candidate_paths = (os.path.relpath(path, self.installdir) for path in glob.glob(python_subdir_pattern))
1407+
python_paths = [path for path in candidate_paths if re.match(r'lib/python\d+\.\d+/site-packages', path)]
1408+
if not python_paths:
1409+
return []
14111410

1412-
# don't use $EBPYTHONPREFIXES unless we can and it's preferred or necesary (due to use of multi_deps)
1413-
use_ebpythonprefixes = False
1414-
multi_deps = self.cfg['multi_deps']
1411+
# determine whether Python is a runtime dependency;
1412+
# if so, we assume it was installed with EasyBuild, and hence is aware of $EBPYTHONPREFIXES
1413+
runtime_deps = [dep['name'] for dep in self.cfg.dependencies(runtime_only=True)]
14151414

1416-
if 'Python' in runtime_deps:
1417-
self.log.info("Found Python runtime dependency, so considering $EBPYTHONPREFIXES...")
1415+
# don't use $EBPYTHONPREFIXES unless we can and it's preferred or necesary (due to use of multi_deps)
1416+
use_ebpythonprefixes = False
1417+
multi_deps = self.cfg['multi_deps']
14181418

1419-
if build_option('prefer_python_search_path') == EBPYTHONPREFIXES:
1420-
self.log.info("Preferred Python search path is $EBPYTHONPREFIXES, so using that")
1421-
use_ebpythonprefixes = True
1419+
if 'Python' in runtime_deps:
1420+
self.log.info("Found Python runtime dependency, so considering $EBPYTHONPREFIXES...")
14221421

1423-
elif multi_deps and 'Python' in multi_deps:
1424-
self.log.info("Python is listed in 'multi_deps', so using $EBPYTHONPREFIXES instead of $PYTHONPATH")
1422+
if build_option('prefer_python_search_path') == EBPYTHONPREFIXES:
1423+
self.log.info("Preferred Python search path is $EBPYTHONPREFIXES, so using that")
14251424
use_ebpythonprefixes = True
14261425

1427-
if python_paths:
1428-
# add paths unless they were already added
1429-
if use_ebpythonprefixes:
1430-
path = '' # EBPYTHONPREFIXES are relative to the install dir
1431-
if path not in self.module_generator.added_paths_per_key[EBPYTHONPREFIXES]:
1432-
lines.append(self.module_generator.prepend_paths(EBPYTHONPREFIXES, path))
1433-
else:
1434-
for python_path in python_paths:
1435-
if python_path not in self.module_generator.added_paths_per_key[PYTHONPATH]:
1436-
lines.append(self.module_generator.prepend_paths(PYTHONPATH, python_path))
1426+
elif multi_deps and 'Python' in multi_deps:
1427+
self.log.info("Python is listed in 'multi_deps', so using $EBPYTHONPREFIXES instead of $PYTHONPATH")
1428+
use_ebpythonprefixes = True
14371429

1438-
return lines
1430+
if use_ebpythonprefixes:
1431+
path = '' # EBPYTHONPREFIXES are relative to the install dir
1432+
lines = self.module_generator.prepend_paths(EBPYTHONPREFIXES, path)
1433+
else:
1434+
lines = self.module_generator.prepend_paths(PYTHONPATH, python_paths)
1435+
return [lines] if lines else []
14391436

14401437
def make_module_extra(self, altroot=None, altversion=None):
14411438
"""

0 commit comments

Comments
 (0)