]> git.proxmox.com Git - rustc.git/blob - src/etc/tidy.py
fd3309dce12cbd01e1c573e05beeddb096a7c2f1
[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 *
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 # 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]
29
30 true = "true".encode('utf8')
31 autocrlf = result.strip() == true if result is not None else False
32
33
34 def report_error_name_no(name, no, s):
35 global err
36 print("%s:%d: %s" % (name, no, s))
37 err = 1
38
39
40 def report_err(s):
41 report_error_name_no(fileinput.filename(), fileinput.filelineno(), s)
42
43
44 def report_warn(s):
45 print("%s:%d: %s" % (fileinput.filename(),
46 fileinput.filelineno(),
47 s))
48
49
50 def do_license_check(name, contents):
51 if not check_license(name, contents):
52 report_error_name_no(name, 1, "incorrect license")
53
54 current_name = ""
55 current_contents = ""
56 check_tab = True
57 check_cr = True
58 check_linelength = True
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]
65
66 try:
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,
123 openhook=fileinput.hook_encoded("utf-8")):
124
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
149 if cr_flag in line:
150 check_cr = False
151 if tab_flag in line:
152 check_tab = False
153 if linelength_flag in line:
154 check_linelength = False
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
189
190 if current_name != "":
191 update_counts(current_name)
192 assert len(current_contents) > 0
193 do_license_check(current_name, current_contents)
194
195 except UnicodeDecodeError as e:
196 report_err("UTF-8 decoding error " + str(e))
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
204
205 sys.exit(err)