1
|
|
|
#!/usr/bin/env python |
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
# ----------------------------------------------------------------------------- |
4
|
|
|
# Copyright (c) 2015 Yann Lanthony |
5
|
|
|
# Copyright (c) 2017-2018 Spyder Project Contributors |
6
|
|
|
# |
7
|
|
|
# Licensed under the terms of the MIT License |
8
|
|
|
# (See LICENSE.txt for details) |
9
|
|
|
# ----------------------------------------------------------------------------- |
10
|
|
|
"""Run checks and format code.""" |
11
|
|
|
|
12
|
|
|
# yapf: disable |
13
|
|
|
|
14
|
|
|
# Standard library imports |
15
|
|
|
from subprocess import PIPE, Popen |
16
|
|
|
import os |
17
|
|
|
import sys |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
# yapf: enable |
21
|
|
|
|
22
|
|
|
# Constants |
23
|
|
|
PY3 = sys.version[0] == '3' |
24
|
|
|
COMMANDS = [ |
25
|
|
|
['pydocstyle', 'qtsass'], |
26
|
|
|
['pycodestyle', 'qtsass'], |
27
|
|
|
['yapf', 'qtsass', '--in-place', '--recursive'], |
28
|
|
|
['isort', '-y'], |
29
|
|
|
] |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
def run_process(cmd_list): |
33
|
|
|
"""Run popen process.""" |
34
|
|
|
|
35
|
|
|
try: |
36
|
|
|
p = Popen(cmd_list, stdout=PIPE, stderr=PIPE) |
37
|
|
|
except OSError: |
38
|
|
|
raise OSError('Could not call command list: "%s"' % cmd_list) |
39
|
|
|
|
40
|
|
|
out, err = p.communicate() |
41
|
|
|
if PY3: |
42
|
|
|
out = out.decode() |
43
|
|
|
err = err.decode() |
44
|
|
|
return out, err |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
def repo_changes(): |
48
|
|
|
"""Check if repo files changed.""" |
49
|
|
|
out, _err = run_process(['git', 'status', '--short']) |
50
|
|
|
out_lines = [l for l in out.split('\n') if l.strip()] |
51
|
|
|
return out_lines |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
def run(): |
55
|
|
|
"""Run linters and formatters.""" |
56
|
|
|
|
57
|
|
|
for cmd_list in COMMANDS: |
58
|
|
|
cmd_str = ' '.join(cmd_list) |
59
|
|
|
print('\nRunning: ' + cmd_str) |
60
|
|
|
|
61
|
|
|
out, err = run_process(cmd_list) |
62
|
|
|
if out: |
63
|
|
|
print(out) |
64
|
|
|
if err: |
65
|
|
|
print(err) |
66
|
|
|
|
67
|
|
|
out_lines = repo_changes() |
68
|
|
|
if out_lines: |
69
|
|
|
print('\nPlease run the linter and formatter script!') |
70
|
|
|
print('\n'.join(out_lines)) |
71
|
|
|
code = 1 |
72
|
|
|
else: |
73
|
|
|
print('\nAll checks passed!') |
74
|
|
|
code = 0 |
75
|
|
|
|
76
|
|
|
print('\n') |
77
|
|
|
sys.exit(code) |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
if __name__ == '__main__': |
81
|
|
|
run() |
82
|
|
|
|