Skip to content

Commit 05fef91

Browse files
authored
Merge pull request #409 from lukaszstolarczuk/add-black
Add black formatter for Python
2 parents 34c584d + cf70e6a commit 05fef91

File tree

7 files changed

+80
-35
lines changed

7 files changed

+80
-35
lines changed

.github/workflows/pr_push.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ jobs:
9494
- name: Install apt packages
9595
run: |
9696
sudo apt-get update
97-
sudo apt-get install -y cmake clang-format-15 cmake-format libhwloc-dev
97+
sudo apt-get install -y black cmake clang-format-15 cmake-format libhwloc-dev
9898
9999
- name: Configure CMake
100100
run: >
@@ -106,12 +106,15 @@ jobs:
106106
-DUMF_BUILD_LIBUMF_POOL_SCALABLE=OFF
107107
-DUMF_BUILD_LIBUMF_POOL_JEMALLOC=OFF
108108
109-
- name: Check clang-format
109+
- name: Check C/C++ formatting
110110
run: cmake --build build --target clang-format-check
111111

112-
- name: Check cmake-format
112+
- name: Check CMake formatting
113113
run: cmake --build build --target cmake-format-check
114114

115+
- name: Check Python formatting
116+
run: cmake --build build --target black-format-check
117+
115118
DocsBuild:
116119
name: Build docs
117120
runs-on: ubuntu-latest

CMakeLists.txt

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ option(UMF_BUILD_EXAMPLES "Build UMF examples" ON)
2525
option(UMF_ENABLE_POOL_TRACKING "Build UMF with pool tracking" ON)
2626
option(UMF_DEVELOPER_MODE "Enable developer checks, treats warnings as errors"
2727
OFF)
28-
option(UMF_FORMAT_CODE_STYLE "Format UMF code with clang-format" OFF)
28+
option(UMF_FORMAT_CODE_STYLE
29+
"Add clang, cmake, and black -format-check and -format-apply targets"
30+
OFF)
2931
option(USE_ASAN "Enable AddressSanitizer checks" OFF)
3032
option(USE_UBSAN "Enable UndefinedBehaviorSanitizer checks" OFF)
3133
option(USE_TSAN "Enable ThreadSanitizer checks" OFF)
@@ -255,13 +257,19 @@ if(UMF_FORMAT_CODE_STYLE)
255257
find_program(CLANG_FORMAT NAMES clang-format-15 clang-format-15.0
256258
clang-format)
257259
find_program(CMAKE_FORMAT NAMES cmake-format)
260+
find_program(BLACK NAMES black)
261+
262+
set(CLANG_FORMAT_REQUIRED "15.0")
263+
set(CMAKE_FORMAT_REQUIRED "0.6")
258264

259-
if(NOT CLANG_FORMAT AND NOT CMAKE_FORMAT)
265+
if(NOT CLANG_FORMAT
266+
AND NOT CMAKE_FORMAT
267+
AND NOT BLACK)
260268
message(
261269
FATAL_ERROR
262270
"UMF_FORMAT_CODE_STYLE=ON, but neither clang-format (required version: "
263-
"${CLANG_FORMAT_REQUIRED}) nor cmake-format (required version: "
264-
"${CMAKE_FORMAT_VERSION}) was found.")
271+
"${CLANG_FORMAT_REQUIRED}), nor cmake-format (required version: "
272+
"${CMAKE_FORMAT_REQUIRED}), nor black was found.")
265273
endif()
266274

267275
if(CLANG_FORMAT)
@@ -271,7 +279,6 @@ if(UMF_FORMAT_CODE_STYLE)
271279

272280
# Check if clang-format (in correct version) is available for code
273281
# formatting.
274-
set(CLANG_FORMAT_REQUIRED "15.0")
275282
if(NOT (CLANG_FORMAT_VERSION VERSION_EQUAL CLANG_FORMAT_REQUIRED))
276283
message(FATAL_ERROR "Required clang-format version is "
277284
"${CLANG_FORMAT_REQUIRED}")
@@ -301,8 +308,8 @@ if(UMF_FORMAT_CODE_STYLE)
301308
file(GLOB_RECURSE format_list ${format_clang_glob})
302309

303310
message(
304-
STATUS "Adding 'clang-format-check' and 'clang-format-apply' make "
305-
"targets")
311+
STATUS
312+
"Adding 'clang-format-check' and 'clang-format-apply' targets")
306313

