]>
git.proxmox.com Git - rustc.git/blob - src/etc/tidy.py
fd3309dce12cbd01e1c573e05beeddb096a7c2f1
1 # Copyright 2010-2014 The Rust Project Developers. See the COPYRIGHT
2 # file at the top-level directory of this distribution and at
3 # http://rust-lang.org/COPYRIGHT.
5 # Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 # http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 # <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 # option. This file may not be copied, modified, or distributed
9 # except according to those terms.
16 from licenseck
import *
21 cr_flag
= "ignore-tidy-cr"
22 tab_flag
= "ignore-tidy-tab"
23 linelength_flag
= "ignore-tidy-linelength"
25 # Be careful to support Python 2.4, 2.6, and 3.x here!
26 config_proc
= subprocess
.Popen(["git", "config", "core.autocrlf"],
27 stdout
=subprocess
.PIPE
)
28 result
= config_proc
.communicate()[0]
30 true
= "true".encode('utf8')
31 autocrlf
= result
.strip() == true
if result
is not None else False
34 def report_error_name_no(name
, no
, s
):
36 print("%s:%d: %s" % (name
, no
, s
))
41 report_error_name_no(fileinput
.filename(), fileinput
.filelineno(), s
)
45 print("%s:%d: %s" % (fileinput
.filename(),
46 fileinput
.filelineno(),
50 def do_license_check(name
, contents
):
51 if not check_license(name
, contents
):
52 report_error_name_no(name
, 1, "incorrect license")
58 check_linelength
= True
61 print "usage: tidy.py <src-dir>"
68 count_non_blank_lines
= 0
70 interesting_files
= ['.rs', '.py', '.js', '.sh', '.c', '.h']
72 file_counts
= {ext
: 0 for ext
in interesting_files
}
73 file_counts
['other'] = 0
75 def update_counts(current_name
):
77 _
, ext
= os
.path
.splitext(current_name
)
79 if ext
in file_counts
:
82 file_counts
['other'] += 1
86 for (dirpath
, dirnames
, filenames
) in os
.walk(src_dir
):
88 # Skip some third-party directories
103 if any(d
in dirpath
for d
in skippable_dirs
):
106 def interesting_file(f
):
109 or "rust_android_dummy" in f
:
112 return any(os
.path
.splitext(f
)[1] == ext
for ext
in interesting_files
)
114 file_names
= [os
.path
.join(dirpath
, f
) for f
in filenames
115 if interesting_file(f
)
116 and not f
.endswith("_gen.rs")
122 for line
in fileinput
.input(file_names
,
123 openhook
=fileinput
.hook_encoded("utf-8")):
125 filename
= fileinput
.filename()
127 if "tidy.py" not in filename
:
129 report_err("TODO is deprecated; use FIXME")
130 match
= re
.match(r
'^.*/(\*|/!?)\s*XXX', line
)
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
:
136 if "snap" in m
.lower():
137 report_warn(match
.group(1))
138 match
= re
.match(r
'^.*//\s*SNAP\s+(\w+)', line
)
141 date
, rev
= snapshot
.curr_snapshot_rev()
142 if not hsh
.startswith(rev
):
143 report_err("snapshot out of date (" + date
147 report_warn("unmatched SNAP line: " + line
)
153 if linelength_flag
in line
:
154 check_linelength
= False
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
165 if check_linelength
and line_len
> cols
:
166 report_err("line longer than %d chars" % cols
)
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
)
175 current_name
= filename
176 current_contents
= ""
179 check_linelength
= True
181 # Put a reasonable limit on the amount of header data we use for
183 if len(current_contents
) < 1000:
184 current_contents
+= line
188 count_non_blank_lines
+= 1
190 if current_name
!= "":
191 update_counts(current_name
)
192 assert len(current_contents
) > 0
193 do_license_check(current_name
, current_contents
)
195 except UnicodeDecodeError as e
:
196 report_err("UTF-8 decoding error " + str(e
))
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
)