rust/src/etc/tidy.py

96 lines
3.0 KiB
Python
Raw Normal View History

#!/usr/bin/env python
2013-01-18 01:28:42 -06:00
# xfail-license
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
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 = ""
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:
if line.find("// XXX") != -1:
report_err("XXX is no longer necessary, use FIXME")
if line.find("TODO") != -1:
report_err("TODO is deprecated; 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)
a, b, c, phash = snapshot.determine_curr_snapshot_info()
if not phash.startswith(hsh):
report_err("Snapshot out of date: " + line)
else:
if "SNAP" in line:
report_warn("Unmatched SNAP line: " + line)
2012-01-06 21:56:33 -06:00
if (line.find('\t') != -1 and
fileinput.filename().find("Makefile") == -1):
report_err("tab character")
if not autocrlf and line.find('\r') != -1:
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
if 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 = ""
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)