summaryrefslogtreecommitdiffstats
path: root/tests/run_py_linters.py
blob: 869b3e46520c5e232f7fb7ab486d6891ff052315 (plain)
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
115
116
117
118
119
120
121
122
123
# SPDX-License-Identifier: LGPL-2.1-or-later

# Copied from https://github.com/python-sdbus/python-sdbus
# Copyright (C) 2020, 2021 igo95862

# This file is part of nvme-cli

# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.

# This library 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
# Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
from __future__ import annotations

from argparse import ArgumentParser
from os import environ
from pathlib import Path
from subprocess import run
from typing import List

source_root = Path(environ['MESON_SOURCE_ROOT'])
build_dir = Path(environ['MESON_BUILD_ROOT'])

tests_dir = source_root / 'tests'

all_python_modules = [
    tests_dir,
]

mypy_cache_dir = build_dir / '.mypy_cache'


def run_mypy(path: Path) -> None:
    print(f"Running mypy on {path}")
    run(
        args=(
            'mypy', '--strict',
            '--cache-dir', mypy_cache_dir,
            '--python-version', '3.8',
            '--namespace-packages',
            '--ignore-missing-imports',
            path,
        ),
        check=False,
        env={'MYPYPATH': str(tests_dir.absolute()), **environ},
    )


def linter_main() -> None:
    run(
        args=(
            'flake8',
            *all_python_modules,
        ),
        check=False,
    )

    for x in all_python_modules:
        run_mypy(x)


def get_all_python_files() -> List[Path]:
    python_files: List[Path] = []

    for python_module in all_python_modules:
        if python_module.is_dir():
            for a_file in python_module.iterdir():
                if a_file.suffix == '.py':
                    python_files.append(a_file)
        else:
            python_files.append(python_module)

    return python_files


def formater_main() -> None:
    all_python_files = get_all_python_files()

    run(
        args=('autopep8', '--in-place', *all_python_files),
        check=False,
    )

    run(
        args=(
            'isort',
            '-m', 'VERTICAL_HANGING_INDENT',
            '--trailing-comma',
            *all_python_files,
        ),
        check=False,
    )


def main() -> None:
    parser = ArgumentParser()
    parser.add_argument(
        'mode',
        choices=('lint', 'format'),
    )

    args = parser.parse_args()

    mode = args.mode

    if mode == 'lint':
        linter_main()
    elif mode == 'format':
        formater_main()
    else:
        raise ValueError('Unknown mode', mode)


if __name__ == '__main__':
    main()