Skip to content

Commit

Permalink
Merge pull request #2657 from ComputeCanada/update_without_dup
Browse files Browse the repository at this point in the history
changed easyconfig.update to be able to specify not to allow duplicate values
  • Loading branch information
boegel committed Nov 14, 2018
2 parents 354d050 + 245164d commit c2ae926
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
13 changes: 10 additions & 3 deletions easybuild/framework/easyconfig/easyconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,15 +446,22 @@ def copy(self):

return ec

def update(self, key, value):
def update(self, key, value, allow_duplicate=True):
"""
Update a string configuration value with a value (i.e. append to it).
"""
prev_value = self[key]
if isinstance(prev_value, basestring):
self[key] = '%s %s ' % (prev_value, value)
if allow_duplicate or value not in prev_value:
self[key] = '%s %s ' % (prev_value, value)
elif isinstance(prev_value, list):
self[key] = prev_value + value
if allow_duplicate:
self[key] = prev_value + value
else:
for item in value:
# add only those items that aren't already in the list
if item not in prev_value:
self[key] = prev_value + [item]
else:
raise EasyBuildError("Can't update configuration value for %s, because it's not a string or list.", key)

Expand Down
12 changes: 12 additions & 0 deletions test/framework/easyconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -1381,6 +1381,18 @@ def test_update(self):
toy_patch_fn = 'toy-0.0_fix-silly-typo-in-printf-statement.patch'
self.assertEqual(ec['patches'], [toy_patch_fn, ('toy-extra.txt', 'toy-0.0'), 'foo.patch', 'bar.patch'])

# for unallowed duplicates
ec.update('configopts', 'SOME_VALUE')
configopts_tmp = ec['configopts']
ec.update('configopts', 'SOME_VALUE', allow_duplicate=False)
self.assertEquals(ec['configopts'], configopts_tmp)

# for unallowed duplicates when a list is used
ec.update('patches', ['foo2.patch', 'bar2.patch'])
patches_tmp = copy.deepcopy(ec['patches'])
ec.update('patches', ['foo2.patch', 'bar2.patch'], allow_duplicate=False)
self.assertEquals(ec['patches'], patches_tmp)

def test_hide_hidden_deps(self):
"""Test use of --hide-deps on hiddendependencies."""
test_dir = os.path.dirname(os.path.abspath(__file__))
Expand Down

0 comments on commit c2ae926

Please sign in to comment.