@@ -43,78 +43,184 @@ def do_license_check(name, contents):
4343 if not check_license (name , contents ):
4444 report_error_name_no (name , 1 , "incorrect license" )
4545
46-
47- file_names = [s for s in sys .argv [1 :] if (not s .endswith ("_gen.rs" ))
48- and (not ".#" in s )]
49-
5046current_name = ""
5147current_contents = ""
5248check_tab = True
5349check_cr = True
5450check_linelength = True
5551
52+ if len (sys .argv ) < 2 :
53+ print "usage: tidy.py <src-dir>"
54+ sys .exit (1 )
55+
56+ src_dir = sys .argv [1 ]
5657
5758try :
58- for line in fileinput .input (file_names ,
59+ count_rs = 0
60+ count_py = 0
61+ count_js = 0
62+ count_sh = 0
63+ count_pl = 0
64+ count_c = 0
65+ count_h = 0
66+ count_other = 0
67+
68+ count_lines = 0
69+ count_non_blank_lines = 0
70+
71+ def update_counts (current_name ):
72+ global count_rs
73+ global count_py
74+ global count_js
75+ global count_sh
76+ global count_pl
77+ global count_c
78+ global count_h
79+ global count_other
80+
81+ if current_name .endswith (".rs" ):
82+ count_rs += 1
83+ if current_name .endswith (".py" ):
84+ count_py += 1
85+ if current_name .endswith (".js" ):
86+ count_js += 1
87+ if current_name .endswith (".sh" ):
88+ count_sh += 1
89+ if current_name .endswith (".pl" ):
90+ count_pl += 1
91+ if current_name .endswith (".c" ):
92+ count_c += 1
93+ if current_name .endswith (".h" ):
94+ count_h += 1
95+
96+ all_paths = set ()
97+
98+ for (dirpath , dirnames , filenames ) in os .walk (src_dir ):
99+
100+ # Skip some third-party directories
101+ if "src/jemalloc" in dirpath : continue
102+ if "src/llvm" in dirpath : continue
103+ if "src/gyp" in dirpath : continue
104+ if "src/libbacktrace" in dirpath : continue
105+ if "src/compiler-rt" in dirpath : continue
106+ if "src/rt/hoedown" in dirpath : continue
107+ if "src/rustllvm" in dirpath : continue
108+ if "src/rt/valgrind" in dirpath : continue
109+ if "src/rt/msvc" in dirpath : continue
110+ if "src/rust-installer" in dirpath : continue
111+
112+ def interesting_file (f ):
113+ if "miniz.c" in f \
114+ or "jquery" in f \
115+ or "rust_android_dummy" in f :
116+ return False
117+
118+ if f .endswith (".rs" ) \
119+ or f .endswith (".py" ) \
120+ or f .endswith (".js" ) \
121+ or f .endswith (".sh" ) \
122+ or f .endswith (".pl" ) \
123+ or f .endswith (".c" ) \
124+ or f .endswith (".h" ) :
125+ return True
126+ else :
127+ return False
128+
129+ file_names = [os .path .join (dirpath , f ) for f in filenames
130+ if interesting_file (f )
131+ and not f .endswith ("_gen.rs" )
132+ and not ".#" is f ]
133+
134+ if not file_names :
135+ continue
136+
137+ for line in fileinput .input (file_names ,
59138 openhook = fileinput .hook_encoded ("utf-8" )):
60139
61- if "tidy.py" not in fileinput .filename ():
140+ filename = fileinput .filename ()
141+
142+ if "tidy.py" not in filename :
143+ if "TODO" in line :
144+ report_err ("TODO is deprecated; use FIXME" )
145+ match = re .match (r'^.*/(\*|/!?)\s*XXX' , line )
146+ if match :
147+ report_err ("XXX is no longer necessary, use FIXME" )
148+ match = re .match (r'^.*//\s*(NOTE.*)$' , line )
149+ if match and "TRAVIS" not in os .environ :
150+ m = match .group (1 )
151+ if "snap" in m .lower ():
152+ report_warn (match .group (1 ))
153+ match = re .match (r'^.*//\s*SNAP\s+(\w+)' , line )
154+ if match :
155+ hsh = match .group (1 )
156+ date , rev = snapshot .curr_snapshot_rev ()
157+ if not hsh .startswith (rev ):
158+ report_err ("snapshot out of date (" + date
159+ + "): " + line )
160+ else :
161+ if "SNAP" in line :
162+ report_warn ("unmatched SNAP line: " + line )
163+
62164 if cr_flag in line :
63165 check_cr = False
64166 if tab_flag in line :
65167 check_tab = False
66168 if linelength_flag in line :
67169 check_linelength = False
68- if "TODO" in line :
69- report_err ("TODO is deprecated; use FIXME" )
70- match = re .match (r'^.*/(\*|/!?)\s*XXX' , line )
71- if match :
72- report_err ("XXX is no longer necessary, use FIXME" )
73- match = re .match (r'^.*//\s*(NOTE.*)$' , line )
74- if match and "TRAVIS" not in os .environ :
75- m = match .group (1 )
76- if "snap" in m .lower ():
77- report_warn (match .group (1 ))
78- match = re .match (r'^.*//\s*SNAP\s+(\w+)' , line )
79- if match :
80- hsh = match .group (1 )
81- date , rev = snapshot .curr_snapshot_rev ()
82- if not hsh .startswith (rev ):
83- report_err ("snapshot out of date (" + date
84- + "): " + line )
85- else :
86- if "SNAP" in line :
87- report_warn ("unmatched SNAP line: " + line )
88-
89- if check_tab and ('\t ' in line and
90- "Makefile" not in fileinput .filename ()):
91- report_err ("tab character" )
92- if check_cr and not autocrlf and '\r ' in line :
93- report_err ("CR character" )
94- if line .endswith (" \n " ) or line .endswith ("\t \n " ):
95- report_err ("trailing whitespace" )
96- line_len = len (line )- 2 if autocrlf else len (line )- 1
97-
98- if check_linelength and line_len > cols :
99- report_err ("line longer than %d chars" % cols )
100-
101- if fileinput .isfirstline () and current_name != "" :
102- do_license_check (current_name , current_contents )
103-
104- if fileinput .isfirstline ():
105- current_name = fileinput .filename ()
106- current_contents = ""
107- check_cr = True
108- check_tab = True
109- check_linelength = True
110170
111- current_contents += line
171+ if check_tab and ('\t ' in line and
172+ "Makefile" not in filename ):
173+ report_err ("tab character" )
174+ if check_cr and not autocrlf and '\r ' in line :
175+ report_err ("CR character" )
176+ if line .endswith (" \n " ) or line .endswith ("\t \n " ):
177+ report_err ("trailing whitespace" )
178+ line_len = len (line )- 2 if autocrlf else len (line )- 1
179+
180+ if check_linelength and line_len > cols :
181+ report_err ("line longer than %d chars" % cols )
182+
183+ if fileinput .isfirstline ():
184+ # This happens at the end of each file except the last.
185+ if current_name != "" :
186+ update_counts (current_name )
187+ assert len (current_contents ) > 0
188+ do_license_check (current_name , current_contents )
189+
190+ current_name = filename
191+ current_contents = ""
192+ check_cr = True
193+ check_tab = True
194+ check_linelength = True
195+
196+ # Put a reasonable limit on the amount of header data we use for
197+ # the licenseck
198+ if len (current_contents ) < 1000 :
199+ current_contents += line
200+
201+ count_lines += 1
202+ if line .strip ():
203+ count_non_blank_lines += 1
112204
113205 if current_name != "" :
206+ update_counts (current_name )
207+ assert len (current_contents ) > 0
114208 do_license_check (current_name , current_contents )
115209
116210except UnicodeDecodeError , e :
117211 report_err ("UTF-8 decoding error " + str (e ))
118212
213+ print
214+ print "* linted .rs files: " + str (count_rs )
215+ print "* linted .py files: " + str (count_py )
216+ print "* linted .js files: " + str (count_js )
217+ print "* linted .sh files: " + str (count_sh )
218+ print "* linted .pl files: " + str (count_pl )
219+ print "* linted .c files: " + str (count_c )
220+ print "* linted .h files: " + str (count_h )
221+ print "* other linted files: " + str (count_other )
222+ print "* total lines of code: " + str (count_lines )
223+ print "* total non-blank lines of code: " + str (count_non_blank_lines )
224+ print
119225
120226sys .exit (err )
0 commit comments