Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Python formatting CMake targets (black, flake8) #257

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Minimal flake8 tweak to be compatible with black's line length of 88 characters
# https://black.readthedocs.io/en/stable/the_black_code_style.html#line-length

[flake8]
max-line-length = 88
extend-ignore = E203
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,6 @@ add_subdirectory(python)

#--- create uninstall target ---------------------------------------------------
include(cmake/EDM4HEPUninstall.cmake)

#--- code format targets -------------------------------------------------------
include(cmake/pythonFormat.cmake)
35 changes: 35 additions & 0 deletions cmake/pythonFormat.cmake
Original file line number Diff line number Diff line change
@@ -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
${PROJECT_SOURCE_DIR}/scripts/*.py ${PROJECT_SOURCE_DIR}/test/*.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
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()

Loading