forked from esphome/esphome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlint-python
executable file
·131 lines (111 loc) · 3.24 KB
/
lint-python
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
#!/usr/bin/env python3
from helpers import (
styled,
print_error_for_file,
get_output,
get_err,
git_ls_files,
filter_changed,
)
import argparse
import colorama
import os
import re
import sys
curfile = None
def print_error(file, lineno, msg):
global curfile
if curfile != file:
print_error_for_file(file, None)
curfile = file
if lineno is not None:
print(f"{styled(colorama.Style.BRIGHT, f'{file}:{lineno}:')} {msg}")
else:
print(f"{styled(colorama.Style.BRIGHT, f'{file}:')} {msg}")
def main():
colorama.init()
parser = argparse.ArgumentParser()
parser.add_argument(
"files", nargs="*", default=[], help="files to be processed (regex on path)"
)
parser.add_argument(
"-c", "--changed", action="store_true", help="Only run on changed files"
)
parser.add_argument(
"-a",
"--apply",
action="store_true",
help="Apply changes to files where possible",
)
args = parser.parse_args()
files = []
for path in git_ls_files():
filetypes = (".py",)
ext = os.path.splitext(path)[1]
if ext in filetypes and path.startswith("esphome"):
path = os.path.relpath(path, os.getcwd())
files.append(path)
# Match against re
file_name_re = re.compile("|".join(args.files))
files = [p for p in files if file_name_re.search(p)]
if args.changed:
files = filter_changed(files)
files.sort()
if not files:
sys.exit(0)
errors = 0
cmd = ["black", "--verbose"] + ([] if args.apply else ["--check"]) + files
print("Running black...")
print()
log = get_err(*cmd)
for line in log.splitlines():
WOULD_REFORMAT = "would reformat"
if line.startswith(WOULD_REFORMAT):
file_ = line[len(WOULD_REFORMAT) + 1 :]
print_error(file_, None, "Please format this file with the black formatter")
errors += 1
cmd = ["flake8"] + files
print()
print("Running flake8...")
print()
log = get_output(*cmd)
for line in log.splitlines():
line = line.split(":", 4)
if len(line) < 4:
continue
file_ = line[0]
linno = line[1]
msg = (":".join(line[3:])).strip()
print_error(file_, linno, msg)
errors += 1
cmd = ["pylint", "-f", "parseable", "--persistent=n"] + files
print()
print("Running pylint...")
print()
log = get_output(*cmd)
for line in log.splitlines():
line = line.split(":", 3)
if len(line) < 3:
continue
file_ = line[0]
linno = line[1]
msg = (":".join(line[2:])).strip()
print_error(file_, linno, msg)
errors += 1
PYUPGRADE_TARGET = "--py39-plus"
cmd = ["pyupgrade", PYUPGRADE_TARGET] + files
print()
print("Running pyupgrade...")
print()
log = get_err(*cmd)
for line in log.splitlines():
REWRITING = "Rewriting"
if line.startswith(REWRITING):
file_ = line[len(REWRITING) + 1 :]
print_error(
file_, None, f"Please run pyupgrade {PYUPGRADE_TARGET} on this file"
)
errors += 1
sys.exit(errors)
if __name__ == "__main__":
main()