henrike@webrtc.org | 1322614 | 2012-02-14 23:54:07 +0000 | [diff] [blame] | 1 | # Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 2 | # |
| 3 | # Use of this source code is governed by a BSD-style license |
| 4 | # that can be found in the LICENSE file in the root of the source |
| 5 | # tree. An additional intellectual property rights grant can be found |
| 6 | # in the file PATENTS. All contributing project authors may |
| 7 | # be found in the AUTHORS file in the root of the source tree. |
| 8 | |
| 9 | # NOTE: This is a hack which disobeys a number of conventions and best |
| 10 | # practices. It's here just to be easily shared. If it's to remain in the |
| 11 | # repository it should be refactored. |
| 12 | |
| 13 | #!/usr/bin/env python |
| 14 | |
| 15 | import stringmanipulation |
| 16 | import filemanagement |
| 17 | import sys |
| 18 | |
| 19 | trace_remove_key_word = 'kTraceModuleCall' |
| 20 | |
| 21 | if((len(sys.argv) != 2) and (len(sys.argv) != 3)): |
| 22 | print 'parameters are: parent directory [--commit]' |
| 23 | quit() |
| 24 | |
| 25 | if((len(sys.argv) == 3) and (sys.argv[2] != '--commit')): |
| 26 | print 'parameters are: parent directory [--commit]' |
| 27 | quit() |
| 28 | |
| 29 | commit = (len(sys.argv) == 3) |
| 30 | |
| 31 | directory = sys.argv[1]; |
| 32 | occurances = [] |
| 33 | |
| 34 | trace_identifier = 'WEBRTC_TRACE(' |
| 35 | extensions = ['.h','.cc','.c','.cpp'] |
| 36 | files_to_fix = [] |
| 37 | for extension in extensions: |
| 38 | files_to_fix.extend(filemanagement.listallfilesinfolder(directory,\ |
| 39 | extension)) |
| 40 | |
| 41 | # This function identifies the begining of a trace statement |
| 42 | def istracebegining(line): |
| 43 | return stringmanipulation.issubstring(line, trace_identifier) != -1 |
| 44 | |
| 45 | def endofstatement(line): |
| 46 | return stringmanipulation.issubstring(line, ';') != -1 |
| 47 | |
| 48 | def removekeywordfound(line): |
| 49 | return stringmanipulation.issubstring(line, trace_remove_key_word) != -1 |
| 50 | |
| 51 | # Used to store temporary result before flushing to real file when finished |
| 52 | def temporaryfilename(): |
| 53 | return 'deleteme.txt' |
| 54 | |
| 55 | |
| 56 | def find_occurances(path, file_name): |
| 57 | full_filename = path + file_name |
| 58 | file_handle = open(full_filename,'r') |
| 59 | line_is_trace = False |
| 60 | last_trace_line = -1 |
| 61 | for line_nr, line in enumerate(file_handle): |
| 62 | if(istracebegining(line)): |
| 63 | line_is_trace = True; |
| 64 | last_trace_line = line_nr |
| 65 | |
| 66 | if(line_is_trace): |
| 67 | if(removekeywordfound(line)): |
| 68 | occurances.append(last_trace_line) |
| 69 | |
| 70 | if(endofstatement(line)): |
| 71 | line_is_trace = False; |
| 72 | |
| 73 | def remove_occurances(path, file_name): |
| 74 | full_file_name = path + file_name |
| 75 | if (not filemanagement.fileexist(full_file_name)): |
| 76 | print 'File ' + full_file_name + ' is not found.' |
| 77 | print 'Should not happen! Ever!' |
| 78 | quit() |
| 79 | |
| 80 | full_temporary_file_name = path + temporaryfilename() |
| 81 | temporary_file = open(full_temporary_file_name,'w') |
| 82 | original_file = open(full_file_name,'r') |
| 83 | next_occurance_id = 0; |
| 84 | removing_statement = False |
| 85 | if(len(occurances) == next_occurance_id): |
| 86 | return |
| 87 | next_occurance = occurances[next_occurance_id] |
| 88 | next_occurance_id += 1 |
| 89 | for line_nr, line in enumerate(original_file): |
| 90 | if(line_nr == next_occurance): |
| 91 | removing_statement = True |
| 92 | if(len(occurances) == next_occurance_id): |
| 93 | next_occurance_id = -1 |
| 94 | else: |
| 95 | next_occurance = occurances[next_occurance_id] |
| 96 | next_occurance_id += 1 |
| 97 | |
| 98 | if (not removing_statement): |
| 99 | temporary_file.writelines(line) |
| 100 | |
| 101 | if(endofstatement(line)): |
| 102 | removing_statement = False; |
| 103 | |
| 104 | temporary_file.close() |
| 105 | original_file.close() |
| 106 | filemanagement.copyfile(full_file_name,full_temporary_file_name) |
| 107 | filemanagement.deletefile(full_temporary_file_name) |
| 108 | |
| 109 | def nextoccurance(): |
| 110 | if (len(occurances) == 0): |
| 111 | return -1 |
| 112 | return_value = occurances[0] |
| 113 | occurances = occurances[1:len(occurances)] |
| 114 | return return_value |
| 115 | |
| 116 | def would_be_removed_occurances(path, file_name): |
| 117 | full_file_name = path + file_name |
| 118 | if (not filemanagement.fileexist(full_file_name)): |
| 119 | print 'File ' + full_file_name + ' is not found.' |
| 120 | print 'Should not happen! Ever!' |
| 121 | quit() |
| 122 | |
| 123 | original_file = open(full_file_name,'r') |
| 124 | removing_statement = False |
| 125 | next_occurance_id = 0; |
| 126 | if(len(occurances) == next_occurance_id): |
| 127 | return |
| 128 | next_occurance = occurances[next_occurance_id] |
| 129 | next_occurance_id += 1 |
| 130 | for line_nr, line in enumerate(original_file): |
| 131 | if(line_nr == next_occurance): |
| 132 | removing_statement = True |
| 133 | if(len(occurances) == next_occurance_id): |
| 134 | return |
| 135 | next_occurance = occurances[next_occurance_id] |
| 136 | next_occurance_id += 1 |
| 137 | |
| 138 | if (removing_statement): |
| 139 | print line_nr |
| 140 | |
| 141 | if(endofstatement(line)): |
| 142 | removing_statement = False; |
| 143 | if(next_occurance == -1): |
| 144 | break |
| 145 | original_file.close() |
| 146 | |
| 147 | for index in range(len(files_to_fix)): |
| 148 | if(commit): |
| 149 | print (100*index)/len(files_to_fix) |
| 150 | |
| 151 | path_dir = files_to_fix[index][0] |
| 152 | filename = files_to_fix[index][1] |
| 153 | |
| 154 | #print path_dir + filename |
| 155 | occurances = [] |
| 156 | find_occurances(path_dir, filename) |
| 157 | |
| 158 | if(not commit): |
| 159 | would_be_removed_occurances(path_dir, filename) |
| 160 | continue |
| 161 | remove_occurances(path_dir, filename) |