""" Phase 7 代码质量验证脚本 """ import os import re from pathlib import Path def check_file_exists(filepath): """检查文件是否存在""" if os.path.exists(filepath): print(f"[OK] {filepath}") return True else: print(f"[FAIL] {filepath} - 文件不存在") return False def check_file_syntax(filepath): """检查文件语法""" try: with open(filepath, 'r', encoding='utf-8') as f: code = f.read() compile(code, filepath, 'exec') print(f"[OK] {filepath} - 语法检查通过") return True except Exception as e: print(f"[FAIL] {filepath} - 语法错误: {e}") return False def check_docstring(filepath): """检查文件是否有docstring""" try: with open(filepath, 'r', encoding='utf-8') as f: content = f.read() # 检查模块docstring has_module_docstring = '"""' in content[:500] # 统计函数docstring function_pattern = r'def\s+\w+\([^)]*\):[^)]*"""' function_docstrings = len(re.findall(function_pattern, content)) # 统计类docstring class_pattern = r'class\s+\w+.*?:[^)]*"""' class_docstrings = len(re.findall(class_pattern, content)) print(f"[OK] {filepath} - 模块文档: {has_module_docstring}, 函数文档: {function_docstrings}, 类文档: {class_docstrings}") return True except Exception as e: print(f"[WARN] {filepath} - 无法检查文档: {e}") return False def check_type_hints(filepath): """检查类型注解""" try: with open(filepath, 'r', encoding='utf-8') as f: content = f.read() # 统计函数定义 def_count = len(re.findall(r'def\s+\w+', content)) # 统计类型注解 param_hints = len(re.findall(r'def\s+\w+\([^)]*:\s*\w+', content)) return_hints = len(re.findall(r'->\s*\w+', content)) coverage = (param_hints + return_hints) / (2 * def_count) * 100 if def_count > 0 else 0 print(f"[OK] {filepath} - 类型注解覆盖率: {coverage:.1f}%") return True except Exception as e: print(f"[WARN] {filepath} - 无法检查类型注解: {e}") return False def main(): """主验证函数""" print("=" * 60) print("Phase 7 代码质量验证") print("=" * 60) base_path = Path("C:/Users/Administrator/asset_management_backend") # 需要检查的文件列表 files_to_check = [ # 模型层 "app/models/system_config.py", "app/models/operation_log.py", "app/models/notification.py", # Schema层 "app/schemas/system_config.py", "app/schemas/operation_log.py", "app/schemas/notification.py", "app/schemas/statistics.py", # CRUD层 "app/crud/system_config.py", "app/crud/operation_log.py", "app/crud/notification.py", # 服务层 "app/services/system_config_service.py", "app/services/operation_log_service.py", "app/services/notification_service.py", "app/services/statistics_service.py", # API层 "app/api/v1/statistics.py", "app/api/v1/system_config.py", "app/api/v1/operation_logs.py", "app/api/v1/notifications.py", # 中间件 "app/middleware/operation_log.py", # 工具 "app/utils/redis_client.py", ] print("\n1. 检查文件是否存在") print("-" * 60) exist_results = [] for filepath in files_to_check: full_path = base_path / filepath exist_results.append(check_file_exists(str(full_path))) print("\n2. 检查文件语法") print("-" * 60) syntax_results = [] for filepath in files_to_check: full_path = base_path / filepath if os.path.exists(str(full_path)): syntax_results.append(check_file_syntax(str(full_path))) print("\n3. 检查文档注释") print("-" * 60) doc_results = [] for filepath in files_to_check: full_path = base_path / filepath if os.path.exists(str(full_path)): doc_results.append(check_docstring(str(full_path))) print("\n4. 检查类型注解") print("-" * 60) type_results = [] for filepath in files_to_check: full_path = base_path / filepath if os.path.exists(str(full_path)): type_results.append(check_type_hints(str(full_path))) # 统计结果 print("\n" + "=" * 60) print("验证结果汇总") print("=" * 60) total_files = len(files_to_check) existing_files = sum(exist_results) syntax_passed = sum(syntax_results) print(f"总文件数: {total_files}") print(f"文件存在: {existing_files}/{total_files} ({existing_files/total_files*100:.1f}%)") print(f"语法检查: {syntax_passed}/{existing_files} ({syntax_passed/existing_files*100 if existing_files > 0 else 0:.1f}%)") if existing_files == total_files and syntax_passed == existing_files: print("\n[SUCCESS] 所有检查通过!Phase 7 开发完成!") return 0 else: print("\n[FAIL] 部分检查未通过,请查看详情") return 1 if __name__ == "__main__": exit(main())