Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci-python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
uses: ./.github/workflows/bazel.yml
with:
name: Lint
run: bazel run //py:ruff -- --check
run: bazel run //py:ruff-check

unit-tests:
name: Unit Tests
Expand Down
9 changes: 7 additions & 2 deletions py/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,11 @@ py_binary(
)

alias(
name = "ruff",
actual = "//py/private:ruff",
name = "ruff-check",
actual = "//py/private:ruff_check",
)

alias(
name = "ruff-format",
actual = "//py/private:ruff_format",
)
15 changes: 13 additions & 2 deletions py/private/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,19 @@ py_binary(
)

py_binary(
name = "ruff",
srcs = ["ruff.py"],
name = "ruff_check",
srcs = ["ruff_check.py"],
data = [
"//py:pyproject.toml",
"@multitool//tools/ruff",
],
visibility = ["//visibility:public"],
deps = ["@rules_python//python/runfiles"],
)

py_binary(
name = "ruff_format",
srcs = ["ruff_format.py"],
data = [
"//py:pyproject.toml",
"@multitool//tools/ruff",
Expand Down
30 changes: 13 additions & 17 deletions py/private/ruff.py → py/private/ruff_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,40 +15,36 @@
# specific language governing permissions and limitations
# under the License.

"""Run ruff linter on Python files outside py/ directory."""
"""Run ruff check on Python files across the project.

Usage:
bazel run //py:ruff-check -- [ruff check args]
"""

import os
import subprocess
import sys

from python.runfiles import Runfiles

LINT_DIRS = ["scripts", "common", "dotnet", "java", "javascript", "rb"]
ALL_DIRS = ["py", "scripts", "common", "dotnet", "java", "javascript", "rb"]
EXCLUDES = ["**/node_modules/**", "**/.bundle/**"]


def run_check(ruff, exclude_args, dirs, extra_args):
"""Run ruff check (linting)."""
cmd = [ruff, "check", "--config=py/pyproject.toml"]
return subprocess.run(cmd + exclude_args + dirs + extra_args).returncode


if __name__ == "__main__":
r = Runfiles.Create()
ruff = r.Rlocation("rules_multitool++multitool+multitool/tools/ruff/ruff")

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

# Check if --check flag is passed (for CI - verify without fixing)
check_only = "--check" in sys.argv
extra_args = [arg for arg in sys.argv[1:] if arg != "--check"]

exclude_args = []
for pattern in EXCLUDES:
exclude_args.extend(["--exclude", pattern])

check_cmd = [ruff, "check", "--config=py/pyproject.toml"]
if not check_only:
check_cmd.extend(["--fix", "--show-fixes"])
check_result = subprocess.run(check_cmd + exclude_args + LINT_DIRS + extra_args)

format_cmd = [ruff, "format", "--config=py/pyproject.toml"]
if check_only:
format_cmd.append("--check")
format_result = subprocess.run(format_cmd + exclude_args + LINT_DIRS)

sys.exit(check_result.returncode or format_result.returncode)
sys.exit(run_check(ruff, exclude_args, ALL_DIRS, sys.argv[1:]))
50 changes: 50 additions & 0 deletions py/private/ruff_format.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Licensed to the Software Freedom Conservancy (SFC) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The SFC licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

"""Run ruff format on Python files across the project.

Usage:
bazel run //py:ruff-format -- [ruff format args]
"""

import os
import subprocess
import sys

from python.runfiles import Runfiles

ALL_DIRS = ["py", "scripts", "common", "dotnet", "java", "javascript", "rb"]
EXCLUDES = ["**/node_modules/**", "**/.bundle/**"]


def run_format(ruff, exclude_args, dirs, extra_args):
"""Run ruff format."""
cmd = [ruff, "format", "--config=py/pyproject.toml"]
return subprocess.run(cmd + exclude_args + dirs + extra_args).returncode


if __name__ == "__main__":
r = Runfiles.Create()
ruff = r.Rlocation("rules_multitool++multitool+multitool/tools/ruff/ruff")

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

exclude_args = []
for pattern in EXCLUDES:
exclude_args.extend(["--exclude", pattern])

sys.exit(run_format(ruff, exclude_args, ALL_DIRS, sys.argv[1:]))
3 changes: 3 additions & 0 deletions py/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -167,5 +167,8 @@ extend-ignore = [
docstring-code-format = true
docstring-code-line-length = 120

[tool.ruff.lint.isort]
known-first-party = ["selenium", "test"]

[tool.ruff.lint.pydocstyle]
convention = "google"
18 changes: 13 additions & 5 deletions rake_tasks/python.rake
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,19 @@ task :version, [:version] do |_task, arguments|
File.open(conf, 'w') { |f| f.puts text }
end

desc 'Run Python linter (ruff check + format)'
desc 'Run Python formatter (ruff format)'
task :format do |_task, arguments|
puts ' Running ruff format...'
Bazel.execute('run', arguments.to_a, '//py:ruff-format')
end

desc 'Run Python linter (ruff check + format + mypy)'
task :lint do |_task, arguments|
args = arguments.to_a
raise ArgumentError, 'arguments not supported in this task' unless arguments.to_a.empty?

Rake::Task['py:format'].invoke
puts ' Running ruff check...'
Bazel.execute('run', args + ['--', 'check', '--fix', 'py/'], '@multitool//tools/ruff:cwd')
puts ' Running ruff format...'
Bazel.execute('run', args + ['--', 'format', 'py/'], '@multitool//tools/ruff:cwd')
Bazel.execute('run', %w[-- --fix --show-fixes], '//py:ruff-check')
puts ' Running mypy...'
Bazel.execute('run', [], '//py:mypy')
end
2 changes: 1 addition & 1 deletion scripts/format.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ bazel run @rules_rust//:rustfmt

section "Python"
Write-Host " python - ruff" -ForegroundColor Green
bazel run //py:ruff
bazel run //py:ruff-format

section "Copyright"
bazel run //scripts:update_copyright
2 changes: 1 addition & 1 deletion scripts/format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ bazel run @rules_rust//:rustfmt

section "Python"
echo " python - ruff" >&2
bazel run //py:ruff
bazel run //py:ruff-format

section "Copyright"
bazel run //scripts:update_copyright