2019-05-12 23:04:36 +02:00
|
|
|
#!/usr/bin/env python
|
2019-04-17 12:06:00 +02:00
|
|
|
|
2019-05-12 23:04:36 +02:00
|
|
|
from __future__ import print_function
|
2019-04-17 12:06:00 +02:00
|
|
|
|
2019-05-12 23:04:36 +02:00
|
|
|
import argparse
|
|
|
|
import collections
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import sys
|
2019-04-17 12:06:00 +02:00
|
|
|
|
2019-05-12 23:04:36 +02:00
|
|
|
sys.path.append(os.path.dirname(__file__))
|
2019-05-24 17:20:06 +02:00
|
|
|
from helpers import get_output, git_ls_files, filter_changed
|
2019-05-12 23:04:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument('files', nargs='*', default=[],
|
|
|
|
help='files to be processed (regex on path)')
|
|
|
|
parser.add_argument('-c', '--changed', action='store_true',
|
|
|
|
help='Only run on changed files')
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
files = []
|
2019-05-24 17:20:06 +02:00
|
|
|
for path in git_ls_files():
|
2019-05-12 23:04:36 +02:00
|
|
|
filetypes = ('.py',)
|
|
|
|
ext = os.path.splitext(path)[1]
|
2019-05-24 17:20:06 +02:00
|
|
|
if ext in filetypes and path.startswith('esphome'):
|
2019-05-12 23:04:36 +02:00
|
|
|
path = os.path.relpath(path, os.getcwd())
|
|
|
|
files.append(path)
|
|
|
|
# Match against re
|
|
|
|
file_name_re = re.compile('|'.join(args.files))
|
|
|
|
files = [p for p in files if file_name_re.search(p)]
|
|
|
|
|
|
|
|
if args.changed:
|
|
|
|
files = filter_changed(files)
|
|
|
|
|
|
|
|
files.sort()
|
2019-05-24 17:20:06 +02:00
|
|
|
if not files:
|
|
|
|
sys.exit(0)
|
2019-05-12 23:04:36 +02:00
|
|
|
|
|
|
|
errors = collections.defaultdict(list)
|
|
|
|
cmd = ['flake8'] + files
|
|
|
|
print("Running flake8...")
|
|
|
|
log = get_output(*cmd)
|
|
|
|
for line in log.splitlines():
|
|
|
|
line = line.split(':')
|
|
|
|
if len(line) < 4:
|
|
|
|
continue
|
|
|
|
file_ = line[0]
|
|
|
|
linno = line[1]
|
|
|
|
msg = (u':'.join(line[3:])).strip()
|
|
|
|
errors[file_].append(u'{}:{} - {}'.format(file_, linno, msg))
|
|
|
|
|
|
|
|
cmd = ['pylint', '-f', 'parseable', '--persistent=n'] + files
|
|
|
|
print("Running pylint...")
|
|
|
|
log = get_output(*cmd)
|
|
|
|
for line in log.splitlines():
|
|
|
|
line = line.split(':')
|
|
|
|
if len(line) < 3:
|
|
|
|
continue
|
|
|
|
file_ = line[0]
|
|
|
|
linno = line[1]
|
|
|
|
msg = (u':'.join(line[3:])).strip()
|
|
|
|
errors[file_].append(u'{}:{} - {}'.format(file_, linno, msg))
|
|
|
|
|
|
|
|
for f, errs in sorted(errors.items()):
|
|
|
|
print("\033[0;32m************* File \033[1;32m{}\033[0m".format(f))
|
|
|
|
for err in errs:
|
|
|
|
print(err)
|
|
|
|
print()
|
|
|
|
|
|
|
|
sys.exit(len(errors))
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|