]> git.lizzy.rs Git - rust.git/blob - src/etc/tidy.py
cleanup: s/impl Copy/#[derive(Copy)]/g
[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, os
12 from licenseck import *
13 import snapshot
14
15 err=0
16 cols=100
17 cr_flag="ignore-tidy-cr"
18 tab_flag="ignore-tidy-tab"
19 linelength_flag="ignore-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 "tidy.py" not in fileinput.filename():
62             if cr_flag in line:
63                 check_cr = False
64             if tab_flag in line:
65                 check_tab = False
66             if linelength_flag in line:
67                 check_linelength = False
68             if "TODO" in line:
69                 report_err("TODO is deprecated; use FIXME")
70             match = re.match(r'^.*/(\*|/!?)\s*XXX', line)
71             if match:
72                 report_err("XXX is no longer necessary, use FIXME")
73             match = re.match(r'^.*//\s*(NOTE.*)$', line)
74             if match and "TRAVIS" not in os.environ:
75                 m = match.group(1)
76                 if "snap" in m.lower():
77                     report_warn(match.group(1))
78             match = re.match(r'^.*//\s*SNAP\s+(\w+)', line)
79             if match:
80                 hsh = match.group(1)
81                 date, rev = snapshot.curr_snapshot_rev()
82                 if not hsh.startswith(rev):
83                     report_err("snapshot out of date (" + date
84                       + "): " + line)
85             else:
86                 if "SNAP" in line:
87                     report_warn("unmatched SNAP line: " + line)
88
89         if check_tab and ('\t' in line and
90             "Makefile" not in fileinput.filename()):
91             report_err("tab character")
92         if check_cr and not autocrlf and '\r' in line:
93             report_err("CR character")
94         if line.endswith(" \n") or line.endswith("\t\n"):
95             report_err("trailing whitespace")
96         line_len = len(line)-2 if autocrlf else len(line)-1
97
98         if check_linelength and line_len > cols:
99             report_err("line longer than %d chars" % cols)
100
101         if fileinput.isfirstline() and current_name != "":
102             do_license_check(current_name, current_contents)
103
104         if fileinput.isfirstline():
105             current_name = fileinput.filename()
106             current_contents = ""
107             check_cr = True
108             check_tab = True
109             check_linelength = True
110
111         current_contents += line
112
113     if current_name != "":
114         do_license_check(current_name, current_contents)
115
116 except UnicodeDecodeError as e:
117     report_err("UTF-8 decoding error " + str(e))
118
119
120 sys.exit(err)