rust/src/etc/tidy.py

121 lines
4.0 KiB
Python
Raw Normal View History

2014-02-02 04:47:02 -06:00
# Copyright 2010-2014 The Rust Project Developers. See the COPYRIGHT
# file at the top-level directory of this distribution and at
# http://rust-lang.org/COPYRIGHT.
#
# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
# http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
# <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
# option. This file may not be copied, modified, or distributed
# except according to those terms.
2010-06-23 23:03:09 -05:00
import sys, fileinput, subprocess, re
2013-01-18 01:28:42 -06:00
from licenseck import *
import snapshot
2010-06-23 23:03:09 -05:00
err=0
2013-03-12 12:55:39 -05:00
cols=100
cr_flag="ignore-tidy-cr"
tab_flag="ignore-tidy-tab"
linelength_flag="ignore-tidy-linelength"
2010-06-23 23:03:09 -05:00
# Be careful to support Python 2.4, 2.6, and 3.x here!
config_proc=subprocess.Popen([ "git", "config", "core.autocrlf" ],
2011-03-19 20:32:46 -05:00
stdout=subprocess.PIPE)
result=config_proc.communicate()[0]
true="true".encode('utf8')
autocrlf=result.strip() == true if result is not None else False
2013-01-18 01:28:42 -06:00
def report_error_name_no(name, no, s):
2010-06-23 23:03:09 -05:00
global err
2013-01-18 01:28:42 -06:00
print("%s:%d: %s" % (name, no, s))
2010-06-23 23:03:09 -05:00
err=1
2013-01-18 01:28:42 -06:00
def report_err(s):
report_error_name_no(fileinput.filename(), fileinput.filelineno(), s)
def report_warn(s):
print("%s:%d: %s" % (fileinput.filename(),
fileinput.filelineno(),
s))
2013-01-18 01:28:42 -06:00
def do_license_check(name, contents):
if not check_license(name, contents):
report_error_name_no(name, 1, "incorrect license")
2012-07-29 17:59:08 -05:00
file_names = [s for s in sys.argv[1:] if (not s.endswith("_gen.rs"))
and (not ".#" in s)]
2013-01-18 01:28:42 -06:00
current_name = ""
current_contents = ""
2014-02-05 16:33:10 -06:00
check_tab = True
check_cr = True
check_linelength = True
2013-01-18 01:28:42 -06:00
2012-01-06 21:56:33 -06:00
try:
for line in fileinput.input(file_names,
openhook=fileinput.hook_encoded("utf-8")):
if fileinput.filename().find("tidy.py") == -1:
2014-02-05 16:33:10 -06:00
if line.find(cr_flag) != -1:
check_cr = False
if line.find(tab_flag) != -1:
check_tab = False
if line.find(linelength_flag) != -1:
check_linelength = False
if line.find("TODO") != -1:
report_err("TODO is deprecated; use FIXME")
match = re.match(r'^.*/(\*|/!?)\s*XXX', line)
if match:
report_err("XXX is no longer necessary, use FIXME")
match = re.match(r'^.*//\s*(NOTE.*)$', line)
if match:
2014-01-05 07:50:52 -06:00
m = match.group(1)
if "snap" in m.lower():
report_warn(match.group(1))
match = re.match(r'^.*//\s*SNAP\s+(\w+)', line)
if match:
hsh = match.group(1)
date, rev = snapshot.curr_snapshot_rev()
if not hsh.startswith(rev):
2014-02-02 04:47:02 -06:00
report_err("snapshot out of date (" + date
+ "): " + line)
else:
if "SNAP" in line:
report_warn("unmatched SNAP line: " + line)
2014-02-05 16:33:10 -06:00
if check_tab and (line.find('\t') != -1 and
2012-01-06 21:56:33 -06:00
fileinput.filename().find("Makefile") == -1):
report_err("tab character")
2014-02-05 16:33:10 -06:00
if check_cr and not autocrlf and line.find('\r') != -1:
2012-01-06 21:56:33 -06:00
report_err("CR character")
if line.endswith(" \n") or line.endswith("\t\n"):
report_err("trailing whitespace")
line_len = len(line)-2 if autocrlf else len(line)-1
2014-02-05 16:33:10 -06:00
if check_linelength and line_len > cols:
2012-01-06 21:56:33 -06:00
report_err("line longer than %d chars" % cols)
2013-01-18 01:28:42 -06:00
if fileinput.isfirstline() and current_name != "":
do_license_check(current_name, current_contents)
if fileinput.isfirstline():
current_name = fileinput.filename()
current_contents = ""
2014-02-05 16:33:10 -06:00
check_cr = True
check_tab = True
check_linelength = True
2013-01-18 01:28:42 -06:00
current_contents += line
if current_name != "":
do_license_check(current_name, current_contents)
2012-01-06 21:56:33 -06:00
except UnicodeDecodeError, e:
report_err("UTF-8 decoding error " + str(e))
2010-06-23 23:03:09 -05:00
sys.exit(err)