]> git.lizzy.rs Git - rust.git/blob - src/etc/tidy.py
auto merge of #12055 : dguenther/rust/tidy_test, r=alexcrichton
[rust.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, fileinput, subprocess, re
12 from licenseck import *
13 import snapshot
14
15 err=0
16 cols=100
17 cr_flag="xfail-tidy-cr"
18 tab_flag="xfail-tidy-tab"
19 linelength_flag="xfail-tidy-linelength"
20
21 # Be careful to support Python 2.4, 2.6, and 3.x here!
22 config_proc=subprocess.Popen([ "git", "config", "core.autocrlf" ],
23                              stdout=subprocess.PIPE)
24 result=config_proc.communicate()[0]
25
26 true="true".encode('utf8')
27 autocrlf=result.strip() == true if result is not None else False
28
29 def report_error_name_no(name, no, s):
30     global err
31     print("%s:%d: %s" % (name, no, s))
32     err=1
33
34 def report_err(s):
35     report_error_name_no(fileinput.filename(), fileinput.filelineno(), s)
36
37 def report_warn(s):
38     print("%s:%d: %s" % (fileinput.filename(),
39                          fileinput.filelineno(),
40                          s))
41
42 def do_license_check(name, contents):
43     if not check_license(name, contents):
44         report_error_name_no(name, 1, "incorrect license")
45
46
47 file_names = [s for s in sys.argv[1:] if (not s.endswith("_gen.rs"))
48                                      and (not ".#" in s)]
49
50 current_name = ""
51 current_contents = ""
52 check_tab = True
53 check_cr = True
54 check_linelength = True
55
56
57 try:
58     for line in fileinput.input(file_names,
59                                 openhook=fileinput.hook_encoded("utf-8")):
60
61         if fileinput.filename().find("tidy.py") == -1:
62             if line.find(cr_flag) != -1:
63                 check_cr = False
64             if line.find(tab_flag) != -1:
65                 check_tab = False
66             if line.find(linelength_flag) != -1:
67                 check_linelength = False
68             if line.find("// XXX") != -1:
69                 report_err("XXX is no longer necessary, use FIXME")
70             if line.find("TODO") != -1:
71                 report_err("TODO is deprecated; use FIXME")
72             match = re.match(r'^.*//\s*(NOTE.*)$', line)
73             if match:
74                 m = match.group(1)
75                 if "snap" in m.lower():
76                     report_warn(match.group(1))
77             match = re.match(r'^.*//\s*SNAP\s+(\w+)', line)
78             if match:
79                 hsh = match.group(1)
80                 date, rev = snapshot.curr_snapshot_rev()
81                 if not hsh.startswith(rev):
82                     report_err("snapshot out of date (" + date
83                       + "): " + line)
84             else:
85                 if "SNAP" in line:
86                     report_warn("unmatched SNAP line: " + line)
87
88         if check_tab and (line.find('\t') != -1 and
89             fileinput.filename().find("Makefile") == -1):
90             report_err("tab character")
91         if check_cr and not autocrlf and line.find('\r') != -1:
92             report_err("CR character")
93         if line.endswith(" \n") or line.endswith("\t\n"):
94             report_err("trailing whitespace")
95         line_len = len(line)-2 if autocrlf else len(line)-1
96
97         if check_linelength and line_len > cols:
98             report_err("line longer than %d chars" % cols)
99
100         if fileinput.isfirstline() and current_name != "":
101             do_license_check(current_name, current_contents)
102
103         if fileinput.isfirstline():
104             current_name = fileinput.filename()
105             current_contents = ""
106             check_cr = True
107             check_tab = True
108             check_linelength = True
109
110         current_contents += line
111
112     if current_name != "":
113         do_license_check(current_name, current_contents)
114
115 except UnicodeDecodeError, e:
116     report_err("UTF-8 decoding error " + str(e))
117
118
119 sys.exit(err)