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