You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 
 

366 lines
8.2 KiB

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
打包脚本 - 将程序打包成ARM环境下的可执行文件
"""
import os
import subprocess
import sys
def check_pyinstaller():
"""检查PyInstaller是否已安装"""
try:
import PyInstaller
print(f"PyInstaller版本: {PyInstaller.__version__}")
return True
except ImportError:
print("PyInstaller未安装,正在安装...")
subprocess.check_call([sys.executable, "-m", "pip", "install", "pyinstaller"])
return True
def create_spec_file():
"""创建PyInstaller规格文件"""
spec_content = '''# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
a = Analysis(
['main.py'],
pathex=[],
binaries=[],
datas=[
('config.yaml', '.'),
('src', 'src'),
],
hiddenimports=[
'dmPython',
'pandas',
'numpy',
'loguru',
'fuzzywuzzy',
'python-Levenshtein',
'yaml',
'tqdm',
'openpyxl',
],
hookspath=[],
hooksconfig={},
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False,
)
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)
exe = EXE(
pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
[],
name='institution_matcher',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
upx_exclude=[],
runtime_tmpdir=None,
console=True,
disable_windowed_traceback=False,
argv_emulation=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None,
)
'''
with open('institution_matcher.spec', 'w', encoding='utf-8') as f:
f.write(spec_content)
print("已创建PyInstaller规格文件: institution_matcher.spec")
def build_executable():
"""构建可执行文件"""
print("开始构建可执行文件...")
# 使用PyInstaller构建
cmd = [
sys.executable, "-m", "PyInstaller",
"--onefile", # 打包成单个文件
"--console", # 控制台应用
"--name", "institution_matcher", # 可执行文件名称
"--add-data", "config.yaml:.", # 添加配置文件
"--add-data", "src:src", # 添加源代码目录
"--paths", "src", # 添加src目录到Python路径
"--hidden-import", "dmPython",
"--hidden-import", "pandas",
"--hidden-import", "numpy",
"--hidden-import", "loguru",
"--hidden-import", "fuzzywuzzy",
"--hidden-import", "fuzzywuzzy.fuzz",
"--hidden-import", "fuzzywuzzy.string_utils",
"--hidden-import", "python-Levenshtein",
"--hidden-import", "yaml",
"--hidden-import", "tqdm",
"--hidden-import", "openpyxl",
"main.py"
]
try:
subprocess.check_call(cmd)
print("可执行文件构建成功!")
return True
except subprocess.CalledProcessError as e:
print(f"构建失败: {e}")
return False
def build_for_arm():
"""为ARM架构构建可执行文件"""
print("为ARM架构构建可执行文件...")
# 检查是否在ARM环境下
import platform
machine = platform.machine()
print(f"当前架构: {machine}")
if 'arm' in machine.lower() or 'aarch64' in machine.lower():
print("检测到ARM架构,直接构建...")
return build_executable()
else:
print("当前不是ARM架构,将构建跨平台版本...")
# 对于非ARM环境,构建通用版本
return build_executable()
def create_install_script():
"""创建安装脚本"""
install_script = '''#!/bin/bash
# 机构名称匹配工具安装脚本
echo "正在安装机构名称匹配工具..."
# 创建安装目录
INSTALL_DIR="/usr/local/bin/institution_matcher"
sudo mkdir -p $INSTALL_DIR
# 复制可执行文件
sudo cp dist/institution_matcher $INSTALL_DIR/
sudo chmod +x $INSTALL_DIR/institution_matcher
# 创建配置文件目录
CONFIG_DIR="/etc/institution_matcher"
sudo mkdir -p $CONFIG_DIR
# 复制配置文件
sudo cp config.yaml $CONFIG_DIR/
# 创建软链接
sudo ln -sf $INSTALL_DIR/institution_matcher /usr/local/bin/institution_matcher
echo "安装完成!"
echo "使用方法: institution_matcher --help"
'''
with open('install.sh', 'w', encoding='utf-8') as f:
f.write(install_script)
# 设置执行权限
os.chmod('install.sh', 0o755)
print("已创建安装脚本: install.sh")
def create_dockerfile():
"""创建Dockerfile"""
dockerfile_content = '''FROM python:3.9-slim
# 设置工作目录
WORKDIR /app
# 安装系统依赖
RUN apt-get update && apt-get install -y \\
gcc \\
g++ \\
&& rm -rf /var/lib/apt/lists/*
# 复制依赖文件
COPY requirements.txt .
# 安装Python依赖
RUN pip install --no-cache-dir -r requirements.txt
# 复制应用代码
COPY . .
# 构建可执行文件
RUN python build.py
# 创建输出目录
RUN mkdir -p output logs temp
# 设置环境变量
ENV PYTHONPATH=/app/src
# 默认命令
CMD ["python", "main.py", "--help"]
'''
with open('Dockerfile', 'w', encoding='utf-8') as f:
f.write(dockerfile_content)
print("已创建Dockerfile")
def create_readme():
"""创建README文件"""
readme_content = '''# 新疆各政企单位机构名称匹配工具
## 项目简介
本项目用于匹配新疆各政企单位的机构名称,支持简称匹配和模糊匹配功能。
## 功能特性
- 支持达蒙DM数据库连接
- 智能机构名称匹配算法
- 简称自动识别和匹配
- 多种输出格式(CSV、Excel、JSON)
- 详细的匹配报告和统计
- 支持ARM架构部署
## 安装要求
- Python 3.8+
- 达蒙DM数据库驱动
- 相关Python依赖包
## 快速开始
### 1. 安装依赖
```bash
pip install -r requirements.txt
```
### 2. 配置数据库
编辑 `config.yaml` 文件,配置数据库连接信息:
```yaml
database:
host: "your_database_host"
port: 5236
username: "your_username"
password: "your_password"
database: "your_database"
```
### 3. 运行程序
```bash
python main.py
```
### 4. 使用打包版本
```bash
# 构建可执行文件
python build.py
# 运行可执行文件
./dist/institution_matcher
```
## 命令行参数
- `--config, -c`: 配置文件路径
- `--output-dir, -o`: 输出目录
- `--save-to-db`: 保存结果到数据库
- `--threshold, -t`: 相似度阈值
- `--max-matches, -m`: 最大匹配数
## 输出文件
- `matching_results_YYYYMMDD_HHMMSS.csv`: 匹配结果CSV文件
- `matching_results_YYYYMMDD_HHMMSS.xlsx`: 匹配结果Excel文件
- `matching_report_YYYYMMDD_HHMMSS.json`: 匹配报告JSON文件
## 匹配算法
- 完全匹配:100%相似度
- 高度匹配:85%-99%相似度
- 中度匹配:75%-84%相似度
- 低度匹配:70%-74%相似度
## 简称支持
支持常见机构简称,如:
- 妇女联合会 → 妇联
- 政治协商会议 → 政协
- 人民代表大会常务委员会 → 人大常委会
## 许可证
MIT License
'''
with open('README.md', 'w', encoding='utf-8') as f:
f.write(readme_content)
print("已创建README.md")
def main():
"""主函数"""
print("="*60)
print("机构名称匹配工具打包脚本")
print("="*60)
try:
# 1. 检查PyInstaller
if not check_pyinstaller():
print("PyInstaller安装失败")
return 1
# 2. 创建规格文件
create_spec_file()
# 3. 构建可执行文件
if not build_for_arm():
print("构建失败")
return 1
# 4. 创建安装脚本
create_install_script()
# 5. 创建Dockerfile
create_dockerfile()
# 6. 创建README
create_readme()
print("\n" + "="*60)
print("打包完成!")
print("可执行文件位置: dist/institution_matcher")
print("安装脚本: install.sh")
print("Dockerfile: Dockerfile")
print("README: README.md")
print("="*60)
return 0
except Exception as e:
print(f"打包过程中出现错误: {e}")
return 1
if __name__ == "__main__":
sys.exit(main())