'Search and replace text in a file using regular expressions. Since this class is not used in the initial
download stage, no recipe step filtering is performed here, and None values are always returned for
filtered_actions and dir.
This step supports the full range of python\'s regular expression engine, including backreferences in
the replacement text. Example::
<action type="regex_replace" filename="Makefile">
<regex>^CFLAGS(\s*)=\s*-g\s*-Wall\s*-O2\s*$$</regex>
<replacement>CFLAGS= -g -Wall -O2 -I$$(NCURSES_INCLUDE_PATH)/ncurses/ -I$$(NCURSES_INCLUDE_PATH) -L$$(NCURSES_LIB_PATH)</replacement>
</action>
Before::
CFLAGS  = -g -Wall -O2
After::
CFLAGS  = -g -Wall -O2 -I$(NCURSES_INCLUDE_PATH)/ncurses/ -I$(NCURSES_INCLUDE_PATH) -L$(NCURSES_LIB_PATH)'

def execute_step(self, tool_dependency, package_name, actions, action_dict, filtered_actions, env_file_builder, install_environment, work_dir, current_dir=None, initial_download=False):

    log_file = os.path.join(install_environment.install_dir, basic_util.INSTALLATION_LOG)
    if os.path.exists(log_file):
        logfile = open(log_file, 'ab')
    else:
        logfile = open(log_file, 'wb')
    if os.path.isabs(action_dict['filename']):
        filename = action_dict['filename']
        if (not (filename.startswith(current_dir) or filename.startswith(install_environment.install_dir))):
            return (tool_dependency, None, None)
    else:
        filename = os.path.abspath(os.path.join(current_dir, action_dict['filename']))
    regex = re.compile(action_dict['regex'])
    replacement = action_dict['replacement']
    temp_fh = tempfile.NamedTemporaryFile(dir=current_dir)
    ofh = temp_fh.file
    total_replacements = 0
    with open(filename, 'r') as haystack:
        for line in haystack:
            (altered_text, replacement_count) = re.subn(regex, replacement, line)
            if (replacement_count > 0):
                ofh.write(altered_text)
                total_replacements += replacement_count
            else:
                ofh.write(line)
        ofh.flush()
    shutil.copyfile(temp_fh.name, filename)
    log_text = 'Successfully    replaced    pattern    %s    with    text    %s    in    file    %s:    %s    replacements    made\n'
    log_text = (log_text % (action_dict['regex'], action_dict['replacement'], filename, total_replacements))
    log.debug(log_text)
    logfile.write(log_text)
    logfile.close()
    return (tool_dependency, None, None)
