Skip to content

Commit faf549b

Browse files
committed
Add parameter to supress the warning when adding a path twice to a module environment variable
1 parent 653a28e commit faf549b

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

easybuild/tools/module_generator.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,12 @@ def get_modules_path(self, fake=False, mod_path_suffix=None):
206206

207207
return os.path.join(mod_path, mod_path_suffix)
208208

209-
def _filter_paths(self, key, paths):
210-
"""Filter out paths already added to key and return the remaining ones"""
209+
def _filter_paths(self, key, paths, warn_exists=True):
210+
"""
211+
Filter out paths already added to key and return the remaining ones
212+
213+
:param warn_exists: Show a warning for paths already added to the key
214+
"""
211215
if self.added_paths_per_key is None:
212216
# For compatibility this is only a warning for now and we don't filter any paths
213217
print_warning('Module creation has not been started. Call start_module_creation first!')
@@ -227,10 +231,12 @@ def _filter_paths(self, key, paths):
227231
paths = list(paths)
228232
filtered_paths = [x for x in paths if x not in added_paths and not added_paths.add(x)]
229233
if filtered_paths != paths:
230-
removed_paths = paths if filtered_paths is None else [x for x in paths if x not in filtered_paths]
231-
print_warning("Suppressed adding the following path(s) to $%s of the module as they were already added: %s",
232-
key, removed_paths,
233-
log=self.log)
234+
if warn_exists:
235+
removed_paths = paths if filtered_paths is None else [x for x in paths if x not in filtered_paths]
236+
print_warning("Suppressed adding the following path(s) to $%s of the module "
237+
"as they were already added: %s",
238+
key, removed_paths,
239+
log=self.log)
234240
if not filtered_paths:
235241
filtered_paths = None
236242
return filtered_paths
@@ -251,7 +257,7 @@ def append_paths(self, key, paths, allow_abs=False, expand_relpaths=True, delim=
251257
return self.update_paths(key, paths, prepend=False, allow_abs=allow_abs, expand_relpaths=expand_relpaths,
252258
delim=delim)
253259

254-
def prepend_paths(self, key, paths, allow_abs=False, expand_relpaths=True, delim=':'):
260+
def prepend_paths(self, key, paths, allow_abs=False, expand_relpaths=True, delim=':', warn_exists=True):
255261
"""
256262
Generate prepend-path statements for the given list of paths.
257263
@@ -260,8 +266,9 @@ def prepend_paths(self, key, paths, allow_abs=False, expand_relpaths=True, delim
260266
:param allow_abs: allow providing of absolute paths
261267
:param expand_relpaths: expand relative paths into absolute paths (by prefixing install dir)
262268
:param delim: delimiter used between paths
269+
:param warn_exists: Show a warning if any path was already added to the variable
263270
"""
264-
paths = self._filter_paths(key, paths)
271+
paths = self._filter_paths(key, paths, warn_exists=warn_exists)
265272
if paths is None:
266273
return ''
267274
return self.update_paths(key, paths, prepend=True, allow_abs=allow_abs, expand_relpaths=expand_relpaths,

test/framework/module_generator.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -882,14 +882,18 @@ def prepend_paths(*args, **kwargs):
882882
# check for warning that is printed when same path is added multiple times
883883
with self.modgen.start_module_creation():
884884
self.modgen.prepend_paths('TEST', 'path1')
885-
self.mock_stderr(True)
886-
self.modgen.prepend_paths('TEST', 'path1')
887-
stderr = self.get_stderr()
888-
self.mock_stderr(False)
885+
with self.mocked_stdout_stderr():
886+
self.modgen.prepend_paths('TEST', 'path1')
887+
stderr = self.get_stderr()
889888
expected_warning = "\nWARNING: Suppressed adding the following path(s) to $TEST of the module "
890889
expected_warning += "as they were already added: path1\n\n"
891890
self.assertEqual(stderr, expected_warning)
892891

892+
with self.mocked_stdout_stderr():
893+
self.modgen.prepend_paths('TEST', 'path1', warn_exists=False)
894+
stderr = self.get_stderr()
895+
self.assertEqual(stderr, '')
896+
893897
def test_det_user_modpath(self):
894898
"""Test for generic det_user_modpath method."""
895899
# None by default

0 commit comments

Comments
 (0)