Skip to content

Commit

Permalink
IDX-2719 - Add ruff linter for bazel smoke target
Browse files Browse the repository at this point in the history
  • Loading branch information
Enzo Desiage committed Apr 13, 2023
1 parent 45f3d07 commit ed78439
Show file tree
Hide file tree
Showing 8 changed files with 262 additions and 42 deletions.
42 changes: 0 additions & 42 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -89,48 +89,6 @@ repos:
crypto/internal/crypto_lib/fs_ni_dkg/miracl_core
) # exclude these directories.
- repo: https://github.com/ambv/black
rev: 20.8b1
hooks:
- id: black
additional_dependencies: ['click==8.0.4']
args:
- --line-length=120


- repo: https://github.com/asottile/reorder_python_imports
rev: v2.4.0
hooks:
- id: reorder-python-imports
args:
- --application-directories=.:gitlab-ci/src/

- repo: https://github.com/pycqa/flake8
rev: '3.8.4'
hooks:
- id: flake8
args: ["--config=gitlab-ci/src/setup.cfg"]

- repo: https://github.com/pre-commit/mirrors-mypy
rev: 'v0.961'
hooks:
- id: mypy
entry: "gitlab-ci/tools/run_mypy"
additional_dependencies: ['types-PyYAML==6.0.11', 'types-pytz==2022.2.1.0']
# Print the number of files as a sanity-check
verbose: true

- repo: https://github.com/pycqa/pydocstyle
rev: 5.1.1
hooks:
- id: pydocstyle
args:
- --ignore=D100,D101,D102,D103,D104,D107,D202,D203,D205,D212,D400,D401,D415
# The default for pydocstyle is to match only on files that end in .py,
# even if pre-commit specifies other files. This makes it so Python
# files will be recognized regardless of extension.
- --match=.*

- repo: https://github.com/willthames/ansible-lint.git
rev: v4.2.0
hooks:
Expand Down
5 changes: 5 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ alias(
actual = "//bazel:buildifier",
)

alias(
name = "ruff-format",
actual = "//bazel/pre-commit:ruff-format",
)

