OpenCloudOS-Kernel/scripts/fix_format.py

89 lines
3.0 KiB
Python
Raw Permalink Normal View History

#!/usr/bin/python3
# -*- coding: UTF-8 -*-
#WARNING: suspect code indent for conditional statements (8, 15)
import os
import sys
def do_fix_file_error(root_kernel, file_name, file_line, error_type):
path_file = root_kernel + os.sep + file_name
fd_file = open(path_file, 'r')
list_file = fd_file.readlines()
fd_file.close()
src_line = file_line - 1
str_src = list_file[src_line]
new_str = ''
if error_type.find("ERROR: code indent should use tabs where possible") >= 0:
str_l = str_src.lstrip()
num_tap = int(str_src.find(str_l) / 8)
for i in range(num_tap):
new_str += '\t'
new_str += str_l
elif error_type.find("ERROR: space required before the open brace '{'") >= 0:
if str_src.count('{') == 1 and str_src.find('{') == 0:
return
else:
new_str = str_src.replace(' {', '{')
new_str = new_str.replace('{', ' {')
if new_str.find(' {') == 0:
new_str = new_str[1:]
elif error_type.find("ERROR: space required before the open parenthesis '('") >= 0:
if str_src.count('(') == 1 and str_src.find('(') == 0:
return
else:
new_str = str_src.replace(' (', '(')
new_str = new_str.replace('(', ' (')
if new_str.find(' (') == 0:
new_str = new_str[1:]
elif error_type.find("ERROR: space required after that close brace '}'") >= 0:
new_str = str_src.replace('} ', '}')
new_str = new_str.replace('}', '} ')
if new_str[-2] == ' ':
new_str = new_str[:-2] + new_str[-1:]
elif error_type.find("ERROR: do not initialise globals to 0") >= 0:
new_str = str_src.rsplit('=', 1)[0].rstrip() + ';\n'
elif error_type.find("ERROR: trailing whitespace") >= 0:
new_str = str_src
while new_str.find('\t\n') >= 0:
new_str = new_str.replace('\t\n', '\n')
elif error_type.find("ERROR:") >= 0:
print(error_type)
else:
return
if len(new_str) > 0 and new_str[-1] == '\n':
new_str = new_str[:-1].rstrip() + '\n'
list_file[src_line] = new_str
write_fd = open(path_file, 'w')
for item in list_file:
write_fd.write(item)
write_fd.flush()
write_fd.close()
def main():
root_kernel = sys.argv[1]
path_format = sys.argv[2]
if not os.path.exists(root_kernel):
print("ERROR: kernel's dir is not exists")
return
if not os.path.exists(path_format):
print("ERROR: format.txt is not exists")
return
fd_format = open(path_format, 'r')
list_format = fd_format.readlines()
fd_format.close()
for index, line in enumerate(list_format):
if line[0] == '#' and line.find('FILE:') >= 0:
str_file = line.split('FILE:', 1)[1].split(':', 1)[0].strip()
int_line_num = int(line.rsplit(':', 2)[1])
str_error = list_format[index - 1][:-1]
do_fix_file_error(root_kernel, str_file, int_line_num, str_error)
if __name__ == '__main__':
main()