zulint: Extract run_parallel and do_lint.

This commit is contained in:
Tim Abbott 2018-08-04 14:26:25 -07:00
parent acf8ec492d
commit f74bb8a9b2
2 changed files with 33 additions and 25 deletions

View File

@ -14,31 +14,10 @@ from lib import sanity_check
sanity_check.check_venv(__file__) sanity_check.check_venv(__file__)
from zulint import lister from zulint import lister
from zulint.command import add_default_linter_arguments from zulint.command import add_default_linter_arguments, do_lint
from typing import cast, Callable, Dict, Iterator, List from typing import cast, Callable, Dict, Iterator, List
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
def run(): def run():
# type: () -> None # type: () -> None
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
@ -188,9 +167,7 @@ def run():
failed = check_pep8(by_lang['py']) failed = check_pep8(by_lang['py'])
return 1 if failed else 0 return 1 if failed else 0
failed = run_parallel(lint_functions) do_lint(lint_functions)
sys.exit(1 if failed else 0)
if __name__ == '__main__': if __name__ == '__main__':
run() run()

View File

@ -4,6 +4,11 @@ from __future__ import print_function
from __future__ import absolute_import from __future__ import absolute_import
import argparse import argparse
import logging
import os
import sys
from typing import Any, Callable, Dict, List
def add_default_linter_arguments(parser): def add_default_linter_arguments(parser):
# type: (argparse.ArgumentParser) -> None # type: (argparse.ArgumentParser) -> None
@ -16,3 +21,29 @@ def add_default_linter_arguments(parser):
parser.add_argument('targets', parser.add_argument('targets',
nargs='*', nargs='*',
help='Specify directories to check') help='Specify directories to check')
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
def do_lint(lint_functions):
# type: (Dict[str, Callable[[], int]]) -> None
failed = run_parallel(lint_functions)
sys.exit(1 if failed else 0)