Skip to content

Commit cb8baea

Browse files
authored
[py] Replace //py:ruff with dedicated ruff-format and ruff-check targets (#16998)
* [py] Add mode support to ruff runner and separate format/lint tasks * split up //py:ruff into //py:ruff-check and //py:ruff-format * [py] Add known-first-party config for isort to preserve import grouping
1 parent 5f703b2 commit cb8baea

File tree

9 files changed

+102
-29
lines changed

9 files changed

+102
-29
lines changed

.github/workflows/ci-python.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ jobs:
4848
uses: ./.github/workflows/bazel.yml
4949
with:
5050
name: Lint
51-
run: bazel run //py:ruff -- --check
51+
run: bazel run //py:ruff-check
5252

5353
unit-tests:
5454
name: Unit Tests

py/BUILD.bazel

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,11 @@ py_binary(
668668
)
669669

670670
alias(
671-
name = "ruff",
672-
actual = "//py/private:ruff",
671+
name = "ruff-check",
672+
actual = "//py/private:ruff_check",
673+
)
674+
675+
alias(
676+
name = "ruff-format",
677+
actual = "//py/private:ruff_format",
673678
)

py/private/BUILD.bazel

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,19 @@ py_binary(
1010
)
1111

1212
py_binary(
13-
name = "ruff",
14-
srcs = ["ruff.py"],
13+
name = "ruff_check",
14+
srcs = ["ruff_check.py"],
15+
data = [
16+
"//py:pyproject.toml",
17+
"@multitool//tools/ruff",
18+
],
19+
visibility = ["//visibility:public"],
20+
deps = ["@rules_python//python/runfiles"],
21+
)
22+
23+
py_binary(
24+
name = "ruff_format",
25+
srcs = ["ruff_format.py"],
1526
data = [
1627
"//py:pyproject.toml",
1728
"@multitool//tools/ruff",
Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,36 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18-
"""Run ruff linter on Python files outside py/ directory."""
18+
"""Run ruff check on Python files across the project.
19+
20+
Usage:
21+
bazel run //py:ruff-check -- [ruff check args]
22+
"""
1923

2024
import os
2125
import subprocess
2226
import sys
2327

2428
from python.runfiles import Runfiles
2529

26-
LINT_DIRS = ["scripts", "common", "dotnet", "java", "javascript", "rb"]
30+
ALL_DIRS = ["py", "scripts", "common", "dotnet", "java", "javascript", "rb"]
2731
EXCLUDES = ["**/node_modules/**", "**/.bundle/**"]
2832

2933

34+
def run_check(ruff, exclude_args, dirs, extra_args):
35+
"""Run ruff check (linting)."""
36+
cmd = [ruff, "check", "--config=py/pyproject.toml"]
37+
return subprocess.run(cmd + exclude_args + dirs + extra_args).returncode
38+
39+
3040
if __name__ == "__main__":
3141
r = Runfiles.Create()
3242
ruff = r.Rlocation("rules_multitool++multitool+multitool/tools/ruff/ruff")
3343

3444
os.chdir(os.environ["BUILD_WORKSPACE_DIRECTORY"])
3545

36-
# Check if --check flag is passed (for CI - verify without fixing)
37-
check_only = "--check" in sys.argv
38-
extra_args = [arg for arg in sys.argv[1:] if arg != "--check"]
39-
4046
exclude_args = []
4147
for pattern in EXCLUDES:
4248
exclude_args.extend(["--exclude", pattern])
4349

44-
check_cmd = [ruff, "check", "--config=py/pyproject.toml"]
45-
if not check_only:
46-
check_cmd.extend(["--fix", "--show-fixes"])
47-
check_result = subprocess.run(check_cmd + exclude_args + LINT_DIRS + extra_args)
48-
49-
format_cmd = [ruff, "format", "--config=py/pyproject.toml"]
50-
if check_only:
51-
format_cmd.append("--check")
52-
format_result = subprocess.run(format_cmd + exclude_args + LINT_DIRS)
53-
54-
sys.exit(check_result.returncode or format_result.returncode)
50+
sys.exit(run_check(ruff, exclude_args, ALL_DIRS, sys.argv[1:]))

py/private/ruff_format.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Licensed to the Software Freedom Conservancy (SFC) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The SFC licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
"""Run ruff format on Python files across the project.
19+
20+
Usage:
21+
bazel run //py:ruff-format -- [ruff format args]
22+
"""
23+
24+
import os
25+
import subprocess
26+
import sys
27+
28+
from python.runfiles import Runfiles
29+
30+
ALL_DIRS = ["py", "scripts", "common", "dotnet", "java", "javascript", "rb"]
31+
EXCLUDES = ["**/node_modules/**", "**/.bundle/**"]
32+
33+
34+
def run_format(ruff, exclude_args, dirs, extra_args):
35+
"""Run ruff format."""
36+
cmd = [ruff, "format", "--config=py/pyproject.toml"]
37+
return subprocess.run(cmd + exclude_args + dirs + extra_args).returncode
38+
39+
40+
if __name__ == "__main__":
41+
r = Runfiles.Create()
42+
ruff = r.Rlocation("rules_multitool++multitool+multitool/tools/ruff/ruff")
43+
44+
os.chdir(os.environ["BUILD_WORKSPACE_DIRECTORY"])
45+
46+
exclude_args = []
47+
for pattern in EXCLUDES:
48+
exclude_args.extend(["--exclude", pattern])
49+
50+
sys.exit(run_format(ruff, exclude_args, ALL_DIRS, sys.argv[1:]))

py/pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,5 +167,8 @@ extend-ignore = [
167167
docstring-code-format = true
168168
docstring-code-line-length = 120
169169

170+
[tool.ruff.lint.isort]
171+
known-first-party = ["selenium", "test"]
172+
170173
[tool.ruff.lint.pydocstyle]
171174
convention = "google"

rake_tasks/python.rake

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,19 @@ task :version, [:version] do |_task, arguments|
151151
File.open(conf, 'w') { |f| f.puts text }
152152
end
153153

154-
desc 'Run Python linter (ruff check + format)'
154+
desc 'Run Python formatter (ruff format)'
155+
task :format do |_task, arguments|
156+
puts ' Running ruff format...'
157+
Bazel.execute('run', arguments.to_a, '//py:ruff-format')
158+
end
159+
160+
desc 'Run Python linter (ruff check + format + mypy)'
155161
task :lint do |_task, arguments|
156-
args = arguments.to_a
162+
raise ArgumentError, 'arguments not supported in this task' unless arguments.to_a.empty?
163+
164+
Rake::Task['py:format'].invoke
157165
puts ' Running ruff check...'
158-
Bazel.execute('run', args + ['--', 'check', '--fix', 'py/'], '@multitool//tools/ruff:cwd')
159-
puts ' Running ruff format...'
160-
Bazel.execute('run', args + ['--', 'format', 'py/'], '@multitool//tools/ruff:cwd')
166+
Bazel.execute('run', %w[-- --fix --show-fixes], '//py:ruff-check')
167+
puts ' Running mypy...'
168+
Bazel.execute('run', [], '//py:mypy')
161169
end

scripts/format.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ bazel run @rules_rust//:rustfmt
3535

3636
section "Python"
3737
Write-Host " python - ruff" -ForegroundColor Green
38-
bazel run //py:ruff
38+
bazel run //py:ruff-format
3939

4040
section "Copyright"
4141
bazel run //scripts:update_copyright

scripts/format.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ bazel run @rules_rust//:rustfmt
3737

3838
section "Python"
3939
echo " python - ruff" >&2
40-
bazel run //py:ruff
40+
bazel run //py:ruff-format
4141

4242
section "Copyright"
4343
bazel run //scripts:update_copyright

0 commit comments

Comments
 (0)