#!/usr/bin/env python3 """ 检查项目中所有Python文件的语法 """ import os import sys def check_python_syntax(file_path): """ 检查单个Python文件的语法 Args: file_path: Python文件路径 Returns: bool: 如果语法正确返回True,否则返回False """ try: with open(file_path, 'rb') as f: code = f.read() # 使用compile函数检查语法 compile(code, file_path, 'exec') return True except SyntaxError as e: print(f"语法错误: {file_path}:{e.lineno}:{e.offset}: {e.msg}") return False except Exception as e: print(f"无法检查文件 {file_path}: {e}") return False def main(): """ 检查项目中所有Python文件的语法 """ # 要检查的目录 directories = ['core', 'models', 'plugins', 'scripts', 'tests'] # 要检查的单独文件 files = ['main.py', 'profile_main.py', 'test_performance_simple.py', 'setup_mypyc.py'] error_count = 0 file_count = 0 # 检查目录中的所有Python文件 for directory in directories: for root, _, filenames in os.walk(directory): for filename in filenames: if filename.endswith('.py'): file_path = os.path.join(root, filename) file_count += 1 if not check_python_syntax(file_path): error_count += 1 # 检查单独的Python文件 for file in files: if os.path.exists(file): file_count += 1 if not check_python_syntax(file): error_count += 1 print(f"\n检查完成: {file_count} 个文件,{error_count} 个语法错误") if error_count > 0: sys.exit(1) else: sys.exit(0) if __name__ == "__main__": main()