Skip to content

Commit df528c6

Browse files
authored
Merge pull request #4754 from Flamefire/dict-improve
Small improvements to `ModuleLoadEnvironment`
2 parents 1c69ff9 + a2c4c8c commit df528c6

File tree

3 files changed

+20
-25
lines changed

3 files changed

+20
-25
lines changed

easybuild/framework/easyblock.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ class LibSymlink(Enum):
134134
- LIB64_TO_LIB: 'lib64' is a symlink to 'lib'
135135
- NEITHER: neither 'lib' is a symlink to 'lib64', nor 'lib64' is a symlink to 'lib'
136136
- """
137-
UNKNOWN, LIB_TO_LIB64, LIB64_TO_LIB, NEITHER = range(0, 4)
137+
LIB_TO_LIB64, LIB64_TO_LIB, NEITHER = range(3)
138138

139139

140140
class EasyBlock(object):
@@ -226,7 +226,7 @@ def __init__(self, ec, logfile=None):
226226
self.install_subdir = None
227227

228228
# track status of symlink between library directories
229-
self.install_lib_symlink = LibSymlink.UNKNOWN
229+
self._install_lib_symlink = None
230230

231231
# indicates whether build should be performed in installation dir
232232
self.build_in_installdir = self.cfg['buildininstalldir']
@@ -311,6 +311,13 @@ def __init__(self, ec, logfile=None):
311311

312312
self.log.info("Init completed for application name %s version %s" % (self.name, self.version))
313313

314+
@property
315+
def install_lib_symlink(self):
316+
"""Return symlink state of lib/lib64 folders"""
317+
if self._install_lib_symlink is None:
318+
self.check_install_lib_symlink()
319+
return self._install_lib_symlink
320+
314321
def post_init(self):
315322
"""
316323
Run post-initialization tasks.
@@ -1719,10 +1726,6 @@ def expand_module_search_path(self, search_path, path_type=ModEnvVarType.PATH_WI
17191726
abs_glob = os.path.join(self.installdir, search_path)
17201727
exp_search_paths = [abs_glob] if search_path == "" else glob.glob(abs_glob)
17211728

1722-
# Explicitly check symlink state between lib dirs if it is still undefined (e.g. --module-only)
1723-
if self.install_lib_symlink == LibSymlink.UNKNOWN:
1724-
self.check_install_lib_symlink()
1725-
17261729
retained_search_paths = []
17271730
for abs_path in exp_search_paths:
17281731
# return relative paths
@@ -1751,16 +1754,16 @@ def expand_module_search_path(self, search_path, path_type=ModEnvVarType.PATH_WI
17511754
return retained_search_paths
17521755

17531756
def check_install_lib_symlink(self):
1754-
"""Check symlink state between library directories in installation prefix"""
1757+
"""Update the symlink state between library directories in installation prefix"""
17551758
lib_dir = os.path.join(self.installdir, 'lib')
17561759
lib64_dir = os.path.join(self.installdir, 'lib64')
17571760

1758-
self.install_lib_symlink = LibSymlink.NEITHER
1761+
self._install_lib_symlink = LibSymlink.NEITHER
17591762
if os.path.exists(lib_dir) and os.path.exists(lib64_dir):
17601763
if os.path.islink(lib_dir) and os.path.samefile(lib_dir, lib64_dir):
1761-
self.install_lib_symlink = LibSymlink.LIB_TO_LIB64
1764+
self._install_lib_symlink = LibSymlink.LIB_TO_LIB64
17621765
elif os.path.islink(lib64_dir) and os.path.samefile(lib_dir, lib64_dir):
1763-
self.install_lib_symlink = LibSymlink.LIB64_TO_LIB
1766+
self._install_lib_symlink = LibSymlink.LIB64_TO_LIB
17641767

17651768
def make_module_req_guess(self):
17661769
"""

easybuild/tools/modules.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -152,18 +152,15 @@ class ModuleEnvironmentVariable:
152152
Contents of environment variable is a list of unique strings
153153
"""
154154

155-
def __init__(self, contents, var_type=None, delim=os.pathsep):
155+
def __init__(self, contents, var_type=ModEnvVarType.PATH_WITH_FILES, delim=os.pathsep):
156156
"""
157157
Initialize new environment variable
158158
Actual contents of the environment variable are held in self.contents
159-
By default, environment variable is a (list of) paths with files in them
159+
By default, the environment variable is a list of paths with files in them
160160
Existence of paths and their contents are not checked at init
161161
"""
162162
self.contents = contents
163163
self.delim = delim
164-
165-
if var_type is None:
166-
var_type = ModEnvVarType.PATH_WITH_FILES
167164
self.type = var_type
168165

169166
self.log = fancylogger.getLogger(self.__class__.__name__, fname=False)
@@ -283,7 +280,7 @@ def __setattr__(self, name, value):
283280

284281
# special variables that require files in their top directories
285282
if name in ('LD_LIBRARY_PATH', 'PATH'):
286-
kwargs.update({'var_type': ModEnvVarType.PATH_WITH_TOP_FILES})
283+
kwargs['var_type'] = ModEnvVarType.PATH_WITH_TOP_FILES
287284

288285
return super().__setattr__(name, ModuleEnvironmentVariable(contents, **kwargs))
289286

@@ -297,8 +294,7 @@ def items(self):
297294
- key = attribute name
298295
- value = its "contents" attribute
299296
"""
300-
for attr in self.__dict__:
301-
yield attr, getattr(self, attr)
297+
return self.__dict__.items()
302298

303299
def update(self, new_env):
304300
"""Update contents of environment from given dictionary"""
@@ -321,10 +317,7 @@ def environ(self):
321317
Return dict with mapping of ModuleEnvironmentVariables names with their contents
322318
Equivalent in shape to os.environ
323319
"""
324-
mapping = {}
325-
for envar_name, envar_contents in self.items():
326-
mapping.update({envar_name: str(envar_contents)})
327-
return mapping
320+
return {envar_name: str(envar_contents) for envar_name, envar_contents in self.items()}
328321

329322

330323
class ModulesTool(object):

test/framework/easyblock.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ def test_make_module_req(self):
439439
for path in ('bin', ('bin', 'testdir'), 'sbin', 'share', ('share', 'man'), 'lib', 'lib64'):
440440
path_components = (path, ) if isinstance(path, str) else path
441441
os.mkdir(os.path.join(eb.installdir, *path_components))
442-
eb.install_lib_symlink = LibSymlink.NEITHER
442+
eb.check_install_lib_symlink()
443443

444444
write_file(os.path.join(eb.installdir, 'foo.jar'), 'foo.jar')
445445
write_file(os.path.join(eb.installdir, 'bla.jar'), 'bla.jar')
@@ -505,7 +505,7 @@ def test_make_module_req(self):
505505
write_file(os.path.join(eb.installdir, 'lib', 'libfoo.so'), 'test')
506506
shutil.rmtree(os.path.join(eb.installdir, 'lib64'))
507507
os.symlink('lib', os.path.join(eb.installdir, 'lib64'))
508-
eb.install_lib_symlink = LibSymlink.LIB64_TO_LIB
508+
eb.check_install_lib_symlink()
509509
with eb.module_generator.start_module_creation():
510510
guess = eb.make_module_req()
511511
if get_module_syntax() == 'Tcl':
@@ -3215,7 +3215,6 @@ def test_expand_module_search_path(self):
32153215
write_file(os.path.join(eb.installdir, 'dir_full_subdirs', 'subdir1', 'file12.txt'), 'test file 1.2')
32163216
write_file(os.path.join(eb.installdir, 'dir_full_subdirs', 'subdir2', 'file21.txt'), 'test file 2.1')
32173217

3218-
self.assertEqual(eb.install_lib_symlink, LibSymlink.UNKNOWN)
32193218
eb.check_install_lib_symlink()
32203219
self.assertEqual(eb.install_lib_symlink, LibSymlink.NEITHER)
32213220

0 commit comments

Comments
 (0)