Skip to content

Commit 5eb6d07

Browse files
committed
move logic to set $PYTHONPATH or $EBPYTHONPREFIX to dedicated make_module_pythonpath method
1 parent 206736e commit 5eb6d07

File tree

1 file changed

+33
-20
lines changed

1 file changed

+33
-20
lines changed

easybuild/framework/easyblock.py

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,6 +1390,37 @@ def make_module_description(self):
13901390
"""
13911391
return self.module_generator.get_description()
13921392

1393+
def make_module_pythonpath(self):
1394+
"""
1395+
Add lines for module file to update $PYTHONPATH or $EBPYTHONPREFIXES,
1396+
if they aren't already present and the standard lib/python*/site-packages subdirectory exists
1397+
"""
1398+
lines = []
1399+
if not os.path.isfile(os.path.join(self.installdir, 'bin', 'python')): # only needed when not a python install
1400+
python_subdir_pattern = os.path.join(self.installdir, 'lib', 'python*', 'site-packages')
1401+
candidate_paths = (os.path.relpath(path, self.installdir) for path in glob.glob(python_subdir_pattern))
1402+
python_paths = [path for path in candidate_paths if re.match(r'lib/python\d+\.\d+/site-packages', path)]
1403+
1404+
runtime_deps = [dep['name'] for dep in self.cfg.dependencies(runtime_only=True)]
1405+
use_ebpythonprefixes = all([
1406+
'Python' in runtime_deps,
1407+
build_option('prefer_python_search_path') == EBPYTHONPREFIXES,
1408+
not self.cfg['force_pythonpath']
1409+
])
1410+
1411+
if python_paths:
1412+
# add paths unless they were already added
1413+
if use_ebpythonprefixes:
1414+
path = '' # EBPYTHONPREFIXES are relative to the install dir
1415+
if path not in self.module_generator.added_paths_per_key[EBPYTHONPREFIXES]:
1416+
lines.append(self.module_generator.prepend_paths(EBPYTHONPREFIXES, path))
1417+
else:
1418+
for python_path in python_paths:
1419+
if python_path not in self.module_generator.added_paths_per_key[PYTHONPATH]:
1420+
lines.append(self.module_generator.prepend_paths(PYTHONPATH, python_path))
1421+
1422+
return lines
1423+
13931424
def make_module_extra(self, altroot=None, altversion=None):
13941425
"""
13951426
Set extra stuff in module file, e.g. $EBROOT*, $EBVERSION*, etc.
@@ -1438,26 +1469,8 @@ def make_module_extra(self, altroot=None, altversion=None):
14381469
value, type(value))
14391470
lines.append(self.module_generator.append_paths(key, value, allow_abs=self.cfg['allow_append_abs_path']))
14401471

1441-
# Add automatic PYTHONPATH or EBPYTHONPREFIXES if they aren't already present and python paths exist
1442-
if not os.path.isfile(os.path.join(self.installdir, 'bin/python')): # only needed when not a python install
1443-
candidate_paths = (os.path.relpath(path, self.installdir)
1444-
for path in glob.glob(f'{self.installdir}/lib/python*/site-packages'))
1445-
python_paths = [path for path in candidate_paths if re.match(r'lib/python\d+\.\d+/site-packages', path)]
1446-
1447-
runtime_deps = [dep['name'] for dep in self.cfg.dependencies(runtime_only=True)]
1448-
use_ebpythonprefixes = 'Python' in runtime_deps and \
1449-
build_option('prefer_python_search_path') == EBPYTHONPREFIXES and not self.cfg['force_pythonpath']
1450-
1451-
if python_paths:
1452-
# Add paths unless they were already added
1453-
if use_ebpythonprefixes:
1454-
path = '' # EBPYTHONPREFIXES are relative to the install dir
1455-
if path not in self.module_generator.added_paths_per_key[EBPYTHONPREFIXES]:
1456-
lines.append(self.module_generator.prepend_paths(EBPYTHONPREFIXES, path))
1457-
else:
1458-
for python_path in python_paths:
1459-
if python_path not in self.module_generator.added_paths_per_key[PYTHONPATH]:
1460-
lines.append(self.module_generator.prepend_paths(PYTHONPATH, python_path))
1472+
# add lines to update $PYTHONPATH or $EBPYTHONPREFIXES
1473+
lines.extend(self.make_module_pythonpath())
14611474

14621475
modloadmsg = self.cfg['modloadmsg']
14631476
if modloadmsg:

0 commit comments

Comments
 (0)