2015-10-04 18:56:26 +02:00
|
|
|
#!/usr/bin/env python2.7
|
2013-02-19 04:40:57 +01:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
import optparse
|
|
|
|
import subprocess
|
2015-12-05 22:47:50 +01:00
|
|
|
import traceback
|
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
|
|
|
parser = optparse.OptionParser()
|
|
|
|
parser.add_option('--full',
|
|
|
|
action='store_true',
|
|
|
|
help='Check some things we typically ignore')
|
2013-11-27 20:11:03 +01:00
|
|
|
parser.add_option('--modified', '-m',
|
|
|
|
action='store_true',
|
|
|
|
help='Only check modified files')
|
2013-02-19 04:40:57 +01:00
|
|
|
(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 = """
|
2013-07-25 22:41:44 +02:00
|
|
|
static/third
|
2013-02-19 04:40:57 +01:00
|
|
|
confirmation
|
2015-10-13 23:34:50 +02:00
|
|
|
frontend_tests/casperjs
|
2013-07-29 23:03:31 +02:00
|
|
|
zerver/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 = """
|
2015-08-18 23:22:51 +02:00
|
|
|
docs/html_unescape.py
|
2013-08-06 22:51:47 +02:00
|
|
|
zproject/test_settings.py
|
2013-09-24 20:28:28 +02:00
|
|
|
zproject/settings.py
|
2013-02-19 04:40:57 +01:00
|
|
|
tools/jslint/jslint.js
|
2013-10-31 22:09:33 +01:00
|
|
|
api/setup.py
|
2013-12-05 22:54:43 +01:00
|
|
|
api/integrations/perforce/git_p4.py
|
2013-02-19 04:40:57 +01:00
|
|
|
""".split()
|
2012-10-25 21:55:52 +02:00
|
|
|
|
2013-11-27 20:11:03 +01:00
|
|
|
if options.modified:
|
|
|
|
# If the user specifies, use `git ls-files -m` to only check modified, non-staged
|
|
|
|
# files in the current checkout. This makes things fun faster.
|
|
|
|
files = map(str.strip, subprocess.check_output(['git', 'ls-files', '-m']).split('\n'))
|
2013-11-27 19:30:44 +01:00
|
|
|
else:
|
2013-11-27 20:11:03 +01:00
|
|
|
files = []
|
|
|
|
|
|
|
|
files += args
|
|
|
|
|
|
|
|
if not files and not options.modified:
|
2013-11-27 19:30:44 +01:00
|
|
|
# If no files are specified on the command line, use the entire git checkout
|
|
|
|
files = map(str.strip, subprocess.check_output(['git', 'ls-files']).split('\n'))
|
2013-02-19 04:40:57 +01:00
|
|
|
|
2013-11-27 20:11:03 +01:00
|
|
|
files = filter(bool, files) # remove empty file caused by trailing \n
|
|
|
|
|
|
|
|
if not files:
|
|
|
|
raise Exception('There are no files to check!')
|
|
|
|
|
2013-11-27 19:30:44 +01:00
|
|
|
# Categorize by language all files we want to check
|
2013-02-19 04:40:57 +01:00
|
|
|
by_lang = defaultdict(list)
|
|
|
|
|
2013-11-27 19:30:44 +01:00
|
|
|
for filepath in files:
|
2013-02-19 04:40:57 +01:00
|
|
|
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
|
2015-10-14 22:31:08 +02:00
|
|
|
with open(filepath) as f:
|
2013-02-19 04:40:57 +01:00
|
|
|
if re.match(r'^#!.*\bpython', f.readline()):
|
|
|
|
exn = '.py'
|
|
|
|
|
|
|
|
by_lang[exn].append(filepath)
|
|
|
|
|
2016-01-17 17:24:31 +01:00
|
|
|
by_lang['.sh'] = filter(None, map(str.strip, subprocess.check_output("grep --files-with-matches '#!.*\(ba\)\?sh' $(find scripts/ tools/ bin/ -type f | grep -v [.])", shell=True).split('\n')))
|
|
|
|
|
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
|
|
|
|
2013-07-18 23:38:15 +02:00
|
|
|
import logging
|
|
|
|
logging.basicConfig(format="%(asctime)s %(message)s")
|
|
|
|
logger = logging.getLogger()
|
|
|
|
# Change this to logging.INFO to see performance data
|
|
|
|
logger.setLevel(logging.WARNING)
|
|
|
|
|
|
|
|
def check_pyflakes():
|
2013-11-27 19:30:44 +01:00
|
|
|
if not by_lang['.py']:
|
|
|
|
return False
|
2013-02-19 04:40:57 +01:00
|
|
|
failed = False
|
2013-07-18 23:38:15 +02:00
|
|
|
pyflakes = subprocess.Popen(['pyflakes'] + by_lang['.py'],
|
|
|
|
stdout = subprocess.PIPE,
|
|
|
|
stderr = subprocess.PIPE)
|
2013-02-19 04:40:57 +01:00
|
|
|
|
2013-07-18 23:38:15 +02:00
|
|
|
# pyflakes writes some output (like syntax errors) to stderr. :/
|
|
|
|
for pipe in (pyflakes.stdout, pyflakes.stderr):
|
|
|
|
for ln in pipe:
|
|
|
|
if options.full or not \
|
|
|
|
('imported but unused' in ln or
|
2013-11-13 03:34:28 +01:00
|
|
|
'redefinition of unused' in ln or
|
2013-08-09 19:15:44 +02:00
|
|
|
("zerver/models.py" in ln and
|
|
|
|
" undefined name 'bugdown'" in ln) or
|
2014-01-14 17:39:18 +01:00
|
|
|
("zerver/lib/tornado_ioloop_logging.py" in ln and
|
|
|
|
"redefinition of function 'instrument_tornado_ioloop'" in ln) or
|
2013-07-18 23:38:15 +02:00
|
|
|
("zephyr_mirror_backend.py:" in ln and
|
|
|
|
"redefinition of unused 'simplejson' from line" in ln)):
|
|
|
|
sys.stdout.write(ln)
|
|
|
|
failed = True
|
|
|
|
return failed
|
2013-02-19 04:40:57 +01:00
|
|
|
|
2015-12-05 22:47:50 +01:00
|
|
|
def custom_check_file(fn, rules, skip_rules=[]):
|
|
|
|
failed = False
|
|
|
|
for i, line in enumerate(open(fn)):
|
|
|
|
skip = False
|
|
|
|
for rule in skip_rules:
|
|
|
|
if re.match(rule, line):
|
|
|
|
skip = True
|
|
|
|
if skip:
|
|
|
|
continue
|
|
|
|
for rule in rules:
|
2015-12-05 22:56:27 +01:00
|
|
|
exclude_list = rule.get('exclude', set())
|
|
|
|
if fn in exclude_list:
|
|
|
|
continue
|
2015-12-05 22:47:50 +01:00
|
|
|
try:
|
|
|
|
if re.search(rule['pattern'], line.strip(rule.get('strip', None))):
|
|
|
|
sys.stdout.write(rule['description'] + ' at %s line %s:\n' % (fn, i+1))
|
|
|
|
print line
|
|
|
|
failed = True
|
|
|
|
except Exception:
|
|
|
|
print "Exception with %s at %s line %s" % (rule['pattern'], fn, i+1)
|
|
|
|
traceback.print_exc()
|
|
|
|
return failed
|
|
|
|
|
|
|
|
whitespace_rules = [
|
|
|
|
{'pattern': '\s+$',
|
|
|
|
'strip': '\n',
|
|
|
|
'description': 'Fix trailing whitespace'},
|
|
|
|
{'pattern': '\t',
|
|
|
|
'strip': '\n',
|
2015-12-05 22:56:27 +01:00
|
|
|
'exclude': set(['zerver/lib/bugdown/codehilite.py']),
|
2015-12-05 22:47:50 +01:00
|
|
|
'description': 'Fix tab-based whitespace'},
|
|
|
|
]
|
|
|
|
js_rules = [
|
|
|
|
{'pattern': '[^_]function\(',
|
|
|
|
'description': 'The keyword "function" should be followed by a space'},
|
|
|
|
{'pattern': '.*blueslip.warning\(.*',
|
|
|
|
'description': 'The module blueslip has no function warning, try using blueslip.warn'},
|
2015-12-05 22:56:27 +01:00
|
|
|
{'pattern': '[)]{$',
|
|
|
|
'description': 'Missing space between ) and {'},
|
|
|
|
{'pattern': 'else{$',
|
|
|
|
'description': 'Missing space between else and {'},
|
|
|
|
] + whitespace_rules
|
2015-12-05 22:47:50 +01:00
|
|
|
python_rules = [
|
|
|
|
{'pattern': "'[^']*'\s+\([^']*$",
|
|
|
|
'description': "Suspicious code with quoting around function name"},
|
|
|
|
{'pattern': '"[^"]*"\s+\([^"]*$',
|
|
|
|
'description': "Suspicious code with quoting around function name"},
|
2015-12-05 22:59:56 +01:00
|
|
|
{'pattern': '".*"%\([a-z_].*\)?$',
|
|
|
|
'description': 'Missing space around "%"'},
|
|
|
|
{'pattern': "'.*'%\([a-z_].*\)?$",
|
2015-12-05 22:56:27 +01:00
|
|
|
'description': 'Missing space around "%"'},
|
|
|
|
# This next check could have false positives, but it seems pretty
|
|
|
|
# rare; if we find any, they can be added to the exclude list for
|
|
|
|
# this rule.
|
2015-12-05 22:47:50 +01:00
|
|
|
{'pattern': '% [a-z_]*\)?$',
|
|
|
|
'description': 'Used % comprehension without a tuple'},
|
2015-12-05 22:56:27 +01:00
|
|
|
] + whitespace_rules
|
2015-12-05 22:47:50 +01:00
|
|
|
python_line_skip_rules = [
|
|
|
|
'\s*[*#]', # comments
|
|
|
|
]
|
2016-01-17 17:24:31 +01:00
|
|
|
bash_rules = [
|
|
|
|
{'pattern': '#!.*sh [-xe]',
|
|
|
|
'description': 'Fix shebang line with proper call to /usr/bin/env for Bash path, change -x|-e switches to set -x|set -e'},
|
|
|
|
]
|
2015-12-05 22:47:50 +01:00
|
|
|
|
2013-07-18 23:38:15 +02:00
|
|
|
def check_custom_checks():
|
|
|
|
failed = False
|
2013-02-19 04:40:57 +01:00
|
|
|
|
2013-06-27 20:03:28 +02:00
|
|
|
for fn in by_lang['.py']:
|
2015-12-05 22:47:50 +01:00
|
|
|
if custom_check_file(fn, python_rules, skip_rules=python_line_skip_rules):
|
2013-06-27 20:03:28 +02:00
|
|
|
failed = True
|
|
|
|
|
2013-07-05 18:53:43 +02:00
|
|
|
for fn in by_lang['.js']:
|
2015-12-05 22:47:50 +01:00
|
|
|
if custom_check_file(fn, js_rules):
|
2013-07-05 18:53:43 +02:00
|
|
|
failed = True
|
|
|
|
|
2016-01-17 17:24:31 +01:00
|
|
|
for fn in by_lang['.sh']:
|
|
|
|
if custom_check_file(fn, bash_rules):
|
|
|
|
failed = True
|
|
|
|
|
2013-07-18 23:38:15 +02:00
|
|
|
return failed
|
2013-03-25 23:44:38 +01:00
|
|
|
|
2013-11-27 19:00:08 +01:00
|
|
|
lint_functions = {}
|
|
|
|
|
|
|
|
def run_parallel():
|
|
|
|
pids = []
|
|
|
|
for name, func in lint_functions.items():
|
|
|
|
pid = os.fork()
|
|
|
|
if pid == 0:
|
|
|
|
logging.info("start " + name)
|
|
|
|
result = func()
|
|
|
|
logging.info("finish " + name)
|
|
|
|
os._exit(result)
|
|
|
|
pids.append(pid)
|
2013-07-18 23:38:15 +02:00
|
|
|
failed = False
|
|
|
|
|
2013-11-27 19:00:08 +01:00
|
|
|
for pid in pids:
|
|
|
|
(_, status) = os.waitpid(pid, 0)
|
|
|
|
if status != 0:
|
|
|
|
failed = True
|
|
|
|
return failed
|
|
|
|
|
|
|
|
def lint(func):
|
|
|
|
lint_functions[func.__name__] = func
|
|
|
|
return func
|
|
|
|
|
|
|
|
try:
|
2013-07-18 23:38:15 +02:00
|
|
|
# Make the lint output bright red
|
|
|
|
sys.stdout.write('\x1B[1;31m')
|
|
|
|
sys.stdout.flush()
|
|
|
|
|
2013-11-27 19:00:08 +01:00
|
|
|
@lint
|
|
|
|
def templates():
|
2013-12-17 18:44:59 +01:00
|
|
|
result = subprocess.call(['tools/check-templates'])
|
2013-11-27 19:00:08 +01:00
|
|
|
return result
|
2013-10-08 14:31:16 +02:00
|
|
|
|
2013-11-27 19:00:08 +01:00
|
|
|
@lint
|
|
|
|
def jslint():
|
2013-07-19 17:41:00 +02:00
|
|
|
result = subprocess.call(['tools/node', 'tools/jslint/check-all.js']
|
|
|
|
+ by_lang['.js'])
|
2013-11-27 19:00:08 +01:00
|
|
|
return result
|
2013-07-18 23:38:15 +02:00
|
|
|
|
2013-11-27 19:00:08 +01:00
|
|
|
@lint
|
|
|
|
def puppet():
|
2013-11-27 19:30:44 +01:00
|
|
|
if not by_lang['.pp']:
|
|
|
|
return 0
|
2013-08-08 22:06:26 +02:00
|
|
|
result = subprocess.call(['puppet', 'parser', 'validate'] + by_lang['.pp'])
|
2013-11-27 19:00:08 +01:00
|
|
|
return result
|
2013-08-08 22:06:26 +02:00
|
|
|
|
2013-11-27 19:00:08 +01:00
|
|
|
@lint
|
|
|
|
def custom():
|
2013-07-18 23:38:15 +02:00
|
|
|
failed = check_custom_checks()
|
2013-11-27 19:00:08 +01:00
|
|
|
return 1 if failed else 0
|
2013-07-18 23:38:15 +02:00
|
|
|
|
2013-11-27 19:00:08 +01:00
|
|
|
@lint
|
|
|
|
def pyflakes():
|
2013-09-19 16:39:51 +02:00
|
|
|
failed = check_pyflakes()
|
2013-11-27 19:00:08 +01:00
|
|
|
return 1 if failed else 0
|
2013-09-19 16:39:51 +02:00
|
|
|
|
2013-11-27 19:00:08 +01:00
|
|
|
failed = run_parallel()
|
2013-09-19 16:43:24 +02:00
|
|
|
|
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')
|