build: 增加 exe 打包脚本与版本信息,修复子目录卷信息查询
- tools/build_exe.ps1:一条命令出 dist\\U盘备份工具.exe(onefile/windowed/图标/版本信息),并打印 SHA-256 - tools/version_info.txt:2.1.0.0 版本资源 - get_volume_info 统一换算到卷根再查询:传子目录/文件也能拿到文件系统,修复 FAT32 4GB 上限与脏卷提示依赖 vol['fs'] 失效的问题 - 新增 2 项体积/卷信息测试(共 32 项)
This commit is contained in:
1 parent
8232b18c9f
commit
550bfaaa00
5 files changed
+106
-9
No files matched your search
@@ -31,16 +31,18 @@ python usb_backup.py
|
||||
|
||||
无需第三方依赖(仅标准库:tkinter / ctypes / hashlib)。
|
||||
|
||||
### 打包为 exe(可选)
|
||||
### 打包为 exe
|
||||
|
||||
```bash
|
||||
pip install pyinstaller
|
||||
pyinstaller --onefile --windowed --icon icon.ico usb_backup.py
|
||||
```powershell
|
||||
python -m pip install pyinstaller
|
||||
powershell -ExecutionPolicy Bypass -File tools\build_exe.ps1
|
||||
```
|
||||
|
||||
打包产物在 `dist/U盘备份工具.exe`,可分发到无 Python 环境的 Windows 机器。
|
||||
产物:`dist\U盘备份工具.exe`(单文件、无控制台、带图标与版本信息,约 12 MB),
|
||||
可分发到无 Python 环境的 Windows 机器;脚本结束会打印 SHA-256。
|
||||
|
||||
> 预编译的 exe 请从 [Releases](../../releases) 下载。
|
||||
> 预编译的 exe 与源码包见 [Releases](../../releases)(当前 v2.1.0)。
|
||||
> 首次运行可能被 SmartScreen/杀软拦一下(未签名单文件 exe 的常态),选择"仍要运行"即可。
|
||||
|
||||
## 备份位置
|
||||
|
||||
@@ -82,6 +84,7 @@ python -m unittest discover -s tests -p "test_*.py" -v # 加固版 18 项回
|
||||
```
|
||||
├── usb_backup.py # 主程序(加固版,单文件)
|
||||
├── legacy/usb_backup_v1.py # 原版,仅作对照(未改动)
|
||||
├── tools/ # build_exe.ps1 / version_info.txt(打包用)
|
||||
├── ANALYSIS.md # 数据分析 / 缺陷报告
|
||||
├── tests/
|
||||
│ ├── repro_v1_defects.py # 原版缺陷复现脚本
|
||||
|
||||
@@ -71,6 +71,28 @@ class TestBackupDirFor(unittest.TestCase):
|
||||
self.assertEqual(path, new)
|
||||
|
||||
|
||||
class TestVolumeInfo(unittest.TestCase):
|
||||
def test_subdirectory_resolves_volume(self):
|
||||
d = tempfile.mkdtemp(prefix="vol-")
|
||||
try:
|
||||
info = ub.get_volume_info(d)
|
||||
self.assertIn(info["fs"], ("NTFS", "FAT32", "exFAT", "ReFS", "FAT16", "FAT"))
|
||||
self.assertGreater(info["total"], 0)
|
||||
self.assertGreaterEqual(info["free"], 0)
|
||||
finally:
|
||||
shutil.rmtree(d, ignore_errors=True)
|
||||
|
||||
def test_file_path_resolves_volume(self):
|
||||
d = tempfile.mkdtemp(prefix="vol-")
|
||||
try:
|
||||
f = os.path.join(d, "a.txt")
|
||||
with open(f, "wb") as fh:
|
||||
fh.write(b"x")
|
||||
self.assertEqual(ub.get_volume_info(f)["fs"], ub.get_volume_info(d)["fs"])
|
||||
finally:
|
||||
shutil.rmtree(d, ignore_errors=True)
|
||||
|
||||
|
||||
class TestConfig(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.dir = tempfile.mkdtemp(prefix="cfg-")
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
# 构建单文件 exe(Windows)
|
||||
#
|
||||
# powershell -ExecutionPolicy Bypass -File tools\build_exe.ps1
|
||||
#
|
||||
# 产物: dist\U盘备份工具.exe
|
||||
# 依赖: python -m pip install pyinstaller
|
||||
|
||||
param(
|
||||
[string]$Name = "U盘备份工具"
|
||||
)
|
||||
|
||||
$ErrorActionPreference = "Stop"
|
||||
$root = Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Path)
|
||||
$work = Join-Path $root "build"
|
||||
$dist = Join-Path $root "dist"
|
||||
|
||||
Write-Host "构建 $Name ..."
|
||||
Push-Location $root
|
||||
try {
|
||||
python -m PyInstaller --noconfirm --clean --onefile --windowed `
|
||||
--icon (Join-Path $root "icon.ico") `
|
||||
--name $Name `
|
||||
--version-file (Join-Path $PSScriptRoot "version_info.txt") `
|
||||
--distpath $dist `
|
||||
--workpath $work `
|
||||
--specpath $work `
|
||||
(Join-Path $root "usb_backup.py")
|
||||
if ($LASTEXITCODE -ne 0) { throw "PyInstaller 失败,退出码 $LASTEXITCODE" }
|
||||
} finally {
|
||||
Pop-Location
|
||||
}
|
||||
|
||||
$exe = Join-Path $dist "$Name.exe"
|
||||
if (-not (Test-Path $exe)) { throw "未生成 $exe" }
|
||||
$size = [math]::Round((Get-Item $exe).Length / 1MB, 2)
|
||||
$hash = (Get-FileHash $exe -Algorithm SHA256).Hash
|
||||
Write-Host ""
|
||||
Write-Host "完成: $exe ($size MB)"
|
||||
Write-Host "SHA256: $hash"
|
||||
@@ -0,0 +1,27 @@
|
||||
VSVersionInfo(
|
||||
ffi=FixedFileInfo(
|
||||
filevers=(2, 1, 0, 0),
|
||||
prodvers=(2, 1, 0, 0),
|
||||
mask=0x3f,
|
||||
flags=0x0,
|
||||
OS=0x40004,
|
||||
fileType=0x1,
|
||||
subtype=0x0,
|
||||
date=(0, 0)
|
||||
),
|
||||
kids=[
|
||||
StringFileInfo([
|
||||
StringTable('080404B0', [
|
||||
StringStruct('CompanyName', 'Udisk Backup'),
|
||||
StringStruct('FileDescription', 'U盘备份工具 - 增量/镜像备份,复制后 SHA-256 校验'),
|
||||
StringStruct('FileVersion', '2.1.0.0'),
|
||||
StringStruct('InternalName', 'usb_backup'),
|
||||
StringStruct('LegalCopyright', 'MIT License'),
|
||||
StringStruct('OriginalFilename', 'U盘备份工具.exe'),
|
||||
StringStruct('ProductName', 'U盘备份工具'),
|
||||
StringStruct('ProductVersion', '2.1.0.0')
|
||||
])
|
||||
]),
|
||||
VarFileInfo([VarStruct('Translation', [2052, 1200])])
|
||||
]
|
||||
)
|
||||
+9
-3
@@ -148,10 +148,16 @@ def _hash_file(path, buf=HASH_BUF):
|
||||
|
||||
|
||||
# ---------- 卷信息 / 脏卷检测 / 落盘 ----------
|
||||
def get_volume_info(drive):
|
||||
"""返回 dict(fs=..., label=..., total=..., free=..., serial=...)"""
|
||||
def get_volume_info(path):
|
||||
"""返回 dict(fs=..., label=..., total=..., free=..., serial=...)
|
||||
|
||||
传目录或文件路径都可以:内部统一换算成卷根再查询(GetVolumeInformationW
|
||||
只接受卷根,传子目录会失败,会让 FAT32 4G 上限等判断失效)。
|
||||
"""
|
||||
info = {"fs": "", "label": "", "total": 0, "free": 0, "serial": 0}
|
||||
root = drive if drive.endswith("\\") else drive + "\\"
|
||||
abs_path = os.path.abspath(_plain(path)) if path else ""
|
||||
drive = os.path.splitdrive(abs_path)[0]
|
||||
root = (drive + "\\") if drive else (abs_path if abs_path.endswith("\\") else abs_path + "\\")
|
||||
try:
|
||||
label = ctypes.create_unicode_buffer(261)
|
||||
fsname = ctypes.create_unicode_buffer(261)
|
||||
|
||||
Reference in new issue
Block a user