forked from mlflow/mlflow
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathruff.py
68 lines (59 loc) · 1.95 KB
/
ruff.py
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
import functools
import json
import os
import re
import subprocess
import sys
RUFF = [sys.executable, "-m", "ruff"]
MESSAGE_REGEX = re.compile(r"^.+:\d+:\d+: ([A-Z0-9]+) (\[\*\] )?.+$")
@functools.lru_cache
def get_rule_name(code: str) -> str:
out = subprocess.check_output([*RUFF, "rule", "--format", "json", code], text=True).strip()
return json.loads(out)["name"]
def transform(stdout: str, is_maintainer: bool) -> str:
transformed = []
for line in stdout.splitlines():
if m := MESSAGE_REGEX.match(line):
if m.group(2) is not None:
command = (
"`ruff --fix .` or comment `@mlflow-automation autoformat`"
if is_maintainer
else "`ruff --fix .`"
)
line = f"{line}. Run {command} to fix this error."
else:
name = get_rule_name(m.group(1))
line = (
f"{line}. See https://beta.ruff.rs/docs/rules/{name} for how to fix this error."
)
transformed.append(line)
return "\n".join(transformed)
def main():
if "GITHUB_ACTIONS" in os.environ:
with subprocess.Popen(
[
*RUFF,
*sys.argv[1:],
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
) as prc:
stdout, stderr = prc.communicate()
is_maintainer = os.environ.get("IS_MAINTAINER", "false").lower() == "true"
sys.stdout.write(transform(stdout, is_maintainer))
sys.stderr.write(stderr)
sys.exit(prc.returncode)
else:
with subprocess.Popen(
[
*RUFF,
"--fix",
"--exit-non-zero-on-fix",
*sys.argv[1:],
]
) as prc:
prc.communicate()
sys.exit(prc.returncode)
if __name__ == "__main__":
main()