Skip to content

Commit 6087d3d

Browse files
Update custom pre-commit hook for file check
Check file names and imports
1 parent 8c4306f commit 6087d3d

File tree

5 files changed

+225
-220
lines changed

5 files changed

+225
-220
lines changed

.pre-commit-config.yaml

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,18 +100,27 @@ repos:
100100
always_run: true
101101
pass_filenames: false
102102

103-
- id: cpp-filename-includes
104-
name: C++ file names and include style
105-
description: 'Check that all C++ files have the correct file name and include style'
103+
- id: cpp-filename
104+
name: C++ file names
105+
description: 'Check that all C++ files have the correct file name'
106+
language: system
107+
types_or: [c++, c]
108+
exclude: 'tests/cut_test/|mm$|dummy.cpp'
109+
entry: utilities/python-venv/bin/check-filenames
110+
require_serial: true # To give a non duplicated error output
111+
112+
- id: cpp-include
113+
name: C++ includes
114+
description: 'Check that all C++ files have correct includes'
106115
language: system
107116
types_or: [c++, c]
108117
exclude: 'tests/cut_test/|mm$'
109-
entry: utilities/python-venv/bin/check-filenames-and-includes
110-
args: ['--paths', 'src/', 'unittests/', 'apps/', '--files']
118+
entry: utilities/python-venv/bin/check-includes
119+
require_serial: true # To give a non duplicated error output
111120

112-
- id: cpp-include-guard
113-
name: Include guards
114-
description: 'Check that all C++ files have the correct include guards'
121+
- id: cpp-header-guard
122+
name: Header guards
123+
description: 'Check that all C++ files have the correct header guards'
115124
language: python
116125
types_or: [c++, c]
117126
files: '\.(h|hpp)$'

utilities/four_c_utils/pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ include = ["four_c_utils"]
1919

2020
[project.scripts]
2121
check-file-header = "four_c_utils.check_file_header:main"
22-
check-filenames-and-includes = "four_c_utils.check_filenames_and_includes:main"
22+
check-filenames = "four_c_utils.check_filenames:main"
23+
check-includes = "four_c_utils.check_includes:main"
2324
check-header-guards = "four_c_utils.check_header_guards:main"
2425
check-test-files = "four_c_utils.check_test_files:main"
2526
check-preprocessor = "four_c_utils.check_preprocessor:main"
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# This file is part of 4C multiphysics licensed under the
2+
# GNU Lesser General Public License v3.0 or later.
3+
#
4+
# See the LICENSE.md file in the top-level for license information.
5+
#
6+
# SPDX-License-Identifier: LGPL-3.0-or-later
7+
"""Check that C++ files have correct filenames"""
8+
9+
import sys
10+
11+
from pathlib import Path
12+
13+
14+
def get_module_name(file: Path) -> str | None:
15+
"""Get the corresponding module name for a file.
16+
17+
The module name is determined by the parent folder
18+
name which is on the same level as the first
19+
.contains_modules file.
20+
21+
Args:
22+
file: The file to get the module name from
23+
24+
Returns:
25+
The module name or None if no module is found
26+
"""
27+
28+
current_dir = file.parent.absolute()
29+
30+
while True:
31+
if current_dir == Path("/"):
32+
return None
33+
if (current_dir / ".contains_modules").is_file():
34+
return file.absolute().relative_to(current_dir).parts[0]
35+
current_dir = current_dir.parent
36+
37+
38+
def check_file_name(file: Path) -> bool:
39+
"""Check that the file has a valid filename.
40+
41+
A valid filename is prefixed by '4C' and the module name.
42+
If it is a test file it is postfixed by '_test'.
43+
44+
Args:
45+
file: The file to check
46+
47+
Returns:
48+
True if the file has a valid filename, False otherwise
49+
"""
50+
51+
module_name = get_module_name(file)
52+
53+
if not file.name.startswith("4C_" if module_name is None else f"4C_{module_name}"):
54+
return False
55+
56+
if "/tests/" in str(file) or "/unittests/" in str(file):
57+
if not file.name.split(".")[0].endswith("_test"):
58+
return False
59+
60+
return True
61+
62+
63+
def main():
64+
"""Check that C++ files have correct filenames."""
65+
66+
wrong_file_names = []
67+
68+
for file in sys.argv[1:]:
69+
if not check_file_name(Path(file)):
70+
wrong_file_names.append(file)
71+
72+
if wrong_file_names:
73+
print("The following files do not adhere to our file naming convention:\n")
74+
for wrong_file in wrong_file_names:
75+
print(f" {wrong_file}")
76+
77+
print(
78+
"\nPlease rename the files to match the naming convention.\n"
79+
"A valid filename must:\n"
80+
" - Be prefixed with '4C_' followed by the module name\n"
81+
" - If it is a test file, it should end with '_test'"
82+
)
83+
84+
sys.exit(1)
85+
86+
87+
if __name__ == "__main__":
88+
main()

utilities/four_c_utils/src/four_c_utils/check_filenames_and_includes.py

Lines changed: 0 additions & 211 deletions
This file was deleted.

0 commit comments

Comments
 (0)