Skip to content

Commit afb5f66

Browse files
authored
Merge pull request #474 from InfiniTensor/chore/specify-ruff-version
chore: specify ruff version
2 parents 4e3c0ee + 72014f9 commit afb5f66

4 files changed

Lines changed: 64 additions & 6 deletions

File tree

‎.github/workflows/ci_test.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
run: |
2929
sudo apt-get update
3030
sudo apt-get install -y clang-format-16
31-
pip install black ruff colorama
31+
pip install black ruff==0.15.20 colorama
3232
3333
- name: Check Format
3434
run: |

‎.github/workflows/ruff.yml‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: Ruff
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
ruff:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v4
10+
- uses: chartboost/ruff-action@v1
11+
with:
12+
src: "./scripts/format.py"
13+
version: "0.15.20"
14+
- uses: chartboost/ruff-action@v1
15+
with:
16+
src: "./scripts/format.py"
17+
args: format --check
18+
version: "0.15.20"

‎pyproject.toml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,9 @@ classifiers = [
1717

1818
[project.urls]
1919
Homepage = "https://github.com/InfiniTensor/InfiniLM"
20+
21+
[tool.ruff]
22+
src = [".", "src"]
23+
24+
[tool.ruff.lint]
25+
select = ["E4", "E7", "E9", "F", "I"]

‎scripts/format.py‎

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import argparse
2-
import subprocess
32
import os
3+
import subprocess
44
from pathlib import Path
5+
56
from colorama import Fore, Style
67

78
# Supported file types and their corresponding formatter categories
@@ -77,8 +78,9 @@ def format_file(file: Path, check: bool, formatter) -> bool:
7778
check=True,
7879
)
7980
print(f"{Fore.CYAN}Formatted: {file}{Style.RESET_ALL}")
80-
81+
elif formatter == "ruff":
8182
ruff_cmd = ["ruff", "check", file]
83+
ruff_format_cmd = ["ruff", "format", file]
8284
try:
8385
if check:
8486
process = subprocess.run(
@@ -93,6 +95,22 @@ def format_file(file: Path, check: bool, formatter) -> bool:
9395
f"Use {Fore.CYAN}ruff check --fix {file}{Style.RESET_ALL} to fix it."
9496
)
9597
formatted = False
98+
99+
ruff_format_cmd.insert(2, "--check")
100+
process = subprocess.run(
101+
ruff_format_cmd,
102+
capture_output=True,
103+
text=True,
104+
check=False,
105+
)
106+
if process.returncode != 0:
107+
print(
108+
f"{Fore.YELLOW}{file} is not ruff-formatted.{Style.RESET_ALL}"
109+
)
110+
print(
111+
f"Use {Fore.CYAN}ruff format {file}{Style.RESET_ALL} to format it."
112+
)
113+
formatted = False
96114
else:
97115
ruff_cmd.insert(2, "--fix")
98116
process = subprocess.run(
@@ -110,6 +128,24 @@ def format_file(file: Path, check: bool, formatter) -> bool:
110128
formatted = False
111129
else:
112130
print(f"{Fore.CYAN}Ruff fixed: {file}{Style.RESET_ALL}")
131+
132+
process = subprocess.run(
133+
ruff_format_cmd,
134+
capture_output=True,
135+
text=True,
136+
check=False,
137+
)
138+
if process.returncode != 0:
139+
print(
140+
f"{Fore.RED}Ruff format failed for {file}.{Style.RESET_ALL}"
141+
)
142+
if process.stdout:
143+
print(process.stdout, end="")
144+
if process.stderr:
145+
print(process.stderr, end="")
146+
formatted = False
147+
else:
148+
print(f"{Fore.CYAN}Ruff formatted: {file}{Style.RESET_ALL}")
113149
except FileNotFoundError:
114150
print(
115151
f"{Fore.RED}Formatter ruff not found, {file} skipped.{Style.RESET_ALL}"
@@ -216,9 +252,7 @@ def main():
216252
parser.add_argument(
217253
"--c", default="clang-format-16", help="C formatter (default: clang-format-16)"
218254
)
219-
parser.add_argument(
220-
"--py", default="black", help="Python formatter (default: black)"
221-
)
255+
parser.add_argument("--py", default="ruff", help="Python formatter (default: ruff)")
222256
args = parser.parse_args()
223257

224258
if args.ref is None and args.path is None:

0 commit comments

Comments
 (0)