1212import re
1313import sys
1414import click
15+ import yaml
1516import chardet
1617import logging
1718import datetime
@@ -25,18 +26,39 @@ def init_logger():
2526 datefmt = date_format ,
2627 )
2728
29+
2830class CheckOut :
2931 def __init__ (self , rtt_repo , rtt_branch ):
3032 self .root = os .getcwd ()
3133 self .rtt_repo = rtt_repo
3234 self .rtt_branch = rtt_branch
3335
36+ def __exclude_file (self , file_path ):
37+ ignore_file_path = os .path .join (self .root , 'ignore_format.yml' )
38+ try :
39+ with open (ignore_file_path ) as f :
40+ ignore_config = yaml .safe_load (f .read ())
41+ file_ignore = ignore_config .get ("file_path" , [])
42+ dir_ignore = ignore_config .get ("dir_path" , [])
43+ except Exception as e :
44+ logging .error (e )
45+ return 1
46+
47+ if file_path in file_ignore :
48+ return 0
49+
50+ file_dir_path = os .path .dirname (file_path )
51+ if file_dir_path in dir_ignore :
52+ return 0
53+
54+ return 1
55+
3456 def get_new_file (self ):
3557 file_list = list ()
3658 try :
37- os .system ('git remote add rtt_repo {}' .format (self .rtt_repo ))
38- os .system ('git fetch rtt_repo' )
39- os .system ('git reset rtt_repo/{} --soft' .format (self .rtt_branch ))
59+ os .system ('git remote add rtt_repo {} 1>/dev/null ' .format (self .rtt_repo ))
60+ os .system ('git fetch rtt_repo 1>/dev/null ' )
61+ os .system ('git reset rtt_repo/{} --soft 1>/dev/null ' .format (self .rtt_branch ))
4062 os .system ('git status > git.txt' )
4163 except Exception as e :
4264 logging .error (e )
@@ -60,7 +82,9 @@ def get_new_file(self):
6082 else :
6183 continue
6284
63- file_list .append (file_path )
85+ result = self .__exclude_file (file_path )
86+ if result != 0 :
87+ file_list .append (file_path )
6488
6589 return file_list
6690
@@ -96,13 +120,11 @@ def check(self):
96120 encoding_check_result = True
97121 format_check_result = True
98122 for file_path in self .file_list :
99- file_lines = ''
100123 code = ''
101124 if file_path .endswith (".c" ) or file_path .endswith (".h" ):
102125 try :
103- with open (file_path , 'r ' ) as f :
126+ with open (file_path , 'rb ' ) as f :
104127 file = f .read ()
105- file_lines = f .readlines ()
106128 # get file encoding
107129 code = chardet .detect (file )['encoding' ]
108130 except Exception as e :
@@ -116,6 +138,8 @@ def check(self):
116138 else :
117139 logging .info ('[{0}]: encoding check success.' .format (file_path ))
118140
141+ with open (file_path , 'r' ) as f :
142+ file_lines = f .readlines ()
119143 format_check_result = self .__check_file (file_lines )
120144
121145 if not encoding_check_result or not format_check_result :
@@ -155,8 +179,8 @@ def check(self):
155179 true_year = '2006-{}' .format (current_year )
156180 if license_year != true_year :
157181 logging .warning ("[{0}]: license year: {} is not true: {}, please update." .format (file_path ,
158- license_year ,
159- true_year ))
182+ license_year ,
183+ true_year ))
160184
161185 else :
162186 logging .info ("[{0}]: license check success." .format (file_path ))
0 commit comments