Files
douyi_guaji_main/packaging/pyinstaller/build_windows.ps1
T

286 lines
10 KiB
PowerShell

param(
[string]$Version = "",
[switch]$SkipInstall,
[switch]$NoCythonForDevOnly,
[switch]$UsePyArmor,
[switch]$NoObfuscateForDevOnly,
[switch]$PackExe,
[switch]$PackExeForce,
[switch]$IncludeLicense,
[switch]$ReuseCache,
[string]$PlatformToolsDir = "",
[string]$UpxDir = ""
)
$ErrorActionPreference = "Stop"
$Root = Resolve-Path (Join-Path $PSScriptRoot "..\..")
Set-Location $Root
$env:DISABLE_MODEL_SOURCE_CHECK = "True"
$env:PADDLE_PDX_DISABLE_MODEL_SOURCE_CHECK = "1"
$env:SKIP_OCR_INIT = "1"
$env:GLOG_minloglevel = "2"
$env:FLAGS_use_mkldnn = "false"
$env:FLAGS_call_stack_level = "0"
if (-not $Version) {
$Version = (python -c "from douyin.core.version import APP_VERSION; print(str(APP_VERSION).lstrip('Vv'))").Trim()
}
if (-not $SkipInstall) {
python -m pip install --upgrade pip
python -m pip install -r requirements.txt
python -m pip install pyinstaller
if ($UsePyArmor) {
python -m pip install pyarmor
} elseif (-not $NoCythonForDevOnly -and -not $NoObfuscateForDevOnly) {
python -m pip install cython setuptools wheel
}
}
$DistRoot = Join-Path $Root "dist\DouyinGuaji-$Version"
if (Test-Path $DistRoot) {
Remove-Item -LiteralPath $DistRoot -Recurse -Force
}
New-Item -ItemType Directory -Force $DistRoot | Out-Null
$BuildSourceRoot = $Root
$HiddenImportsFile = Join-Path $Root "build\obfuscation-hidden-imports.json"
if ($UsePyArmor -and -not $NoObfuscateForDevOnly) {
$BuildSourceRoot = Join-Path $Root "build\obfuscated-src"
python -m tools.obfuscation_staging --source-root $Root --staging-root $BuildSourceRoot --hidden-imports-output $HiddenImportsFile
if ($LASTEXITCODE -ne 0) {
throw "PyArmor obfuscation failed with exit code $LASTEXITCODE"
}
} elseif (-not $NoCythonForDevOnly -and -not $NoObfuscateForDevOnly) {
$BuildSourceRoot = Join-Path $Root "build\cythonized-src"
python -m tools.cython_staging --source-root $Root --staging-root $BuildSourceRoot --hidden-imports-output $HiddenImportsFile
if ($LASTEXITCODE -ne 0) {
throw "Cython core compile failed with exit code $LASTEXITCODE"
}
} else {
Write-Warning "Development plaintext build requested. Do not distribute this package."
}
$CommonArgs = @(
"--noconfirm",
"--distpath", $DistRoot,
"--workpath", (Join-Path $Root "build\pyinstaller"),
"--paths", $BuildSourceRoot,
"--add-data", "config\README.md;config",
"--hidden-import", "PIL._tkinter_finder",
"--hidden-import", "ttkbootstrap",
"--hidden-import", "logging.handlers",
"--hidden-import", "cryptography.hazmat.primitives.serialization"
)
if (-not $ReuseCache) {
$CommonArgs = @("--clean") + $CommonArgs
}
$GuiArgs = @(
"--collect-submodules", "gui"
)
$CliArgs = @(
"--collect-submodules", "douyin",
"--collect-submodules", "operations"
)
$ExcludedOptionalModules = @(
"easyocr",
"matplotlib",
"modelscope",
"pandas",
"pytest",
"scipy",
"skimage",
"tensorflow",
"torch",
"torchvision"
)
foreach ($name in $ExcludedOptionalModules) {
$CommonArgs += @("--exclude-module", $name)
}
if ((-not $NoObfuscateForDevOnly) -and (-not $NoCythonForDevOnly -or $UsePyArmor) -and (Test-Path $HiddenImportsFile)) {
$HiddenImports = Get-Content -LiteralPath $HiddenImportsFile -Raw | ConvertFrom-Json
$CliOnlyHiddenImportRoots = @("cv2", "numpy", "paddleocr", "paddle")
foreach ($name in $HiddenImports) {
$nameText = [string]$name
$rootName = ($nameText -split "\.")[0]
if ($CliOnlyHiddenImportRoots -contains $rootName) {
$CliArgs += @("--hidden-import", $nameText)
} else {
$CommonArgs += @("--hidden-import", $nameText)
}
}
}
python -m PyInstaller @CommonArgs @GuiArgs --name DouyinGuajiGUI --windowed (Join-Path $BuildSourceRoot "gui\gui_dashboard_app_entry.py")
python -m PyInstaller @CommonArgs @GuiArgs --name web_gui --console (Join-Path $BuildSourceRoot "gui\web_gui_entry.py")
python -m PyInstaller @CommonArgs @CliArgs --name run_cli --console (Join-Path $BuildSourceRoot "bin\run_cli_entry.py")
foreach ($name in @("DouyinGuajiGUI", "web_gui", "run_cli")) {
$srcDir = Join-Path $DistRoot $name
Copy-Item -LiteralPath (Join-Path $srcDir "$name.exe") -Destination (Join-Path $DistRoot "$name.exe") -Force
$srcInternal = Join-Path $srcDir "_internal"
$dstInternal = Join-Path $DistRoot "_internal"
if (Test-Path $srcInternal) {
New-Item -ItemType Directory -Force $dstInternal | Out-Null
Get-ChildItem -LiteralPath $srcInternal -Force | Copy-Item -Destination $dstInternal -Recurse -Force
}
Remove-Item -LiteralPath $srcDir -Recurse -Force
}
$AssetsTarget = Join-Path $DistRoot "assets"
if (Test-Path $AssetsTarget) {
Remove-Item -LiteralPath $AssetsTarget -Recurse -Force
}
New-Item -ItemType Directory -Force $AssetsTarget | Out-Null
$RuntimeAssetFiles = Get-ChildItem -LiteralPath (Join-Path $Root "assets") -File | Where-Object {
$_.Extension -ieq ".png"
}
if (-not $RuntimeAssetFiles) {
throw "Missing runtime assets: assets\*.png"
}
foreach ($asset in $RuntimeAssetFiles) {
Copy-Item -LiteralPath $asset.FullName -Destination (Join-Path $AssetsTarget $asset.Name) -Force
}
$WebuiTarget = Join-Path $DistRoot "webui"
if (Test-Path $WebuiTarget) {
Remove-Item -LiteralPath $WebuiTarget -Recurse -Force
}
New-Item -ItemType Directory -Force $WebuiTarget | Out-Null
Copy-Item -LiteralPath (Join-Path $Root "webui\index.html") -Destination (Join-Path $WebuiTarget "index.html") -Force
Copy-Item -LiteralPath (Join-Path $Root "webui\styles.css") -Destination (Join-Path $WebuiTarget "styles.css") -Force
Copy-Item -LiteralPath (Join-Path $Root "webui\app.js") -Destination (Join-Path $WebuiTarget "app.js") -Force
foreach ($dir in @("css", "js", "partials")) {
$srcDir = Join-Path $Root "webui\$dir"
if (-not (Test-Path $srcDir)) {
continue
}
$dstDir = Join-Path $WebuiTarget $dir
New-Item -ItemType Directory -Force $dstDir | Out-Null
Get-ChildItem -LiteralPath $srcDir -File | Where-Object {
$_.Name -notlike "README*"
} | Copy-Item -Destination $dstDir -Force
}
New-Item -ItemType Directory -Force (Join-Path $DistRoot "config") | Out-Null
Copy-Item -LiteralPath (Join-Path $Root "config\README.md") -Destination (Join-Path $DistRoot "config\README.md") -Force
foreach ($dir in @("logs", "pic", "state")) {
$runtimeDir = Join-Path $DistRoot $dir
New-Item -ItemType Directory -Force $runtimeDir | Out-Null
New-Item -ItemType File -Force (Join-Path $runtimeDir ".keep") | Out-Null
}
$PlatformToolCandidates = @()
if ($PlatformToolsDir) {
$PlatformToolCandidates += $PlatformToolsDir
}
foreach ($envName in @("ANDROID_PLATFORM_TOOLS", "ANDROID_HOME", "ANDROID_SDK_ROOT")) {
$envValue = [Environment]::GetEnvironmentVariable($envName)
if (-not $envValue) {
continue
}
if ($envName -eq "ANDROID_PLATFORM_TOOLS") {
$PlatformToolCandidates += $envValue
} else {
$PlatformToolCandidates += (Join-Path $envValue "platform-tools")
}
}
if ($env:LOCALAPPDATA) {
$PlatformToolCandidates += (Join-Path $env:LOCALAPPDATA "platform-tools")
}
$PlatformToolCandidates += (Join-Path $Root "platform-tools")
$ResolvedPlatformTools = ""
foreach ($candidate in $PlatformToolCandidates) {
if (-not $candidate) {
continue
}
$adbCandidate = Join-Path $candidate "adb.exe"
if (Test-Path $adbCandidate) {
$ResolvedPlatformTools = (Resolve-Path $candidate).Path
break
}
}
if (-not $ResolvedPlatformTools) {
throw "Missing Android platform-tools. Provide -PlatformToolsDir or install it under LOCALAPPDATA\platform-tools."
}
$PlatformToolsTarget = Join-Path $DistRoot "platform-tools"
if (Test-Path $PlatformToolsTarget) {
Remove-Item -LiteralPath $PlatformToolsTarget -Recurse -Force
}
New-Item -ItemType Directory -Force $PlatformToolsTarget | Out-Null
$PlatformToolFiles = @(
"adb.exe",
"AdbWinApi.dll",
"AdbWinUsbApi.dll",
"NOTICE.txt",
"source.properties"
)
foreach ($name in $PlatformToolFiles) {
$src = Join-Path $ResolvedPlatformTools $name
if ($name -in @("adb.exe", "AdbWinApi.dll", "AdbWinUsbApi.dll") -and -not (Test-Path $src)) {
throw "Missing required platform-tools file: $name"
}
if (Test-Path $src) {
Copy-Item -LiteralPath $src -Destination (Join-Path $PlatformToolsTarget $name) -Force
}
}
$AndroidToolsTarget = Join-Path $DistRoot "android-tools"
if (Test-Path $AndroidToolsTarget) {
Remove-Item -LiteralPath $AndroidToolsTarget -Recurse -Force
}
New-Item -ItemType Directory -Force $AndroidToolsTarget | Out-Null
$AdbKeyboardApk = Join-Path $Root "tools\adb\ADBKeyboard.apk"
if (-not (Test-Path $AdbKeyboardApk)) {
throw "Missing Android helper: tools\adb\ADBKeyboard.apk"
}
Copy-Item -LiteralPath $AdbKeyboardApk -Destination (Join-Path $AndroidToolsTarget "ADBKeyboard.apk") -Force
$licensePath = Join-Path $Root "license.dat"
if ($IncludeLicense -and (Test-Path $licensePath)) {
Copy-Item -LiteralPath $licensePath -Destination (Join-Path $DistRoot "license.dat") -Force
}
if ($PackExe) {
$PackArgs = @($DistRoot)
if ($UpxDir) {
$PackArgs += @("--upx-dir", $UpxDir)
}
if ($PackExeForce) {
$PackArgs += "--force"
}
python -m tools.exe_packing @PackArgs
if ($LASTEXITCODE -ne 0) {
throw "UPX packing failed with exit code $LASTEXITCODE"
}
}
$ManifestArgs = @($DistRoot)
if ($IncludeLicense) {
$ManifestArgs += "--allow-license"
}
python -m tools.distribution_manifest @ManifestArgs
if ($LASTEXITCODE -ne 0) {
throw "Distribution manifest check failed with exit code $LASTEXITCODE"
}
python -m tools.exe_packing $DistRoot --smoke-only
if ($LASTEXITCODE -ne 0) {
throw "Executable smoke check failed with exit code $LASTEXITCODE"
}
python -m tools.distribution_manifest @ManifestArgs
if ($LASTEXITCODE -ne 0) {
throw "Post-smoke distribution manifest check failed with exit code $LASTEXITCODE"
}
Write-Host "[OK] Portable package: $DistRoot"
Write-Host "[NEXT] If Inno Setup is installed: iscc packaging\inno\douyin_guaji.iss /DMyAppVersion=$Version"