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 check_license
21 cr_flag = "ignore-tidy-cr"
22 tab_flag = "ignore-tidy-tab"
23 linelength_flag = "ignore-tidy-linelength"
25 interesting_files = ['.rs', '.py', '.js', '.sh', '.c', '.h']
26 uninteresting_files = ['miniz.c', 'jquery', 'rust_android_dummy']
29 def report_error_name_no(name, no, s):
31 print("%s:%d: %s" % (name, no, s))
36 report_error_name_no(fileinput.filename(), fileinput.filelineno(), s)
40 print("%s:%d: %s" % (fileinput.filename(),
41 fileinput.filelineno(),
45 def do_license_check(name, contents):
46 if not check_license(name, contents):
47 report_error_name_no(name, 1, "incorrect license")
50 def update_counts(current_name):
52 global count_other_linted_files
54 _, ext = os.path.splitext(current_name)
56 if ext in interesting_files:
59 count_other_linted_files += 1
62 def interesting_file(f):
63 if any(x in f for x in uninteresting_files):
66 return any(os.path.splitext(f)[1] == ext for ext in interesting_files)
69 # Be careful to support Python 2.4, 2.6, and 3.x here!
70 config_proc = subprocess.Popen(["git", "config", "core.autocrlf"],
71 stdout=subprocess.PIPE)
72 result = config_proc.communicate()[0]
74 true = "true".encode('utf8')
75 autocrlf = result.strip() == true if result is not None else False
81 check_linelength = True
84 print("usage: tidy.py <src-dir>")
90 count_non_blank_lines = 0
91 count_other_linted_files = 0
93 file_counts = {ext: 0 for ext in interesting_files}
98 for (dirpath, dirnames, filenames) in os.walk(src_dir):
99 # Skip some third-party directories
111 'src/rust-installer',
115 if any(d in dirpath for d in skippable_dirs):
118 file_names = [os.path.join(dirpath, f) for f in filenames
119 if interesting_file(f)
120 and not f.endswith("_gen.rs")
126 for line in fileinput.input(file_names,
127 openhook=fileinput.hook_encoded("utf-8")):
129 filename = fileinput.filename()
131 if "tidy.py" not in filename:
133 report_err("TODO is deprecated; use FIXME")
134 match = re.match(r'^.*/(\*|/!?)\s*XXX', line)
136 report_err("XXX is no longer necessary, use FIXME")
137 match = re.match(r'^.*//\s*(NOTE.*)$', line)
138 if match and "TRAVIS" not in os.environ:
140 if "snap" in m.lower():
141 report_warn(match.group(1))
142 match = re.match(r'^.*//\s*SNAP\s+(\w+)', line)
145 date, rev = snapshot.curr_snapshot_rev()
146 if not hsh.startswith(rev):
147 report_err("snapshot out of date (" + date
151 report_warn("unmatched SNAP line: " + line)
157 if linelength_flag in line:
158 check_linelength = False
160 if check_tab and ('\t' in line and
161 "Makefile" not in filename):
162 report_err("tab character")
163 if check_cr and not autocrlf and '\r' in line:
164 report_err("CR character")
165 if line.endswith(" \n") or line.endswith("\t\n"):
166 report_err("trailing whitespace")
167 line_len = len(line)-2 if autocrlf else len(line)-1
169 if check_linelength and line_len > cols:
170 report_err("line longer than %d chars" % cols)
172 if fileinput.isfirstline():
173 # This happens at the end of each file except the last.
174 if current_name != "":
175 update_counts(current_name)
176 assert len(current_contents) > 0
177 do_license_check(current_name, current_contents)
179 current_name = filename
180 current_contents = ""
183 check_linelength = True
185 # Put a reasonable limit on the amount of header data we use for
187 if len(current_contents) < 1000:
188 current_contents += line
192 count_non_blank_lines += 1
194 if current_name != "":
195 update_counts(current_name)
196 assert len(current_contents) > 0
197 do_license_check(current_name, current_contents)
199 except UnicodeDecodeError as e:
200 report_err("UTF-8 decoding error " + str(e))
203 for ext in sorted(file_counts, key=file_counts.get, reverse=True):
204 print("* linted {} {} files".format(file_counts[ext], ext))
205 print("* linted {} other files".format(count_other_linted_files))
206 print("* total lines of code: {}".format(count_lines))
207 print("* total non-blank lines of code: {}".format(count_non_blank_lines))