2013-02-19 04:40:57 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
import optparse
|
|
|
|
import subprocess
|
2012-10-04 20:30:01 +02:00
|
|
|
|
2013-02-19 04:40:57 +01:00
|
|
|
from os import path
|
|
|
|
from collections import defaultdict
|
2012-10-04 20:30:01 +02:00
|
|
|
|
2013-02-19 04:40:57 +01:00
|
|
|
from humbug_tools import check_output
|
2012-10-15 22:12:16 +02:00
|
|
|
|
2013-02-19 04:40:57 +01:00
|
|
|
parser = optparse.OptionParser()
|
|
|
|
parser.add_option('--full',
|
|
|
|
action='store_true',
|
|
|
|
help='Check some things we typically ignore')
|
|
|
|
(options, args) = parser.parse_args()
|
2012-10-25 21:55:52 +02:00
|
|
|
|
2013-02-19 04:40:57 +01:00
|
|
|
os.chdir(path.join(path.dirname(__file__), '..'))
|
2012-10-04 20:30:01 +02:00
|
|
|
|
2012-10-04 21:27:52 +02:00
|
|
|
|
2013-02-19 04:40:57 +01:00
|
|
|
# Exclude some directories and files from lint checking
|
2012-10-20 03:00:05 +02:00
|
|
|
|
2013-02-19 04:40:57 +01:00
|
|
|
exclude_trees = """
|
|
|
|
zephyr/static/third
|
|
|
|
confirmation
|
|
|
|
zephyr/tests/frontend/casperjs
|
|
|
|
zephyr/migrations
|
2013-03-26 16:20:06 +01:00
|
|
|
node_modules
|
2013-02-19 04:40:57 +01:00
|
|
|
""".split()
|
2012-10-04 20:30:01 +02:00
|
|
|
|
2013-02-19 04:40:57 +01:00
|
|
|
exclude_files = """
|
|
|
|
humbug/test_settings.py
|
|
|
|
tools/jslint/jslint.js
|
|
|
|
""".split()
|
2012-10-25 21:55:52 +02:00
|
|
|
|
2013-02-19 04:40:57 +01:00
|
|
|
|
|
|
|
# Categorize by language all files known to Git
|
|
|
|
|
|
|
|
git_files = map(str.strip, check_output(['git', 'ls-files']).split('\n'))
|
|
|
|
by_lang = defaultdict(list)
|
|
|
|
|
|
|
|
for filepath in git_files:
|
|
|
|
if (not filepath or not path.isfile(filepath)
|
|
|
|
or (filepath in exclude_files)
|
|
|
|
or any(filepath.startswith(d+'/') for d in exclude_trees)):
|
|
|
|
continue
|
|
|
|
|
|
|
|
_, exn = path.splitext(filepath)
|
|
|
|
if not exn:
|
|
|
|
# No extension; look at the first line
|
|
|
|
with file(filepath) as f:
|
|
|
|
if re.match(r'^#!.*\bpython', f.readline()):
|
|
|
|
exn = '.py'
|
|
|
|
|
|
|
|
by_lang[exn].append(filepath)
|
|
|
|
|
2013-06-21 23:58:02 +02:00
|
|
|
def check_trailing_whitespace(fn):
|
|
|
|
failed = False
|
|
|
|
for i, line in enumerate(open(fn)):
|
|
|
|
if re.search('\s+$', line.strip('\n')):
|
|
|
|
sys.stdout.write('Fix whitespace at %s line %s\n' % (fn, i+1))
|
|
|
|
failed = True
|
|
|
|
return failed
|
2013-02-19 04:40:57 +01:00
|
|
|
|
2013-05-31 20:51:37 +02:00
|
|
|
# Invoke the appropriate lint checker for each language,
|
|
|
|
# and also check files for extra whitespace.
|
2013-02-19 04:40:57 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
failed = False
|
|
|
|
|
|
|
|
# Make the lint output bright red
|
|
|
|
sys.stdout.write('\x1B[1;31m')
|
|
|
|
sys.stdout.flush()
|
|
|
|
|
|
|
|
try:
|
|
|
|
subprocess.check_call(['tools/node', 'tools/jslint/check-all.js']
|
|
|
|
+ by_lang['.js'])
|
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
failed = True
|
|
|
|
|
2013-05-31 20:51:37 +02:00
|
|
|
for fn in by_lang['.js'] + by_lang['.py']:
|
2013-06-21 23:58:02 +02:00
|
|
|
if check_trailing_whitespace(fn):
|
2013-05-31 20:51:37 +02:00
|
|
|
failed = True
|
|
|
|
|
2013-02-19 04:40:57 +01:00
|
|
|
pyflakes = subprocess.Popen(['pyflakes'] + by_lang['.py'],
|
2013-03-25 23:44:38 +01:00
|
|
|
stdout = subprocess.PIPE,
|
|
|
|
stderr = subprocess.PIPE)
|
|
|
|
|
|
|
|
# pyflakes writes some output (like syntax errors) to stderr. :/
|
|
|
|
for pipe in (pyflakes.stdout, pyflakes.stderr):
|
|
|
|
for ln in pipe:
|
2013-05-06 23:24:52 +02:00
|
|
|
if options.full or not \
|
|
|
|
('imported but unused' in ln or
|
|
|
|
("zephyr_mirror_backend.py:" in ln and
|
|
|
|
"redefinition of unused 'simplejson' from line" in ln)):
|
2013-03-25 23:44:38 +01:00
|
|
|
sys.stdout.write(ln)
|
|
|
|
failed = True
|
2013-02-19 04:40:57 +01:00
|
|
|
|
|
|
|
sys.exit(1 if failed else 0)
|
|
|
|
|
|
|
|
finally:
|
|
|
|
# Restore normal terminal colors
|
|
|
|
sys.stdout.write('\x1B[0m')
|