#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ DataOps-platform 项目启动脚本 """ import os import sys import subprocess import platform def check_python_version(): """检查Python版本""" if sys.version_info < (3, 8): print("错误: 需要Python 3.8或更高版本") print(f"当前版本: {sys.version}") return False print(f"Python版本: {sys.version}") return True def create_virtual_env(): """创建虚拟环境""" if not os.path.exists("venv"): print("创建虚拟环境...") try: subprocess.run([sys.executable, "-m", "venv", "venv"], check=True) print("虚拟环境创建成功") except subprocess.CalledProcessError: print("错误: 创建虚拟环境失败") return False else: print("虚拟环境已存在") return True def activate_virtual_env(): """激活虚拟环境""" if platform.system() == "Windows": activate_script = os.path.join("venv", "Scripts", "activate.bat") if os.path.exists(activate_script): print("激活虚拟环境...") # 在Windows上,我们需要使用cmd来运行activate.bat os.system(f'cmd /c "{activate_script} && pip install -r requirements.txt"') else: print("错误: 虚拟环境激活脚本未找到") return False else: activate_script = os.path.join("venv", "bin", "activate") if os.path.exists(activate_script): print("激活虚拟环境...") os.system(f"source {activate_script} && pip install -r requirements.txt") else: print("错误: 虚拟环境激活脚本未找到") return False return True def install_dependencies(): """安装项目依赖""" print("安装项目依赖...") try: if platform.system() == "Windows": subprocess.run([os.path.join("venv", "Scripts", "python.exe"), "-m", "pip", "install", "-r", "requirements.txt"], check=True) else: subprocess.run([os.path.join("venv", "bin", "python"), "-m", "pip", "install", "-r", "requirements.txt"], check=True) print("依赖安装成功") return True except subprocess.CalledProcessError: print("错误: 依赖安装失败") return False def set_environment_variables(): """设置环境变量""" os.environ["FLASK_ENV"] = "development" os.environ["FLASK_APP"] = "application.py" os.environ["FLASK_DEBUG"] = "1" print("环境变量设置完成") def run_application(): """运行应用""" print("启动DataOps-platform...") print("访问地址: http://localhost:5500") print("按 Ctrl+C 停止应用") print("-" * 50) try: if platform.system() == "Windows": subprocess.run([os.path.join("venv", "Scripts", "python.exe"), "application.py"]) else: subprocess.run([os.path.join("venv", "bin", "python"), "application.py"]) except KeyboardInterrupt: print("\n应用已停止") except Exception as e: print(f"启动应用时出错: {e}") def main(): """主函数""" print("=" * 50) print("DataOps-platform 项目启动器") print("=" * 50) # 检查Python版本 if not check_python_version(): return # 创建虚拟环境 if not create_virtual_env(): return # 安装依赖 if not install_dependencies(): return # 设置环境变量 set_environment_variables() # 运行应用 run_application() if __name__ == "__main__": main()