def file_replace()

in comment_out_module_versionss.py [0:0]


def file_replace(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    
    parse_state =  Parse_State.Scanning   
    for line_index in range(len(lines)):
        line = lines[line_index]
        if parse_state == Parse_State.Scanning and re.search(pat_match_module,line):
           parse_state = Parse_State.ModuleStart 
        elif  parse_state == Parse_State.ModuleStart or parse_state == Parse_State.InModule:
            if re.search(pat_match_version,line):
                repl_line = re.sub(pat_match_version,re_repl_version,line)
                lines[line_index] = repl_line
                patch_applied += 1
                num_replacements += 1
                print("replacing:" + line + " with:\n"+ repl_line + " in file:" + fname) 
            elif re.search(pat_match_source,line) or re.search(pat_match_count,line) or re.search(pat_match_empty,line):
                parse_state = Parse_State.InModule
            else:    
                parse_state = Parse_State.Scanning
    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)