@@ -51,78 +51,184 @@ def do_license_check(name, contents):
5151 if not check_license (name , contents ):
5252 report_error_name_no (name , 1 , "incorrect license" )
5353
54-
55- file_names = [s for s in sys .argv [1 :] if (not s .endswith ("_gen.rs" ))
56- and (not ".#" in s )]
57-
5854current_name = ""
5955current_contents = ""
6056check_tab = True
6157check_cr = True
6258check_linelength = True
6359
60+ if len (sys .argv ) < 2 :
61+ print "usage: tidy.py <src-dir>"
62+ sys .exit (1 )
63+
64+ src_dir = sys .argv [1 ]
6465
6566try :
66- for line in fileinput .input (file_names ,
67+ count_rs = 0
68+ count_py = 0
69+ count_js = 0
70+ count_sh = 0
71+ count_pl = 0
72+ count_c = 0
73+ count_h = 0
74+ count_other = 0
75+
76+ count_lines = 0
77+ count_non_blank_lines = 0
78+
79+ def update_counts (current_name ):
80+ global count_rs
81+ global count_py
82+ global count_js
83+ global count_sh
84+ global count_pl
85+ global count_c
86+ global count_h
87+ global count_other
88+
89+ if current_name .endswith (".rs" ):
90+ count_rs += 1
91+ if current_name .endswith (".py" ):
92+ count_py += 1
93+ if current_name .endswith (".js" ):
94+ count_js += 1
95+ if current_name .endswith (".sh" ):
96+ count_sh += 1
97+ if current_name .endswith (".pl" ):
98+ count_pl += 1
99+ if current_name .endswith (".c" ):
100+ count_c += 1
101+ if current_name .endswith (".h" ):
102+ count_h += 1
103+
104+ all_paths = set ()
105+
106+ for (dirpath , dirnames , filenames ) in os .walk (src_dir ):
107+
108+ # Skip some third-party directories
109+ if "src/jemalloc" in dirpath : continue
110+ if "src/llvm" in dirpath : continue
111+ if "src/gyp" in dirpath : continue
112+ if "src/libbacktrace" in dirpath : continue
113+ if "src/compiler-rt" in dirpath : continue
114+ if "src/rt/hoedown" in dirpath : continue
115+ if "src/rustllvm" in dirpath : continue
116+ if "src/rt/valgrind" in dirpath : continue
117+ if "src/rt/msvc" in dirpath : continue
118+ if "src/rust-installer" in dirpath : continue
119+
120+ def interesting_file (f ):
121+ if "miniz.c" in f \
122+ or "jquery" in f \
123+ or "rust_android_dummy" in f :
124+ return False
125+
126+ if f .endswith (".rs" ) \
127+ or f .endswith (".py" ) \
128+ or f .endswith (".js" ) \
129+ or f .endswith (".sh" ) \
130+ or f .endswith (".pl" ) \
131+ or f .endswith (".c" ) \
132+ or f .endswith (".h" ) :
133+ return True
134+ else :
135+ return False
136+
137+ file_names = [os .path .join (dirpath , f ) for f in filenames
138+ if interesting_file (f )
139+ and not f .endswith ("_gen.rs" )
140+ and not ".#" is f ]
141+
142+ if not file_names :
143+ continue
144+
145+ for line in fileinput .input (file_names ,
67146 openhook = fileinput .hook_encoded ("utf-8" )):
68147
69- if "tidy.py" not in fileinput .filename ():
148+ filename = fileinput .filename ()
149+
150+ if "tidy.py" not in filename :
151+ if "TODO" in line :
152+ report_err ("TODO is deprecated; use FIXME" )
153+ match = re .match (r'^.*/(\*|/!?)\s*XXX' , line )
154+ if match :
155+ report_err ("XXX is no longer necessary, use FIXME" )
156+ match = re .match (r'^.*//\s*(NOTE.*)$' , line )
157+ if match and "TRAVIS" not in os .environ :
158+ m = match .group (1 )
159+ if "snap" in m .lower ():
160+ report_warn (match .group (1 ))
161+ match = re .match (r'^.*//\s*SNAP\s+(\w+)' , line )
162+ if match :
163+ hsh = match .group (1 )
164+ date , rev = snapshot .curr_snapshot_rev ()
165+ if not hsh .startswith (rev ):
166+ report_err ("snapshot out of date (" + date
167+ + "): " + line )
168+ else :
169+ if "SNAP" in line :
170+ report_warn ("unmatched SNAP line: " + line )
171+
70172 if cr_flag in line :
71173 check_cr = False
72174 if tab_flag in line :
73175 check_tab = False
74176 if linelength_flag in line :
75177 check_linelength = False
76- if "TODO" in line :
77- report_err ("TODO is deprecated; use FIXME" )
78- match = re .match (r'^.*/(\*|/!?)\s*XXX' , line )
79- if match :
80- report_err ("XXX is no longer necessary, use FIXME" )
81- match = re .match (r'^.*//\s*(NOTE.*)$' , line )
82- if match and "TRAVIS" not in os .environ :
83- m = match .group (1 )
84- if "snap" in m .lower ():
85- report_warn (match .group (1 ))
86- match = re .match (r'^.*//\s*SNAP\s+(\w+)' , line )
87- if match :
88- hsh = match .group (1 )
89- date , rev = snapshot .curr_snapshot_rev ()
90- if not hsh .startswith (rev ):
91- report_err ("snapshot out of date (" + date
92- + "): " + line )
93- else :
94- if "SNAP" in line :
95- report_warn ("unmatched SNAP line: " + line )
96-
97- if check_tab and ('\t ' in line and
98- "Makefile" not in fileinput .filename ()):
99- report_err ("tab character" )
100- if check_cr and not autocrlf and '\r ' in line :
101- report_err ("CR character" )
102- if line .endswith (" \n " ) or line .endswith ("\t \n " ):
103- report_err ("trailing whitespace" )
104- line_len = len (line )- 2 if autocrlf else len (line )- 1
105-
106- if check_linelength and line_len > cols :
107- report_err ("line longer than %d chars" % cols )
108-
109- if fileinput .isfirstline () and current_name != "" :
110- do_license_check (current_name , current_contents )
111-
112- if fileinput .isfirstline ():
113- current_name = fileinput .filename ()
114- current_contents = ""
115- check_cr = True
116- check_tab = True
117- check_linelength = True
118178
119- current_contents += line
179+ if check_tab and ('\t ' in line and
180+ "Makefile" not in filename ):
181+ report_err ("tab character" )
182+ if check_cr and not autocrlf and '\r ' in line :
183+ report_err ("CR character" )
184+ if line .endswith (" \n " ) or line .endswith ("\t \n " ):
185+ report_err ("trailing whitespace" )
186+ line_len = len (line )- 2 if autocrlf else len (line )- 1
187+
188+ if check_linelength and line_len > cols :
189+ report_err ("line longer than %d chars" % cols )
190+
191+ if fileinput .isfirstline ():
192+ # This happens at the end of each file except the last.
193+ if current_name != "" :
194+ update_counts (current_name )
195+ assert len (current_contents ) > 0
196+ do_license_check (current_name , current_contents )
197+
198+ current_name = filename
199+ current_contents = ""
200+ check_cr = True
201+ check_tab = True
202+ check_linelength = True
203+
204+ # Put a reasonable limit on the amount of header data we use for
205+ # the licenseck
206+ if len (current_contents ) < 1000 :
207+ current_contents += line
208+
209+ count_lines += 1
210+ if line .strip ():
211+ count_non_blank_lines += 1
120212
121213 if current_name != "" :
214+ update_counts (current_name )
215+ assert len (current_contents ) > 0
122216 do_license_check (current_name , current_contents )
123217
124218except UnicodeDecodeError as e :
125219 report_err ("UTF-8 decoding error " + str (e ))
126220
221+ print
222+ print "* linted .rs files: " + str (count_rs )
223+ print "* linted .py files: " + str (count_py )
224+ print "* linted .js files: " + str (count_js )
225+ print "* linted .sh files: " + str (count_sh )
226+ print "* linted .pl files: " + str (count_pl )
227+ print "* linted .c files: " + str (count_c )
228+ print "* linted .h files: " + str (count_h )
229+ print "* other linted files: " + str (count_other )
230+ print "* total lines of code: " + str (count_lines )
231+ print "* total non-blank lines of code: " + str (count_non_blank_lines )
232+ print
127233
128234sys .exit (err )
0 commit comments