From f95599112078cd05af2f837375fed129c894a914 Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Thu, 18 Aug 2016 11:05:04 -0700 Subject: [PATCH] tools: Add bright_red_output() to tools/lint-all --- tools/lint-all | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/tools/lint-all b/tools/lint-all index 18b7faa33a..e11f7db68c 100755 --- a/tools/lint-all +++ b/tools/lint-all @@ -1,6 +1,7 @@ #!/usr/bin/env python from __future__ import print_function from __future__ import absolute_import +from contextlib import contextmanager import logging import os import re @@ -11,7 +12,7 @@ import traceback try: import lister - from typing import cast, Any, Callable, Dict, List, Optional + from typing import cast, Any, Callable, Dict, List, Optional, Generator except ImportError as e: print("ImportError: {}".format(e)) print("You need to run the Zulip linters inside a Zulip dev environment.") @@ -36,6 +37,19 @@ zproject/settings.py zproject/test_settings.py """.split() +@contextmanager +def bright_red_output(): + # type: () -> Generator[None, None, None] + # Make the lint output bright red + sys.stdout.write('\x1B[1;31m') + sys.stdout.flush() + try: + yield + finally: + # Restore normal terminal colors + sys.stdout.write('\x1B[0m') + + def check_pyflakes(options, by_lang): # type: (Any, Dict[str, List[str]]) -> bool if not by_lang['py']: @@ -393,11 +407,7 @@ def run(): lint_functions[func.__name__] = func return func - try: - # Make the lint output bright red - sys.stdout.write('\x1B[1;31m') - sys.stdout.flush() - + with bright_red_output(): @lint def templates(): # type: () -> int @@ -454,11 +464,7 @@ def run(): failed = run_parallel(lint_functions) - sys.exit(1 if failed else 0) - - finally: - # Restore normal terminal colors - sys.stdout.write('\x1B[0m') + sys.exit(1 if failed else 0) if __name__ == '__main__': run()