diff --git a/CMakeLists.txt b/CMakeLists.txt index 90a14a170..3458d487b 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -224,3 +224,6 @@ if(BUILD_TESTING) endif() #--- add CMake infrastructure -------------------------------------------------- include(cmake/podioCreateConfig.cmake) + +#--- code format targets ------------------------------------------------------- +include(cmake/pythonFormat.cmake) diff --git a/cmake/pythonFormat.cmake b/cmake/pythonFormat.cmake new file mode 100644 index 000000000..ca7b0627c --- /dev/null +++ b/cmake/pythonFormat.cmake @@ -0,0 +1,35 @@ +# Additional target to run python linters and formatters on python scripts +# +# Requires black/flake8 to be available in the environment + + +# Get all our Python files +file(GLOB_RECURSE ALL_PYTHON_FILES ${PROJECT_SOURCE_DIR}/python/*.py) + + +# Black is rather simple because there are no options... +find_program(BLACK_EXECUTABLE black) +if(BLACK_EXECUTABLE) + add_custom_target( + black + COMMAND ${BLACK_EXECUTABLE} + ${ALL_PYTHON_FILES} + ) + set_target_properties(black PROPERTIES EXCLUDE_FROM_ALL TRUE) +else() + message(STATUS "Failed to find black executable - no target to run black can be set") +endif() + +find_program(FLAKE8_EXECUTABLE flake8) +if(FLAKE8_EXECUTABLE) + add_custom_target( + flake8 + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + COMMAND ${FLAKE8_EXECUTABLE} + --config=${PROJECT_SOURCE_DIR}/.flake8 + ${ALL_PYTHON_FILES} + ) + set_target_properties(flake8 PROPERTIES EXCLUDE_FROM_ALL TRUE) +else() + message(STATUS "Failed to find flake8 executable - no target to run flake8 can be set") +endif()