mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-29 17:22:56 +08:00
37 lines
1.3 KiB
Python
37 lines
1.3 KiB
Python
|
import os
|
||
|
import sys
|
||
|
import subprocess
|
||
|
from pathlib import Path
|
||
|
|
||
|
def format_files_in_dir(dir_path):
|
||
|
# 遍历目录及其子目录中的所有文件
|
||
|
for filepath in dir_path.rglob('*'):
|
||
|
# 只处理.c, .h, .cpp文件
|
||
|
if filepath.suffix in ['.c', '.h', '.cpp']:
|
||
|
try:
|
||
|
# 使用clang-format命令格式化文件
|
||
|
subprocess.run(['clang-format', '-i', str(filepath)], check=True)
|
||
|
print(f'Formatted file: {filepath}')
|
||
|
except subprocess.CalledProcessError as e:
|
||
|
print(f'Error formatting file: {filepath}. Error message: {str(e)}')
|
||
|
|
||
|
# 确保文件最后一行是空行
|
||
|
with open(filepath, 'r+', encoding='utf-8') as file:
|
||
|
lines = file.readlines()
|
||
|
if lines and not lines[-1].endswith('\n'):
|
||
|
# 如果最后一行不是空行,添加一个
|
||
|
file.write('\n')
|
||
|
print(f'Added newline to file: {filepath}')
|
||
|
|
||
|
# 从命令行参数获取目录列表
|
||
|
dirs = sys.argv[1:]
|
||
|
|
||
|
# 对每个目录进行处理
|
||
|
for dir_path_str in dirs:
|
||
|
dir_path = Path(dir_path_str)
|
||
|
if dir_path.is_dir():
|
||
|
format_files_in_dir(dir_path)
|
||
|
print(f'Finished processing directory: {dir_path_str}')
|
||
|
else:
|
||
|
print(f"'{dir_path_str}' is not a directory. Skipping...")
|