]> xenbits.xensource.com Git - libvirt.git/commitdiff
build-aux: introduce 'black' tool for python formatting
authorDaniel P. Berrangé <berrange@redhat.com>
Wed, 1 Feb 2023 16:19:16 +0000 (11:19 -0500)
committerDaniel P. Berrangé <berrange@redhat.com>
Fri, 3 Nov 2023 18:06:35 +0000 (14:06 -0400)
The 'black' tool is intended to be an opinionated formatting
tool for python code. It is complementary to flake8 which
validates coding bad practices, but (mostly) ignores code
layout issues.

Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
build-aux/Makefile.in
build-aux/meson.build
build-aux/syntax-check.mk
meson.build

index 7ee4680847a17f8340cc40f202d9d3c83ded464d..db44ee36a42d3ab2e97f8fd558ac89e3f83ecdde 100644 (file)
@@ -3,6 +3,7 @@
 top_srcdir = @top_srcdir@
 top_builddir = @top_builddir@
 FLAKE8 = @flake8_path@
+BLACK = @black_path@
 RUNUTF8 = @runutf8@
 PYTHON = @PYTHON3@
 GREP = @GREP@
index f96d46c4454a4eec1a8baef8e19c33ce61e5e4f6..06be6ab6c8a94df57b2bbca454e646adcab6e5d7 100644 (file)
@@ -5,6 +5,10 @@ if git and tests_enabled[0]
   if flake8_prog.found()
     flake8_path = flake8_prog.full_path()
   endif
+  black_path = ''
+  if black_prog.found()
+    black_path = black_prog.full_path()
+  endif
 
   if host_machine.system() == 'freebsd' or host_machine.system() == 'darwin'
     make_prog = find_program('gmake')
@@ -35,6 +39,7 @@ if git and tests_enabled[0]
     'top_srcdir': meson.project_source_root(),
     'top_builddir': meson.project_build_root(),
     'flake8_path': flake8_path,
+    'black_path': black_path,
     'runutf8': ' '.join(runutf8),
     'PYTHON3': python3_prog.full_path(),
     'GREP': grep_prog.full_path(),
index 070737842ee0aa105230ca1483393657bc842aae..721e82f49ad6aed9f891ba41305987b327d75282 100644 (file)
@@ -608,6 +608,16 @@ sc_flake8:
                echo 'skipping test $@: flake8 not installed' 1>&2; \
        fi
 
+sc_black:
+       if [ -n "$(BLACK)" ]; then \
+               DOT_PY=$$($(VC_LIST_EXCEPT) | $(GREP) '\.py$$'); \
+               BANG_PY=$$($(VC_LIST_EXCEPT) | xargs grep -l '^#!/usr/bin/env python3$$'); \
+               ALL_PY=$$(printf "%s\n%s" "$$DOT_PY" "$$BANG_PY" | sort -u); \
+               echo "$$ALL_PY" | xargs --no-run-if-empty $(BLACK) --check; \
+       else \
+               echo 'skipping test $@: black not installed' 1>&2; \
+       fi
+
 # mymain() in test files should use return, not exit, for nicer output
 sc_prohibit_exit_in_tests:
        @prohibit='\<exit *\(' \
@@ -1307,6 +1317,11 @@ syntax-check: sc_spacing-check \
                echo "* flake8 not installed, sc_flake8 has been skipped  *" >&2; \
                echo "*****************************************************" >&2; \
        fi
+       if [ -z "$(BLACK)" ]; then \
+               echo "*****************************************************" >&2; \
+               echo "* black not installed, sc_black has been skipped    *" >&2; \
+               echo "*****************************************************" >&2; \
+       fi
 endif
 
 # Don't include duplicate header in the source (either *.c or *.h)
@@ -1486,6 +1501,9 @@ exclude_file_name_regexp--sc_prohibit_select = \
   ^build-aux/syntax-check\.mk|src/util/vireventglibwatch\.c|tests/meson\.build$$
 
 
+exclude_file_name_regexp--sc_black = \
+  ^tools/|src/|tests/|ci/|run\.in|scripts/[^/]*\.py
+
 ## -------------- ##
 ## Implementation ##
 ## -------------- ##
index dc0969abcc92e26e3e6e11010b53b6e545c7b05f..5b095bb809f5af26b56ff5b1ba650c1cee56abc7 100644 (file)
@@ -809,6 +809,7 @@ endforeach
 
 optional_programs = [
   'augparse',
+  'black',
   'dmidecode',
   'ebtables',
   'flake8',