Optimize Health Check Procedures

This commit is contained in:
马一丁
2025-11-26 00:45:17 +08:00
parent e417214523
commit b41a783d52
+87 -61
View File
@@ -9,6 +9,13 @@ from pathlib import Path
from loguru import logger from loguru import logger
from ctypes import util as ctypes_util from ctypes import util as ctypes_util
BOX_CONTENT_WIDTH = 62
def _box_line(text: str = "") -> str:
"""Render a single line inside the 66-char help box."""
return f"{text:<{BOX_CONTENT_WIDTH}}\n"
def _get_platform_specific_instructions(): def _get_platform_specific_instructions():
""" """
@@ -19,55 +26,71 @@ def _get_platform_specific_instructions():
""" """
system = platform.system() system = platform.system()
def _box_lines(lines):
return "".join(_box_line(line) for line in lines)
if system == "Darwin": # macOS if system == "Darwin": # macOS
return ( return _box_lines(
"║ 🍎 macOS 系统解决方案: ║\n" [
"║ ║\n" "🍎 macOS 系统解决方案:",
"║ 1. 安装系统依赖: ║\n" "",
"║ brew install pango gdk-pixbuf libffi ║\n" "步骤 1: 安装依赖(宿主机执行)",
"║ ║\n" " brew install pango gdk-pixbuf libffi",
"║ 2. 设置环境变量(重要!): ║\n" "",
"║ Apple Silicon: export DYLD_LIBRARY_PATH=/opt/homebrew/lib:$DYLD_LIBRARY_PATH ║\n" "步骤 2: 设置 DYLD_LIBRARY_PATH(必做)",
"║ Intel Mac: export DYLD_LIBRARY_PATH=/usr/local/lib:$DYLD_LIBRARY_PATH ║\n" " Apple Silicon:",
"║ ║\n" " export DYLD_LIBRARY_PATH=/opt/homebrew/lib:$DYLD_LIBRARY_PATH",
"║ 3. 永久生效(推荐): ║\n" " Intel:",
"║ echo 'export DYLD_LIBRARY_PATH=/opt/homebrew/lib:$DYLD_LIBRARY_PATH' >> ~/.zshrc ║\n" " export DYLD_LIBRARY_PATH=/usr/local/lib:$DYLD_LIBRARY_PATH",
"║ 或 echo 'export DYLD_LIBRARY_PATH=/usr/local/lib:$DYLD_LIBRARY_PATH' >> ~/.zshrc ║\n" "",
"║ source ~/.zshrc ║\n" "步骤 3: 永久生效(推荐)",
" 将 export DYLD_LIBRARY_PATH=... 追加到 ~/.zshrc",
" Apple 用 /opt/homebrew/libIntel 用 /usr/local/lib",
" 执行 source ~/.zshrc 后再打开新终端",
"",
"步骤 4: 新开终端执行验证",
" python -m ReportEngine.utils.dependency_check",
" 输出含 “✓ Pango 依赖检测通过” 即配置正确",
]
) )
elif system == "Linux": elif system == "Linux":
return ( return _box_lines(
"║ 🐧 Linux 系统解决方案: ║\n" [
"║ ║\n" "🐧 Linux 系统解决方案:",
"║ Ubuntu/Debian: ║\n" "",
"║ sudo apt-get install libpango-1.0-0 libpangoft2-1.0-0 \\\n" "Ubuntu/Debian(宿主机执行):",
"║ libgdk-pixbuf2.0-0 libffi-dev libcairo2 ║\n" " sudo apt-get update",
" \n" " sudo apt-get install -y \\",
"║ CentOS/RHEL: ║\n" " libpango-1.0-0 libpangoft2-1.0-0 libffi-dev libcairo2",
" sudo yum install pango gdk-pixbuf2 libffi-devel cairo ║\n" " libgdk-pixbuf-2.0-0(缺失时改为 libgdk-pixbuf2.0-0",
"║ ║\n" "",
"║ 若仍提示缺库:export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH ║\n" "CentOS/RHEL",
" sudo ldconfig ║\n" " sudo yum install -y pango gdk-pixbuf2 libffi-devel cairo",
"",
"Docker 部署无需额外安装,镜像已包含依赖",
]
) )
elif system == "Windows": elif system == "Windows":
return ( return _box_lines(
"║ 🪟 Windows 系统解决方案: ║\n" [
"║ ║\n" "🪟 Windows 系统解决方案:",
"║ 1. 安装 GTK3 Runtime\n" "",
"║ https://github.com/tschoonj/GTK-for-Windows-Runtime-Environment-Installer/releases ║\n" "步骤 1: 安装 GTK3 Runtime(宿主机执行)",
"║ ║\n" " 下载页: README 中的 GTK3 Runtime 链接(建议默认路径)",
"║ 2. 将 GTK 安装目录下的 bin 加入 PATH(需新开终端): ║\n" "",
"║ set PATH=C:\\Program Files\\GTK3-Runtime Win64\\bin;%PATH%\n" "步骤 2: 将 GTK 安装目录下的 bin 加入 PATH(需新终端)",
" (若自定义路径,请替换为实际安装路径) ║\n" " set PATH=C:\\Program Files\\GTK3-Runtime Win64\\bin;%PATH%",
"║ ║\n" " 自定义路径请替换,或设置环境变量 GTK_BIN_PATH",
" 3. 验证:在新终端运行 ║\n" " 可选: 永久添加 PATH 示例:",
" python -m ReportEngine.utils.dependency_check ║\n" " setx PATH \"C:\\Program Files\\GTK3-Runtime Win64\\bin;%PATH%\"",
"║ 看到 ✓ 提示即表示 PDF 导出可用 ║\n" "",
"步骤 3: 验证(新终端执行)",
" python -m ReportEngine.utils.dependency_check",
" 输出含 “✓ Pango 依赖检测通过” 即配置正确",
]
) )
else: else:
return ( return _box_lines(["请查看 PDF 导出 README 了解您系统的安装方法"])
"║ 请查看 README.md 了解您系统的安装方法 ║\n"
)
def _ensure_windows_gtk_paths(): def _ensure_windows_gtk_paths():
@@ -247,34 +270,37 @@ def check_pango_available():
platform_instructions = _get_platform_specific_instructions() platform_instructions = _get_platform_specific_instructions()
windows_hint = "" windows_hint = ""
if platform.system() == "Windows": if platform.system() == "Windows":
prefix = "已尝试自动添加 GTK 路径: "
max_path_len = BOX_CONTENT_WIDTH - len(prefix)
path_display = added_path or "未找到默认路径" path_display = added_path or "未找到默认路径"
# 控制长度,避免破坏提示框宽度 if len(path_display) > max_path_len:
if len(path_display) > 38: path_display = path_display[: max_path_len - 3] + "..."
path_display = path_display[:35] + "..." windows_hint = _box_line(prefix + path_display)
windows_hint = f"║ 已尝试自动添加 GTK 路径: {path_display:<38}\n" arch_note = _box_line("🔍 若已安装仍报错:确认 Python 与 GTK 位数一致后重开终端")
arch_note = "║ 🔍 若已安装仍报错:确认 Python/GTK 位数一致,重开终端 ║\n"
else: else:
arch_note = "" arch_note = ""
missing_note = "" missing_note = ""
if missing_native: if missing_native:
missing_str = ", ".join(missing_native) missing_str = ", ".join(missing_native)
missing_note = f"未识别到的依赖: {missing_str:<46}\n" missing_note = _box_line(f"未识别到的依赖: {missing_str}")
if 'gobject' in error_msg.lower() or 'pango' in error_msg.lower() or 'gdk' in error_msg.lower(): if 'gobject' in error_msg.lower() or 'pango' in error_msg.lower() or 'gdk' in error_msg.lower():
box_top = "" + "" * 64 + "\n"
box_bottom = "" + "" * 64 + ""
return False, ( return False, (
"╔════════════════════════════════════════════════════════════════╗\n" box_top
"⚠️ PDF 导出依赖缺失\n" + _box_line("⚠️ PDF 导出依赖缺失")
"║ ║\n" + _box_line()
"📄 PDF 导出功能将不可用(其他功能不受影响)\n" + _box_line("📄 PDF 导出功能将不可用(其他功能不受影响)")
"║ ║\n" + _box_line()
f"{windows_hint}" + windows_hint
f"{arch_note}" + arch_note
f"{missing_note}" + missing_note
f"{platform_instructions}" + platform_instructions
"║ ║\n" + _box_line()
"║ 📖 完整文档:根目录 README.md ‘源码启动’的第二步 ║\n" + _box_line("📖 文档:static/Partial README for PDF Exporting/README.md")
"╚════════════════════════════════════════════════════════════════╝" + box_bottom
) )
return False, f"⚠ PDF 依赖加载失败: {error_msg};缺失/未识别: {', '.join(missing_native) if missing_native else '未知'}" return False, f"⚠ PDF 依赖加载失败: {error_msg};缺失/未识别: {', '.join(missing_native) if missing_native else '未知'}"
except ImportError as e: except ImportError as e:
@@ -299,7 +325,7 @@ def log_dependency_status():
else: else:
logger.warning(message) logger.warning(message)
logger.info("💡 提示:PDF 导出功能需要 Pango 库支持,但不影响系统其他功能的正常使用") logger.info("💡 提示:PDF 导出功能需要 Pango 库支持,但不影响系统其他功能的正常使用")
logger.info("📚 安装说明请参考:根目录下的 README.md 文件") logger.info("📚 安装说明请参考:static/Partial README for PDF Exporting/README.md")
return is_available return is_available