Skip to content

Commit

Permalink
Add parameter to supress the warning when adding a path twice to a mo…
Browse files Browse the repository at this point in the history
…dule environment variable
  • Loading branch information
Flamefire committed Nov 8, 2024
1 parent 688ca04 commit fd76ee4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
22 changes: 14 additions & 8 deletions easybuild/tools/module_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,12 @@ def get_modules_path(self, fake=False, mod_path_suffix=None):

return os.path.join(mod_path, mod_path_suffix)

def _filter_paths(self, key, paths):
"""Filter out paths already added to key and return the remaining ones"""
def _filter_paths(self, key, paths, warn_exists=True):
"""
Filter out paths already added to key and return the remaining ones
:param warn_exists: Show a warning for paths already added to the key
"""
if self.added_paths_per_key is None:
# For compatibility this is only a warning for now and we don't filter any paths
print_warning('Module creation has not been started. Call start_module_creation first!')
Expand All @@ -227,10 +231,11 @@ def _filter_paths(self, key, paths):
paths = list(paths)
filtered_paths = [x for x in paths if x not in added_paths and not added_paths.add(x)]
if filtered_paths != paths:
removed_paths = paths if filtered_paths is None else [x for x in paths if x not in filtered_paths]
print_warning("Suppressed adding the following path(s) to $%s of the module as they were already added: %s",
key, removed_paths,
log=self.log)
if warn_exists:
removed_paths = paths if filtered_paths is None else [x for x in paths if x not in filtered_paths]
print_warning("Suppressed adding the following path(s) to $%s of the module as they were already added: %s",
key, removed_paths,
log=self.log)
if not filtered_paths:
filtered_paths = None
return filtered_paths
Expand All @@ -249,16 +254,17 @@ def append_paths(self, key, paths, allow_abs=False, expand_relpaths=True):
return ''
return self.update_paths(key, paths, prepend=False, allow_abs=allow_abs, expand_relpaths=expand_relpaths)

def prepend_paths(self, key, paths, allow_abs=False, expand_relpaths=True):
def prepend_paths(self, key, paths, allow_abs=False, expand_relpaths=True, warn_exists=True):
"""
Generate prepend-path statements for the given list of paths.
:param key: environment variable to append paths to
:param paths: list of paths to append
:param allow_abs: allow providing of absolute paths
:param expand_relpaths: expand relative paths into absolute paths (by prefixing install dir)
:param warn_exists: Show a warning if any path was already added to the variable
"""
paths = self._filter_paths(key, paths)
paths = self._filter_paths(key, paths, warn_exists=warn_exists)
if paths is None:
return ''
return self.update_paths(key, paths, prepend=True, allow_abs=allow_abs, expand_relpaths=expand_relpaths)
Expand Down
12 changes: 8 additions & 4 deletions test/framework/module_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -882,14 +882,18 @@ def prepend_paths(*args, **kwargs):
# check for warning that is printed when same path is added multiple times
with self.modgen.start_module_creation():
self.modgen.prepend_paths('TEST', 'path1')
self.mock_stderr(True)
self.modgen.prepend_paths('TEST', 'path1')
stderr = self.get_stderr()
self.mock_stderr(False)
with self.mocked_stdout_stderr():
self.modgen.prepend_paths('TEST', 'path1')
stderr = self.get_stderr()
expected_warning = "\nWARNING: Suppressed adding the following path(s) to $TEST of the module "
expected_warning += "as they were already added: path1\n\n"
self.assertEqual(stderr, expected_warning)

with self.mocked_stdout_stderr():
self.modgen.prepend_paths('TEST', 'path1', warn_exists=False)
stderr = self.get_stderr()
self.assertEqual(stderr, '')

def test_det_user_modpath(self):
"""Test for generic det_user_modpath method."""
# None by default
Expand Down

0 comments on commit fd76ee4

Please sign in to comment.