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