def file_replace_mod()

in comment_out_module_versionss.py [0:0]


def file_replace_mod(fname):
    global num_replacements, num_patched_files, num_scanned_files
    # first, see if the pattern is even in the file.
    with open(fname) as f:
        lines = f.readlines()
        f.close()
    num_scanned_files += 1    
    patch_applied=0    
    start_line_index = 0
    end_line_range = len(lines)
    while start_line_index < end_line_range:
        module_block_found = False
        version_search_index_start = start_line_index
        version_search_index_end = end_line_range
        for line_index in range(start_line_index, end_line_range):
           # search module declaration 
           line = lines[line_index]
           if re.search(pat_match_module,line):
               # module block start found, determine module block end
               module_block_found = True
               version_search_index_start = line_index + 1
               indent_level = 0
               for line_index_tmp in range(line_index,end_line_range):
                   line = lines[line_index_tmp]
                   count_open_bracket = line.count("{")
                   count_close_bracket = line.count("}")
                   indent_level += count_open_bracket - count_close_bracket
                   if indent_level == 0:
                       version_search_index_end = line_index_tmp
                       break
               # if module start line exit loop will restart with new start line
               break
        # no more module blocks  
        if not module_block_found:
           break
        source_attribute_found = False
        for line_index_tmp in range(version_search_index_start,version_search_index_end):
           line = lines[line_index_tmp] 
           if re.search(pat_match_source,line):
               source_attribute_found = True
               break
        if source_attribute_found:
            for line_index_tmp in range(version_search_index_start,version_search_index_end):
               line = lines[line_index_tmp] 
               if re.search(pat_match_version,line):
                   repl_line = re.sub(pat_match_version,re_repl_version,line)
                   lines[line_index_tmp] = repl_line
                   patch_applied += 1
                   num_replacements += 1
                   print("replacing:" + line + " with:\n"+ repl_line + " in file:" + fname) 
                   break
        # restart the for loop after each module block 
        start_line_index = version_search_index_end + 1
                
    if patch_applied > 0:
        num_patched_files += 1
        out_fname = fname + ".tmp"
        out = open(out_fname, "w")
        for line in lines:
           out.write(line)
        out.close()
        os.remove(fname)
        os.rename(out_fname, fname)