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