Files
NeoBot/check_syntax.py
K2cr2O1 8beeaef424 feat: 实现统一的错误处理机制和增强日志系统
添加错误码定义和统一响应格式
增强日志记录功能,支持模块专用日志记录器
实现全局异常捕获和友好错误提示
更新文档说明错误处理机制
2026-01-19 14:05:14 +08:00

70 lines
1.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/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()