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, os
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 "tidy.py" not in fileinput.filename():
66 if linelength_flag in line:
67 check_linelength = False
69 report_err("TODO is deprecated; use FIXME")
70 match = re.match(r'^.*/(\*|/!?)\s*XXX', line)
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:
76 if "snap" in m.lower():
77 report_warn(match.group(1))
78 match = re.match(r'^.*//\s*SNAP\s+(\w+)', line)
81 date, rev = snapshot.curr_snapshot_rev()
82 if not hsh.startswith(rev):
83 report_err("snapshot out of date (" + date
87 report_warn("unmatched SNAP line: " + line)
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
98 if check_linelength and line_len > cols:
99 report_err("line longer than %d chars" % cols)
101 if fileinput.isfirstline() and current_name != "":
102 do_license_check(current_name, current_contents)
104 if fileinput.isfirstline():
105 current_name = fileinput.filename()
106 current_contents = ""
109 check_linelength = True
111 current_contents += line
113 if current_name != "":
114 do_license_check(current_name, current_contents)
116 except UnicodeDecodeError as e:
117 report_err("UTF-8 decoding error " + str(e))