verify_structure.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #!/usr/bin/env python3
  2. """
  3. 项目结构验证脚本
  4. 验证重构后的项目结构是否符合要求
  5. """
  6. import os
  7. import sys
  8. from pathlib import Path
  9. # 项目根目录
  10. PROJECT_ROOT = Path(__file__).parent.parent
  11. # 期望的目录结构
  12. EXPECTED_STRUCTURE = {
  13. "src/app/server": ["__init__.py", "app.py"],
  14. "src/app/logger": ["__init__.py", "loggering.py"],
  15. "src/app/base": [
  16. "__init__.py",
  17. "async_mysql_connection.py",
  18. "async_redis_connection.py",
  19. "milvus_connection.py"
  20. ],
  21. "src/app/core": ["__init__.py", "exceptions.py"],
  22. "src/app/models": [
  23. "__init__.py",
  24. "base.py",
  25. "user.py",
  26. "app.py",
  27. "token.py",
  28. "log.py",
  29. "knowledge_base.py"
  30. ],
  31. "src/app/schemas": [
  32. "__init__.py",
  33. "base.py",
  34. "user.py",
  35. "auth.py"
  36. ],
  37. "src/app/utils": ["__init__.py", "security.py"],
  38. "src/app/config": [
  39. "__init__.py",
  40. "settings.py",
  41. "database.py",
  42. "simple_settings.py"
  43. ],
  44. "src/app/system": ["__init__.py"],
  45. "src/app/system/models": ["__init__.py"],
  46. "src/app/system/schemas": ["__init__.py"],
  47. "src/app/oauth": ["__init__.py"],
  48. "src/app/oauth/models": ["__init__.py"],
  49. "src/app/oauth/schemas": ["__init__.py"],
  50. "src/app/sample": ["__init__.py"],
  51. "src/app/sample/models": ["__init__.py"],
  52. "src/app/sample/schemas": ["__init__.py"],
  53. "src/views": [
  54. "__init__.py",
  55. "system_view.py",
  56. "oauth_view.py",
  57. "sample_view.py"
  58. ],
  59. }
  60. # 期望的根目录文件
  61. EXPECTED_ROOT_FILES = [
  62. "run_server.py",
  63. "full_server.py",
  64. "README.md",
  65. "REFACTOR_README.md",
  66. "MIGRATION_GUIDE.md",
  67. ".env.example",
  68. ".gitignore"
  69. ]
  70. def check_directory_structure():
  71. """检查目录结构"""
  72. print("=" * 60)
  73. print("检查项目目录结构")
  74. print("=" * 60)
  75. all_passed = True
  76. for directory, files in EXPECTED_STRUCTURE.items():
  77. dir_path = PROJECT_ROOT / directory
  78. # 检查目录是否存在
  79. if not dir_path.exists():
  80. print(f"❌ 目录不存在: {directory}")
  81. all_passed = False
  82. continue
  83. print(f"✅ 目录存在: {directory}")
  84. # 检查文件是否存在
  85. for file in files:
  86. file_path = dir_path / file
  87. if not file_path.exists():
  88. print(f" ❌ 文件缺失: {directory}/{file}")
  89. all_passed = False
  90. else:
  91. print(f" ✅ 文件存在: {file}")
  92. return all_passed
  93. def check_root_files():
  94. """检查根目录文件"""
  95. print("\n" + "=" * 60)
  96. print("检查根目录文件")
  97. print("=" * 60)
  98. all_passed = True
  99. for file in EXPECTED_ROOT_FILES:
  100. file_path = PROJECT_ROOT / file
  101. if not file_path.exists():
  102. print(f"⚠️ 文件缺失: {file}")
  103. if file not in [".env.example"]: # 某些文件可选
  104. all_passed = False
  105. else:
  106. print(f"✅ 文件存在: {file}")
  107. return all_passed
  108. def check_imports():
  109. """检查关键模块是否可以导入"""
  110. print("\n" + "=" * 60)
  111. print("检查模块导入")
  112. print("=" * 60)
  113. # 添加 src 到路径
  114. sys.path.insert(0, str(PROJECT_ROOT / "src"))
  115. modules_to_check = [
  116. "app.server.app",
  117. "app.logger.loggering",
  118. "app.base",
  119. "app.core.exceptions",
  120. "app.system",
  121. "app.oauth",
  122. "app.sample",
  123. "views.system_view",
  124. "views.oauth_view",
  125. "views.sample_view",
  126. ]
  127. all_passed = True
  128. for module in modules_to_check:
  129. try:
  130. __import__(module)
  131. print(f"✅ 模块可导入: {module}")
  132. except ImportError as e:
  133. print(f"❌ 模块导入失败: {module}")
  134. print(f" 错误: {e}")
  135. all_passed = False
  136. except Exception as e:
  137. print(f"⚠️ 模块导入警告: {module}")
  138. print(f" 错误: {e}")
  139. return all_passed
  140. def check_module_structure():
  141. """检查模块结构是否符合要求"""
  142. print("\n" + "=" * 60)
  143. print("检查模块结构")
  144. print("=" * 60)
  145. modules = ["system", "oauth", "sample"]
  146. all_passed = True
  147. for module in modules:
  148. module_path = PROJECT_ROOT / "src" / "app" / module
  149. # 检查是否有 models 和 schemas 子目录
  150. models_path = module_path / "models"
  151. schemas_path = module_path / "schemas"
  152. if models_path.exists() and (models_path / "__init__.py").exists():
  153. print(f"✅ {module}/models 结构正确")
  154. else:
  155. print(f"❌ {module}/models 结构不正确")
  156. all_passed = False
  157. if schemas_path.exists() and (schemas_path / "__init__.py").exists():
  158. print(f"✅ {module}/schemas 结构正确")
  159. else:
  160. print(f"❌ {module}/schemas 结构不正确")
  161. all_passed = False
  162. return all_passed
  163. def main():
  164. """主函数"""
  165. print("\n" + "=" * 60)
  166. print("LQAdminPlatform 项目结构验证")
  167. print("=" * 60)
  168. print(f"项目根目录: {PROJECT_ROOT}")
  169. print("=" * 60 + "\n")
  170. results = []
  171. # 执行各项检查
  172. results.append(("目录结构", check_directory_structure()))
  173. results.append(("根目录文件", check_root_files()))
  174. results.append(("模块结构", check_module_structure()))
  175. results.append(("模块导入", check_imports()))
  176. # 输出总结
  177. print("\n" + "=" * 60)
  178. print("验证结果总结")
  179. print("=" * 60)
  180. all_passed = True
  181. for name, passed in results:
  182. status = "✅ 通过" if passed else "❌ 失败"
  183. print(f"{name}: {status}")
  184. if not passed:
  185. all_passed = False
  186. print("=" * 60)
  187. if all_passed:
  188. print("\n🎉 所有检查通过!项目结构符合要求。")
  189. return 0
  190. else:
  191. print("\n⚠️ 部分检查未通过,请查看上面的详细信息。")
  192. return 1
  193. if __name__ == "__main__":
  194. sys.exit(main())