查找所有 C++ 文件,排除指定目录 Args: root_dir: 根目录路径 exclude_dirs: 要排除的目录列表(相对于 root_dir) Returns: C++ 文件路径列表
(root_dir, exclude_dirs)
| 10 | |
| 11 | |
| 12 | def find_cpp_files(root_dir, exclude_dirs): |
| 13 | """ |
| 14 | 查找所有 C++ 文件,排除指定目录 |
| 15 | |
| 16 | Args: |
| 17 | root_dir: 根目录路径 |
| 18 | exclude_dirs: 要排除的目录列表(相对于 root_dir) |
| 19 | |
| 20 | Returns: |
| 21 | C++ 文件路径列表 |
| 22 | """ |
| 23 | root_path = Path(root_dir).resolve() |
| 24 | exclude_paths = [root_path / d for d in exclude_dirs] |
| 25 | |
| 26 | cpp_extensions = { |
| 27 | ".cpp", |
| 28 | ".hpp", |
| 29 | ".h", |
| 30 | ".cc", |
| 31 | ".cxx", |
| 32 | ".c++", |
| 33 | ".h++", |
| 34 | ".hh", |
| 35 | ".hxx", |
| 36 | ".cppm", |
| 37 | } |
| 38 | cpp_files = [] |
| 39 | |
| 40 | for file_path in root_path.rglob("*"): |
| 41 | # 只处理文件,跳过目录 |
| 42 | if not file_path.is_file(): |
| 43 | continue |
| 44 | |
| 45 | # 跳过排除的目录及其子目录中的文件 |
| 46 | try: |
| 47 | file_path_resolved = file_path.resolve() |
| 48 | if any( |
| 49 | exclude_path.resolve() in file_path_resolved.parents |
| 50 | for exclude_path in exclude_paths |
| 51 | ): |
| 52 | continue |
| 53 | except (OSError, ValueError): |
| 54 | # 如果路径解析失败(例如符号链接问题),跳过 |
| 55 | continue |
| 56 | |
| 57 | # 检查是否是 C++ 文件 |
| 58 | if file_path.suffix in cpp_extensions: |
| 59 | cpp_files.append(file_path) |
| 60 | |
| 61 | return sorted(cpp_files) |
| 62 | |
| 63 | |
| 64 | def format_file(file_path, clang_format_cmd="clang-format", dry_run=False): |