PCM_Report/rebuild_with_sqlserver_fix.ps1

180 lines
5.5 KiB
PowerShell
Raw Normal View History

2025-12-11 14:32:31 +08:00
# SQL Server Package Fix - Rebuild Script
# Rebuild using the fixed spec file
$ErrorActionPreference = "Stop"
Set-StrictMode -Version Latest
Write-Host ("=" * 80) -ForegroundColor Cyan
Write-Host "SQL Server Package Fix - Rebuild" -ForegroundColor Cyan
Write-Host ("=" * 80) -ForegroundColor Cyan
Write-Host ""
# Go to script directory
Set-Location -Path $PSScriptRoot
# Activate virtual environment
Write-Host "Activating virtual environment..." -ForegroundColor Yellow
if (-not (Test-Path .\.venv)) {
Write-Host "Virtual environment does not exist, creating..." -ForegroundColor Yellow
python -m venv .venv
}
. .\.venv\Scripts\Activate.ps1
Write-Host "OK Virtual environment activated" -ForegroundColor Green
Write-Host ""
# Check required packages
Write-Host "Checking required packages..." -ForegroundColor Yellow
$packages = @("pymssql", "pyodbc", "pyinstaller")
foreach ($pkg in $packages) {
try {
$result = python -c "import $pkg; print(f'OK {$pkg}: {$pkg.__version__}')" 2>&1
Write-Host " $result" -ForegroundColor Green
} catch {
Write-Host " X $pkg not installed, installing..." -ForegroundColor Red
pip install $pkg
}
}
Write-Host ""
# Clean old build files
Write-Host "Cleaning old build files..." -ForegroundColor Yellow
$dirsToClean = @("build", "dist")
foreach ($dir in $dirsToClean) {
if (Test-Path .\$dir) {
try {
Remove-Item .\$dir -Recurse -Force -ErrorAction Stop
Write-Host " OK Removed $dir directory" -ForegroundColor Green
} catch {
Write-Host " ! Could not remove $dir directory (may be in use)" -ForegroundColor Yellow
}
}
}
Write-Host ""
# Check spec file
Write-Host "Checking spec file..." -ForegroundColor Yellow
if (-not (Test-Path .\docx_creator_fixed.spec)) {
Write-Host "X docx_creator_fixed.spec does not exist!" -ForegroundColor Red
exit 1
}
Write-Host "OK spec file exists" -ForegroundColor Green
Write-Host ""
# Check hook file
Write-Host "Checking hook file..." -ForegroundColor Yellow
if (-not (Test-Path .\hook-pymssql.py)) {
Write-Host "X hook-pymssql.py does not exist!" -ForegroundColor Red
exit 1
}
Write-Host "OK hook file exists" -ForegroundColor Green
Write-Host ""
# Check SQL Server related files
Write-Host "Checking SQL Server related files..." -ForegroundColor Yellow
# Create temporary Python script
$checkScript = @'
import sys
from pathlib import Path
try:
import pymssql
pymssql_path = Path(pymssql.__file__).parent
print(f'pymssql path: {pymssql_path}')
dlls = list(pymssql_path.glob('*.dll'))
pyds = list(pymssql_path.glob('*.pyd'))
print(f' DLL files ({len(dlls)}):')
for dll in dlls:
print(f' - {dll.name}')
print(f' PYD files ({len(pyds)}):')
for pyd in pyds:
print(f' - {pyd.name}')
except Exception as e:
print(f'X pymssql check failed: {e}')
try:
import pyodbc
pyodbc_path = Path(pyodbc.__file__).parent
print(f'pyodbc path: {pyodbc_path}')
pyds = list(pyodbc_path.glob('*.pyd'))
print(f' PYD files ({len(pyds)}):')
for pyd in pyds:
print(f' - {pyd.name}')
except Exception as e:
print(f'X pyodbc check failed: {e}')
'@
# Write script to temp file
$tempScript = Join-Path $env:TEMP "check_sql_modules.py"
$checkScript | Out-File -FilePath $tempScript -Encoding UTF8
# Run Python script
python $tempScript
# Delete temp file
Remove-Item $tempScript -ErrorAction SilentlyContinue
Write-Host ""
# Start packaging
Write-Host ("=" * 80) -ForegroundColor Cyan
Write-Host "Starting packaging..." -ForegroundColor Cyan
Write-Host ("=" * 80) -ForegroundColor Cyan
Write-Host ""
pyinstaller --noconfirm --clean .\docx_creator_fixed.spec
if ($LASTEXITCODE -eq 0) {
Write-Host ""
Write-Host ("=" * 80) -ForegroundColor Green
Write-Host "OK Packaging successful!" -ForegroundColor Green
Write-Host ("=" * 80) -ForegroundColor Green
Write-Host ""
Write-Host "Output directory: $(Join-Path $PSScriptRoot 'dist\docx_creator')" -ForegroundColor Green
Write-Host ""
# Check packaging results
Write-Host "Checking packaging results..." -ForegroundColor Yellow
$distPath = Join-Path $PSScriptRoot "dist\docx_creator"
Write-Host "SQL Server related files:" -ForegroundColor Yellow
$sqlFiles = @(
"sybdb.dll",
"libtds.dll",
"_mssql.pyd",
"pyodbc.pyd"
)
foreach ($file in $sqlFiles) {
$filePath = Join-Path $distPath $file
if (Test-Path $filePath) {
$size = (Get-Item $filePath).Length
Write-Host " OK $file ($size bytes)" -ForegroundColor Green
} else {
Write-Host " X $file (not found)" -ForegroundColor Red
}
}
Write-Host ""
Write-Host "Next steps:" -ForegroundColor Cyan
Write-Host "1. Test the program: .\dist\docx_creator\docx_creator.exe" -ForegroundColor White
Write-Host "2. Or run diagnostic tool:" -ForegroundColor White
Write-Host " Copy-Item .\test_sqlserver_in_exe.py .\dist\docx_creator\" -ForegroundColor White
Write-Host " cd .\dist\docx_creator" -ForegroundColor White
Write-Host " python test_sqlserver_in_exe.py" -ForegroundColor White
} else {
Write-Host ""
Write-Host ("=" * 80) -ForegroundColor Red
Write-Host "X Packaging failed!" -ForegroundColor Red
Write-Host ("=" * 80) -ForegroundColor Red
Write-Host ""
Write-Host "Please check the error messages above" -ForegroundColor Red
}
Write-Host ""