From cb7d236696c9ba034a60564f9f9439efa5970942 Mon Sep 17 00:00:00 2001 From: "Michael Hirsch, Ph.D" Date: Fri, 5 Jul 2019 17:52:03 -0400 Subject: [PATCH] pythonic file checks --- mesonbuild/mlog.py | 11 +++++++---- run_project_tests.py | 34 +++++++++++++++------------------- 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/mesonbuild/mlog.py b/mesonbuild/mlog.py index 79dee478136b..d13defb3b173 100644 --- a/mesonbuild/mlog.py +++ b/mesonbuild/mlog.py @@ -40,10 +40,13 @@ def _windows_ansi() -> bool: # original behavior return bool(kernel.SetConsoleMode(stdout, mode.value | 0x4) or os.environ.get('ANSICON')) -if platform.system().lower() == 'windows': - colorize_console = os.isatty(sys.stdout.fileno()) and _windows_ansi() # type: bool -else: - colorize_console = os.isatty(sys.stdout.fileno()) and os.environ.get('TERM') != 'dumb' +try: + if platform.system().lower() == 'windows': + colorize_console = os.isatty(sys.stdout.fileno()) and _windows_ansi() # type: bool + else: + colorize_console = os.isatty(sys.stdout.fileno()) and os.environ.get('TERM') != 'dumb' +except Exception: + colorize_console = False log_dir = None # type: Optional[str] log_file = None # type: Optional[TextIO] log_fname = 'meson-log.txt' # type: str diff --git a/run_project_tests.py b/run_project_tests.py index 154da00af51e..797e1a96765a 100755 --- a/run_project_tests.py +++ b/run_project_tests.py @@ -766,19 +766,14 @@ def _run_tests(all_tests, log_name_base, failfast, extra_args): ET.ElementTree(element=junit_root).write(xmlname, xml_declaration=True, encoding='UTF-8') return passing_tests, failing_tests, skipped_tests -def check_file(fname): - linenum = 1 - with open(fname, 'rb') as f: - lines = f.readlines() +def check_file(file: Path): + lines = file.read_bytes().split(b'\n') tabdetector = re.compile(br' *\t') - for line in lines: + for i, line in enumerate(lines): if re.match(tabdetector, line): - print("File %s contains a tab indent on line %d. Only spaces are permitted." % (fname, linenum)) - sys.exit(1) - if b'\r' in line: - print("File %s contains DOS line ending on line %d. Only unix-style line endings are permitted." % (fname, linenum)) - sys.exit(1) - linenum += 1 + raise SystemExit("File {} contains a tab indent on line {:d}. Only spaces are permitted.".format(file, i + 1)) + if line.endswith(b'\r'): + raise SystemExit("File {} contains DOS line ending on line {:d}. Only unix-style line endings are permitted.".format(file, i + 1)) def check_format(): check_suffixes = {'.c', @@ -800,20 +795,21 @@ def check_format(): '.build', '.md', } - for (root, _, files) in os.walk('.'): + for (root, _, filenames) in os.walk('.'): if '.dub' in root: # external deps are here continue if '.pytest_cache' in root: continue if 'meson-logs' in root or 'meson-private' in root: continue - for fname in files: - if os.path.splitext(fname)[1].lower() in check_suffixes: - bn = os.path.basename(fname) - if bn == 'sitemap.txt' or bn == 'meson-test-run.txt': + if '.eggs' in root or '_cache' in root: # e.g. .mypy_cache + continue + for fname in filenames: + file = Path(fname) + if file.suffix.lower() in check_suffixes: + if file.name in ('sitemap.txt', 'meson-test-run.txt'): continue - fullname = os.path.join(root, fname) - check_file(fullname) + check_file(root / file) def check_meson_commands_work(): global backend, compile_commands, test_commands, install_commands @@ -905,4 +901,4 @@ def detect_system_compiler(): tests = list(g) if len(tests) != 1: print('WARNING: The %s suite contains duplicate "%s" tests: "%s"' % (name, k, '", "'.join(tests))) - sys.exit(failing_tests) + raise SystemExit(failing_tests)