1 # Copyright (C) 2020 Red Hat, Inc.
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.
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.
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/>.
20 from typing
import List
, Mapping
, Optional
23 # TODO: Empty this list!
25 '030', '040', '041', '044', '045', '055', '056', '057', '065', '093',
26 '096', '118', '124', '132', '136', '139', '147', '148', '149',
27 '151', '152', '155', '163', '165', '194', '196', '202',
28 '203', '205', '206', '207', '208', '210', '211', '212', '213', '216',
29 '218', '219', '224', '228', '234', '235', '236', '237', '238',
30 '240', '242', '245', '246', '248', '255', '256', '257', '258', '260',
31 '262', '264', '266', '274', '277', '280', '281', '295', '296', '298',
32 '299', '302', '303', '304', '307',
33 'nbd-fault-injector.py', 'qcow2.py', 'qcow2_format.py', 'qed.py'
37 def is_python_file(filename
):
38 if not os
.path
.isfile(filename
):
41 if filename
.endswith('.py'):
44 with
open(filename
, encoding
='utf-8') as f
:
46 first_line
= f
.readline()
47 return re
.match('^#!.*python', first_line
) is not None
48 except UnicodeDecodeError: # Ignore binary files
52 def get_test_files() -> List
[str]:
53 named_tests
= [f
'tests/{entry}' for entry
in os
.listdir('tests')]
54 check_tests
= set(os
.listdir('.') + named_tests
) - set(SKIP_FILES
)
55 return list(filter(is_python_file
, check_tests
))
61 env
: Optional
[Mapping
[str, str]] = None,
62 suppress_output
: bool = False,
65 Run a python-based linting tool.
67 :param suppress_output: If True, suppress all stdout/stderr output.
68 :raise CalledProcessError: If the linter process exits with failure.
71 (sys
.executable
, '-m', tool
, *args
),
74 stdout
=subprocess
.PIPE
if suppress_output
else None,
75 stderr
=subprocess
.STDOUT
if suppress_output
else None,
76 universal_newlines
=True,
82 Used by the Python CI system as an entry point to run these linters.
84 def show_usage() -> None:
85 print(f
"Usage: {sys.argv[0]} < --mypy | --pylint >", file=sys
.stderr
)
88 if len(sys
.argv
) != 2:
91 files
= get_test_files()
93 if sys
.argv
[1] == '--pylint':
94 run_linter('pylint', files
)
95 elif sys
.argv
[1] == '--mypy':
96 # mypy bug #9852; disable incremental checking as a workaround.
97 args
= ['--no-incremental'] + files
98 run_linter('mypy', args
)
100 print(f
"Unrecognized argument: '{sys.argv[1]}'", file=sys
.stderr
)
104 if __name__
== '__main__':