Files
diskmanager2/COMPAT_SUMMARY.md
2026-02-10 02:54:13 +08:00

202 lines
4.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Linux 存储管理器 - 跨平台兼容性实现总结
## 目标
实现一个打包后的软件,可以在 **Arch Linux****CentOS 8** 两个系统上运行,统一使用 **Tkinter** 作为 GUI 后端。
## 解决方案概述
### 核心设计
1. **统一入口点** (`main.py`)
- 直接使用 Tkinter无需检测 GUI 后端
- 简化启动逻辑
2. **平台检测模块** (`platform_compat.py`)
- 检测操作系统类型 (Arch/CentOS/RHEL/Ubuntu/Debian)
- 检查系统前提条件
- 提供安装指南
3. **系统命令兼容模块** (`system_compat.py`)
- 处理不同系统版本的命令差异
- 自动降级到兼容的命令格式
- 手动解析回退方案
### 平台支持
| 平台 | Python | GUI后端 |
|------|--------|---------|
| Arch Linux | 3.7+ | Tkinter |
| CentOS 8 | 3.6 | Tkinter |
| CentOS 8 Stream | 3.9+ | Tkinter |
| RHEL 8 | 3.6 | Tkinter |
| Ubuntu/Debian | 3.x | Tkinter |
### 为什么选择 Tkinter?
1. **Python 内置** - 无需额外安装
2. **跨版本兼容** - 支持 Python 3.6+ (包括 CentOS 8)
3. **跨发行版** - 所有 Linux 发行版都原生支持
4. **打包简单** - PyInstaller 处理更稳定
## 兼容性修改详情
### 1. Python 3.6 兼容性修复
#### system_info.py
```python
# 修改前 (Python 3.7+)
result = subprocess.run(
command_list,
capture_output=check_output, # 3.7+
text=True, # 3.7+
...
)
# 修改后 (兼容 Python 3.6)
result = subprocess.run(
command_list,
stdout=subprocess.PIPE if check_output else None,
stderr=subprocess.PIPE if check_output else None,
encoding='utf-8' if check_output else None,
...
)
```
#### platform_compat.py
- `_command_exists()`: 使用 `stdout=subprocess.PIPE` 代替 `capture_output=True`
#### system_compat.py
- `_get_lsblk_version()`: 使用 `stdout/stderr` 管道代替 `capture_output`
- `_get_lvm_version()`: 同上
- 手动解码字节到字符串
#### smart_monitor.py
-`dataclasses` 模块提供回退实现
```python
if sys.version_info >= (3, 7):
from dataclasses import dataclass
else:
def dataclass(cls): ... # 简化实现
```
#### disk_operations_tkinter.py
- 修复 f-string 中的中文引号问题 (Python 3.6 f-string 限制)
### 2. 系统命令差异处理
#### lsblk PATH 列
- **问题**: CentOS 8 的 lsblk (2.32) 不支持 PATH 列
- **解决**: 使用 KNAME 列代替,在解析时构建路径
```python
# 命令修改
"NAME,FSTYPE,...,PATH" "NAME,KNAME,FSTYPE,...,PKNAME"
# 解析时处理
dev['path'] = f"/dev/{dev['kname']}"
```
#### JSON 格式支持
- **lsblk JSON**: util-linux 2.23+ (CentOS 8 ✓)
- **LVM JSON**: LVM 2.02.166+ (CentOS 8 ✓)
- **回退**: 普通格式 + 手动解析
## 文件列表
### 新增文件
| 文件 | 说明 |
|------|------|
| `main.py` | 统一入口点 (Tkinter) |
| `platform_compat.py` | 平台检测模块 |
| `system_compat.py` | 命令兼容性模块 |
| `build-compat.sh` | 跨平台构建脚本 |
| `run.sh` | 启动脚本 |
| `disk-manager-compat.spec` | PyInstaller配置 |
| `README_COMPAT.md` | 使用文档 |
| `COMPAT_SUMMARY.md` | 本文档 |
### 修改的文件
| 文件 | 修改内容 |
|------|----------|
| `system_info.py` | 使用兼容性模块;修复 subprocess 调用 |
| `smart_monitor.py` | 添加 dataclass 回退 |
| `disk_operations_tkinter.py` | 修复 f-string 引号 |
| `AGENTS.md` | 添加兼容性文档 |
### 保留的原文件
- `mainwindow_tkinter.py` - Tkinter 主窗口
- `disk_operations_tkinter.py` - 磁盘操作
- `lvm_operations_tkinter.py` - LVM 操作
- `raid_operations_tkinter.py` - RAID 操作
- 其他所有原始文件
## 使用方式
### 源码运行
```bash
# 统一入口
python3 main.py
# 或使用启动脚本
./run.sh
# 直接运行主模块
python3 mainwindow_tkinter.py
```
### 构建打包
```bash
# 使用新的构建脚本
./build-compat.sh
# 或手动打包
pyinstaller -F disk-manager-compat.spec
# 输出
# dist/disk-manager
```
### 安装依赖
#### Arch Linux
```bash
sudo pacman -S python tk parted mdadm lvm2
sudo pacman -S dosfstools e2fsprogs xfsprogs ntfs-3g
pip3 install pexpect
```
#### CentOS 8
```bash
sudo yum install -y python3 python3-tkinter parted mdadm lvm2
sudo yum install -y dosfstools e2fsprogs xfsprogs ntfs-3g
pip3 install pexpect
```
## 测试验证
在 CentOS 8 环境测试通过:
```
Python 3.6.8
平台: Linux-4.18.0-348.el8.x86_64
GUI后端: tkinter
lsblk: 正常获取块设备
系统命令: 自动适配
```
## 注意事项
1. **权限要求**: 磁盘管理操作需要 root/sudo 权限
2. **Tkinter 安装**: 确保系统已安装 python3-tkinter/tk
3. **可选依赖**: mkfs.ntfs 等命令为可选,缺失时相应功能不可用
4. **打包文件**: 单个可执行文件可在两个系统运行
## 后续建议
1. 考虑添加更多发行版支持 (Ubuntu/Debian/SUSE)
2. 可以添加容器化部署选项
3. 考虑添加主题支持改进 Tkinter 外观