| 1 |
|
| 2 |
import argparse |
| 3 |
import os |
| 4 |
from pathlib import Path |
| 5 |
import re |
| 6 |
from subprocess import list2cmdline, run |
| 7 |
from tempfile import NamedTemporaryFile |
| 8 |
|
| 9 |
CLANG_FORMAT_VERSION = '18.1.6' |
| 10 |
|
| 11 |
INCLUDE_REGEX = re.compile( |
| 12 |
r'^(include|source|tests|verification)/.*\.(c|h|inl)$') |
| 13 |
EXCLUDE_REGEX = re.compile(r'^$') |
| 14 |
|
| 15 |
arg_parser = argparse.ArgumentParser(description="Check with clang-format") |
| 16 |
arg_parser.add_argument('-i', '--inplace-edit', action='store_true', |
| 17 |
help="Edit files inplace") |
| 18 |
args = arg_parser.parse_args() |
| 19 |
|
| 20 |
os.chdir(Path(__file__).parent) |
| 21 |
|
| 22 |
|
| 23 |
filepaths_file = NamedTemporaryFile(delete=False) |
| 24 |
for dirpath, dirnames, filenames in os.walk('.'): |
| 25 |
for filename in filenames: |
| 26 |
|
| 27 |
filepath = Path(dirpath, filename).as_posix() |
| 28 |
if not INCLUDE_REGEX.match(filepath): |
| 29 |
continue |
| 30 |
if EXCLUDE_REGEX.match(filepath): |
| 31 |
continue |
| 32 |
|
| 33 |
filepaths_file.write(f"{filepath}\n".encode()) |
| 34 |
filepaths_file.close() |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
cmd = ['pipx', 'run', f'clang-format=={CLANG_FORMAT_VERSION}', |
| 39 |
f'--files={filepaths_file.name}'] |
| 40 |
if args.inplace_edit: |
| 41 |
cmd += ['-i'] |
| 42 |
else: |
| 43 |
cmd += ['--Werror', '--dry-run'] |
| 44 |
|
| 45 |
print(f"{Path.cwd()}$ {list2cmdline(cmd)}") |
| 46 |
if run(cmd).returncode: |
| 47 |
exit(1) |