2014-01-29 04:07:27 +01:00
|
|
|
""" Run different codecheckers over a codebase.
|
|
|
|
|
2014-02-07 19:44:56 +01:00
|
|
|
Runs flake8, pylint, pep257 and a CRLF/whitespace/conflict-checker by default.
|
2014-01-29 04:07:27 +01:00
|
|
|
"""
|
|
|
|
|
2014-02-06 14:01:23 +01:00
|
|
|
# Copyright 2014 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
|
|
|
|
#
|
|
|
|
# This file is part of qutebrowser.
|
|
|
|
#
|
|
|
|
# qutebrowser is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# qutebrowser is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2014-01-28 15:25:40 +01:00
|
|
|
import sys
|
2014-01-28 15:49:52 +01:00
|
|
|
import subprocess
|
2014-01-28 22:44:39 +01:00
|
|
|
import os
|
|
|
|
import os.path
|
2014-01-28 16:13:23 +01:00
|
|
|
from collections import OrderedDict
|
2014-01-28 15:25:40 +01:00
|
|
|
|
2014-02-10 11:07:21 +01:00
|
|
|
try:
|
|
|
|
import pep257
|
|
|
|
except ImportError:
|
|
|
|
do_check_257 = False
|
|
|
|
else:
|
|
|
|
do_check_257 = True
|
2014-01-29 04:07:27 +01:00
|
|
|
from pkg_resources import load_entry_point, DistributionNotFound
|
|
|
|
|
2014-01-28 16:13:23 +01:00
|
|
|
status = OrderedDict()
|
2014-02-07 10:58:53 +01:00
|
|
|
|
|
|
|
options = {
|
|
|
|
'target': 'qutebrowser',
|
|
|
|
'disable': {
|
|
|
|
'pylint': [
|
|
|
|
# short variable names can be nice
|
|
|
|
'invalid-name',
|
|
|
|
# Basically unavoidable with Qt
|
|
|
|
'too-many-public-methods',
|
|
|
|
'no-self-use',
|
|
|
|
# These don't even exist in python3
|
|
|
|
'super-on-old-class',
|
|
|
|
'old-style-class',
|
|
|
|
# False-positives
|
|
|
|
'abstract-class-little-used',
|
|
|
|
# map/filter can be nicer than comprehensions
|
|
|
|
'bad-builtin',
|
|
|
|
# I disagree with these
|
|
|
|
'star-args',
|
|
|
|
'fixme',
|
2014-02-12 17:00:50 +01:00
|
|
|
'too-many-instance-attributes',
|
2014-02-07 10:58:53 +01:00
|
|
|
'global-statement',
|
2014-02-07 11:01:58 +01:00
|
|
|
'no-init',
|
2014-02-10 07:09:40 +01:00
|
|
|
# visual noise
|
|
|
|
'locally-disabled',
|
2014-02-07 10:58:53 +01:00
|
|
|
],
|
|
|
|
'flake8': [
|
2014-02-10 11:07:21 +01:00
|
|
|
'E241', # Multiple spaces after ,
|
2014-02-07 10:58:53 +01:00
|
|
|
],
|
2014-02-07 19:44:56 +01:00
|
|
|
'pep257': [
|
2014-02-10 11:07:21 +01:00
|
|
|
'D102', # Docstring missing, will be handled by others
|
2014-02-07 19:44:56 +01:00
|
|
|
],
|
2014-02-07 10:58:53 +01:00
|
|
|
},
|
2014-02-10 11:07:21 +01:00
|
|
|
'exclude': ['appdirs.py'],
|
2014-02-07 10:58:53 +01:00
|
|
|
'other': {
|
|
|
|
'pylint': ['--output-format=colorized', '--reports=no'],
|
|
|
|
'flake8': ['--max-complexity=10'],
|
|
|
|
},
|
|
|
|
}
|
2014-01-28 15:25:40 +01:00
|
|
|
|
2014-02-10 11:07:21 +01:00
|
|
|
|
2014-01-28 15:25:40 +01:00
|
|
|
def run(name, args=None):
|
2014-02-07 19:44:56 +01:00
|
|
|
""" Run a checker via distutils with optional args.
|
2014-01-29 04:07:27 +01:00
|
|
|
|
|
|
|
name -- Name of the checker/binary
|
|
|
|
args -- Option list of arguments to pass
|
|
|
|
"""
|
2014-02-07 10:58:53 +01:00
|
|
|
sys.argv = [name, options['target']]
|
2014-01-28 15:25:40 +01:00
|
|
|
if args is not None:
|
|
|
|
sys.argv += args
|
|
|
|
print("====== {} ======".format(name))
|
|
|
|
try:
|
|
|
|
load_entry_point(name, 'console_scripts', name)()
|
|
|
|
except SystemExit as e:
|
|
|
|
status[name] = e
|
2014-01-28 15:49:52 +01:00
|
|
|
except DistributionNotFound:
|
|
|
|
if args is None:
|
|
|
|
args = []
|
|
|
|
try:
|
|
|
|
status[name] = subprocess.call([name] + args)
|
|
|
|
except FileNotFoundError as e:
|
|
|
|
print('{}: {}'.format(e.__class__.__name__, e))
|
|
|
|
status[name] = None
|
2014-01-28 15:25:40 +01:00
|
|
|
except Exception as e:
|
2014-01-28 15:50:11 +01:00
|
|
|
print('{}: {}'.format(e.__class__.__name__, e))
|
2014-01-28 15:25:40 +01:00
|
|
|
status[name] = None
|
|
|
|
print()
|
|
|
|
|
2014-02-10 11:07:21 +01:00
|
|
|
|
2014-02-07 19:44:56 +01:00
|
|
|
def check_pep257(args=None):
|
|
|
|
sys.argv = ['pep257', options['target']]
|
|
|
|
if args is not None:
|
|
|
|
sys.argv += args
|
|
|
|
print("====== pep257 ======")
|
|
|
|
try:
|
|
|
|
status['pep257'] = pep257.main(*pep257.parse_options())
|
|
|
|
except Exception as e:
|
|
|
|
print('{}: {}'.format(e.__class__.__name__, e))
|
2014-02-10 11:07:21 +01:00
|
|
|
status['pep257'] = None
|
2014-02-07 19:44:56 +01:00
|
|
|
print()
|
|
|
|
|
2014-02-10 11:07:21 +01:00
|
|
|
|
2014-02-04 22:10:27 +01:00
|
|
|
def check_line():
|
|
|
|
"""Checks a filetree for CRLFs, conflict markers and weird whitespace"""
|
|
|
|
print("====== line ======")
|
2014-01-28 22:44:39 +01:00
|
|
|
ret = []
|
|
|
|
try:
|
2014-02-07 10:58:53 +01:00
|
|
|
for (dirpath, dirnames, filenames) in os.walk(options['target']):
|
2014-02-10 08:21:09 +01:00
|
|
|
for name in (e for e in filenames if e.endswith('.py')):
|
2014-01-28 22:44:39 +01:00
|
|
|
fn = os.path.join(dirpath, name)
|
2014-02-04 22:10:27 +01:00
|
|
|
ret.append(_check_line(fn))
|
|
|
|
status['line'] = all(ret)
|
2014-01-28 22:44:39 +01:00
|
|
|
except Exception as e:
|
|
|
|
print('{}: {}'.format(e.__class__.__name__, e))
|
2014-02-04 22:10:27 +01:00
|
|
|
status['line'] = None
|
2014-01-28 22:44:39 +01:00
|
|
|
print()
|
|
|
|
|
2014-02-10 11:07:21 +01:00
|
|
|
|
2014-02-04 22:10:27 +01:00
|
|
|
def _check_line(fn):
|
2014-01-28 22:44:39 +01:00
|
|
|
with open(fn, 'rb') as f:
|
|
|
|
for line in f:
|
|
|
|
if b'\r\n' in line:
|
|
|
|
print('Found CRLF in {}'.format(fn))
|
|
|
|
return False
|
2014-02-10 08:21:09 +01:00
|
|
|
elif any(line.decode('UTF-8').startswith(c * 7) for c in "<>=|"):
|
2014-02-04 22:10:27 +01:00
|
|
|
print('Found conflict marker in {}'.format(fn))
|
|
|
|
return False
|
|
|
|
elif any([line.decode('UTF-8').rstrip('\r\n').endswith(c)
|
|
|
|
for c in " \t"]):
|
|
|
|
print('Found whitespace at line ending in {}'.format(fn))
|
|
|
|
return False
|
|
|
|
elif b' \t' in line or b'\t ' in line:
|
|
|
|
print('Found tab-space mix in {}'.format(fn))
|
|
|
|
return False
|
2014-01-28 22:44:39 +01:00
|
|
|
return True
|
|
|
|
|
2014-02-10 11:07:21 +01:00
|
|
|
|
2014-02-07 19:39:55 +01:00
|
|
|
def _get_args(checker):
|
|
|
|
args = []
|
|
|
|
if checker == 'pylint':
|
|
|
|
try:
|
|
|
|
args += ['--disable=' + ','.join(options['disable']['pylint'])]
|
|
|
|
args += ['--ignore=' + ','.join(options['exclude'])]
|
|
|
|
args += options['other']['pylint']
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
elif checker == 'flake8':
|
|
|
|
try:
|
|
|
|
args += ['--ignore=' + ','.join(options['disable']['flake8'])]
|
|
|
|
args += ['--exclude=' + ','.join(options['exclude'])]
|
|
|
|
args += options['other']['flake8']
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2014-02-07 19:44:56 +01:00
|
|
|
elif checker == 'pep257':
|
|
|
|
args = []
|
|
|
|
try:
|
|
|
|
args += ['--ignore=' + ','.join(options['disable']['pep257'])]
|
2014-02-10 11:07:21 +01:00
|
|
|
args += ['--match=(?!{}).*\.py'.format('|'.join(
|
|
|
|
options['exclude']))]
|
2014-02-07 19:44:56 +01:00
|
|
|
args += options['other']['pep257']
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2014-02-07 19:39:55 +01:00
|
|
|
return args
|
2014-02-07 10:58:53 +01:00
|
|
|
|
2014-02-10 11:07:21 +01:00
|
|
|
if do_check_257:
|
|
|
|
check_pep257(_get_args('pep257'))
|
2014-02-07 19:39:55 +01:00
|
|
|
for checker in ['pylint', 'flake8']:
|
|
|
|
# FIXME what the hell is the flake8 exit status?
|
|
|
|
run(checker, _get_args(checker))
|
2014-02-04 22:10:27 +01:00
|
|
|
check_line()
|
2014-01-28 15:25:40 +01:00
|
|
|
|
|
|
|
print('Exit status values:')
|
|
|
|
for (k, v) in status.items():
|
|
|
|
print(' {} - {}'.format(k, v))
|
2014-01-30 20:49:08 +01:00
|
|
|
|
2014-02-10 08:21:09 +01:00
|
|
|
if all(val in [True, 0] for val in status):
|
2014-01-30 20:49:08 +01:00
|
|
|
sys.exit(0)
|
|
|
|
else:
|
|
|
|
sys.exit(1)
|