Skip to content

Commit b90bd46

Browse files
committed
mac:build.sh
1 parent b25b540 commit b90bd46

File tree

5 files changed

+346
-27
lines changed

5 files changed

+346
-27
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ config.json
1010
/build
1111
.DS_Store
1212
/venv
13+
.DS_Store

Build_Instructions.md

Lines changed: 87 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ This document explains how to package `main.py` and `main_sse.py` into standalon
1010

1111
### Build Scripts
1212
- `build.bat` - Windows batch script for automated build process
13-
- `build.ps1` - PowerShell script for automated build process (recommended)
13+
- `build.ps1` - PowerShell script for automated build process (Windows recommended)
14+
- `build.sh` - Shell script for automated build process (macOS recommended)
1415

15-
## Usage
16+
## Windows Usage
1617

1718
### Method 1: Using Batch Script (Recommended)
1819
1. Double-click to run `build.bat`
@@ -44,26 +45,81 @@ pyinstaller build_main.spec --clean --noconfirm
4445
pyinstaller build_main_sse.spec --clean --noconfirm
4546
```
4647

48+
## macOS Usage
49+
50+
### Method 1: Using Shell Script (Recommended)
51+
1. Open terminal and navigate to project directory
52+
2. Execute the following commands:
53+
```bash
54+
chmod +x build.sh
55+
./build.sh
56+
```
57+
3. The script will automatically:
58+
- Check and install PyInstaller (if not installed)
59+
- Clean previous build files
60+
- Package both programs sequentially
61+
- Display build results
62+
63+
### Method 2: Manual Build
64+
If the automated script encounters issues, you can manually execute the following commands:
65+
66+
```bash
67+
# Install PyInstaller (if not installed)
68+
pip3 install pyinstaller
69+
70+
# Clean previous build files
71+
rm -rf dist
72+
rm -rf build
73+
74+
# Package main.py
75+
pyinstaller build_main.spec --clean --noconfirm
76+
77+
# Package main_sse.py
78+
pyinstaller build_main_sse.spec --clean --noconfirm
79+
```
80+
4781
## Output Files
4882

83+
### Windows
4984
After successful packaging, two executable files will be generated in the `dist` directory:
5085

5186
- `UserBank_Stdio_Core.exe` - Standard MCP server based on `main.py`
5287
- `UserBank_SSE_Core.exe` - SSE mode server based on `main_sse.py`
5388

89+
### macOS
90+
After successful packaging, two executable files will be generated in the `dist` directory:
91+
92+
- `UserBank_Stdio_Core` - Standard MCP server based on `main.py`
93+
- `UserBank_SSE_Core` - SSE mode server based on `main_sse.py`
94+
5495
## Running Instructions
5596

56-
### UserBank_Stdio_Core.exe
97+
### Windows
98+
99+
#### UserBank_Stdio_Core.exe
57100
- This is the standard MCP server
58101
- Double-click to run and start the server
59102
- Suitable for MCP client connections
60103

61-
### UserBank_SSE_Core.exe
104+
#### UserBank_SSE_Core.exe
62105
- This is the SSE mode HTTP server
63106
- Double-click to start the web server
64107
- Default listening port is determined by configuration file
65108
- Supports CORS and can be accessed through browsers
66109

110+
### macOS
111+
112+
#### UserBank_Stdio_Core
113+
- This is the standard MCP server
114+
- Run in terminal: `./dist/UserBank_Stdio_Core`
115+
- Suitable for MCP client connections
116+
117+
#### UserBank_SSE_Core
118+
- This is the SSE mode HTTP server
119+
- Run in terminal: `./dist/UserBank_SSE_Core`
120+
- Default listening port is determined by configuration file
121+
- Supports CORS and can be accessed through browsers
122+
67123
## Important Notes
68124

69125
1. **Dependency Inclusion**: The packaging process automatically includes all necessary dependency files:
@@ -73,28 +129,34 @@ After successful packaging, two executable files will be generated in the `dist`
73129

74130
2. **Hidden Imports**: Configuration files include all necessary hidden imports to ensure no module missing issues at runtime
75131

76-
3. **File Size**: Generated exe files may be large (typically 50-100MB), which is normal as they contain the complete Python runtime and all dependencies
132+
3. **File Size**: Generated executable files may be large (typically 50-100MB), which is normal as they contain the complete Python runtime and all dependencies
77133

78-
4. **Runtime Environment**: Generated exe files can run on any Windows machine without requiring Python installation
134+
4. **Runtime Environment**:
135+
- Windows: Generated exe files can run on any Windows machine without requiring Python installation
136+
- macOS: Generated executables can run on any macOS machine without requiring Python installation
79137

80-
5. **Configuration Files**: Ensure configuration files and database files are in the same directory as the exe files or in correct relative paths
138+
5. **Configuration Files**: Ensure configuration files and database files are in the same directory as the executable files or in correct relative paths
81139

82140
## Troubleshooting
83141

84142
### If Build Fails
85-
1. Ensure all dependencies are properly installed: `pip install -r requirements.txt`
143+
1. Ensure all dependencies are properly installed:
144+
- Windows: `pip install -r requirements.txt`
145+
- macOS: `pip3 install -r requirements.txt`
86146
2. Check for syntax errors or import errors
87147
3. Review PyInstaller's detailed error messages
88148

89149
### If Runtime Errors Occur
90150
1. Check if configuration file paths are correct
91151
2. Ensure database files exist and are accessible
92-
3. Review error messages in console output
152+
3. Review error messages in console/terminal output
93153

94154
### Common Issues
95155
- **Module Not Found**: Check if `hiddenimports` list includes all necessary modules
96156
- **File Path Errors**: Ensure file paths in `datas` list are correct
97-
- **Permission Issues**: Ensure sufficient permissions to create and run exe files
157+
- **Permission Issues**:
158+
- Windows: Ensure sufficient permissions to create and run exe files
159+
- macOS: Ensure sufficient permissions to create and run executables, may need to use `chmod +x` command
98160

99161
## Custom Configuration
100162

@@ -104,4 +166,18 @@ If you need to modify the packaging configuration, you can edit the `.spec` file
104166
- Add `icon` field to set program icon
105167
- Modify `console` field to control console window visibility
106168
- Add additional data files in `datas`
107-
- Add additional hidden import modules in `hiddenimports`
169+
- Add additional hidden import modules in `hiddenimports`
170+
171+
## Security Considerations
172+
173+
1. **Code Signing**:
174+
- Windows: Digital certificate signing recommended
175+
- macOS: Code signing recommended to avoid security warnings
176+
2. **Permission Settings**: Ensure application has appropriate file system access permissions
177+
3. **Firewall Settings**: If using SSE mode, ensure firewall allows application network access
178+
179+
## Performance Optimization
180+
181+
1. **UPX Compression**: UPX compression enabled by default to reduce file size
182+
2. **Resource Optimization**: Resource inclusion can be optimized by modifying `.spec` files
183+
3. **Launch Optimization**: Launch time can be optimized by adjusting PyInstaller parameters

build.sh

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
#!/bin/bash
2+
3+
echo "========================================"
4+
echo "Personal Profile MCP 打包脚本"
5+
echo "========================================"
6+
7+
# 检查 Python3 是否安装
8+
if ! command -v python3 &> /dev/null; then
9+
echo "错误:未找到 Python3,请先安装 Python3"
10+
exit 1
11+
fi
12+
13+
# 检查 venv 模块是否可用
14+
if ! python3 -c "import venv" &> /dev/null; then
15+
echo "错误:Python3 venv 模块不可用,请安装 python3-venv"
16+
exit 1
17+
fi
18+
19+
# 创建虚拟环境
20+
echo ""
21+
echo "检查虚拟环境..."
22+
if [ -d "venv" ]; then
23+
echo "虚拟环境已存在,直接使用..."
24+
else
25+
echo "创建新的虚拟环境..."
26+
python3 -m venv venv
27+
if [ $? -ne 0 ]; then
28+
echo "创建虚拟环境失败!"
29+
exit 1
30+
fi
31+
fi
32+
33+
# 激活虚拟环境
34+
echo "激活虚拟环境..."
35+
source venv/bin/activate
36+
if [ $? -ne 0 ]; then
37+
echo "激活虚拟环境失败!"
38+
exit 1
39+
fi
40+
41+
# 升级 pip
42+
echo "升级 pip..."
43+
python -m pip install --upgrade pip
44+
45+
# 安装依赖
46+
echo ""
47+
echo "安装项目依赖..."
48+
if [ -f "requirements.txt" ]; then
49+
# 检查是否已经安装了所有依赖
50+
if ! pip freeze | grep -q -f requirements.txt; then
51+
echo "安装/更新依赖..."
52+
pip install -r requirements.txt
53+
if [ $? -ne 0 ]; then
54+
echo "安装依赖失败!"
55+
exit 1
56+
fi
57+
else
58+
echo "依赖已安装,跳过安装步骤"
59+
fi
60+
else
61+
echo "警告:未找到 requirements.txt 文件"
62+
fi
63+
64+
# 安装 PyInstaller
65+
echo ""
66+
echo "检查 PyInstaller 是否已安装..."
67+
if ! pip show pyinstaller >/dev/null 2>&1; then
68+
echo "PyInstaller 未安装,正在安装..."
69+
pip install pyinstaller
70+
if [ $? -ne 0 ]; then
71+
echo "PyInstaller 安装失败!"
72+
exit 1
73+
fi
74+
else
75+
echo "PyInstaller 已安装"
76+
fi
77+
78+
echo ""
79+
echo "清理之前的构建文件..."
80+
rm -rf dist
81+
rm -rf build
82+
83+
echo ""
84+
echo "========================================"
85+
echo "开始打包 main.py (UserBank_Stdio_Core)"
86+
echo "========================================"
87+
pyinstaller build_main.spec --clean --noconfirm
88+
if [ $? -ne 0 ]; then
89+
echo "main.py 打包失败!"
90+
exit 1
91+
fi
92+
93+
echo ""
94+
echo "========================================"
95+
echo "开始打包 main_sse.py (UserBank_SSE_Core)"
96+
echo "========================================"
97+
pyinstaller build_main_sse.spec --clean --noconfirm
98+
if [ $? -ne 0 ]; then
99+
echo "main_sse.py 打包失败!"
100+
exit 1
101+
fi
102+
103+
# 代码签名部分
104+
echo ""
105+
echo "========================================"
106+
echo "开始代码签名"
107+
echo "========================================"
108+
109+
# 检查是否有开发者证书
110+
if security find-identity -v -p codesigning | grep -q "Developer ID Application"; then
111+
# 获取开发者证书
112+
CERTIFICATE=$(security find-identity -v -p codesigning | grep "Developer ID Application" | head -n 1 | awk -F'"' '{print $2}')
113+
114+
echo "使用证书: $CERTIFICATE"
115+
116+
# 签名 UserBank_Stdio_Core
117+
echo "签名 UserBank_Stdio_Core..."
118+
codesign --force --deep --sign "$CERTIFICATE" "dist/UserBank_Stdio_Core"
119+
if [ $? -ne 0 ]; then
120+
echo "UserBank_Stdio_Core 签名失败!"
121+
exit 1
122+
fi
123+
124+
# 签名 UserBank_SSE_Core
125+
echo "签名 UserBank_SSE_Core..."
126+
codesign --force --deep --sign "$CERTIFICATE" "dist/UserBank_SSE_Core"
127+
if [ $? -ne 0 ]; then
128+
echo "UserBank_SSE_Core 签名失败!"
129+
exit 1
130+
fi
131+
132+
# 验证签名
133+
echo "验证签名..."
134+
codesign --verify --deep --strict "dist/UserBank_Stdio_Core"
135+
codesign --verify --deep --strict "dist/UserBank_SSE_Core"
136+
137+
echo "代码签名完成!"
138+
else
139+
echo "警告:未找到开发者证书,跳过签名步骤"
140+
echo "如需签名,请先安装开发者证书"
141+
fi
142+
143+
echo ""
144+
echo "========================================"
145+
echo "打包完成!"
146+
echo "========================================"
147+
echo ""
148+
echo "生成的文件位置:"
149+
echo "- UserBank_Stdio_Core: dist/UserBank_Stdio_Core"
150+
echo "- UserBank_SSE_Core: dist/UserBank_SSE_Core"
151+
echo ""
152+
echo "您可以将这些可执行文件复制到任何 macOS 机器上运行。"
153+
echo "注意:如果未签名,首次运行时可能需要执行 chmod +x 命令赋予执行权限。"
154+
echo ""
155+
156+
# 自动赋予执行权限
157+
chmod +x dist/UserBank_Stdio_Core
158+
chmod +x dist/UserBank_SSE_Core
159+
160+
echo "已自动赋予执行权限。"
161+
162+
# 退出虚拟环境
163+
deactivate
164+
165+
echo "按回车键退出..."
166+
read

main_sse.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,8 @@ def get_table_schema(table_name: str = None) -> Dict[str, Any]:
276276
# Get server configuration from config file
277277
host = config_manager.get_server_host()
278278
port = config_manager.get_server_port()
279-
280-
print(f"Server will start at {host}:{port}")
281-
279+
print("\n\n")
280+
print(f"Server will start at {host}:{port}/sse")
281+
print("\n\n")
282282
# Start server using uvicorn
283283
uvicorn.run(http_app, host=host, port=port)

0 commit comments

Comments
 (0)