-
Notifications
You must be signed in to change notification settings - Fork 212
/
run_clang_format.py
executable file
·68 lines (60 loc) · 2.46 KB
/
run_clang_format.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#! /usr/bin/python
# Copyright 2016-2021 NXP
# All rights reserved.
#
# SPDX-License-Identifier: BSD-3-Clause
# To use this script run
# $./run_clang_format.py
from __future__ import print_function
import subprocess
import sys
import os
# Folders to scan
folders = [
"erpc_c",
"erpcgen/src",
"erpcsniffer/src",
"test"]
with open(".clang-format-ignore") as f:
exceptionsTmp = f.read().split("\n")
exceptions = [exception for exception in exceptionsTmp if exception != "" and exception[0] != "#"]
# For windows use "\\" instead of "/" path separators.
if os.environ.get('OS', '') == 'Windows_NT':
folders = [os.path.normpath(folder) for folder in folders]
exceptions = [os.path.normpath(e) for e in exceptions]
# Files with this extensions will be formatted/
extensions = [".h", ".hpp", ".c", ".cpp"]
# Check that the clang-format is installed and matches the required version. The clang-format binary
# chosen is specified by the environment variable $CLANG_FORMAT_PATH or "clang-format" using the
# regular $PATH resolution mechanism if $CLANG_FORMAT_PATH is undefined.
clang_format_path = os.environ.get("CLANG_FORMAT_PATH")
clang_format_bin = clang_format_path if clang_format_path is not None else "clang-format"
try:
cf = subprocess.Popen(
[clang_format_bin, "--version"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except FileNotFoundError:
print("clang-format is not in $PATH")
exit(1)
clang_format_stdout, clang_format_stderr = cf.communicate()
if cf.returncode != 0:
print("Failed to determine clang-format version. stderr=\"%s\"" % (
clang_format_stderr.decode('utf-8')))
exit(1)
clang_format_stdout = clang_format_stdout.decode("utf-8")
if "clang-format version 16.0.0" not in clang_format_stdout:
print("clang-format is not the required version: 16.0.0")
exit(1)
# processing formatting
for folder in folders:
print('*****************************************************************************')
print(folder)
for path, subdirs, files in os.walk(folder):
for name in files:
if any(ext in name for ext in extensions):
file = "./" + os.path.join(path, name)
if file in exceptions:
print("Ignored: ", file)
else:
print("Formatting: ", file)
subprocess.call(["clang-format", "-i", file])
print('*****************************************************************************\n')