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.
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.
11 import sys, fileinput, subprocess, re
12 from licenseck import *
17 cr_flag="ignore-tidy-cr"
18 tab_flag="ignore-tidy-tab"
19 linelength_flag="ignore-tidy-linelength"
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]
26 true="true".encode('utf8')
27 autocrlf=result.strip() == true if result is not None else False
29 def report_error_name_no(name, no, s):
31 print("%s:%d: %s" % (name, no, s))
35 report_error_name_no(fileinput.filename(), fileinput.filelineno(), s)
38 print("%s:%d: %s" % (fileinput.filename(),
39 fileinput.filelineno(),
42 def do_license_check(name, contents):
43 if not check_license(name, contents):
44 report_error_name_no(name, 1, "incorrect license")
47 file_names = [s for s in sys.argv[1:] if (not s.endswith("_gen.rs"))
54 check_linelength = True
58 for line in fileinput.input(file_names,
59 openhook=fileinput.hook_encoded("utf-8")):
61 if fileinput.filename().find("tidy.py") == -1:
62 if line.find(cr_flag) != -1:
64 if line.find(tab_flag) != -1:
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)
75 if "snap" in m.lower():
76 report_warn(match.group(1))
77 match = re.match(r'^.*//\s*SNAP\s+(\w+)', line)
80 date, rev = snapshot.curr_snapshot_rev()
81 if not hsh.startswith(rev):
82 report_err("snapshot out of date (" + date
86 report_warn("unmatched SNAP line: " + line)
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
97 if check_linelength and line_len > cols:
98 report_err("line longer than %d chars" % cols)
100 if fileinput.isfirstline() and current_name != "":
101 do_license_check(current_name, current_contents)
103 if fileinput.isfirstline():
104 current_name = fileinput.filename()
105 current_contents = ""
108 check_linelength = True
110 current_contents += line
112 if current_name != "":
113 do_license_check(current_name, current_contents)
115 except UnicodeDecodeError, e:
116 report_err("UTF-8 decoding error " + str(e))