Skip to content

Commit ae170a3

Browse files
Flamefireboegel
andcommitted
Apply suggestions from code review
Co-authored-by: Kenneth Hoste <[email protected]>
1 parent 8730ea7 commit ae170a3

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

easybuild/tools/filetools.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,15 +1473,16 @@ def apply_regex_substitutions(paths, regex_subs, backup='.orig.eb', on_missing_m
14731473
:param paths: list of paths to files to patch (or just a single filepath)
14741474
:param regex_subs: list of substitutions to apply, specified as (<regexp pattern>, <replacement string>)
14751475
:param backup: create backup of original file with specified suffix (no backup if value evaluates to False)
1476-
:param on_missing_match: Define what to do when the file when no match was found in the file.
1476+
:param on_missing_match: Define what to do when no match was found in the file.
14771477
Can be 'error' to raise an error, 'warn' to print a warning or 'ignore' to do nothing
14781478
Defaults to value of --strict
14791479
"""
14801480
if on_missing_match is None:
14811481
on_missing_match = build_option('strict')
1482-
ALLOWED_VALUES = (run.ERROR, run.WARN, run.IGNORE)
1483-
if on_missing_match not in ALLOWED_VALUES:
1484-
raise EasyBuildError('Invalid value passed to on_missing_match: %s', on_missing_match)
1482+
allowed_values = (run.ERROR, run.WARN, run.IGNORE)
1483+
if on_missing_match not in allowed_values:
1484+
raise EasyBuildError('Invalid value passed to on_missing_match: %s (allowed: %s)',
1485+
on_missing_match, ', '.join(allowed_values))
14851486

14861487
if isinstance(paths, string_type):
14871488
paths = [paths]
@@ -1537,9 +1538,15 @@ def apply_regex_substitutions(paths, regex_subs, backup='.orig.eb', on_missing_m
15371538
if on_missing_match == run.ERROR:
15381539
raise EasyBuildError(msg)
15391540
elif on_missing_match == run.WARN:
1540-
print_warning(msg)
1541+
_log.warning(msg)
15411542
else:
1542-
_log.info(msg)
1543+
msg = 'Nothing found to replace in %s' % path
1544+
if on_missing_match == run.ERROR:
1545+
raise EasyBuildError(msg)
1546+
elif on_missing_match == run.WARN:
1547+
print_warning(msg)
1548+
else:
1549+
_log.info(msg)
15431550

15441551
except (IOError, OSError) as err:
15451552
raise EasyBuildError("Failed to patch %s: %s", path, err)

test/framework/filetools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1208,7 +1208,7 @@ def test_apply_regex_substitutions(self):
12081208
(r"^(FC\s*=\s*).*$", r"\1${FC}"),
12091209
(r"^(.FLAGS)\s*=\s*-O3\s-g(.*)$", r"\1 = -O2\2"),
12101210
]
1211-
regex_subs_copy = regex_subs.copy()
1211+
regex_subs_copy = regex_subs[:]
12121212
ft.apply_regex_substitutions(testfile, regex_subs)
12131213

12141214
expected_testtxt = '\n'.join([

0 commit comments

Comments
 (0)