xref: /openbmc/qemu/tests/qemu-iotests/297 (revision 7a90bcc2)
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 subprocess
22import sys
23from typing import List, Mapping, Optional
24
25import iotests
26
27
28# TODO: Empty this list!
29SKIP_FILES = (
30    '030', '040', '041', '044', '045', '055', '056', '057', '065', '093',
31    '096', '118', '124', '132', '136', '139', '147', '148', '149',
32    '151', '152', '155', '163', '165', '194', '196', '202',
33    '203', '205', '206', '207', '208', '210', '211', '212', '213', '216',
34    '218', '219', '224', '228', '234', '235', '236', '237', '238',
35    '240', '242', '245', '246', '248', '255', '256', '257', '258', '260',
36    '262', '264', '266', '274', '277', '280', '281', '295', '296', '298',
37    '299', '302', '303', '304', '307',
38    'nbd-fault-injector.py', 'qcow2.py', 'qcow2_format.py', 'qed.py'
39)
40
41
42def is_python_file(filename):
43    if not os.path.isfile(filename):
44        return False
45
46    if filename.endswith('.py'):
47        return True
48
49    with open(filename, encoding='utf-8') as f:
50        try:
51            first_line = f.readline()
52            return re.match('^#!.*python', first_line) is not None
53        except UnicodeDecodeError:  # Ignore binary files
54            return False
55
56
57def get_test_files() -> List[str]:
58    named_tests = [f'tests/{entry}' for entry in os.listdir('tests')]
59    check_tests = set(os.listdir('.') + named_tests) - set(SKIP_FILES)
60    return list(filter(is_python_file, check_tests))
61
62
63def run_linter(
64        tool: str,
65        args: List[str],
66        env: Optional[Mapping[str, str]] = None,
67        suppress_output: bool = False,
68) -> None:
69    """
70    Run a python-based linting tool.
71
72    :param suppress_output: If True, suppress all stdout/stderr output.
73    :raise CalledProcessError: If the linter process exits with failure.
74    """
75    subprocess.run(
76        ('python3', '-m', tool, *args),
77        env=env,
78        check=True,
79        stdout=subprocess.PIPE if suppress_output else None,
80        stderr=subprocess.STDOUT if suppress_output else None,
81        universal_newlines=True,
82    )
83
84
85def main() -> None:
86    for linter in ('pylint', 'mypy'):
87        try:
88            run_linter(linter, ['--version'], suppress_output=True)
89        except subprocess.CalledProcessError:
90            iotests.notrun(f"'{linter}' not found")
91
92    files = get_test_files()
93
94    iotests.logger.debug('Files to be checked:')
95    iotests.logger.debug(', '.join(sorted(files)))
96
97    env = os.environ.copy()
98    env['MYPYPATH'] = env['PYTHONPATH']
99
100    print('=== pylint ===')
101    sys.stdout.flush()
102    try:
103        run_linter('pylint', files, env=env)
104    except subprocess.CalledProcessError:
105        # pylint failure will be caught by diffing the IO.
106        pass
107
108    print('=== mypy ===')
109    sys.stdout.flush()
110    try:
111        run_linter('mypy', files, env=env, suppress_output=True)
112    except subprocess.CalledProcessError as exc:
113        if exc.output:
114            print(exc.output)
115
116
117iotests.script_main(main)
118