OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
2 | |
3 import stringmanipulation | |
4 import filemanagement | |
5 import sys | |
6 | |
7 extensions = ['.h'] | |
8 | |
9 ignore_these = ['my_ignore_header.h'] | |
10 | |
11 if((len(sys.argv) != 2) and (len(sys.argv) != 3)): | |
12 print 'parameters are: directory [--commit]' | |
13 quit() | |
14 | |
15 directory = sys.argv[1]; | |
16 if(not filemanagement.pathexist(directory)): | |
17 print 'path ' + directory + ' does not exist' | |
18 quit() | |
19 | |
20 if((len(sys.argv) == 3) and (sys.argv[2] != '--commit')): | |
21 print 'parameters are: parent directory extension new extension [--commit]' | |
22 quit() | |
23 | |
24 commit = False | |
25 if(len(sys.argv) == 3): | |
26 commit = True | |
27 | |
28 for extension in extensions: | |
29 files_to_fix = filemanagement.listallfilesinfolder(directory,\ | |
30 extension) | |
31 | |
32 def buildincludeguardname(path,filename): | |
33 full_file_name = 'WEBRTC_' + path + filename | |
34 full_file_name = full_file_name.upper() | |
35 full_file_name = stringmanipulation.replaceoccurances(full_file_name, '/', '
_') | |
36 full_file_name = stringmanipulation.replaceoccurances(full_file_name, '\\',
'_') | |
37 full_file_name = stringmanipulation.replaceoccurances(full_file_name, '.', '
_') | |
38 full_file_name += '_' | |
39 return full_file_name | |
40 | |
41 def buildnewincludeguardset(path,filename): | |
42 include_guard_name = buildincludeguardname(path,filename) | |
43 if(include_guard_name == ''): | |
44 return [] | |
45 return_value = [] | |
46 return_value.append('#ifndef ' + include_guard_name) | |
47 return_value.append('#define ' + include_guard_name) | |
48 return_value.append(include_guard_name) | |
49 return return_value | |
50 | |
51 def printincludeguardset(include_guard_set): | |
52 print 'First line: ' + include_guard_set[0] | |
53 print 'Second line: ' + include_guard_set[1] | |
54 print 'Last line: ' + include_guard_set[2] | |
55 return | |
56 | |
57 include_guard_begin_identifier = ['#ifndef', '#if !defined'] | |
58 include_guard_second_identifier = ['#define'] | |
59 def findincludeguardidentifier(line): | |
60 for begin_identifier in include_guard_begin_identifier: | |
61 line = stringmanipulation.removealloccurances(line,begin_identifier) | |
62 for second_identifier in include_guard_begin_identifier: | |
63 line = stringmanipulation.removealloccurances(line,second_identifier) | |
64 removed_prefix = [True,''] | |
65 line = stringmanipulation.whitespacestoonespace(line) | |
66 while(removed_prefix[0]): | |
67 removed_prefix = stringmanipulation.removeprefix(line,' ') | |
68 line = removed_prefix[1] | |
69 line = stringmanipulation.removealloccurances(line,'(') | |
70 if(line == ''): | |
71 return '' | |
72 word_pos = stringmanipulation.getword(line,0) | |
73 return_value = line[0:word_pos[1]] | |
74 return_value = return_value.rstrip('\r\n') | |
75 return return_value | |
76 | |
77 def findoldincludeguardset(path,filename): | |
78 return_value = [] | |
79 full_file_name = path + filename | |
80 file_pointer = open(full_file_name,'r') | |
81 include_guard_name = '' | |
82 for line in file_pointer: | |
83 if (include_guard_name == ''): | |
84 for compare_string in include_guard_begin_identifier: | |
85 if (stringmanipulation.issubstring(compare_string, line) != -1): | |
86 include_guard_name = findincludeguardidentifier(line) | |
87 if (include_guard_name == ''): | |
88 break | |
89 line = line.rstrip('\r\n') | |
90 return_value.append(line) | |
91 break | |
92 else: | |
93 for compare_string in include_guard_second_identifier: | |
94 if (stringmanipulation.issubstring(compare_string, line) != -1): | |
95 if (stringmanipulation.issubstring(include_guard_name, line)
!= -1): | |
96 line = line.rstrip('\r\n') | |
97 return_value.append(line) | |
98 return_value.append(include_guard_name) | |
99 return return_value | |
100 include_guard_name = '' | |
101 return_value = [] | |
102 return [] | |
103 | |
104 failed_files = [] | |
105 for index in range(len(files_to_fix)): | |
106 if(commit): | |
107 print (100*index)/len(files_to_fix) | |
108 path_dir = files_to_fix[index][0] | |
109 filename = files_to_fix[index][1] | |
110 is_ignore = False | |
111 for ignore_names in ignore_these: | |
112 if(filename == ignore_names): | |
113 is_ignore = True | |
114 break | |
115 if(is_ignore): | |
116 continue | |
117 old_include_guard_set = findoldincludeguardset(path_dir,filename) | |
118 if (len(old_include_guard_set) != 3) : | |
119 failed_files.append('unable to figure out the include guards for ' + fil
ename) | |
120 continue | |
121 | |
122 new_include_guard_set = buildnewincludeguardset(path_dir,filename) | |
123 if (len(new_include_guard_set) != 3) : | |
124 failed_files.append('unable to figure out new the include guards for ' +
filename) | |
125 continue | |
126 | |
127 if(not commit): | |
128 print 'old guard: ' + old_include_guard_set[2] | |
129 print 'new guard: ' + new_include_guard_set[2] | |
130 continue | |
131 | |
132 for index in range(2): | |
133 # enough to only replace for file. However, no function for that | |
134 for extension in extensions: | |
135 filemanagement.replacestringinfolder(path_dir,old_include_guard_set[
index],new_include_guard_set[index],extension) | |
136 # special case for last to avoid complications | |
137 for extension in extensions: | |
138 filemanagement.replacestringinfolder(path_dir,' ' + old_include_guard_se
t[2],' ' + new_include_guard_set[2],extension) | |
139 filemanagement.replacestringinfolder(path_dir,'\\/\\/' + old_include_gua
rd_set[2],'\\/\\/ ' + new_include_guard_set[2],extension) | |
140 | |
141 | |
142 if(len(failed_files) > 0): | |
143 print 'Following failures should be investigated manually:' | |
144 for line in failed_files: | |
145 print line | |
OLD | NEW |