File: run_checks_and_format.py

package info (click to toggle)
qtsass 0.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 344 kB
  • sloc: python: 1,337; sh: 7; makefile: 2
file content (78 lines) | stat: -rw-r--r-- 1,772 bytes parent folder | download
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
# Copyright (c) 2015 Yann Lanthony
# Copyright (c) 2017-2018 Spyder Project Contributors
#
# Licensed under the terms of the MIT License
# (See LICENSE.txt for details)
# -----------------------------------------------------------------------------
"""Run checks and format code."""

# yapf: disable

# Standard library imports
from subprocess import PIPE, Popen
import sys


# yapf: enable

# Constants
COMMANDS = [
    ['pydocstyle', 'qtsass'],
    ['pycodestyle', 'qtsass'],
    ['yapf', 'qtsass', '--in-place', '--recursive'],
    ['isort', '-y'],
]


def run_process(cmd_list):
    """Run popen process."""

    try:
        p = Popen(cmd_list, stdout=PIPE, stderr=PIPE)
    except OSError:
        raise OSError('Could not call command list: "%s"' % cmd_list)

    out, err = p.communicate()
    out = out.decode()
    err = err.decode()
    return out, err


def repo_changes():
    """Check if repo files changed."""
    out, _err = run_process(['git', 'status', '--short'])
    out_lines = [l for l in out.split('\n') if l.strip()]
    return out_lines


def run():
    """Run linters and formatters."""

    for cmd_list in COMMANDS:
        cmd_str = ' '.join(cmd_list)
        print('\nRunning: ' + cmd_str)

        out, err = run_process(cmd_list)
        if out:
            print(out)
        if err:
            print(err)

    out_lines = repo_changes()
    if out_lines:
        print('\nPlease run the linter and formatter script!')
        print('\n'.join(out_lines))
        code = 1
    else:
        print('\nAll checks passed!')
        code = 0

    print('\n')
    sys.exit(code)


if __name__ == '__main__':
    run()