初次提交
This commit is contained in:
186
backend/build.py
Normal file
186
backend/build.py
Normal file
@@ -0,0 +1,186 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
云盘应用打包脚本
|
||||
用于将FastAPI应用打包为可执行文件
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
|
||||
def clean_build():
|
||||
"""清理之前的构建文件"""
|
||||
print("清理之前的构建文件...")
|
||||
|
||||
# 清理PyInstaller生成的文件
|
||||
dirs_to_clean = ['build', 'dist', '__pycache__']
|
||||
for dir_name in dirs_to_clean:
|
||||
if os.path.exists(dir_name):
|
||||
shutil.rmtree(dir_name)
|
||||
print(f" ✅ 已删除: {dir_name}")
|
||||
|
||||
# 清理.spec文件
|
||||
if os.path.exists('main.spec'):
|
||||
os.remove('main.spec')
|
||||
print(f" ✅ 已删除: main.spec")
|
||||
|
||||
def check_dependencies():
|
||||
"""检查依赖是否安装"""
|
||||
print("📦 检查依赖...")
|
||||
|
||||
required_packages = ['fastapi', 'uvicorn', 'pydantic', 'sqlalchemy', 'loguru']
|
||||
missing_packages = []
|
||||
|
||||
for package in required_packages:
|
||||
try:
|
||||
__import__(package)
|
||||
print(f" ✅ {package}")
|
||||
except ImportError:
|
||||
missing_packages.append(package)
|
||||
print(f" ❌ {package} (缺失)")
|
||||
|
||||
if missing_packages:
|
||||
print(f"\n❌ 缺少以下依赖: {', '.join(missing_packages)}")
|
||||
print("请运行: pip install -r requirements.txt")
|
||||
return False
|
||||
|
||||
print("✅ 所有依赖都已安装")
|
||||
return True
|
||||
|
||||
def build_executable():
|
||||
"""构建可执行文件"""
|
||||
print("🔨 开始构建可执行文件...")
|
||||
|
||||
# 使用自定义的spec文件进行构建
|
||||
import subprocess
|
||||
result = subprocess.run([
|
||||
sys.executable, '-m', 'PyInstaller',
|
||||
'build.spec',
|
||||
'--clean',
|
||||
'--noconfirm'
|
||||
], capture_output=True, text=True)
|
||||
|
||||
if result.returncode == 0:
|
||||
print("✅ 构建成功!")
|
||||
print(f"📁 可执行文件位置: {os.path.abspath('dist/cloud-drive-server.exe')}")
|
||||
return True
|
||||
else:
|
||||
print("❌ 构建失败!")
|
||||
print("错误信息:")
|
||||
print(result.stderr)
|
||||
return False
|
||||
|
||||
def create_deployment_package():
|
||||
"""创建部署包"""
|
||||
print("📦 创建部署包...")
|
||||
|
||||
dist_dir = Path('dist')
|
||||
deploy_dir = Path('deploy')
|
||||
|
||||
# 创建部署目录
|
||||
if deploy_dir.exists():
|
||||
shutil.rmtree(deploy_dir)
|
||||
deploy_dir.mkdir()
|
||||
|
||||
# 复制可执行文件
|
||||
exe_path = dist_dir / 'cloud-drive-server.exe'
|
||||
if exe_path.exists():
|
||||
shutil.copy2(exe_path, deploy_dir / 'cloud-drive-server.exe')
|
||||
print(" ✅ 复制可执行文件")
|
||||
|
||||
# 复制配置文件
|
||||
config_files = ['requirements.txt', '.env.example']
|
||||
for config_file in config_files:
|
||||
if os.path.exists(config_file):
|
||||
shutil.copy2(config_file, deploy_dir / config_file)
|
||||
print(f" ✅ 复制配置文件: {config_file}")
|
||||
|
||||
# 创建启动脚本
|
||||
start_script = deploy_dir / 'start.bat'
|
||||
with open(start_script, 'w', encoding='utf-8') as f:
|
||||
f.write("""@echo off
|
||||
echo 🚀 启动云盘服务器...
|
||||
echo 📝 确保MySQL和Redis服务已启动
|
||||
echo.
|
||||
cloud-drive-server.exe
|
||||
pause
|
||||
""")
|
||||
print(" ✅ 创建启动脚本: start.bat")
|
||||
|
||||
# 创建README
|
||||
readme_path = deploy_dir / 'README.md'
|
||||
with open(readme_path, 'w', encoding='utf-8') as f:
|
||||
f.write("""# 云盘应用部署包
|
||||
|
||||
## 快速启动
|
||||
|
||||
1. **确保数据库和缓存服务运行**
|
||||
- MySQL服务器已启动
|
||||
- Redis服务器已启动(可选)
|
||||
|
||||
2. **配置环境变量**
|
||||
- 复制 `.env.example` 为 `.env`
|
||||
- 修改 `.env` 中的数据库连接信息
|
||||
|
||||
3. **启动应用**
|
||||
- Windows: 双击 `start.bat` 或运行 `cloud-drive-server.exe`
|
||||
- 访问 http://localhost:8000
|
||||
|
||||
## 配置说明
|
||||
|
||||
在 `.env` 文件中配置以下参数:
|
||||
|
||||
```env
|
||||
# 数据库配置
|
||||
DATABASE_URL=mysql+pymysql://username:password@localhost:3306/database_name
|
||||
|
||||
# JWT密钥
|
||||
SECRET_KEY=your-secret-key-here
|
||||
|
||||
# 其他配置...
|
||||
```
|
||||
|
||||
## API文档
|
||||
|
||||
启动后访问:
|
||||
- Swagger UI: http://localhost:8000/docs
|
||||
- ReDoc: http://localhost:8000/redoc
|
||||
|
||||
## 故障排除
|
||||
|
||||
1. **端口被占用**: 修改 `.env` 中的 `PORT` 配置
|
||||
2. **数据库连接失败**: 检查MySQL服务状态和连接配置
|
||||
3. **缺少依赖**: 确保所有依赖已正确安装
|
||||
""")
|
||||
print(" ✅ 创建README文档")
|
||||
|
||||
print(f"📁 部署包位置: {deploy_dir.absolute()}")
|
||||
return True
|
||||
|
||||
def main():
|
||||
"""主函数"""
|
||||
print("🏗️ 云盘应用打包工具")
|
||||
print("=" * 50)
|
||||
|
||||
# 1. 检查依赖
|
||||
if not check_dependencies():
|
||||
sys.exit(1)
|
||||
|
||||
# 2. 清理之前的构建
|
||||
clean_build()
|
||||
|
||||
# 3. 构建可执行文件
|
||||
if not build_executable():
|
||||
sys.exit(1)
|
||||
|
||||
# 4. 创建部署包
|
||||
if not create_deployment_package():
|
||||
sys.exit(1)
|
||||
|
||||
print("\n🎉 打包完成!")
|
||||
print("📁 部署包位于 'deploy' 目录")
|
||||
print("🚀 可以将整个 deploy 文件夹复制到目标服务器运行")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user