summaryrefslogtreecommitdiffstats
path: root/tests/qemu-iotests/297
blob: 433b7323368de4f0353961b57f47546f6073a759 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env python3
# group: meta
#
# Copyright (C) 2020 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import os
import re
import shutil
import subprocess
import sys

import iotests


# TODO: Empty this list!
SKIP_FILES = (
    '030', '040', '041', '044', '045', '055', '056', '057', '065', '093',
    '096', '118', '124', '132', '136', '139', '147', '148', '149',
    '151', '152', '155', '163', '165', '169', '194', '196', '199', '202',
    '203', '205', '206', '207', '208', '210', '211', '212', '213', '216',
    '218', '219', '222', '224', '228', '234', '235', '236', '237', '238',
    '240', '242', '245', '246', '248', '255', '256', '257', '258', '260',
    '262', '264', '266', '274', '277', '280', '281', '295', '296', '298',
    '299', '302', '303', '304', '307',
    'nbd-fault-injector.py', 'qcow2.py', 'qcow2_format.py', 'qed.py'
)


def is_python_file(filename):
    if not os.path.isfile(filename):
        return False

    if filename.endswith('.py'):
        return True

    with open(filename) as f:
        try:
            first_line = f.readline()
            return re.match('^#!.*python', first_line) is not None
        except UnicodeDecodeError:  # Ignore binary files
            return False


def run_linters():
    files = [filename for filename in (set(os.listdir('.')) - set(SKIP_FILES))
             if is_python_file(filename)]

    iotests.logger.debug('Files to be checked:')
    iotests.logger.debug(', '.join(sorted(files)))

    print('=== pylint ===')
    sys.stdout.flush()

    # Todo notes are fine, but fixme's or xxx's should probably just be
    # fixed (in tests, at least)
    env = os.environ.copy()
    qemu_module_path = os.path.join(os.path.dirname(__file__),
                                    '..', '..', 'python')
    try:
        env['PYTHONPATH'] += os.pathsep + qemu_module_path
    except KeyError:
        env['PYTHONPATH'] = qemu_module_path
    subprocess.run(('pylint-3', '--score=n', '--notes=FIXME,XXX', *files),
                   env=env, check=False)

    print('=== mypy ===')
    sys.stdout.flush()

    # We have to call mypy separately for each file.  Otherwise, it
    # will interpret all given files as belonging together (i.e., they
    # may not both define the same classes, etc.; most notably, they
    # must not both define the __main__ module).
    env['MYPYPATH'] = env['PYTHONPATH']
    for filename in files:
        p = subprocess.run(('mypy',
                            '--warn-unused-configs',
                            '--disallow-subclassing-any',
                            '--disallow-any-generics',
                            '--disallow-incomplete-defs',
                            '--disallow-untyped-decorators',
                            '--no-implicit-optional',
                            '--warn-redundant-casts',
                            '--warn-unused-ignores',
                            '--no-implicit-reexport',
                            '--namespace-packages',
                            filename),
                           env=env,
                           check=False,
                           stdout=subprocess.PIPE,
                           stderr=subprocess.STDOUT,
                           universal_newlines=True)

        if p.returncode != 0:
            print(p.stdout)


for linter in ('pylint-3', 'mypy'):
    if shutil.which(linter) is None:
        iotests.notrun(f'{linter} not found')

iotests.script_main(run_linters)