Debian Codegraph @

format-check.py

raw symbols SCIP
1 #!/usr/bin/env python3
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(r'^source/.*\.(c|h)$')
12 EXCLUDE_REGEX = re.compile(r'^$')
13
14 arg_parser = argparse.ArgumentParser(description="Check with clang-format")
15 arg_parser.add_argument('-i', '--inplace-edit', action='store_true',
16 help="Edit files inplace")
17 args = arg_parser.parse_args()
18
19 os.chdir(Path(__file__).parent)
20
21 # create file containing list of all files to format
22 filepaths_file = NamedTemporaryFile(delete=False)
23 for dirpath, dirnames, filenames in os.walk('.'):
24 for filename in filenames:
25 # our regexes expect filepath to use forward slash
26 filepath = Path(dirpath, filename).as_posix()
27 if not INCLUDE_REGEX.match(filepath):
28 continue
29 if EXCLUDE_REGEX.match(filepath):
30 continue
31
32 filepaths_file.write(f"{filepath}\n".encode())
33 filepaths_file.close()
34
35 # use pipx to run clang-format from PyPI
36 # this is a simple way to run the same clang-format version regardless of OS
37 cmd = ['pipx', 'run', f'clang-format=={CLANG_FORMAT_VERSION}',
38 f'--files={filepaths_file.name}']
39 if args.inplace_edit:
40 cmd += ['-i']
41 else:
42 cmd += ['--Werror', '--dry-run']
43
44 print(f"{Path.cwd()}$ {list2cmdline(cmd)}")
45 if run(cmd).returncode:
46 exit(1)