2018-08-04 23:18:50 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
import argparse
|
2018-08-04 23:26:25 +02:00
|
|
|
import logging
|
|
|
|
import os
|
2018-08-04 23:33:19 +02:00
|
|
|
import subprocess
|
2018-08-04 23:26:25 +02:00
|
|
|
import sys
|
2018-12-17 19:58:33 +01:00
|
|
|
|
|
|
|
if False:
|
2018-12-17 19:52:08 +01:00
|
|
|
# See https://zulip.readthedocs.io/en/latest/testing/mypy.html#mypy-in-production-scripts
|
2019-06-21 18:40:38 +02:00
|
|
|
from typing import Callable, Dict, List
|
2018-08-04 23:26:25 +02:00
|
|
|
|
2018-08-04 23:56:46 +02:00
|
|
|
from zulint.printer import print_err, colors
|
2019-06-21 18:40:38 +02:00
|
|
|
from zulint import lister
|
2018-08-04 23:18:50 +02:00
|
|
|
|
|
|
|
def add_default_linter_arguments(parser):
|
|
|
|
# type: (argparse.ArgumentParser) -> None
|
|
|
|
parser.add_argument('--modified', '-m',
|
|
|
|
action='store_true',
|
|
|
|
help='Only check modified files')
|
2019-06-18 15:27:10 +02:00
|
|
|
parser.add_argument('--verbose-timing', '-vt',
|
2018-08-04 23:18:50 +02:00
|
|
|
action='store_true',
|
|
|
|
help='Print verbose timing output')
|
|
|
|
parser.add_argument('targets',
|
|
|
|
nargs='*',
|
|
|
|
help='Specify directories to check')
|
2019-06-18 15:59:24 +02:00
|
|
|
parser.add_argument('--skip',
|
|
|
|
default=[],
|
|
|
|
type=split_arg_into_list,
|
|
|
|
help='Specify linters to skip, eg: --skip=mypy,gitlint')
|
2019-06-18 19:11:26 +02:00
|
|
|
parser.add_argument('--only',
|
|
|
|
default=[],
|
|
|
|
type=split_arg_into_list,
|
|
|
|
help='Specify linters to run, eg: --only=mypy,gitlint')
|
2019-06-18 18:38:20 +02:00
|
|
|
parser.add_argument('--list', '-l',
|
|
|
|
action='store_true',
|
|
|
|
help='List all the registered linters')
|
2019-06-21 18:40:38 +02:00
|
|
|
parser.add_argument('--groups', '-g',
|
|
|
|
default=[],
|
|
|
|
type=split_arg_into_list,
|
|
|
|
help='Only run linter for languages in the group(s), e.g.: '
|
|
|
|
'--groups=backend,frontend')
|
2019-06-18 15:59:24 +02:00
|
|
|
|
|
|
|
def split_arg_into_list(arg):
|
|
|
|
# type: (str) -> List[str]
|
|
|
|
return [linter for linter in arg.split(',')]
|
2018-08-04 23:26:25 +02:00
|
|
|
|
|
|
|
def run_parallel(lint_functions):
|
|
|
|
# type: (Dict[str, Callable[[], int]]) -> bool
|
|
|
|
pids = []
|
|
|
|
for name, func in lint_functions.items():
|
|
|
|
pid = os.fork()
|
|
|
|
if pid == 0:
|
|
|
|
logging.info("start " + name)
|
|
|
|
result = func()
|
|
|
|
logging.info("finish " + name)
|
|
|
|
sys.stdout.flush()
|
|
|
|
sys.stderr.flush()
|
|
|
|
os._exit(result)
|
|
|
|
pids.append(pid)
|
|
|
|
failed = False
|
|
|
|
|
|
|
|
for pid in pids:
|
|
|
|
(_, status) = os.waitpid(pid, 0)
|
|
|
|
if status != 0:
|
|
|
|
failed = True
|
|
|
|
return failed
|
|
|
|
|
2018-08-04 23:33:19 +02:00
|
|
|
class LinterConfig:
|
|
|
|
lint_functions = {} # type: Dict[str, Callable[[], int]]
|
|
|
|
|
2019-06-21 18:40:38 +02:00
|
|
|
def __init__(self, args):
|
|
|
|
# type: (argparse.Namespace) -> None
|
2019-06-21 18:36:41 +02:00
|
|
|
self.args = args
|
2019-06-21 18:40:38 +02:00
|
|
|
self.by_lang = {} # type: Dict[str, List[str]]
|
|
|
|
|
|
|
|
def list_files(self, file_types=[], groups={}, use_shebang=True, group_by_ftype=True, exclude=[]):
|
|
|
|
# type: (List[str], Dict[str, List[str]], bool, bool, List[str]) -> Dict[str, List[str]]
|
|
|
|
assert file_types or groups, "Atleast one of `file_types` or `groups` must be specified."
|
|
|
|
|
|
|
|
if self.args.groups:
|
|
|
|
file_types = [ft for group in self.args.groups for ft in groups[group]]
|
|
|
|
else:
|
|
|
|
file_types.extend({ft for group in groups.values() for ft in group})
|
|
|
|
|
|
|
|
self.by_lang = lister.list_files(self.args.targets, modified_only=self.args.modified,
|
|
|
|
ftypes=file_types, use_shebang=use_shebang,
|
|
|
|
group_by_ftype=group_by_ftype, exclude=exclude)
|
|
|
|
return self.by_lang
|
2018-08-04 23:33:19 +02:00
|
|
|
|
|
|
|
def lint(self, func):
|
|
|
|
# type: (Callable[[], int]) -> Callable[[], int]
|
|
|
|
self.lint_functions[func.__name__] = func
|
|
|
|
return func
|
|
|
|
|
2018-12-17 05:58:06 +01:00
|
|
|
def external_linter(self, name, command, target_langs=[], pass_targets=True):
|
|
|
|
# type: (str, List[str], List[str], bool) -> None
|
2018-08-04 23:33:19 +02:00
|
|
|
"""Registers an external linter program to be run as part of the
|
|
|
|
linter. This program will be passed the subset of files being
|
|
|
|
linted that have extensions in target_langs. If there are no
|
|
|
|
such files, exits without doing anything.
|
|
|
|
|
|
|
|
If target_langs is empty, just runs the linter unconditionally.
|
|
|
|
"""
|
|
|
|
color = next(colors)
|
|
|
|
|
|
|
|
def run_linter():
|
|
|
|
# type: () -> int
|
|
|
|
targets = [] # type: List[str]
|
|
|
|
if len(target_langs) != 0:
|
|
|
|
targets = [target for lang in target_langs for target in self.by_lang[lang]]
|
|
|
|
if len(targets) == 0:
|
|
|
|
# If this linter has a list of languages, and
|
|
|
|
# no files in those languages are to be checked,
|
|
|
|
# then we can safely return success without
|
|
|
|
# invoking the external linter.
|
|
|
|
return 0
|
|
|
|
|
2018-12-17 05:58:06 +01:00
|
|
|
if pass_targets:
|
|
|
|
full_command = command + targets
|
|
|
|
else:
|
|
|
|
full_command = command
|
|
|
|
p = subprocess.Popen(full_command,
|
2018-08-04 23:33:19 +02:00
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.STDOUT)
|
|
|
|
|
|
|
|
assert p.stdout # use of subprocess.PIPE indicates non-None
|
|
|
|
for line in iter(p.stdout.readline, b''):
|
|
|
|
print_err(name, color, line)
|
|
|
|
|
|
|
|
return p.wait() # Linter exit code
|
|
|
|
|
|
|
|
self.lint_functions[name] = run_linter
|
|
|
|
|
2019-06-21 18:36:41 +02:00
|
|
|
def set_logger(self):
|
|
|
|
# type: () -> None
|
2019-06-20 19:35:57 +02:00
|
|
|
logging.basicConfig(format="%(asctime)s %(message)s")
|
|
|
|
logger = logging.getLogger()
|
2019-06-21 18:36:41 +02:00
|
|
|
if self.args.verbose_timing:
|
2019-06-20 19:35:57 +02:00
|
|
|
logger.setLevel(logging.INFO)
|
|
|
|
else:
|
|
|
|
logger.setLevel(logging.WARNING)
|
|
|
|
|
2019-06-21 18:36:41 +02:00
|
|
|
def do_lint(self):
|
|
|
|
# type: () -> None
|
|
|
|
assert not self.args.only or not self.args.skip, "Only one of --only or --skip can be used at once."
|
|
|
|
if self.args.only:
|
|
|
|
self.lint_functions = {linter: self.lint_functions[linter] for linter in self.args.only}
|
|
|
|
for linter in self.args.skip:
|
2019-06-18 19:12:54 +02:00
|
|
|
del self.lint_functions[linter]
|
2019-06-21 18:36:41 +02:00
|
|
|
if self.args.list:
|
2019-06-18 18:38:20 +02:00
|
|
|
print("\n".join(self.lint_functions.keys()))
|
|
|
|
sys.exit()
|
2019-06-21 18:36:41 +02:00
|
|
|
self.set_logger()
|
2019-06-18 19:11:26 +02:00
|
|
|
|
2018-08-04 23:33:19 +02:00
|
|
|
failed = run_parallel(self.lint_functions)
|
|
|
|
sys.exit(1 if failed else 0)
|