1#!/usr/bin/env python3 2# group: meta 3# 4# Copyright (C) 2020 Red Hat, Inc. 5# 6# This program is free software; you can redistribute it and/or modify 7# it under the terms of the GNU General Public License as published by 8# the Free Software Foundation; either version 2 of the License, or 9# (at your option) any later version. 10# 11# This program is distributed in the hope that it will be useful, 12# but WITHOUT ANY WARRANTY; without even the implied warranty of 13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14# GNU General Public License for more details. 15# 16# You should have received a copy of the GNU General Public License 17# along with this program. If not, see <http://www.gnu.org/licenses/>. 18 19import os 20import re 21import shutil 22import subprocess 23import sys 24from typing import List, Mapping, Optional 25 26import iotests 27 28 29# TODO: Empty this list! 30SKIP_FILES = ( 31 '030', '040', '041', '044', '045', '055', '056', '057', '065', '093', 32 '096', '118', '124', '132', '136', '139', '147', '148', '149', 33 '151', '152', '155', '163', '165', '194', '196', '202', 34 '203', '205', '206', '207', '208', '210', '211', '212', '213', '216', 35 '218', '219', '224', '228', '234', '235', '236', '237', '238', 36 '240', '242', '245', '246', '248', '255', '256', '257', '258', '260', 37 '262', '264', '266', '274', '277', '280', '281', '295', '296', '298', 38 '299', '302', '303', '304', '307', 39 'nbd-fault-injector.py', 'qcow2.py', 'qcow2_format.py', 'qed.py' 40) 41 42 43def is_python_file(filename): 44 if not os.path.isfile(filename): 45 return False 46 47 if filename.endswith('.py'): 48 return True 49 50 with open(filename, encoding='utf-8') as f: 51 try: 52 first_line = f.readline() 53 return re.match('^#!.*python', first_line) is not None 54 except UnicodeDecodeError: # Ignore binary files 55 return False 56 57 58def get_test_files() -> List[str]: 59 named_tests = [f'tests/{entry}' for entry in os.listdir('tests')] 60 check_tests = set(os.listdir('.') + named_tests) - set(SKIP_FILES) 61 return list(filter(is_python_file, check_tests)) 62 63 64def run_linter( 65 tool: str, 66 args: List[str], 67 env: Optional[Mapping[str, str]] = None, 68 suppress_output: bool = False, 69) -> None: 70 """ 71 Run a python-based linting tool. 72 73 :param suppress_output: If True, suppress all stdout/stderr output. 74 :raise CalledProcessError: If the linter process exits with failure. 75 """ 76 subprocess.run( 77 ('python3', '-m', tool, *args), 78 env=env, 79 check=True, 80 stdout=subprocess.PIPE if suppress_output else None, 81 stderr=subprocess.STDOUT if suppress_output else None, 82 universal_newlines=True, 83 ) 84 85 86def main() -> None: 87 for linter in ('pylint-3', 'mypy'): 88 if shutil.which(linter) is None: 89 iotests.notrun(f'{linter} not found') 90 91 files = get_test_files() 92 93 iotests.logger.debug('Files to be checked:') 94 iotests.logger.debug(', '.join(sorted(files))) 95 96 env = os.environ.copy() 97 env['MYPYPATH'] = env['PYTHONPATH'] 98 99 print('=== pylint ===') 100 sys.stdout.flush() 101 try: 102 run_linter('pylint', files, env=env) 103 except subprocess.CalledProcessError: 104 # pylint failure will be caught by diffing the IO. 105 pass 106 107 print('=== mypy ===') 108 sys.stdout.flush() 109 try: 110 run_linter('mypy', files, env=env, suppress_output=True) 111 except subprocess.CalledProcessError as exc: 112 if exc.output: 113 print(exc.output) 114 115 116iotests.script_main(main) 117