aboutsummaryrefslogtreecommitdiff
path: root/tools/refactoring/removetrace.py
blob: 43c622dc4052b3c978fb72e3cce9208a001cbf27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
#
# Use of this source code is governed by a BSD-style license
# that can be found in the LICENSE file in the root of the source
# tree. An additional intellectual property rights grant can be found
# in the file PATENTS.  All contributing project authors may
# be found in the AUTHORS file in the root of the source tree.

# NOTE: This is a hack which disobeys a number of conventions and best
# practices. It's here just to be easily shared. If it's to remain in the
# repository it should be refactored.

#!/usr/bin/env python

import stringmanipulation
import filemanagement
import sys

trace_remove_key_word = 'kTraceModuleCall'

if((len(sys.argv) != 2) and (len(sys.argv) != 3)):
    print 'parameters are: parent directory [--commit]'
    quit()

if((len(sys.argv) == 3) and (sys.argv[2] != '--commit')):
    print 'parameters are: parent directory [--commit]'
    quit()

commit = (len(sys.argv) == 3)

directory = sys.argv[1];
occurances = []

trace_identifier = 'WEBRTC_TRACE('
extensions = ['.h','.cc','.c','.cpp']
files_to_fix = []
for extension in extensions:
    files_to_fix.extend(filemanagement.listallfilesinfolder(directory,\
                                                       extension))

# This function identifies the begining of a trace statement
def istracebegining(line):
    return stringmanipulation.issubstring(line, trace_identifier) != -1

def endofstatement(line):
    return stringmanipulation.issubstring(line, ';') != -1

def removekeywordfound(line):
    return stringmanipulation.issubstring(line, trace_remove_key_word) != -1

# Used to store temporary result before flushing to real file when finished
def temporaryfilename():
    return 'deleteme.txt'


def find_occurances(path, file_name):
    full_filename = path + file_name
    file_handle = open(full_filename,'r')
    line_is_trace = False
    last_trace_line = -1
    for line_nr, line in enumerate(file_handle):
        if(istracebegining(line)):
            line_is_trace = True;
            last_trace_line = line_nr

        if(line_is_trace):
            if(removekeywordfound(line)):
                occurances.append(last_trace_line)

        if(endofstatement(line)):
            line_is_trace = False;

def remove_occurances(path, file_name):
    full_file_name = path + file_name
    if (not filemanagement.fileexist(full_file_name)):
        print 'File ' + full_file_name + ' is not found.'
        print 'Should not happen! Ever!'
        quit()

    full_temporary_file_name = path + temporaryfilename()
    temporary_file = open(full_temporary_file_name,'w')
    original_file = open(full_file_name,'r')
    next_occurance_id = 0;
    removing_statement = False
    if(len(occurances) == next_occurance_id):
        return
    next_occurance = occurances[next_occurance_id]
    next_occurance_id += 1
    for line_nr, line in enumerate(original_file):
        if(line_nr == next_occurance):
            removing_statement = True
            if(len(occurances) == next_occurance_id):
                next_occurance_id = -1
            else:
                next_occurance = occurances[next_occurance_id]
                next_occurance_id += 1

        if (not removing_statement):
            temporary_file.writelines(line)

        if(endofstatement(line)):
            removing_statement = False;

    temporary_file.close()
    original_file.close()
    filemanagement.copyfile(full_file_name,full_temporary_file_name)
    filemanagement.deletefile(full_temporary_file_name)

def nextoccurance():
    if (len(occurances) == 0):
        return -1
    return_value = occurances[0]
    occurances = occurances[1:len(occurances)]
    return return_value

def would_be_removed_occurances(path, file_name):
    full_file_name = path + file_name
    if (not filemanagement.fileexist(full_file_name)):
        print 'File ' + full_file_name + ' is not found.'
        print 'Should not happen! Ever!'
        quit()

    original_file = open(full_file_name,'r')
    removing_statement = False
    next_occurance_id = 0;
    if(len(occurances) == next_occurance_id):
        return
    next_occurance = occurances[next_occurance_id]
    next_occurance_id += 1
    for line_nr, line in enumerate(original_file):
        if(line_nr == next_occurance):
            removing_statement = True
            if(len(occurances) == next_occurance_id):
                return
            next_occurance = occurances[next_occurance_id]
            next_occurance_id += 1

        if (removing_statement):
            print line_nr

        if(endofstatement(line)):
            removing_statement = False;
            if(next_occurance == -1):
                break
    original_file.close()

for index in range(len(files_to_fix)):
    if(commit):
        print (100*index)/len(files_to_fix)

    path_dir = files_to_fix[index][0]
    filename = files_to_fix[index][1]

    #print path_dir + filename
    occurances = []
    find_occurances(path_dir, filename)

    if(not commit):
        would_be_removed_occurances(path_dir, filename)
        continue
    remove_occurances(path_dir, filename)