]> git.proxmox.com Git - rustc.git/blob - src/etc/tidy.py
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / etc / tidy.py
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.
4 #
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.
10
11 import sys
12 import fileinput
13 import subprocess
14 import re
15 import os
16 from licenseck import check_license
17 import snapshot
18
19 err = 0
20 cols = 100
21 cr_flag = "ignore-tidy-cr"
22 tab_flag = "ignore-tidy-tab"
23 linelength_flag = "ignore-tidy-linelength"
24
25 interesting_files = ['.rs', '.py', '.js', '.sh', '.c', '.h']
26 uninteresting_files = ['miniz.c', 'jquery', 'rust_android_dummy']
27 stable_whitelist = {
28 'src/bootstrap',
29 'src/build_helper',
30 'src/libcollectionstest',
31 'src/libcore',
32 'src/libstd',
33 'src/rustc/std_shim',
34 'src/rustc/test_shim',
35 'src/test'
36 }
37
38
39 def report_error_name_no(name, no, s):
40 global err
41 print("%s:%d: %s" % (name, no, s))
42 err = 1
43
44
45 def report_err(s):
46 report_error_name_no(fileinput.filename(), fileinput.filelineno(), s)
47
48
49 def report_warn(s):
50 print("%s:%d: %s" % (fileinput.filename(),
51 fileinput.filelineno(),
52 s))
53
54
55 def do_license_check(name, contents):
56 if not check_license(name, contents):
57 report_error_name_no(name, 1, "incorrect license")
58
59
60 def update_counts(current_name):
61 global file_counts
62 global count_other_linted_files
63
64 _, ext = os.path.splitext(current_name)
65
66 if ext in interesting_files:
67 file_counts[ext] += 1
68 else:
69 count_other_linted_files += 1
70
71
72 def interesting_file(f):
73 if any(x in f for x in uninteresting_files):
74 return False
75
76 return any(os.path.splitext(f)[1] == ext for ext in interesting_files)
77
78
79 # Be careful to support Python 2.4, 2.6, and 3.x here!
80 config_proc = subprocess.Popen(["git", "config", "core.autocrlf"],
81 stdout=subprocess.PIPE)
82 result = config_proc.communicate()[0]
83
84 true = "true".encode('utf8')
85 autocrlf = result.strip() == true if result is not None else False
86
87 current_name = ""
88 current_contents = ""
89 check_tab = True
90 check_cr = True
91 check_linelength = True
92
93 if len(sys.argv) < 2:
94 print("usage: tidy.py <src-dir>")
95 sys.exit(1)
96
97 src_dir = sys.argv[1]
98
99 count_lines = 0
100 count_non_blank_lines = 0
101 count_other_linted_files = 0
102
103 file_counts = {ext: 0 for ext in interesting_files}
104
105 all_paths = set()
106 needs_unstable_attr = set()
107
108 try:
109 for (dirpath, dirnames, filenames) in os.walk(src_dir):
110 # Skip some third-party directories
111 skippable_dirs = {
112 'src/jemalloc',
113 'src/llvm',
114 'src/gyp',
115 'src/libbacktrace',
116 'src/libuv',
117 'src/compiler-rt',
118 'src/rt/hoedown',
119 'src/rustllvm',
120 'src/rt/valgrind',
121 'src/rt/msvc',
122 'src/rust-installer',
123 'src/liblibc',
124 }
125
126 dirpath = os.path.normpath(dirpath)
127 if any(os.path.normpath(d) in dirpath for d in skippable_dirs):
128 continue
129
130 file_names = [os.path.join(dirpath, f) for f in filenames
131 if interesting_file(f)
132 and not f.endswith("_gen.rs")
133 and not ".#" is f]
134
135 if not file_names:
136 continue
137
138 for line in fileinput.input(file_names,
139 openhook=fileinput.hook_encoded("utf-8")):
140
141 filename = fileinput.filename()
142
143 if "tidy.py" not in filename:
144 if "TODO" in line:
145 report_err("TODO is deprecated; use FIXME")
146 match = re.match(r'^.*/(\*|/!?)\s*XXX', line)
147 if match:
148 report_err("XXX is no longer necessary, use FIXME")
149 match = re.match(r'^.*//\s*(NOTE.*)$', line)
150 if match and "TRAVIS" not in os.environ:
151 m = match.group(1)
152 if "snap" in m.lower():
153 report_warn(match.group(1))
154 match = re.match(r'^.*//\s*SNAP\s+(\w+)', line)
155 if match:
156 hsh = match.group(1)
157 date, rev = snapshot.curr_snapshot_rev()
158 if not hsh.startswith(rev):
159 report_err("snapshot out of date (" + date
160 + "): " + line)
161 else:
162 if "SNAP " in line:
163 report_warn("unmatched SNAP line: " + line)
164 search = re.search(r'^#!\[unstable', line)
165 if search:
166 needs_unstable_attr.discard(filename)
167
168 if cr_flag in line:
169 check_cr = False
170 if tab_flag in line:
171 check_tab = False
172 if linelength_flag in line:
173 check_linelength = False
174
175 if check_tab and ('\t' in line and
176 "Makefile" not in filename):
177 report_err("tab character")
178 if check_cr and not autocrlf and '\r' in line:
179 report_err("CR character")
180 if line.endswith(" \n") or line.endswith("\t\n"):
181 report_err("trailing whitespace")
182 line_len = len(line)-2 if autocrlf else len(line)-1
183
184 if check_linelength and line_len > cols:
185 report_err("line longer than %d chars" % cols)
186
187 if fileinput.isfirstline():
188 # This happens at the end of each file except the last.
189 if current_name != "":
190 update_counts(current_name)
191 assert len(current_contents) > 0
192 do_license_check(current_name, current_contents)
193
194 current_name = filename
195 current_contents = ""
196 check_cr = True
197 check_tab = True
198 check_linelength = True
199 if all(f not in filename for f in stable_whitelist) and \
200 re.search(r'src/.*/lib\.rs', filename):
201 needs_unstable_attr.add(filename)
202
203 # Put a reasonable limit on the amount of header data we use for
204 # the licenseck
205 if len(current_contents) < 1000:
206 current_contents += line
207
208 count_lines += 1
209 if line.strip():
210 count_non_blank_lines += 1
211
212 if current_name != "":
213 update_counts(current_name)
214 assert len(current_contents) > 0
215 do_license_check(current_name, current_contents)
216 for f in needs_unstable_attr:
217 report_error_name_no(f, 1, "requires unstable attribute")
218
219 except UnicodeDecodeError as e:
220 report_err("UTF-8 decoding error " + str(e))
221
222 print
223 for ext in sorted(file_counts, key=file_counts.get, reverse=True):
224 print("* linted {} {} files".format(file_counts[ext], ext))
225 print("* linted {} other files".format(count_other_linted_files))
226 print("* total lines of code: {}".format(count_lines))
227 print("* total non-blank lines of code: {}".format(count_non_blank_lines))
228 print()
229
230 sys.exit(err)