有时候写了一堆文件,从哪里抄来的,或者字节写的,没有按照linux kernel的代码标准,这时候手动去改,非常繁琐
.
写一个python脚本, 自动将源代码文件(如.c文件)格式化/规范化, 如删除多余的空行空格, 按照linux kernel标准格式化.
当前的使用方法 : python3 format.py 跟上文件名, 后续可扩展对目录下所有文件操作.
import sysimport osconditional_str = ["if","while","for"]def table_space(tag_count):total_str=""if tag_count == 0:return total_strfor num in range(0,tag_count):total_str += '\t'return total_strdef is_space_line(line):if '' == line.strip():return Trueelse:return Falsedef delete_redundant_space(flag_dict):flag_dict['line'] = flag_dict['line'].strip() + '\n'flag_dict['line_need'] = Truedef insert_space_char(flag_dict):line = flag_dict['line']for con_str in conditional_str:temp_val = con_str + '('if temp_val in line:nPos=flag_dict['line'].index(temp_val)list_line = list(flag_dict['line'])list_line.insert(nPos + len(temp_val) - 1,' ')flag_dict['line'] = "".join(list_line)def calculate_table_space(flag_dict):line = flag_dict['line']while True:if '#' == line[0]:flag_dict['last_tag_cnt'] = 0breakif '\n' == line:# print("=======kong hang=========")breakif '{' in line and '}' in line:break;if '{' in line:flag_dict['tag_cnt'] += 1if '}' in line:flag_dict['tag_cnt'] -= 1flag_dict['last_tag_cnt'] -= 1breakdef add_table_space_to_line(flag_dict):flag_dict['line'] = table_space(flag_dict['last_tag_cnt'] ) + flag_dict['line']def judge_conditional_statement(flag_dict,fp2):line = flag_dict['line']flag_dict['line'] = flag_dict['line']if flag_dict['undoing_if'] == True:if is_space_line(line) == True:flag_dict['line_need'] = Falsereturnflag_dict['undoing_if'] = Falseif '\t{' in line:flag_dict['line_need'] = Falseelse:fp2.seek(fp2.tell() - 3)fp2.write('\n')flag_dict['line'] = '\t' + flag_dict['line']returnfor con_str in conditional_str:temp_val1 = '\t' + con_str + ' 'temp_val2 = '\t' + con_str + '('if (temp_val1 in line or temp_val2 in line) and ')' in line and '{' not in line:flag_dict['line_need'] = Falseflag_dict['last_line_need'] = Trueflag_dict['last_line'] = flag_dict['line'].rstrip() + " {\n"flag_dict['undoing_if'] = Truereturndef format_a_file(srcfile,dstfile):flag_dict = {'line_need' : False,'line' : "",'last_line_need' : False,'last_line' : "",'tag_cnt' : 0,'last_tag_cnt' : 0,'undoing_if' : False,}with open(srcfile, 'r') as fp1, open(dstfile, 'w') as fp2:lines = fp1.readlines()for line in lines:flag_dict['line'] = lineflag_dict['last_tag_cnt'] = flag_dict['tag_cnt']delete_redundant_space(flag_dict)calculate_table_space(flag_dict)add_table_space_to_line(flag_dict)insert_space_char(flag_dict)if flag_dict['last_line_need'] == True:fp2.write(flag_dict['last_line'])flag_dict['last_line_need'] = Falsejudge_conditional_statement(flag_dict,fp2)if flag_dict['line_need'] == True:fp2.write(flag_dict['line'])def format_all_files(srcfile):print("1111111111")if __name__ == '__main__':print(str(sys.argv[0]) + " enter")print(len(sys.argv))print(os.getcwd())# srcfile = sys.argv[1]# dstfile = sys.argv[2]srcfile = "FtModule.cpp"dstfile = '1.txt'if True:format_all_files(srcfile)exitprint(("Format %s to %s") % (srcfile,dstfile))curdir = os.getcwd()print(curdir)#print(table_space(3) + 'wwww')format_a_file(srcfile,dstfile)#os.rename(srcfile, 'FtModule.cpp')
如果觉得《[工具]-脚本自动化工具:按照linux kernel标准格式化输出文件(format_file)》对你有帮助,请点赞、收藏,并留下你的观点哦!