|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Batch boolean comparison style checker for all C++ source files. |
| 4 | +Scans the entire sources/ directory (excluding thirdparty) and reports all style violations. |
| 5 | +Console shows progress with pass/fail indicators. |
| 6 | +Log file 'boolean_style_check_results.txt' contains ONLY files with violations (no clean files). |
| 7 | +""" |
| 8 | + |
| 9 | +import os |
| 10 | +import sys |
| 11 | +import subprocess |
| 12 | +from pathlib import Path |
| 13 | + |
| 14 | + |
| 15 | +def find_cpp_files(): |
| 16 | + """Find all C++ source files in sources/ directory, excluding thirdparty""" |
| 17 | + sources_dir = Path("sources") |
| 18 | + cpp_files = [] |
| 19 | + |
| 20 | + if not sources_dir.exists(): |
| 21 | + print("Error: sources/ directory not found") |
| 22 | + return [] |
| 23 | + |
| 24 | + # Extensions to check |
| 25 | + extensions = [".cxx", ".cpp", ".cc", ".hxx", ".hpp", ".h"] |
| 26 | + |
| 27 | + for ext in extensions: |
| 28 | + # Find all files with this extension |
| 29 | + for file_path in sources_dir.rglob(f"*{ext}"): |
| 30 | + # Skip thirdparty directory |
| 31 | + if "thirdparty" not in str(file_path): |
| 32 | + cpp_files.append(file_path) |
| 33 | + |
| 34 | + return sorted(cpp_files) |
| 35 | + |
| 36 | + |
| 37 | +def run_boolean_check(file_path): |
| 38 | + """Run the boolean style checker on a single file and return violations""" |
| 39 | + try: |
| 40 | + result = subprocess.run( |
| 41 | + [sys.executable, "scripts/custom_boolean_check.py", str(file_path)], |
| 42 | + capture_output=True, |
| 43 | + text=True, |
| 44 | + cwd=".", |
| 45 | + ) |
| 46 | + |
| 47 | + violations = [] |
| 48 | + for line in result.stdout.strip().split("\n"): |
| 49 | + if line.strip() and "No boolean style violations found" not in line: |
| 50 | + violations.append(line) |
| 51 | + |
| 52 | + return violations |
| 53 | + |
| 54 | + except Exception as e: |
| 55 | + return [f"Error checking {file_path}: {e}"] |
| 56 | + |
| 57 | + |
| 58 | +def log_and_print(message, log_file=None): |
| 59 | + """Print to console and optionally write to log file""" |
| 60 | + print(message) |
| 61 | + if log_file: |
| 62 | + with open(log_file, "a") as f: |
| 63 | + f.write(message + "\n") |
| 64 | + |
| 65 | + |
| 66 | +def main(): |
| 67 | + # Log file will be created only if violations are found |
| 68 | + log_file = None # Will be set when first violation is found |
| 69 | + |
| 70 | + log_and_print("Checking boolean comparison style across all source files...", None) |
| 71 | + log_and_print("=" * 70, None) |
| 72 | + |
| 73 | + cpp_files = find_cpp_files() |
| 74 | + |
| 75 | + if not cpp_files: |
| 76 | + log_and_print("No C++ source files found in sources/ directory", log_file) |
| 77 | + return |
| 78 | + |
| 79 | + log_and_print(f"Found {len(cpp_files)} C++ source files to check", log_file) |
| 80 | + log_and_print("", log_file) |
| 81 | + |
| 82 | + total_violations = 0 |
| 83 | + files_with_violations = 0 |
| 84 | + files_checked = 0 |
| 85 | + |
| 86 | + for file_path in cpp_files: |
| 87 | + files_checked += 1 |
| 88 | + print(f"[{files_checked:2d}/{len(cpp_files):2d}] Checking {file_path}") |
| 89 | + |
| 90 | + violations = run_boolean_check(file_path) |
| 91 | + |
| 92 | + if violations: |
| 93 | + # Initialize log file on first violation |
| 94 | + if log_file is None: |
| 95 | + log_file = "boolean_style_check_results.txt" |
| 96 | + with open(log_file, "w") as f: |
| 97 | + f.write("Boolean Comparison Style Check Results\n") |
| 98 | + f.write("=" * 70 + "\n") |
| 99 | + f.write(f"Generated: {Path.cwd()}\n\n") |
| 100 | + |
| 101 | + files_with_violations += 1 |
| 102 | + total_violations += len(violations) |
| 103 | + |
| 104 | + # Log detailed info to file |
| 105 | + log_and_print( |
| 106 | + f"[{files_checked:2d}/{len(cpp_files):2d}] Checking {file_path}", |
| 107 | + log_file, |
| 108 | + ) |
| 109 | + log_and_print(f" [FAIL] {len(violations)} violations found:", log_file) |
| 110 | + for violation in violations: |
| 111 | + log_and_print(f" {violation}", log_file) |
| 112 | + log_and_print("", log_file) |
| 113 | + |
| 114 | + # Console summary only |
| 115 | + print(f" [FAIL] {len(violations)} violations found:") |
| 116 | + else: |
| 117 | + print(" [PASS] No violations") |
| 118 | + |
| 119 | + # Summary |
| 120 | + if total_violations == 0: |
| 121 | + print() |
| 122 | + print("SUCCESS: All files pass boolean comparison style checks!") |
| 123 | + return 0 |
| 124 | + else: |
| 125 | + # Only show summary when there are violations |
| 126 | + print() |
| 127 | + log_and_print("=" * 70, log_file) |
| 128 | + log_and_print("SUMMARY", log_file) |
| 129 | + log_and_print(f" Files checked: {files_checked}", log_file) |
| 130 | + log_and_print(f" Files with violations: {files_with_violations}", log_file) |
| 131 | + log_and_print(f" Total violations: {total_violations}", log_file) |
| 132 | + log_and_print("", log_file) |
| 133 | + log_and_print(f"Results saved to: {log_file}", log_file) |
| 134 | + warning_msg = "WARNING: Style violations found. Consider fixing them for better code consistency." |
| 135 | + log_and_print(warning_msg, log_file) |
| 136 | + return 1 |
| 137 | + |
| 138 | + |
| 139 | +if __name__ == "__main__": |
| 140 | + sys.exit(main()) |
0 commit comments