5858from easybuild .tools import run
5959# import build_log must stay, to use of EasyBuildLog
6060from easybuild .tools .build_log import EasyBuildError , dry_run_msg , print_msg , print_warning
61- from easybuild .tools .config import ( DEFAULT_WAIT_ON_LOCK_INTERVAL , GENERIC_EASYBLOCK_PKG , build_option , install_path ,
62- IGNORE , WARN , ERROR )
61+ from easybuild .tools .config import DEFAULT_WAIT_ON_LOCK_INTERVAL , ERROR , GENERIC_EASYBLOCK_PKG , IGNORE , WARN
62+ from easybuild . tools . config import build_option , install_path
6363from easybuild .tools .py2vs3 import HTMLParser , std_urllib , string_type
6464from easybuild .tools .utilities import natural_keys , nub , remove_unwanted_chars
6565
@@ -1474,14 +1474,24 @@ def apply_patch(patch_file, dest, fn=None, copy=False, level=None, use_git_am=Fa
14741474 return True
14751475
14761476
1477- def apply_regex_substitutions (paths , regex_subs , backup = '.orig.eb' ):
1477+ def apply_regex_substitutions (paths , regex_subs , backup = '.orig.eb' , on_missing_match = None ):
14781478 """
14791479 Apply specified list of regex substitutions.
14801480
14811481 :param paths: list of paths to files to patch (or just a single filepath)
14821482 :param regex_subs: list of substitutions to apply, specified as (<regexp pattern>, <replacement string>)
14831483 :param backup: create backup of original file with specified suffix (no backup if value evaluates to False)
1484+ :param on_missing_match: Define what to do when no match was found in the file.
1485+ Can be 'error' to raise an error, 'warn' to print a warning or 'ignore' to do nothing
1486+ Defaults to the value of --strict
14841487 """
1488+ if on_missing_match is None :
1489+ on_missing_match = build_option ('strict' )
1490+ allowed_values = (ERROR , IGNORE , WARN )
1491+ if on_missing_match not in allowed_values :
1492+ raise EasyBuildError ('Invalid value passed to on_missing_match: %s (allowed: %s)' ,
1493+ on_missing_match , ', ' .join (allowed_values ))
1494+
14851495 if isinstance (paths , string_type ):
14861496 paths = [paths ]
14871497
@@ -1495,9 +1505,7 @@ def apply_regex_substitutions(paths, regex_subs, backup='.orig.eb'):
14951505 else :
14961506 _log .info ("Applying following regex substitutions to %s: %s" , paths , regex_subs )
14971507
1498- compiled_regex_subs = []
1499- for regex , subtxt in regex_subs :
1500- compiled_regex_subs .append ((re .compile (regex ), subtxt ))
1508+ compiled_regex_subs = [(re .compile (regex ), subtxt ) for (regex , subtxt ) in regex_subs ]
15011509
15021510 for path in paths :
15031511 try :
@@ -1517,6 +1525,7 @@ def apply_regex_substitutions(paths, regex_subs, backup='.orig.eb'):
15171525
15181526 if backup :
15191527 copy_file (path , path + backup )
1528+ replacement_msgs = []
15201529 with open_file (path , 'w' ) as out_file :
15211530 lines = txt_utf8 .split ('\n ' )
15221531 del txt_utf8
@@ -1525,11 +1534,21 @@ def apply_regex_substitutions(paths, regex_subs, backup='.orig.eb'):
15251534 match = regex .search (line )
15261535 if match :
15271536 origtxt = match .group (0 )
1528- _log . info ( "Replacing line %d in %s : '%s' -> '%s'",
1529- (line_id + 1 ), path , origtxt , subtxt )
1537+ replacement_msgs . append ( "Replaced in line %d: '%s' -> '%s'" %
1538+ (line_id + 1 , origtxt , subtxt ) )
15301539 line = regex .sub (subtxt , line )
15311540 lines [line_id ] = line
15321541 out_file .write ('\n ' .join (lines ))
1542+ if replacement_msgs :
1543+ _log .info ('Applied the following substitutions to %s:\n %s' , path , '\n ' .join (replacement_msgs ))
1544+ else :
1545+ msg = 'Nothing found to replace in %s' % path
1546+ if on_missing_match == ERROR :
1547+ raise EasyBuildError (msg )
1548+ elif on_missing_match == WARN :
1549+ _log .warning (msg )
1550+ else :
1551+ _log .info (msg )
15331552
15341553 except (IOError , OSError ) as err :
15351554 raise EasyBuildError ("Failed to patch %s: %s" , path , err )
0 commit comments