307314
add_custom_target(
308315
clang-format-check
@@ -312,7 +319,7 @@ if(UMF_FORMAT_CODE_STYLE)
312319

313320
add_custom_target(
314321
clang-format-apply
315-
COMMAND ${CLANG_FORMAT} --style=file --i ${format_list}
322+
COMMAND ${CLANG_FORMAT} --style=file -i ${format_list}
316323
COMMENT "Format files using clang-format")
317324
endif()
318325

@@ -323,7 +330,6 @@ if(UMF_FORMAT_CODE_STYLE)
323330

324331
# Check if cmake-format (in correct version) is available for cmake
325332
# files formatting.
326-
set(CMAKE_FORMAT_REQUIRED "0.6")
327333
if(NOT (CMAKE_FORMAT_VERSION VERSION_EQUAL CMAKE_FORMAT_REQUIRED))
328334
message(FATAL_ERROR "Required cmake-format version is"
329335
"${CMAKE_FORMAT_REQUIRED}")
@@ -351,8 +357,8 @@ if(UMF_FORMAT_CODE_STYLE)
351357
list(APPEND format_cmake_list "${PROJECT_SOURCE_DIR}/CMakeLists.txt")
352358

353359
message(
354-
STATUS "Adding 'cmake-format-check' and 'cmake-format-apply' make "
355-
"targets")
360+
STATUS
361+
"Adding 'cmake-format-check' and 'cmake-format-apply' targets")
356362

357363
add_custom_target(
358364
cmake-format-check
@@ -365,28 +371,59 @@ if(UMF_FORMAT_CODE_STYLE)
365371
COMMENT "Format Cmake files using cmake-format")
366372
endif()
367373

368-
# Add a convenience target for running both tools at once - available only
369-
# if both are found.
370-
if(CLANG_FORMAT AND CMAKE_FORMAT)
374+
if(BLACK)
375+
# black should maintain backward compatibility, we don't have to require
376+
# a specific version
377+
get_program_version_major_minor(${BLACK} BLACK_VERSION)
378+
message(STATUS "Found black: ${BLACK} (version: ${BLACK_VERSION})")
379+
380+
message(
381+
STATUS
382+
"Adding 'black-format-check' and 'black-format-apply' targets")
383+
384+
add_custom_target(
385+
black-format-check
386+
COMMAND ${BLACK} --check --verbose ${CMAKE_SOURCE_DIR}
387+
COMMENT "Check Python files formatting using black formatter")
388+
389+
add_custom_target(
390+
black-format-apply
391+
COMMAND ${BLACK} ${CMAKE_SOURCE_DIR}
392+
COMMENT "Format Python files using black formatter")
393+
endif()
394+
395+
# Add a convenience target for running all tools at once - available only if
396+
# all are found.
397+
if(CLANG_FORMAT
398+
AND CMAKE_FORMAT
399+
AND BLACK)
371400
add_custom_target(
372401
format-check
373402
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target
374403
clang-format-check
375404
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target
376405
cmake-format-check
377-
COMMENT "Running both clang-format-check and cmake-format-check")
406+
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target
407+
black-format-check
408+
COMMENT "Running all formatting checks")
378409

379410
add_custom_target(
380411
format-apply
381412
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target
382413
clang-format-apply
383414
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target
384415
cmake-format-apply
385-
COMMENT "Format files using clang-format and cmake-format")
416+
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target
417+
black-format-apply
418+
COMMENT "Format C/C++, CMake, and Python files")
419+
message(
420+
STATUS
421+
" Adding convenience targets 'format-check' and 'format-apply'."
422+
)
386423
else()
387424
message(
388425
STATUS
389-
" Convenience targets 'make format-check' and 'make format-apply' are "
426+
" Convenience targets 'format-check' and 'format-apply' are "
390427
"not available. Use commands specific for found tools (see the log above)."
391428
)
392429
endif()

