build: add Python code formatting with black

NOTE: this will fail tests as of this commit, as the code is not
formatted yet.

We didn't explicitly specify a minimum Python version before that, so I
used v3.7 here, because it's the minimal version Ruff - a Python linter
I will add in the next commit - supports.
This commit is contained in:
Yaroslav Chvanov
2023-03-01 16:14:58 +03:00
parent bc9fa92667
commit c723490fc3
6 changed files with 45 additions and 1 deletions

View File

@@ -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
PIP_PACKAGES: meson ninja libevdev pyudev pytest sphinx_rtd_theme black
jobs:
build-and-dist:

View File

@@ -556,6 +556,8 @@ configure_file(input : 'tools/ratbagctl.test.py.in',
env_test = environment()
env_test.set('LIBRATBAG_DATA_DIR', libratbag_data_dir_devel)
# See: https://github.com/mesonbuild/meson/issues/3306.
env_test.set('MESON_SOURCE_ROOT', meson.source_root())
ratbagctl_test = find_program(join_paths(meson.build_root(), 'ratbagctl.test'))
test('ratbagctl-test', ratbagctl_test, args: ['-v'], env : env_test)
@@ -630,4 +632,21 @@ custom_target('ratbag-command',
#### output files ####
configure_file(output: 'config.h', install: false, configuration: config_h)
#### code formatting #####
run_target(
'python-black',
command : 'tools/python-black.sh',
)
python_black_check = find_program(join_paths(meson.source_root(), 'test/python-black-check.sh'))
run_target(
'python-black-check',
command : python_black_check,
)
test(
'python-black-check',
python_black_check,
env: env_test,
)
subdir('doc')

2
pyproject.toml Normal file
View File

@@ -0,0 +1,2 @@
[tool.black]
target-version = ["py37"]

10
test/python-black-check.sh Executable file
View File

@@ -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
black --check $(git ls-files "*.py" "*.py.in")

10
tools/python-black.sh Executable file
View File

@@ -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
black $(git ls-files "*.py" "*.py.in")

View File

@@ -26,7 +26,10 @@ import sys
import argparse
import re
# This must be on a single line, as we replace it using merge_ratbagd.py while building.
# fmt: off
from ratbagd import Ratbagd, RatbagdDevice, RatbagdProfile, RatbagdMacro, RatbagdResolution, RatbagdButton, RatbagdLed, RatbagdUnavailable, RatbagError, RatbagErrorCode, RatbagErrorCapability, RatbagDeviceType # NOQA
# fmt: on
RATBAGD_API_VERSION = int("@RATBAGD_API_VERSION@")