build脚本GBK编码+参数解析修复(去除所有前缀后匹配)
This commit is contained in:
+39
-40
@@ -1,11 +1,10 @@
|
||||
@echo off
|
||||
setlocal enabledelayedexpansion
|
||||
|
||||
REM === WindowMsgBot 构建脚本 ===
|
||||
REM 自动化打包和签名流程
|
||||
REM 用法: build.bat /skip-cert /skip-sign
|
||||
REM build.bat --skip-cert --skip-sign (PowerShell兼容)
|
||||
REM build.bat skip-cert skip-sign (无前缀)
|
||||
REM === WindowMsgBot Build Script ===
|
||||
REM Usage: build.bat [/skip-cert] [/skip-sign]
|
||||
REM build.bat [--skip-cert] [--skip-sign]
|
||||
REM build.bat [skip-cert] [skip-sign]
|
||||
|
||||
set "SCRIPT_DIR=%~dp0"
|
||||
set "PROJECT_DIR=%SCRIPT_DIR%.."
|
||||
@@ -15,103 +14,103 @@ set "PYINSTALLER=%PYTHON% -m PyInstaller"
|
||||
set "SKIP_CERT=0"
|
||||
set "SKIP_SIGN=0"
|
||||
|
||||
REM 解析参数(兼容 / -- 无前缀 三种写法)
|
||||
:parse_args
|
||||
if "%~1"=="" goto :start_build
|
||||
set "_arg=%~1"
|
||||
REM Strip all prefix chars: / - --
|
||||
set "_arg=!_arg:/=!"
|
||||
set "_arg=!_arg:--=!"
|
||||
if "!_arg!"=="skip-cert" set "SKIP_CERT=1"
|
||||
if "!_arg!"=="skip-sign" set "SKIP_SIGN=1"
|
||||
set "_arg=!_arg:-=!"
|
||||
if "!_arg!"=="skipcert" set "SKIP_CERT=1"
|
||||
if "!_arg!"=="skipsign" set "SKIP_SIGN=1"
|
||||
shift
|
||||
goto :parse_args
|
||||
|
||||
:start_build
|
||||
echo === WindowMsgBot 构建脚本 ===
|
||||
echo 项目目录: %PROJECT_DIR%
|
||||
echo === WindowMsgBot Build ===
|
||||
echo Project: %PROJECT_DIR%
|
||||
echo Python: %PYTHON%
|
||||
echo.
|
||||
|
||||
REM 检查Python
|
||||
REM Check Python
|
||||
if not exist "%PYTHON%" (
|
||||
echo 错误: Python不存在: %PYTHON%
|
||||
echo 请先创建虚拟环境
|
||||
echo [ERROR] Python not found: %PYTHON%
|
||||
pause
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
REM 检查PyInstaller
|
||||
REM Check PyInstaller
|
||||
"%PYTHON%" -c "import PyInstaller" >nul 2>&1
|
||||
if %errorlevel% neq 0 (
|
||||
echo 安装PyInstaller...
|
||||
echo Installing PyInstaller...
|
||||
"%PYTHON%" -m pip install pyinstaller
|
||||
)
|
||||
|
||||
REM 1. 生成证书(可选)
|
||||
REM 1. Certificate (optional)
|
||||
if "%SKIP_CERT%"=="0" (
|
||||
echo 1. 生成自签名证书...
|
||||
echo 1. Generating self-signed certificate...
|
||||
if exist "%SCRIPT_DIR%code_signing_cert.pfx" (
|
||||
echo 证书已存在,跳过生成
|
||||
echo Certificate already exists, skipping
|
||||
) else (
|
||||
powershell -ExecutionPolicy Bypass -File "%SCRIPT_DIR%generate_cert.ps1"
|
||||
if %errorlevel% neq 0 (
|
||||
echo ⚠ 证书生成失败,继续构建...
|
||||
echo [WARN] Certificate generation failed, continuing...
|
||||
)
|
||||
)
|
||||
) else (
|
||||
echo 1. 跳过证书生成
|
||||
echo 1. Skipped certificate generation
|
||||
)
|
||||
|
||||
REM 2. 清理旧构建(只清理PyInstaller输出,保留build目录里的spec和hooks)
|
||||
REM 2. Clean old builds
|
||||
echo.
|
||||
echo 2. 清理旧构建文件...
|
||||
echo 2. Cleaning old builds...
|
||||
if exist "%PROJECT_DIR%\build\build" rmdir /s /q "%PROJECT_DIR%\build\build"
|
||||
if exist "%PROJECT_DIR%\dist" rmdir /s /q "%PROJECT_DIR%\dist"
|
||||
|
||||
REM 3. PyInstaller打包
|
||||
REM 3. PyInstaller build
|
||||
echo.
|
||||
echo 3. PyInstaller打包...
|
||||
echo 3. Building with PyInstaller...
|
||||
|
||||
REM 禁用 CUDA 防止 PyInstaller isolated 子进程 access violation 崩溃
|
||||
REM Disable CUDA to prevent PyInstaller isolated subprocess access violation
|
||||
set "PYTORCH_NO_CUDA_DISPATCH=1"
|
||||
set "CUDA_VISIBLE_DEVICES="
|
||||
|
||||
cd /d "%PROJECT_DIR%"
|
||||
"%PYINSTALLER%" "%SCRIPT_DIR%windowmsgbot.spec" --noconfirm --clean
|
||||
if %errorlevel% neq 0 (
|
||||
echo 错误: PyInstaller打包失败!
|
||||
echo [ERROR] PyInstaller build failed!
|
||||
pause
|
||||
exit /b 1
|
||||
)
|
||||
echo ✓ 打包完成
|
||||
echo Build complete
|
||||
|
||||
REM 4. 签名(可选)
|
||||
REM 4. Sign (optional)
|
||||
if "%SKIP_SIGN%"=="0" (
|
||||
echo.
|
||||
echo 4. 签名exe文件...
|
||||
echo 4. Signing exe...
|
||||
if exist "%SCRIPT_DIR%code_signing_cert.pfx" (
|
||||
call "%SCRIPT_DIR%sign.bat"
|
||||
) else (
|
||||
echo ⚠ 证书不存在,跳过签名
|
||||
echo [WARN] Certificate not found, skipping signing
|
||||
)
|
||||
) else (
|
||||
echo.
|
||||
echo 4. 跳过签名
|
||||
echo 4. Skipped signing
|
||||
)
|
||||
|
||||
REM 5. 输出结果
|
||||
REM 5. Output
|
||||
echo.
|
||||
echo 5. 构建结果...
|
||||
echo 5. Build result...
|
||||
if exist "%PROJECT_DIR%\dist\WindowMsgBot\WindowMsgBot.exe" (
|
||||
echo ✓ 可执行文件: %PROJECT_DIR%\dist\WindowMsgBot\WindowMsgBot.exe
|
||||
for %%A in ("%PROJECT_DIR%\dist\WindowMsgBot\WindowMsgBot.exe") do echo 大小: %%~zA 字节
|
||||
echo ✓ 输出目录: %PROJECT_DIR%\dist\WindowMsgBot\
|
||||
echo EXE: %PROJECT_DIR%\dist\WindowMsgBot\WindowMsgBot.exe
|
||||
for %%A in ("%PROJECT_DIR%\dist\WindowMsgBot\WindowMsgBot.exe") do echo Size: %%~zA bytes
|
||||
echo Output: %PROJECT_DIR%\dist\WindowMsgBot\
|
||||
) else (
|
||||
echo ✗ 可执行文件未生成
|
||||
echo [ERROR] EXE not generated
|
||||
)
|
||||
|
||||
echo.
|
||||
echo === 构建完成 ===
|
||||
echo 输出目录: %PROJECT_DIR%\dist
|
||||
echo === Build Complete ===
|
||||
echo Output: %PROJECT_DIR%\dist
|
||||
echo.
|
||||
pause
|
||||
pause
|
||||
|
||||
+44
-44
@@ -1,5 +1,5 @@
|
||||
# 生成自签名代码签名证书 - 降低360敏感度
|
||||
# 使用方法: 以管理员身份运行 PowerShell, 执行 .\generate_cert.ps1
|
||||
# 生成自签名代码签名证书 - 降低360敏感度
|
||||
# 使用方法: 以管理员身份运行 PowerShell, 执行 .\generate_cert.ps1
|
||||
|
||||
param(
|
||||
[string]$CertPassword = "WindowMsgBot2026",
|
||||
@@ -7,10 +7,10 @@ param(
|
||||
[int]$ValidYears = 5
|
||||
)
|
||||
|
||||
Write-Host "=== 生成自签名代码签名证书 ===" -ForegroundColor Cyan
|
||||
Write-Host "=== 生成自签名代码签名证书 ===" -ForegroundColor Cyan
|
||||
|
||||
# 1. 生成证书
|
||||
Write-Host "1. 生成自签名证书..." -ForegroundColor Yellow
|
||||
# 1. 生成证书
|
||||
Write-Host "1. 生成自签名证书..." -ForegroundColor Yellow
|
||||
$cert = New-SelfSignedCertificate `
|
||||
-Subject $CertSubject `
|
||||
-Type CodeSigningCert `
|
||||
@@ -22,14 +22,14 @@ $cert = New-SelfSignedCertificate `
|
||||
-FriendlyName "WindowMsgBot Code Signing Certificate"
|
||||
|
||||
if (-not $cert) {
|
||||
Write-Error "证书生成失败!"
|
||||
Write-Error "证书生成失败!"
|
||||
exit 1
|
||||
}
|
||||
|
||||
Write-Host " 证书指纹: $($cert.Thumbprint)" -ForegroundColor Green
|
||||
Write-Host " 证书指纹: $($cert.Thumbprint)" -ForegroundColor Green
|
||||
|
||||
# 2. 导出PFX文件
|
||||
Write-Host "2. 导出PFX文件..." -ForegroundColor Yellow
|
||||
# 2. 导出PFX文件
|
||||
Write-Host "2. 导出PFX文件..." -ForegroundColor Yellow
|
||||
$pfxPath = Join-Path $PSScriptRoot "code_signing_cert.pfx"
|
||||
$password = ConvertTo-SecureString -String $CertPassword -Force -AsPlainText
|
||||
|
||||
@@ -38,68 +38,68 @@ Export-PfxCertificate `
|
||||
-FilePath $pfxPath `
|
||||
-Password $password
|
||||
|
||||
Write-Host " PFX已保存: $pfxPath" -ForegroundColor Green
|
||||
Write-Host " PFX已保存: $pfxPath" -ForegroundColor Green
|
||||
|
||||
# 3. 导出CER文件(公钥)
|
||||
Write-Host "3. 导出CER公钥文件..." -ForegroundColor Yellow
|
||||
# 3. 导出CER文件(公钥)
|
||||
Write-Host "3. 导出CER公钥文件..." -ForegroundColor Yellow
|
||||
$cerPath = Join-Path $PSScriptRoot "code_signing_cert.cer"
|
||||
Export-Certificate `
|
||||
-Cert $cert `
|
||||
-FilePath $cerPath
|
||||
|
||||
Write-Host " CER已保存: $cerPath" -ForegroundColor Green
|
||||
Write-Host " CER已保存: $cerPath" -ForegroundColor Green
|
||||
|
||||
# 4. 安装到受信任的根证书颁发机构
|
||||
Write-Host "4. 安装到受信任的根证书颁发机构..." -ForegroundColor Yellow
|
||||
# 4. 安装到受信任的根证书颁发机构
|
||||
Write-Host "4. 安装到受信任的根证书颁发机构..." -ForegroundColor Yellow
|
||||
$store = New-Object System.Security.Cryptography.X509Certificates.X509Store(
|
||||
"Root", "LocalMachine")
|
||||
$store.Open("ReadWrite")
|
||||
$store.Add($cert)
|
||||
$store.Close()
|
||||
|
||||
Write-Host " 证书已安装到受信任的根证书存储" -ForegroundColor Green
|
||||
Write-Host " 证书已安装到受信任的根证书存储" -ForegroundColor Green
|
||||
|
||||
# 5. 验证安装
|
||||
Write-Host "5. 验证证书安装..." -ForegroundColor Yellow
|
||||
# 5. 验证安装
|
||||
Write-Host "5. 验证证书安装..." -ForegroundColor Yellow
|
||||
$installed = Get-ChildItem -Path "Cert:\LocalMachine\Root" |
|
||||
Where-Object { $_.Thumbprint -eq $cert.Thumbprint }
|
||||
|
||||
if ($installed) {
|
||||
Write-Host " ✓ 证书已成功安装到受信任存储" -ForegroundColor Green
|
||||
Write-Host " ? 证书已成功安装到受信任存储" -ForegroundColor Green
|
||||
} else {
|
||||
Write-Host " ⚠ 证书可能未安装到受信任存储" -ForegroundColor Yellow
|
||||
Write-Host " ? 证书可能未安装到受信任存储" -ForegroundColor Yellow
|
||||
}
|
||||
|
||||
# 6. 输出证书信息
|
||||
Write-Host "`n=== 证书信息 ===" -ForegroundColor Cyan
|
||||
Write-Host "主题: $($cert.Subject)"
|
||||
Write-Host "指纹: $($cert.Thumbprint)"
|
||||
Write-Host "有效期: $($cert.NotBefore) 至 $($cert.NotAfter)"
|
||||
Write-Host "用途: $($cert.EnhancedKeyUsageList.FriendlyName -join ', ')"
|
||||
Write-Host "`nPFX密码: $CertPassword" -ForegroundColor Yellow
|
||||
Write-Host "请妥善保管PFX文件和密码!" -ForegroundColor Red
|
||||
# 6. 输出证书信息
|
||||
Write-Host "`n=== 证书信息 ===" -ForegroundColor Cyan
|
||||
Write-Host "主题: $($cert.Subject)"
|
||||
Write-Host "指纹: $($cert.Thumbprint)"
|
||||
Write-Host "有效期: $($cert.NotBefore) 至 $($cert.NotAfter)"
|
||||
Write-Host "用途: $($cert.EnhancedKeyUsageList.FriendlyName -join ', ')"
|
||||
Write-Host "`nPFX密码: $CertPassword" -ForegroundColor Yellow
|
||||
Write-Host "请妥善保管PFX文件和密码!" -ForegroundColor Red
|
||||
|
||||
# 7. 创建证书信息文件
|
||||
Write-Host "7. 创建证书信息文件..." -ForegroundColor Yellow
|
||||
# 7. 创建证书信息文件
|
||||
Write-Host "7. 创建证书信息文件..." -ForegroundColor Yellow
|
||||
$infoPath = Join-Path $PSScriptRoot "cert_info.txt"
|
||||
@"
|
||||
证书信息
|
||||
证书信息
|
||||
========
|
||||
主题: $($cert.Subject)
|
||||
指纹: $($cert.Thumbprint)
|
||||
有效期: $($cert.NotBefore) 至 $($cert.NotAfter)
|
||||
PFX文件: code_signing_cert.pfx
|
||||
CER文件: code_signing_cert.cer
|
||||
PFX密码: $CertPassword
|
||||
主题: $($cert.Subject)
|
||||
指纹: $($cert.Thumbprint)
|
||||
有效期: $($cert.NotBefore) 至 $($cert.NotAfter)
|
||||
PFX文件: code_signing_cert.pfx
|
||||
CER文件: code_signing_cert.cer
|
||||
PFX密码: $CertPassword
|
||||
|
||||
使用说明
|
||||
使用说明
|
||||
========
|
||||
1. PFX文件用于signtool签名
|
||||
2. CER文件可分发给其他人验证签名
|
||||
3. 证书已安装到本机受信任存储
|
||||
1. PFX文件用于signtool签名
|
||||
2. CER文件可分发给其他人验证签名
|
||||
3. 证书已安装到本机受信任存储
|
||||
"@ | Out-File -FilePath $infoPath -Encoding UTF8
|
||||
|
||||
Write-Host " 证书信息已保存: $infoPath" -ForegroundColor Green
|
||||
Write-Host " 证书信息已保存: $infoPath" -ForegroundColor Green
|
||||
|
||||
Write-Host "`n=== 完成 ===" -ForegroundColor Cyan
|
||||
Write-Host "下一步: 使用 sign.bat 脚本签名exe文件" -ForegroundColor Yellow
|
||||
Write-Host "`n=== 完成 ===" -ForegroundColor Cyan
|
||||
Write-Host "下一步: 使用 sign.bat 脚本签名exe文件" -ForegroundColor Yellow
|
||||
+36
-36
@@ -1,9 +1,9 @@
|
||||
@echo off
|
||||
setlocal enabledelayedexpansion
|
||||
|
||||
REM === WindowMsgBot 签名脚本 ===
|
||||
REM 使用自签名证书签名exe文件,降低360敏感度
|
||||
REM 用法: sign.bat [exe路径]
|
||||
REM === WindowMsgBot 签名脚本 ===
|
||||
REM 使用自签名证书签名exe文件,降低360敏感度
|
||||
REM 用法: sign.bat [exe路径]
|
||||
|
||||
set "SCRIPT_DIR=%~dp0"
|
||||
set "PROJECT_DIR=%SCRIPT_DIR%.."
|
||||
@@ -11,7 +11,7 @@ set "CERT_PFX=%SCRIPT_DIR%code_signing_cert.pfx"
|
||||
set "CERT_PASS=WindowMsgBot2026"
|
||||
set "OUTPUT_DIR=%PROJECT_DIR%\dist"
|
||||
|
||||
REM 如果没有指定exe文件,查找默认的exe(onedir模式在子目录中)
|
||||
REM 如果没有指定exe文件,查找默认的exe(onedir模式在子目录中)
|
||||
if "%~1"=="" (
|
||||
if exist "%OUTPUT_DIR%\WindowMsgBot\WindowMsgBot.exe" (
|
||||
set "EXE_PATH=%OUTPUT_DIR%\WindowMsgBot\WindowMsgBot.exe"
|
||||
@@ -22,26 +22,26 @@ if "%~1"=="" (
|
||||
set "EXE_PATH=%~1"
|
||||
)
|
||||
|
||||
echo === WindowMsgBot 代码签名 ===
|
||||
echo === WindowMsgBot 代码签名 ===
|
||||
echo.
|
||||
|
||||
REM 检查exe文件是否存在
|
||||
REM 检查exe文件是否存在
|
||||
if not exist "%EXE_PATH%" (
|
||||
echo 错误: exe文件不存在: %EXE_PATH%
|
||||
echo 请先运行 PyInstaller 构建: pyinstaller build/windowmsgbot.spec
|
||||
echo 错误: exe文件不存在: %EXE_PATH%
|
||||
echo 请先运行 PyInstaller 构建: pyinstaller build/windowmsgbot.spec
|
||||
pause
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
REM 检查证书文件是否存在
|
||||
REM 检查证书文件是否存在
|
||||
if not exist "%CERT_PFX%" (
|
||||
echo 错误: 证书文件不存在: %CERT_PFX%
|
||||
echo 请先运行证书生成脚本: powershell -ExecutionPolicy Bypass -File build\generate_cert.ps1
|
||||
echo 错误: 证书文件不存在: %CERT_PFX%
|
||||
echo 请先运行证书生成脚本: powershell -ExecutionPolicy Bypass -File build\generate_cert.ps1
|
||||
pause
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
REM 查找signtool.exe
|
||||
REM 查找signtool.exe
|
||||
set "SIGNTOOL="
|
||||
for %%i in (
|
||||
"%ProgramFiles%\Windows Kits\10\bin\x64\signtool.exe"
|
||||
@@ -56,64 +56,64 @@ for %%i in (
|
||||
)
|
||||
)
|
||||
|
||||
REM 尝试从PATH查找
|
||||
REM 尝试从PATH查找
|
||||
where signtool.exe >nul 2>&1
|
||||
if %errorlevel% equ 0 (
|
||||
set "SIGNTOOL=signtool.exe"
|
||||
goto :found_sign
|
||||
)
|
||||
|
||||
echo 错误: 未找到 signtool.exe
|
||||
echo 请安装 Windows SDK 或 Windows Kits
|
||||
echo 下载地址: https://developer.microsoft.com/en-us/windows/downloads/windows-sdk/
|
||||
echo 错误: 未找到 signtool.exe
|
||||
echo 请安装 Windows SDK 或 Windows Kits
|
||||
echo 下载地址: https://developer.microsoft.com/en-us/windows/downloads/windows-sdk/
|
||||
pause
|
||||
exit /b 1
|
||||
|
||||
:found_sign
|
||||
echo 使用signtool: %SIGNTOOL%
|
||||
echo 使用signtool: %SIGNTOOL%
|
||||
echo.
|
||||
|
||||
REM 1. 签名exe
|
||||
echo 1. 签名exe文件...
|
||||
REM 1. 签名exe
|
||||
echo 1. 签名exe文件...
|
||||
"%SIGNTOOL%" sign /f "%CERT_PFX%" /p "%CERT_PASS%" /tr http://timestamp.digicert.com /td sha256 /fd sha256 "%EXE_PATH%"
|
||||
if %errorlevel% neq 0 (
|
||||
echo 错误: 签名失败!
|
||||
echo 错误: 签名失败!
|
||||
pause
|
||||
exit /b 1
|
||||
)
|
||||
echo ✓ 签名成功
|
||||
echo ? 签名成功
|
||||
|
||||
REM 2. 验证签名
|
||||
REM 2. 验证签名
|
||||
echo.
|
||||
echo 2. 验证签名...
|
||||
echo 2. 验证签名...
|
||||
"%SIGNTOOL%" verify /pa "%EXE_PATH%"
|
||||
if %errorlevel% equ 0 (
|
||||
echo ✓ 签名验证通过
|
||||
echo ? 签名验证通过
|
||||
) else (
|
||||
echo ⚠ 签名验证失败,但签名可能已应用
|
||||
echo ? 签名验证失败,但签名可能已应用
|
||||
)
|
||||
|
||||
REM 3. 显示签名信息
|
||||
REM 3. 显示签名信息
|
||||
echo.
|
||||
echo 3. 签名信息...
|
||||
echo 3. 签名信息...
|
||||
"%SIGNTOOL%" verify /v "%EXE_PATH%" 2>nul | findstr /i "Signing"
|
||||
echo.
|
||||
|
||||
REM 4. 复制签名后的exe到项目根目录
|
||||
echo 4. 复制签名后的exe...
|
||||
REM 4. 复制签名后的exe到项目根目录
|
||||
echo 4. 复制签名后的exe...
|
||||
copy "%EXE_PATH%" "%PROJECT_DIR%\WindowMsgBot_signed.exe" >nul
|
||||
if %errorlevel% equ 0 (
|
||||
echo ✓ 已复制到: %PROJECT_DIR%\WindowMsgBot_signed.exe
|
||||
echo ? 已复制到: %PROJECT_DIR%\WindowMsgBot_signed.exe
|
||||
) else (
|
||||
echo ⚠ 复制失败
|
||||
echo ? 复制失败
|
||||
)
|
||||
|
||||
echo.
|
||||
echo === 完成 ===
|
||||
echo 签名后的exe: %EXE_PATH%
|
||||
echo 备份副本: %PROJECT_DIR%\WindowMsgBot_signed.exe
|
||||
echo === 完成 ===
|
||||
echo 签名后的exe: %EXE_PATH%
|
||||
echo 备份副本: %PROJECT_DIR%\WindowMsgBot_signed.exe
|
||||
echo.
|
||||
echo 注意: 自签名证书需要在目标机器上安装证书才能验证签名
|
||||
echo 证书文件: %CERT_PFX%
|
||||
echo 注意: 自签名证书需要在目标机器上安装证书才能验证签名
|
||||
echo 证书文件: %CERT_PFX%
|
||||
echo.
|
||||
pause
|
||||
Reference in New Issue
Block a user