File: run_py_linters.py

package info (click to toggle)
python-sdbus 0.14.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 996 kB
  • sloc: python: 7,911; ansic: 2,507; makefile: 9; sh: 4
file content (142 lines) | stat: -rwxr-xr-x 3,508 bytes parent folder | download | duplicates (3)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# SPDX-License-Identifier: LGPL-2.1-or-later

# Copyright (C) 2020, 2021 igo95862

# This file is part of python-sdbus

# 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 SubprocessError, run

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

tools_dir = source_root / 'tools'
src_dir = source_root / 'src'
test_dir = source_root / 'test'
wheel_build_dir = source_root / 'wheel-build'
examples_dir = source_root / 'examples'

all_python_modules = [
    tools_dir, test_dir, wheel_build_dir,
    src_dir / 'sdbus',
    src_dir / 'sdbus_async/dbus_daemon',
    src_dir / 'sdbus_block/dbus_daemon',
    source_root / 'setup.py',
]

mypy_cache_dir = build_dir / '.mypy_cache'


def run_mypy() -> None:
    print('Running mypy on all modules')
    run(
        args=(
            'mypy', '--strict', '--pretty',
            '--cache-dir', mypy_cache_dir,
            '--python-version', '3.9',
            '--namespace-packages',
            '--explicit-package-bases',
            *all_python_modules,
        ),
        check=True,
        env={'MYPYPATH': str(src_dir.absolute()), **environ},
    )


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


def linter_main() -> None:
    is_success = True

    try:
        run_flake8()
    except SubprocessError:
        is_success = False

    try:
        run_mypy()
    except SubprocessError:
        is_success = False

    if not is_success:
        raise SystemExit(1)


def get_all_python_files() -> list[Path]:
    python_files: list[Path] = [source_root / 'setup.py']

    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:

    run(
        args=('autopep8', '--recursive', '--in-place', *all_python_modules),
        check=True,
    )

    run(
        args=(
            'isort',
            '-m', 'VERTICAL_HANGING_INDENT',
            '--trailing-comma',
            *all_python_modules,
        ),
        check=True,
    )


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()