xref: /openbmc/qemu/tests/qemu-iotests/297 (revision 5dd0be53)
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 subprocess
21import sys
22from typing import List
23
24import iotests
25import linters
26
27
28# Looking for something?
29#
30#  List of files to exclude from linting: linters.py
31#  mypy configuration:                    mypy.ini
32#  pylint configuration:                  pylintrc
33
34
35def check_linter(linter: str) -> bool:
36    try:
37        linters.run_linter(linter, ['--version'], suppress_output=True)
38    except subprocess.CalledProcessError:
39        iotests.case_notrun(f"'{linter}' not found")
40        return False
41    return True
42
43
44def test_pylint(files: List[str]) -> None:
45    print('=== pylint ===')
46    sys.stdout.flush()
47
48    if not check_linter('pylint'):
49        return
50
51    linters.run_linter('pylint', files)
52
53
54def test_mypy(files: List[str]) -> None:
55    print('=== mypy ===')
56    sys.stdout.flush()
57
58    if not check_linter('mypy'):
59        return
60
61    env = os.environ.copy()
62    env['MYPYPATH'] = env['PYTHONPATH']
63
64    linters.run_linter('mypy', files, env=env, suppress_output=True)
65
66
67def main() -> None:
68    files = linters.get_test_files()
69
70    iotests.logger.debug('Files to be checked:')
71    iotests.logger.debug(', '.join(sorted(files)))
72
73    for test in (test_pylint, test_mypy):
74        try:
75            test(files)
76        except subprocess.CalledProcessError as exc:
77            # Linter failure will be caught by diffing the IO.
78            if exc.output:
79                print(exc.output)
80
81
82iotests.script_main(main)
83