setup.py•3.53 kB
#!/usr/bin/env python3
"""
MCP Server WeChat 安装脚本
"""
import os
import sys
import subprocess
from pathlib import Path
def run_command(command, description):
"""运行命令并处理错误"""
print(f"正在执行: {description}")
try:
result = subprocess.run(
command,
shell=True,
check=True,
capture_output=True,
text=True
)
print(f"✅ {description} 成功")
return True
except subprocess.CalledProcessError as e:
print(f"❌ {description} 失败")
print(f"错误: {e.stderr}")
return False
def check_python_version():
"""检查 Python 版本"""
version = sys.version_info
if version.major < 3 or (version.major == 3 and version.minor < 8):
print(f"❌ 需要 Python 3.8 或更高版本,当前版本: {version.major}.{version.minor}.{version.micro}")
return False
print(f"✅ Python 版本检查通过: {version.major}.{version.minor}.{version.micro}")
return True
def install_dependencies():
"""安装依赖"""
commands = [
("pip install --upgrade pip", "升级 pip"),
("pip install -r requirements.txt", "安装运行依赖"),
("pip install -r requirements-dev.txt", "安装开发依赖"),
]
for command, description in commands:
if not run_command(command, description):
return False
return True
def setup_browser():
"""设置浏览器"""
print("正在设置浏览器...")
try:
result = subprocess.run(
"playwright install chromium",
shell=True,
check=True,
capture_output=True,
text=True
)
print("✅ 浏览器设置成功")
return True
except subprocess.CalledProcessError as e:
print(f"❌ 浏览器设置失败")
print(f"错误: {e.stderr}")
return False
def setup_pre_commit():
"""设置预提交钩子"""
return run_command("pre-commit install", "设置预提交钩子")
def create_env_file():
"""创建环境变量文件"""
env_file = Path(".env")
if env_file.exists():
print("✅ .env 文件已存在,跳过创建")
return True
env_content = """# 日志级别
LOG_LEVEL=INFO
# 浏览器配置
HEADLESS=true
BROWSER_TIMEOUT=30
# 请求限制
MAX_CONCURRENT_REQUESTS=5
REQUEST_TIMEOUT=30
"""
try:
with open(env_file, "w") as f:
f.write(env_content)
print("✅ .env 文件创建成功")
return True
except Exception as e:
print(f"❌ .env 文件创建失败: {e}")
return False
def main():
"""主函数"""
print("🚀 开始安装 MCP Server WeChat")
print("=" * 50)
# 检查 Python 版本
if not check_python_version():
sys.exit(1)
# 安装依赖
if not install_dependencies():
sys.exit(1)
# 设置浏览器
if not setup_browser():
sys.exit(1)
# 设置预提交钩子
if not setup_pre_commit():
sys.exit(1)
# 创建环境变量文件
if not create_env_file():
sys.exit(1)
print("=" * 50)
print("🎉 MCP Server WeChat 安装完成!")
print("\n下一步:")
print("1. 根据需要修改 .env 文件")
print("2. 运行 'python -m src.mcp_server_wechat.server' 启动服务器")
print("3. 查看 README.md 了解使用方法")
if __name__ == "__main__":
main()