From 3aa82d2b35b3010f726079ab11312016b225bda0 Mon Sep 17 00:00:00 2001 From: Yaroslav Chvanov Date: Wed, 1 Mar 2023 17:08:53 +0300 Subject: [PATCH] build: add Ruff for Python code linting --- .github/workflows/main.yml | 2 +- meson.build | 17 +++++++++++++++++ pyproject.toml | 21 +++++++++++++++++++++ test/python-ruff-check.sh | 10 ++++++++++ tools/python-ruff.sh | 10 ++++++++++ 5 files changed, 59 insertions(+), 1 deletion(-) create mode 100755 test/python-ruff-check.sh create mode 100755 tools/python-ruff.sh diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index eefc7bda..a9c842c8 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -8,7 +8,7 @@ env: libjson-glib-dev libsystemd-dev libudev-dev libunistring-dev python3-dev python3-evdev swig python3-sphinx UBUNTU_DEP_TEST: check python3-gi python3-lxml valgrind - PIP_PACKAGES: meson ninja libevdev pyudev pytest sphinx_rtd_theme black + PIP_PACKAGES: meson ninja libevdev pyudev pytest sphinx_rtd_theme black ruff jobs: build-and-dist: diff --git a/meson.build b/meson.build index e021e50a..cf2209f0 100644 --- a/meson.build +++ b/meson.build @@ -649,4 +649,21 @@ test( env: env_test, ) +#### code linting ##### +run_target( + 'python-ruff', + command : 'tools/python-ruff.sh', +) + +python_ruff_check = find_program(join_paths(meson.source_root(), 'test/python-ruff-check.sh')) +run_target( + 'python-ruff-check', + command : python_ruff_check, +) +test( + 'python-ruff-check', + python_ruff_check, + env: env_test, +) + subdir('doc') diff --git a/pyproject.toml b/pyproject.toml index 5a8e741b..2362fa25 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,2 +1,23 @@ [tool.black] target-version = ["py37"] + +[tool.ruff] +target-version = "py37" +select = [ + # pycodestyle + "E", + "W", + # pyflakes + "F", + # pyupgrade + # Use modern Python features for the best possible develop experience. + "UP", + # flake8-bugbear + # Find obvious bugs. + "B", +] +ignore = [ + # Line too long. + # We have some places where it's just impossible to make it fit, + "E501", +] diff --git a/test/python-ruff-check.sh b/test/python-ruff-check.sh new file mode 100755 index 00000000..f4c7ad16 --- /dev/null +++ b/test/python-ruff-check.sh @@ -0,0 +1,10 @@ +#!/bin/sh + +if [ -z "$MESON_SOURCE_ROOT" ]; then + echo "Expected \$MESON_SOURCE_ROOT to be set. Are you sure you are running this through ninja?" + exit 100 +fi + +cd "$MESON_SOURCE_ROOT" || exit 101 + +ruff check $(git ls-files "*.py" "*.py.in") diff --git a/tools/python-ruff.sh b/tools/python-ruff.sh new file mode 100755 index 00000000..98de940f --- /dev/null +++ b/tools/python-ruff.sh @@ -0,0 +1,10 @@ +#!/bin/sh + +if [ -z "$MESON_SOURCE_ROOT" ]; then + echo "Expected \$MESON_SOURCE_ROOT to be set. Are you sure you are running this through ninja?" + exit 100 +fi + +cd "$MESON_SOURCE_ROOT" || exit 101 + +ruff check --fix $(git ls-files "*.py" "*.py.in")