test_suite(
name = "precommit",
tests = ["//bazel:buildifier_test"],
Expand Down
5 changes: 5 additions & 0 deletions WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,11 @@ load("//bazel:trivy.bzl", "trivy_scan")

trivy_scan(name = "trivy")

# ruff binary for fast python linting
load("//bazel/pre-commit:ruff.bzl", "ruff")

ruff(name = "ruff")

# ormolu binary for haskell linting
load("//bazel/pre-commit:ormolu.bzl", "ormolu")

Expand Down
40 changes: 40 additions & 0 deletions bazel/pre-commit/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,43 @@
sh_test(
name = "ruff-lint",
size = "small",
srcs = ["ruff-lint.sh"],
data = [
"//:WORKSPACE.bazel",
"@ruff",
],
env = {
# Hack to escape the sandbox and grep on the actual repository
"WORKSPACE": "$(rootpath //:WORKSPACE.bazel)",
"ruff_path": "$(rootpath @ruff//:ruff)",
},
tags = [
"external", # force test to be unconditionally executed.
"local", # precludes the action or test from being remotely cached, remotely executed, or run inside the sandbox.
"smoke", # it should be run before committing code changes into the version control system.
],
)

sh_binary(
name = "ruff-format",
srcs = ["ruff-format.sh"],
data = [
"//:WORKSPACE.bazel",
"@ruff",
],
env = {
# Hack to escape the sandbox and grep on the actual repository
"WORKSPACE": "$(rootpath //:WORKSPACE.bazel)",
"ruff_path": "$(rootpath @ruff//:ruff)",
},
tags = [
"external", # force test to be unconditionally executed.
"local", # precludes the action or test from being remotely cached, remotely executed, or run inside the sandbox.
"smoke", # it should be run before committing code changes into the version control system.
],
visibility = ["//visibility:public"],
)

sh_test(
name = "buf",
size = "small",
Expand Down
9 changes: 9 additions & 0 deletions bazel/pre-commit/ruff-format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

set -euxo pipefail

RUFF_PATH="$(readlink "$ruff_path")"
REPO_PATH="$(dirname "$(readlink "$WORKSPACE")")"
cd "$REPO_PATH"

"$RUFF_PATH" check . --fix
17 changes: 17 additions & 0 deletions bazel/pre-commit/ruff-lint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bash

set -euo pipefail

RUFF_PATH="$(readlink "$ruff_path")"
REPO_PATH="$(dirname "$(readlink "$WORKSPACE")")"
cd "$REPO_PATH"

if ! "$RUFF_PATH" check . -q; then
cat >&2 <<EOF
[-] Linting Python files failed
Please run the following command to fix it:
$ bazel run //:ruff-format
EOF
exit 1
fi
51 changes: 51 additions & 0 deletions bazel/pre-commit/ruff.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""
The module fetches ruff binary to be used by bazel smoke
"""

RUFF_BUILD = """
package(default_visibility = ["//visibility:public"])
exports_files(["ruff"])
"""

VERSION = "0.0.260"
SHA256 = {
"aarch64-apple-darwin": "4e045df5e55f1e23b34910865fe66c8e9d4ea98dbdb5320fc8ff09b8c337d69e",
"x86_64-apple-darwin": "3b251413bd5dfa60997489b33024b5f596cb3781f5cf3763529fb24cd97059c0",
"x86_64-unknown-linux-gnu": "abb106ee7d1434faa733e6dd442b1d306fa32e0840fde24fbbf96c2289968c63",
}

URL = "https://github.com/charliermarsh/ruff/releases/download/v{version}/ruff-{platform}.tar.gz"

def _ruff_impl(repository_ctx):
os_arch = repository_ctx.os.arch

if os_arch == "x86_64" or os_arch == "amd64":
arch = "x86_64"
elif os_arch == "aarch64":
arch = "aarch64"
else:
fail("Unsupported architecture: '" + os_arch + "'")

os_name = repository_ctx.os.name
if os_name == "linux":
platform = arch + "-unknown-linux-gnu"
elif os_name == "mac os x":
platform = arch + "-apple-darwin"

else:
fail("Unsupported operating system: " + os_name)

if platform not in SHA256:
fail("Unsupported platform: '" + platform + "'")

repository_ctx.report_progress("Fetching " + repository_ctx.name)
repository_ctx.download_and_extract(url = URL.format(version = VERSION, platform = platform), sha256 = SHA256[platform])
repository_ctx.file("BUILD.bazel", RUFF_BUILD, executable = True)

_ruff = repository_rule(
implementation = _ruff_impl,
attrs = {},
)

def ruff(name):
_ruff(name = name)
135 changes: 135 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
[tool.ruff]
unfixable = []

select = [
"C9", # Mccabe
"F", # Pyflakes
"I", # Isort
"N", # Pep-8 naming
"D", # Pydocstyle
"E", # Pydocstyle error
"W", # Pydocstyle warning
]

# Allow autofix for all enabled rules (when `--fix`) is provided.
fixable = [
"A",
"B",
"C",
"D",
"E",
"F",
"G",
"I",
"N",
"Q",
"S",
"T",
"W",
"ANN",
"ARG",
"BLE",
"COM",
"DJ",
"DTZ",
"EM",
"ERA",
"EXE",
"FBT",
"ICN",
"INP",
"ISC",
"NPY",
"PD",
"PGH",
"PIE",
"PL",
"PT",
"PTH",
"PYI",
"RET",
"RSE",
"RUF",
"SIM",
"SLF",
"TCH",
"TID",
"TRY",
"UP",
"YTT",
]

# Exclude a variety of commonly ignored directories.
exclude = [
".bzr",
".direnv",
".eggs",
".git",
".hg",
".mypy_cache",
".nox",
".pants.d",
".pytype",
".ruff_cache",
".svn",
".tox",
".venv",
"__pypackages__",
"_build",
"buck-out",
"build",
"dist",
"node_modules",
"venv",
"env",
]

line-length = 120

# Allow unused variables when underscore-prefixed.
dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"

target-version = "py37"

ignore = [
"C901",
"D10",
"D100",
"D101",
"D102",
"D103",
"D104",
"D107",
"D202",
"D203",
"D205",
"D212",
"D400",
"D401",
"D407",
"D415",
"D416",
"D417",
"E402",
"E501",
"E501",
"E713",
"F403",
"I001",
"N801",
"N802",
"N803",
"N804",
"N805",
"N806",
"N818",
"N999",
]

[tool.ruff.mccabe]
# Unlike Flake8, default to a complexity level of 10.
max-complexity = 10

[tool.isort]
profile = "black"
remove_redundant_aliases = true

0 comments on commit ed78439

Please sign in to comment.