run_project.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. DataOps-platform 项目启动脚本
  5. """
  6. import os
  7. import sys
  8. import subprocess
  9. import platform
  10. def check_python_version():
  11. """检查Python版本"""
  12. if sys.version_info < (3, 8):
  13. print("错误: 需要Python 3.8或更高版本")
  14. print(f"当前版本: {sys.version}")
  15. return False
  16. print(f"Python版本: {sys.version}")
  17. return True
  18. def create_virtual_env():
  19. """创建虚拟环境"""
  20. if not os.path.exists("venv"):
  21. print("创建虚拟环境...")
  22. try:
  23. subprocess.run([sys.executable, "-m", "venv", "venv"], check=True)
  24. print("虚拟环境创建成功")
  25. except subprocess.CalledProcessError:
  26. print("错误: 创建虚拟环境失败")
  27. return False
  28. else:
  29. print("虚拟环境已存在")
  30. return True
  31. def activate_virtual_env():
  32. """激活虚拟环境"""
  33. if platform.system() == "Windows":
  34. activate_script = os.path.join("venv", "Scripts", "activate.bat")
  35. if os.path.exists(activate_script):
  36. print("激活虚拟环境...")
  37. # 在Windows上,我们需要使用cmd来运行activate.bat
  38. os.system(f'cmd /c "{activate_script} && pip install -r requirements.txt"')
  39. else:
  40. print("错误: 虚拟环境激活脚本未找到")
  41. return False
  42. else:
  43. activate_script = os.path.join("venv", "bin", "activate")
  44. if os.path.exists(activate_script):
  45. print("激活虚拟环境...")
  46. os.system(f"source {activate_script} && pip install -r requirements.txt")
  47. else:
  48. print("错误: 虚拟环境激活脚本未找到")
  49. return False
  50. return True
  51. def install_dependencies():
  52. """安装项目依赖"""
  53. print("安装项目依赖...")
  54. try:
  55. if platform.system() == "Windows":
  56. subprocess.run([os.path.join("venv", "Scripts", "python.exe"), "-m", "pip", "install", "-r", "requirements.txt"], check=True)
  57. else:
  58. subprocess.run([os.path.join("venv", "bin", "python"), "-m", "pip", "install", "-r", "requirements.txt"], check=True)
  59. print("依赖安装成功")
  60. return True
  61. except subprocess.CalledProcessError:
  62. print("错误: 依赖安装失败")
  63. return False
  64. def set_environment_variables():
  65. """设置环境变量"""
  66. os.environ["FLASK_ENV"] = "development"
  67. os.environ["FLASK_APP"] = "application.py"
  68. os.environ["FLASK_DEBUG"] = "1"
  69. print("环境变量设置完成")
  70. def run_application():
  71. """运行应用"""
  72. print("启动DataOps-platform...")
  73. print("访问地址: http://localhost:5500")
  74. print("按 Ctrl+C 停止应用")
  75. print("-" * 50)
  76. try:
  77. if platform.system() == "Windows":
  78. subprocess.run([os.path.join("venv", "Scripts", "python.exe"), "application.py"])
  79. else:
  80. subprocess.run([os.path.join("venv", "bin", "python"), "application.py"])
  81. except KeyboardInterrupt:
  82. print("\n应用已停止")
  83. except Exception as e:
  84. print(f"启动应用时出错: {e}")
  85. def main():
  86. """主函数"""
  87. print("=" * 50)
  88. print("DataOps-platform 项目启动器")
  89. print("=" * 50)
  90. # 检查Python版本
  91. if not check_python_version():
  92. return
  93. # 创建虚拟环境
  94. if not create_virtual_env():
  95. return
  96. # 安装依赖
  97. if not install_dependencies():
  98. return
  99. # 设置环境变量
  100. set_environment_variables()
  101. # 运行应用
  102. run_application()
  103. if __name__ == "__main__":
  104. main()