Bo Lv | 72d0e90 | 2023-01-02 14:27:34 +0000 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | # -*- coding: utf-8 -*- |
| 3 | |
| 4 | # Amlogic gerrit code auto-fix script |
| 5 | # Author: xiaobo.gu@amlogic.com |
| 6 | # Init version: 2015.05.01 |
| 7 | |
| 8 | import sys, os |
| 9 | import json |
| 10 | |
| 11 | MESSAGE_INFO_1 = "trailing spaces" |
| 12 | MESSAGE_INFO_2 = "spacing around parenthesis" |
| 13 | MESSAGE_INFO_3 = "spacing around ==" |
| 14 | MESSAGE_INFO_4 = "spacing around !=" |
| 15 | MESSAGE_INFO_5 = "do {" |
| 16 | MESSAGE_INFO_6 = "file should not be executable" |
| 17 | MESSAGE_INFO_7 = "possibly incorrect mixed spaces then tabs indentation" |
| 18 | MESSAGE_INFO_8 = "file should not have carriage returns" |
| 19 | MESSAGE_INFO_9 = "make sure indent style matches rest of file" |
| 20 | MESSAGE_INFO_10 = "spacing around &&" |
| 21 | MESSAGE_INFO_11 = "spacing around ||" |
| 22 | MESSAGE_INFO_12 = "spacing around >=" |
| 23 | MESSAGE_INFO_13 = "spacing around <=" |
| 24 | |
| 25 | class fixer(object): |
| 26 | def __init__(self, filename): |
| 27 | self.filename = filename |
| 28 | self.info_review = None |
| 29 | self.info_comment = None |
| 30 | self.info_labels = None |
| 31 | self.cur_file_name = "" |
| 32 | self.cur_file_lines = 0 |
| 33 | self.cur_file_content = "" |
| 34 | self.cur_line_content = "" |
| 35 | self.cur_line = 0 |
| 36 | self.cur_message = "" |
| 37 | self.verified = -1 |
| 38 | |
| 39 | def read_log(self): |
| 40 | self.file_handle = open(self.filename) |
| 41 | self.info_str = self.file_handle.read() |
| 42 | self.file_handle.close() |
| 43 | self.info_review = json.loads(self.info_str) |
| 44 | self.info_comment = self.info_review.get("comments") |
| 45 | self.info_labels = self.info_review.get("labels") |
| 46 | |
| 47 | def fix(self): |
| 48 | self.verified = self.info_labels.get("Verified") |
| 49 | if(1 == self.verified): |
| 50 | print "Verified +1, Quit..." |
| 51 | return |
| 52 | for file_info in self.info_comment: |
| 53 | print file_info |
| 54 | #print self.cur_file_content |
| 55 | #for line in self.cur_file_content: |
| 56 | # print line, |
| 57 | # start fix for each warning line |
| 58 | for message_info in self.info_comment[file_info]: |
| 59 | self.cur_file_name = file_info |
| 60 | self.cur_message = message_info.get("message") |
| 61 | self.cur_line = (int)(message_info.get("line")) - 1 # index-1 |
| 62 | |
| 63 | if (self.cur_line >= 0): |
| 64 | # <0 always means line0 error, |
| 65 | # that means file format error etc.. |
| 66 | # it's no need open the file |
| 67 | cur_file = open(file_info) # open current file |
| 68 | self.cur_file_content = cur_file.readlines() # get all content of current file, and split info lines |
| 69 | cur_file.close() # close current file |
| 70 | self.cur_file_lines = len(self.cur_file_content) |
| 71 | self.cur_line_content = str(self.cur_file_content[self.cur_line]) |
| 72 | self.message_handler() |
| 73 | if (self.cur_line >= 0): |
| 74 | # <0 always means line0 error, |
| 75 | # that means file format error etc.. |
| 76 | # it's no need write back current line |
| 77 | self.cur_file_content[self.cur_line] = self.cur_line_content |
| 78 | cur_file = open(file_info, 'w') # open current file |
| 79 | cur_file.writelines(self.cur_file_content) # save file |
| 80 | cur_file.close() # close current file |
| 81 | |
| 82 | def message_handler(self): |
| 83 | if (self.cur_message.find(MESSAGE_INFO_1) >= 0): |
| 84 | self.message_1() |
| 85 | if (self.cur_message.find(MESSAGE_INFO_2) >= 0): |
| 86 | self.message_2() |
| 87 | if (self.cur_message.find(MESSAGE_INFO_3) >= 0): |
| 88 | self.message_3() |
| 89 | if (self.cur_message.find(MESSAGE_INFO_4) >= 0): |
| 90 | self.message_4() |
| 91 | if (self.cur_message.find(MESSAGE_INFO_5) >= 0): |
| 92 | self.message_5() |
| 93 | if (self.cur_message.find(MESSAGE_INFO_6) >= 0): |
| 94 | self.message_6() |
| 95 | if (self.cur_message.find(MESSAGE_INFO_7) >= 0): |
| 96 | self.message_7() |
| 97 | if (self.cur_message.find(MESSAGE_INFO_8) >= 0): |
| 98 | self.message_8() |
| 99 | if (self.cur_message.find(MESSAGE_INFO_9) >= 0): |
| 100 | self.message_9() |
| 101 | if (self.cur_message.find(MESSAGE_INFO_10) >= 0): |
| 102 | self.message_10() |
| 103 | if (self.cur_message.find(MESSAGE_INFO_11) >= 0): |
| 104 | self.message_11() |
| 105 | if (self.cur_message.find(MESSAGE_INFO_12) >= 0): |
| 106 | self.message_12() |
| 107 | if (self.cur_message.find(MESSAGE_INFO_13) >= 0): |
| 108 | self.message_13() |
| 109 | |
| 110 | def message_1(self): |
| 111 | # acknowledge bug: can not fix last line with last blank character |
| 112 | '''MESSAGE_INFO_1 start''' |
| 113 | #print " -", self.cur_line, self.cur_line_content, |
| 114 | cur_line_length = len(self.cur_line_content) |
| 115 | #print "cur_line_length", cur_line_length |
| 116 | #print self.cur_line_content |
| 117 | for cur_length in range(cur_line_length-1): |
| 118 | #print cur_length |
| 119 | cur_char_pos = cur_line_length-2-cur_length |
| 120 | #print cur_char_pos |
| 121 | #print self.cur_line_content[cur_char_pos] |
| 122 | #print self.cur_line_content |
| 123 | #print self.cur_line_content[0:cur_char_pos], "test" |
| 124 | if (self.cur_line_content[cur_char_pos] == ' ') or \ |
| 125 | (self.cur_line_content[cur_char_pos] == ' ') : |
| 126 | self.cur_line_content = self.cur_line_content[0:cur_char_pos] + '\n' |
| 127 | #print self.cur_line_content |
| 128 | else: |
| 129 | break |
| 130 | '''MESSAGE_INFO_1 end''' |
| 131 | |
| 132 | def message_2(self): |
| 133 | '''MESSAGE_INFO_2 start''' |
| 134 | cur_line_length = len(self.cur_line_content) |
| 135 | # search parenthesis from left |
| 136 | pare_pos_left = self.cur_line_content.find('(') |
| 137 | #print "self.cur_line_content[pare_pos_left]:", self.cur_line_content[pare_pos_left-1] |
| 138 | #print self.cur_line_content |
| 139 | # insert blank char if doesn't have one |
| 140 | if (pare_pos_left > 0) and (' ' != self.cur_line_content[pare_pos_left-1]): |
| 141 | self.cur_line_content = self.cur_line_content[0:pare_pos_left] + ' ' + self.cur_line_content[pare_pos_left:cur_line_length] |
| 142 | #print self.cur_line_content |
| 143 | # re-calculate cur line length, maybe previous operations changed it's content |
| 144 | cur_line_length = len(self.cur_line_content) |
| 145 | # search parenthesis from right |
| 146 | pare_pos_right = self.cur_line_content.rfind(')') |
| 147 | if ((pare_pos_right+1) <= cur_line_length): |
| 148 | #print "self.cur_line_content[pare_pos_right]:", self.cur_line_content[pare_pos_right+1] |
| 149 | #print self.cur_line_content |
| 150 | if (pare_pos_right > 0) and (' ' != self.cur_line_content[pare_pos_right+1]) and \ |
| 151 | ('\n' != self.cur_line_content[pare_pos_right+1]): |
| 152 | self.cur_line_content = self.cur_line_content[0:pare_pos_right+1] + ' ' + self.cur_line_content[pare_pos_right+1:cur_line_length] |
| 153 | #print self.cur_line_content |
| 154 | '''MESSAGE_INFO_2 end''' |
| 155 | |
| 156 | def message_3(self): |
| 157 | self.message_space_around("==") |
| 158 | |
| 159 | def message_4(self): |
| 160 | self.message_space_around("!=") |
| 161 | |
| 162 | def message_5(self): |
| 163 | cur_line_length = len(self.cur_line_content) |
| 164 | msg_pos = self.cur_line_content.find("do") |
| 165 | #print "self.cur_line_content[msg_pos+2]", self.cur_line_content[msg_pos+2] |
| 166 | if (' ' != self.cur_line_content[msg_pos+2]): |
| 167 | self.cur_line_content = self.cur_line_content[0:msg_pos+2] + ' ' + self.cur_line_content[msg_pos+2:cur_line_length] |
| 168 | |
| 169 | def message_6(self): |
| 170 | shell_cmd = "chmod -x " + self.cur_file_name |
| 171 | os.system(shell_cmd) |
| 172 | |
| 173 | def message_7(self): |
| 174 | cur_line_length = len(self.cur_line_content) |
| 175 | cur_line_first_noblank_pos = 0 |
| 176 | # find out first non-blank(' '&' ') char |
| 177 | for cur_char_pos in range(cur_line_length): |
| 178 | if (' ' != self.cur_line_content[cur_char_pos]) and \ |
| 179 | (' ' != self.cur_line_content[cur_char_pos]): |
| 180 | cur_line_first_noblank_pos = cur_char_pos |
| 181 | break |
| 182 | #print self.cur_line_content |
| 183 | # replace these 4' 's with tab, 1,2,3 blanks will be deleted |
| 184 | no_blank_str = self.cur_line_content[0:cur_line_first_noblank_pos] |
| 185 | no_blank_str = no_blank_str.replace(" ", " ") |
| 186 | no_blank_str = no_blank_str.replace(" ", "") |
| 187 | no_blank_str = no_blank_str.replace(" ", "") |
| 188 | no_blank_str = no_blank_str.replace(" ", "") |
| 189 | self.cur_line_content = no_blank_str + self.cur_line_content[cur_line_first_noblank_pos:cur_line_length] |
| 190 | |
| 191 | def message_8(self): |
| 192 | shell_cmd = "dos2unix -o -f " + self.cur_file_name |
| 193 | os.system(shell_cmd) |
| 194 | |
| 195 | def message_9(self): |
| 196 | cur_line_length = len(self.cur_line_content) |
| 197 | cur_line_first_noblank_pos = 0 |
| 198 | # find out first non-blank(' '&' ') char |
| 199 | for cur_char_pos in range(cur_line_length): |
| 200 | if (' ' != self.cur_line_content[cur_char_pos]) and \ |
| 201 | (' ' != self.cur_line_content[cur_char_pos]): |
| 202 | cur_line_first_noblank_pos = cur_char_pos |
| 203 | break |
| 204 | no_blank_str = self.cur_line_content[0:cur_line_first_noblank_pos] |
| 205 | no_blank_str_tmp = no_blank_str.replace(" ", " ") |
| 206 | if (no_blank_str_tmp == no_blank_str): |
| 207 | no_blank_str = no_blank_str.replace(" ", " ") |
| 208 | else: |
| 209 | no_blank_str = no_blank_str_tmp |
| 210 | #print self.cur_line_content |
| 211 | self.cur_line_content = no_blank_str + self.cur_line_content[cur_line_first_noblank_pos:cur_line_length] |
| 212 | #print self.cur_line_content |
| 213 | |
| 214 | def message_10(self): |
| 215 | self.message_space_around("&&") |
| 216 | |
| 217 | def message_11(self): |
| 218 | self.message_space_around("||") |
| 219 | |
| 220 | def message_12(self): |
| 221 | self.message_space_around(">=") |
| 222 | |
| 223 | def message_13(self): |
| 224 | self.message_space_around("<=") |
| 225 | |
| 226 | def message_space_around(self, symbol): |
| 227 | replace_symbol = [] |
| 228 | replace_symbol.append(' ' + symbol + ' ') |
| 229 | replace_symbol.append(' ' + symbol) |
| 230 | replace_symbol.append(symbol + ' ') |
| 231 | #print self.cur_line_content |
| 232 | for rep in range(len(replace_symbol)): |
| 233 | self.cur_line_content = self.cur_line_content.replace(replace_symbol[rep], symbol) |
| 234 | self.cur_line_content = self.cur_line_content.replace(symbol, replace_symbol[0]) |
| 235 | #print self.cur_line_content |
| 236 | |
| 237 | def printf(self): |
| 238 | #print "comment: ", self.info_comment |
| 239 | #print "labels: ", self.info_labels |
| 240 | for file_info in self.info_comment: |
| 241 | print file_info |
| 242 | for message_info in self.info_comment[file_info]: |
| 243 | print " ", message_info |
| 244 | |
| 245 | def run(self): |
| 246 | self.read_log() |
| 247 | #self.printf() |
| 248 | self.fix() |
| 249 | |
| 250 | if __name__=='__main__': |
| 251 | if len(sys.argv) != 2: |
| 252 | print 'auto_fix.py [review_log_file]' |
| 253 | exit(1) |
| 254 | fixer = fixer(sys.argv[1]) |
| 255 | fixer.run() |