CONTRIBUTING.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,11 @@ To enable additional checks (including `-Werror` / `/WX` compilation flag), swit
6262
["CMake standard options"](./README.md#cmake-standard-options) section in the top-level Readme.
6363

6464
### Code style
65-
We use `clang-format` to verify and apply code style changes to source files.
65+
We use `clang-format` to verify and apply code style changes to C/C++ source files.
6666
To see all rules we require, please take a look at `.clang-format` file in the
6767
root directory of this repository. Similarly, we use `cmake-format` tool and
6868
`.cmake-format` file to verify and apply code style changes to CMake files.
69+
For Python source files we use `black` tool.
6970

7071
To enable code style checks and re-formatting, CMake option `UMF_FORMAT_CODE_STYLE` has to
7172
be switched on. You'll then have additional CMake targets available.
@@ -74,7 +75,7 @@ To verify correct coding style of your changes execute (assuming `build` is your
7475

7576
```bash
7677
$ cmake -B build -DUMF_FORMAT_CODE_STYLE=ON
77-
$ cmake --build build --target format-checks
78+
$ cmake --build build --target format-check
7879
```
7980

8081
We run these checks in our Continuous Integration (CI). So, if any issues were found,
@@ -87,9 +88,10 @@ $ cmake --build build --target format-apply
8788
# Remember to review introduced changes
8889
```
8990

90-
If you wish to use only `clang-format` or only `cmake-format`, you can execute the corresponding
91-
`clang-format-check` and `clang-format-apply` for source files, or `cmake-format-check` and
92-
`cmake-format-apply` for CMake files, respectively.
91+
If you wish to use only `clang-format`, or `cmake-format`, or `black`, you can execute the corresponding
92+
`clang-format-check` and `clang-format-apply` for C/C++ source files, or `cmake-format-check` and
93+
`cmake-format-apply` for CMake files, or `black-format-check` and `black-format-apply` for Python
94+
source files, respectively.
9395

9496
**NOTE**: We use specific versions of formatting tools to ensure consistency across the project. The required versions are:
9597
- clang-format version **15.0**, which can be installed with the command: `python -m pip install clang-format==15.0.7`.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ List of options provided by CMake:
102102
| UMF_BUILD_EXAMPLES | Build UMF examples | ON/OFF | ON |
103103
| UMF_ENABLE_POOL_TRACKING | Build UMF with pool tracking | ON/OFF | ON |
104104
| UMF_DEVELOPER_MODE | Treat warnings as errors and enables additional checks | ON/OFF | OFF |
105-
| UMF_FORMAT_CODE_STYLE | Add clang and cmake -format-check and -format-apply targets to make | ON/OFF | OFF |
105+
| UMF_FORMAT_CODE_STYLE | Add clang, cmake, and black -format-check and -format-apply targets to make | ON/OFF | OFF |
106106
| USE_ASAN | Enable AddressSanitizer checks | ON/OFF | OFF |
107107
| USE_UBSAN | Enable UndefinedBehaviorSanitizer checks | ON/OFF | OFF |
108108
| USE_TSAN | Enable ThreadSanitizer checks | ON/OFF | OFF |

scripts/generate_docs.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from pathlib import Path
99
from shutil import rmtree
10-
import subprocess # nosec B404
10+
import subprocess # nosec B404
1111
import time
1212

1313

@@ -41,8 +41,9 @@ def _prepare_docs_dir(docs_path: Path) -> None:
4141
def _generate_xml(config_path: Path, docs_path: Path) -> None:
4242
print("Generating XML files with doxygen...", flush=True)
4343
try:
44-
subprocess.run(["doxygen", Path(config_path, "Doxyfile")], text=True
45-
).check_returncode() # nosec B603, B607
44+
subprocess.run(
45+
["doxygen", Path(config_path, "Doxyfile")], text=True
46+
).check_returncode() # nosec B603, B607
4647
print(f"All XML files generated in {docs_path}", flush=True)
4748
except subprocess.CalledProcessError as ex:
4849
print("Generating XML files failed!")
@@ -53,8 +54,9 @@ def _generate_xml(config_path: Path, docs_path: Path) -> None:
5354
def _generate_html(config_path: Path, docs_path: Path) -> None:
5455
print("Generating HTML pages with sphinx...", flush=True)
5556
try:
56-
subprocess.run(["sphinx-build", config_path, Path(docs_path, "html")], text=True
57-
).check_returncode() # nosec B603, B607
57+
subprocess.run(
58+
["sphinx-build", config_path, Path(docs_path, "html")], text=True
59+
).check_returncode() # nosec B603, B607
5860
print(f"All HTML files generated in {docs_path}", flush=True)
5961
except subprocess.CalledProcessError as ex:
6062
print("Generating HTML pages failed!")

test/test_installation.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import difflib
1010
from pathlib import Path
1111
import platform
12-
import subprocess # nosec B404
12+
import subprocess # nosec B404
1313
import sys
1414
from typing import List
1515

@@ -61,7 +61,7 @@ def _create_match_list(self) -> List[str]:
6161
lib_ext_static = "a"
6262
lib_ext_shared = "so"
6363
lib_prefix = "lib"
64-
else: # MacOS
64+
else: # MacOS
6565
lib_ext_static = "a"
6666
lib_ext_shared = "dylib"
6767
lib_prefix = "lib"
@@ -138,7 +138,7 @@ def install_umf(self) -> None:
138138

139139
install_cmd = f"cmake --install {self.build_dir} --config {self.build_type.title()} --prefix {self.install_dir}"
140140
try:
141-
subprocess.run(install_cmd.split()).check_returncode() # nosec B603
141+
subprocess.run(install_cmd.split()).check_returncode() # nosec B603
142142
except subprocess.CalledProcessError:
143143
sys.exit(f"Error: UMF installation command '{install_cmd}' failed")
144144

@@ -175,7 +175,7 @@ def uninstall_umf(self) -> None:
175175
"""
176176
uninstall_cmd = f"cmake --build {self.build_dir} --target uninstall"
177177
try:
178-
subprocess.run(uninstall_cmd.split()).check_returncode() # nosec B603
178+
subprocess.run(uninstall_cmd.split()).check_returncode() # nosec B603
179179
except subprocess.CalledProcessError:
180180
sys.exit(f"Error: UMF uninstallation command '{uninstall_cmd}' failed")
181181

third_party/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Formatting the source code
33
clang-format==15.0.7
44
cmake-format==0.6.13
5+
black==23.7.0
56
# Generating HTML documentation
67
pygments==2.15.1
78
sphinxcontrib_applehelp==1.0.4

0 commit comments

Comments
 (0)