1212import re
1313import sys
1414import click
15+ import yaml
1516import chardet
1617import logging
1718import datetime
@@ -25,18 +26,63 @@ 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+ dir_number = file_path .split ('/' )
38+ ignore_path = file_path
39+
40+ # gets the file path depth.
41+ for i in dir_number :
42+ # current directory.
43+ dir_name = os .path .dirname (ignore_path )
44+ ignore_path = dir_name
45+ # judge the ignore file exists in the current directory.
46+ ignore_file_path = os .path .join (dir_name , ".ignore_format.yml" )
47+ if not os .path .exists (ignore_file_path ):
48+ continue
49+ try :
50+ with open (ignore_file_path ) as f :
51+ ignore_config = yaml .safe_load (f .read ())
52+ file_ignore = ignore_config .get ("file_path" , [])
53+ dir_ignore = ignore_config .get ("dir_path" , [])
54+ except Exception as e :
55+ logging .error (e )
56+ continue
57+
58+ try :
59+ # judge file_path in the ignore file.
60+ for file in file_ignore :
61+ if file is not None :
62+ file_real_path = os .path .join (dir_name , file )
63+ if file_real_path == file_path :
64+ logging .info ("ignore file path: {}" .format (file_real_path ))
65+ return 0
66+
67+ file_dir_path = os .path .dirname (file_path )
68+ for _dir in dir_ignore :
69+ if _dir is not None :
70+ dir_real_path = os .path .join (dir_name , _dir )
71+ if dir_real_path == file_dir_path :
72+ logging .info ("ignore dir path: {}" .format (dir_real_path ))
73+ return 0
74+ except Exception as e :
75+ logging .error (e )
76+ continue
77+
78+ return 1
79+
3480 def get_new_file (self ):
3581 file_list = list ()
3682 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 ))
83+ os .system ('git remote add rtt_repo {} 1>/dev/null ' .format (self .rtt_repo ))
84+ os .system ('git fetch rtt_repo 1>/dev/null ' )
85+ os .system ('git reset rtt_repo/{} --soft 1>/dev/null ' .format (self .rtt_branch ))
4086 os .system ('git status > git.txt' )
4187 except Exception as e :
4288 logging .error (e )
@@ -60,7 +106,9 @@ def get_new_file(self):
60106 else :
61107 continue
62108
63- file_list .append (file_path )
109+ result = self .__exclude_file (file_path )
110+ if result != 0 :
111+ file_list .append (file_path )
64112
65113 return file_list
66114
@@ -69,20 +117,20 @@ class FormatCheck:
69117 def __init__ (self , file_list ):
70118 self .file_list = file_list
71119
72- def __check_file (self , file_lines ):
120+ def __check_file (self , file_lines , file_path ):
73121 line_num = 1
74122 check_result = False
75123 for line in file_lines :
76124 # check line start
77125 line_start = line .replace (' ' , '' )
78126 # find tab
79127 if line_start .startswith ('\t ' ):
80- logging .error ("line[{}]: please use space replace tab at the start of this line." .format (line_num ))
128+ logging .error ("{} line[{}]: please use space replace tab at the start of this line." .format (file_path , line_num ))
81129 check_result = False
82130 # check line end
83131 lin_end = line .split ('\n ' )[0 ]
84132 if lin_end .endswith (' ' ) or lin_end .endswith ('\t ' ):
85- logging .error ("line[{}]: please delete extra space at the end of this line." .format (line_num ))
133+ logging .error ("{} line[{}]: please delete extra space at the end of this line." .format (file_path , line_num ))
86134 check_result = False
87135 line_num += 1
88136
@@ -96,13 +144,11 @@ def check(self):
96144 encoding_check_result = True
97145 format_check_result = True
98146 for file_path in self .file_list :
99- file_lines = ''
100147 code = ''
101148 if file_path .endswith (".c" ) or file_path .endswith (".h" ):
102149 try :
103- with open (file_path , 'r ' ) as f :
150+ with open (file_path , 'rb ' ) as f :
104151 file = f .read ()
105- file_lines = f .readlines ()
106152 # get file encoding
107153 code = chardet .detect (file )['encoding' ]
108154 except Exception as e :
@@ -116,7 +162,9 @@ def check(self):
116162 else :
117163 logging .info ('[{0}]: encoding check success.' .format (file_path ))
118164
119- format_check_result = self .__check_file (file_lines )
165+ with open (file_path , 'r' ) as f :
166+ file_lines = f .readlines ()
167+ format_check_result = self .__check_file (file_lines , file_path )
120168
121169 if not encoding_check_result or not format_check_result :
122170 logging .error ("files format check fail." )
@@ -155,8 +203,8 @@ def check(self):
155203 true_year = '2006-{}' .format (current_year )
156204 if license_year != true_year :
157205 logging .warning ("[{0}]: license year: {} is not true: {}, please update." .format (file_path ,
158- license_year ,
159- true_year ))
206+ license_year ,
207+ true_year ))
160208
161209 else :
162210 logging .info ("[{0}]: license check success." .format (file_path ))
0 commit comments