-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Python formatting CMake targets (black, flake8)
Add CMake targets for running black and flake8 on Python source files CMake will look to find the relevant binaries and setup the targets if they are there If they are not found a STATUS message is printed Targets are excluded from "all" to avoid running them by accident
- Loading branch information
1 parent
46a594b
commit 73d1969
Showing
3 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# 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}/*.py) | ||
|
||
# Black is rather simple because there are no options... | ||
find_program(BLACK_EXECUTABLE black) | ||
if(BLACK_EXECUTABLE) | ||
add_custom_target( | ||
black | ||
COMMAND black | ||
${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 | ||
--config=${CMAKE_CURRENT_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() | ||
|