forked from mlflow/mlflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruff.py
More file actions
56 lines (49 loc) · 1.65 KB
/
Copy pathruff.py
File metadata and controls
56 lines (49 loc) · 1.65 KB
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
import os
import re
import subprocess
import sys
RUFF = [sys.executable, "-m", "ruff", "check", "--output-format=concise"]
MESSAGE_REGEX = re.compile(r"^.+:\d+:\d+: ([A-Z0-9]+) (\[\*\] )?.+$")
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 `/autoformat`" if is_maintainer else "`ruff --fix .`"
)
line = f"{line}. Run {command} to fix this error."
else:
line = (
f"{line}. See https://docs.astral.sh/ruff/rules/{m.group(1)} for how to fix "
f"this error."
)
transformed.append(line)
return "\n".join(transformed)
def main() -> None:
if "NO_FIX" 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()