xref: /openbmc/qemu/tests/qemu-iotests/297 (revision c293ba55)
159aec869SMax Reitz#!/usr/bin/env python3
29dd003a9SVladimir Sementsov-Ogievskiy# group: meta
319b7868eSKevin Wolf#
419b7868eSKevin Wolf# Copyright (C) 2020 Red Hat, Inc.
519b7868eSKevin Wolf#
619b7868eSKevin Wolf# This program is free software; you can redistribute it and/or modify
719b7868eSKevin Wolf# it under the terms of the GNU General Public License as published by
819b7868eSKevin Wolf# the Free Software Foundation; either version 2 of the License, or
919b7868eSKevin Wolf# (at your option) any later version.
1019b7868eSKevin Wolf#
1119b7868eSKevin Wolf# This program is distributed in the hope that it will be useful,
1219b7868eSKevin Wolf# but WITHOUT ANY WARRANTY; without even the implied warranty of
1319b7868eSKevin Wolf# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1419b7868eSKevin Wolf# GNU General Public License for more details.
1519b7868eSKevin Wolf#
1619b7868eSKevin Wolf# You should have received a copy of the GNU General Public License
1719b7868eSKevin Wolf# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1819b7868eSKevin Wolf
1959aec869SMax Reitzimport os
2059aec869SMax Reitzimport subprocess
2159aec869SMax Reitzimport sys
22*c293ba55SJohn Snowfrom typing import List
2319b7868eSKevin Wolf
2459aec869SMax Reitzimport iotests
25*c293ba55SJohn Snowimport linters
2619b7868eSKevin Wolf
2719b7868eSKevin Wolf
28*c293ba55SJohn Snow# Looking for something?
29*c293ba55SJohn Snow#
30*c293ba55SJohn Snow#  List of files to exclude from linting: linters.py
31*c293ba55SJohn Snow#  mypy configuration:                    mypy.ini
32*c293ba55SJohn Snow#  pylint configuration:                  pylintrc
3359aec869SMax Reitz
3459aec869SMax Reitz
3585cfec53SJohn Snowdef check_linter(linter: str) -> bool:
367a90bcc2SJohn Snow    try:
37*c293ba55SJohn Snow        linters.run_linter(linter, ['--version'], suppress_output=True)
387a90bcc2SJohn Snow    except subprocess.CalledProcessError:
3985cfec53SJohn Snow        iotests.case_notrun(f"'{linter}' not found")
4085cfec53SJohn Snow        return False
4185cfec53SJohn Snow    return True
4259aec869SMax Reitz
4385cfec53SJohn Snow
4485cfec53SJohn Snowdef test_pylint(files: List[str]) -> None:
4585cfec53SJohn Snow    print('=== pylint ===')
4685cfec53SJohn Snow    sys.stdout.flush()
4785cfec53SJohn Snow
4885cfec53SJohn Snow    if not check_linter('pylint'):
4985cfec53SJohn Snow        return
5085cfec53SJohn Snow
51*c293ba55SJohn Snow    linters.run_linter('pylint', files)
5285cfec53SJohn Snow
5385cfec53SJohn Snow
5485cfec53SJohn Snowdef test_mypy(files: List[str]) -> None:
5585cfec53SJohn Snow    print('=== mypy ===')
5685cfec53SJohn Snow    sys.stdout.flush()
5785cfec53SJohn Snow
5885cfec53SJohn Snow    if not check_linter('mypy'):
5985cfec53SJohn Snow        return
6085cfec53SJohn Snow
6185cfec53SJohn Snow    env = os.environ.copy()
6285cfec53SJohn Snow    env['MYPYPATH'] = env['PYTHONPATH']
6385cfec53SJohn Snow
64*c293ba55SJohn Snow    linters.run_linter('mypy', files, env=env, suppress_output=True)
6585cfec53SJohn Snow
6685cfec53SJohn Snow
6785cfec53SJohn Snowdef main() -> None:
68*c293ba55SJohn Snow    files = linters.get_test_files()
692d804f55SJohn Snow
702d804f55SJohn Snow    iotests.logger.debug('Files to be checked:')
712d804f55SJohn Snow    iotests.logger.debug(', '.join(sorted(files)))
722d804f55SJohn Snow
7385cfec53SJohn Snow    for test in (test_pylint, test_mypy):
74752f425dSJohn Snow        try:
7585cfec53SJohn Snow            test(files)
76752f425dSJohn Snow        except subprocess.CalledProcessError as exc:
7785cfec53SJohn Snow            # Linter failure will be caught by diffing the IO.
78752f425dSJohn Snow            if exc.output:
79752f425dSJohn Snow                print(exc.output)
80447aebdaSJohn Snow
81447aebdaSJohn Snow
82447aebdaSJohn Snowiotests.script_main(main)
83