-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat_code.py
More file actions
executable file
·78 lines (57 loc) · 2.4 KB
/
format_code.py
File metadata and controls
executable file
·78 lines (57 loc) · 2.4 KB
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
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python3
"""Format C/C++ sources and proto files with clang-format using repo .clang-format."""
import os
import shutil
import subprocess
import sys
from pathlib import Path
from typing import List
FORMAT_DIRS = ("src", "include", "test", "proto")
SOURCE_SUFFIXES = {".cc", ".cpp", ".cxx", ".h", ".hpp", ".hh", ".proto"}
def collect_source_files(root: Path) -> List[str]:
files: List[str] = []
for relative_dir in FORMAT_DIRS:
base_dir = root / relative_dir
if not base_dir.is_dir():
continue
for current_root, dirnames, filenames in os.walk(base_dir):
# Skip third-party or generated trees to avoid unrelated diffs.
dirnames[:] = [dirname for dirname in dirnames if dirname not in ("gen", "ttidl")]
for filename in filenames:
file_path = Path(current_root) / filename
if file_path.suffix in SOURCE_SUFFIXES:
files.append(str(file_path))
files.sort()
return files
def main() -> int:
root = Path(__file__).resolve().parent
clang_format = os.environ.get("CLANG_FORMAT", "clang-format")
if shutil.which(clang_format) is None:
print(
f"error: '{clang_format}' not found. Install LLVM clang-format or set CLANG_FORMAT.",
file=sys.stderr,
)
return 1
files = collect_source_files(root)
if not files:
print(f"no source files matched under {' '.join(FORMAT_DIRS)}")
return 0
# 分离 proto 文件和 C++ 文件
proto_files = [f for f in files if f.endswith('.proto')]
cpp_files = [f for f in files if not f.endswith('.proto')]
formatted_count = 0
# 格式化 C++ 文件
if cpp_files:
subprocess.run([clang_format, "-i", "--style=file", *cpp_files], check=True)
formatted_count += len(cpp_files)
print(f"formatted {len(cpp_files)} C/C++ file(s) with .clang-format")
# 格式化 proto 文件(使用 clang-format 的 proto 支持)
if proto_files:
subprocess.run([clang_format, "-i", "--style=file", *proto_files], check=True)
formatted_count += len(proto_files)
print(f"formatted {len(proto_files)} proto file(s) with .clang-format")
if formatted_count == 0:
print(f"no source files matched under {' '.join(FORMAT_DIRS)}")
return 0
if __name__ == "__main__":
raise SystemExit(main())