-
Notifications
You must be signed in to change notification settings - Fork 3
/
vcs-diff-lint-csdiff-ruff
executable file
·64 lines (51 loc) · 1.66 KB
/
vcs-diff-lint-csdiff-ruff
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
#! /usr/bin/python3
"""
The csdiff tool doesn't support the Ruff's JSON output yet. So this just a
trivial wrapper which reads Ruff's report and transforms it to JSON which is
supported by csdiff.
The script accepts the same parameters as `ruff check` itself.
"""
import os
import sys
import json
from subprocess import Popen, PIPE
def ruff_check():
"""
Run `ruff check` and return a dict with its results
"""
cmd = ["ruff", "check", "--output-format=json"] + sys.argv[1:]
with Popen(cmd, stdout=PIPE) as proc:
out, _err = proc.communicate(timeout=60)
return json.loads(out)
def ruff_code_to_name():
"""
Introspect ruff and map all possible noqa codes to a human-readable names.
"""
cmd = ["ruff", "rule", "--all", "--output-format=json"]
with Popen(cmd, stdout=PIPE) as proc:
out, _err = proc.communicate(timeout=60)
return {i["code"]: i["name"] for i in json.loads(out)}
def main():
"""
The main fuction
"""
defects = ruff_check()
code_to_name_map = ruff_code_to_name()
for defect in defects:
path = os.path.relpath(defect["filename"])
column = defect["location"]["column"] or ""
colsep = ":" if column else ""
event = "{0}[{1}]".format(
defect["code"], code_to_name_map[defect["code"]])
print("Error: RUFF_WARNING:")
print("{file}:{line}{colsep}{column}: {event}: {msg}".format(
file=path,
line=defect["location"]["row"],
colsep=colsep,
column=column,
event=event,
msg=defect["message"],
))
print()
if __name__ == "__main__":
sys.exit(main())