PCM_Report/fix_permissions.ps1

109 lines
3.8 KiB
PowerShell
Raw Permalink Normal View History

2025-12-11 14:32:31 +08:00
# 修复权限问题的脚本
Param(
[switch]$Force = $false
)
$ErrorActionPreference = "Continue"
Set-StrictMode -Version Latest
Write-Host "修复PySide6权限问题" -ForegroundColor Yellow
Write-Host "=" * 50
# 1. 检查是否有Python进程在运行
Write-Host "检查Python进程..." -ForegroundColor Yellow
$pythonProcesses = Get-Process -Name "python*" -ErrorAction SilentlyContinue
if ($pythonProcesses) {
Write-Host "发现运行中的Python进程:" -ForegroundColor Red
$pythonProcesses | Format-Table -Property Id, ProcessName, Path -AutoSize
if ($Force) {
Write-Host "强制终止Python进程..." -ForegroundColor Red
$pythonProcesses | Stop-Process -Force
Start-Sleep -Seconds 2
} else {
Write-Host "请手动关闭这些进程,或使用 -Force 参数强制终止" -ForegroundColor Yellow
return
}
}
# 2. 检查是否有docx_creator进程在运行
Write-Host "检查docx_creator进程..." -ForegroundColor Yellow
$appProcesses = Get-Process -Name "*docx*" -ErrorAction SilentlyContinue
if ($appProcesses) {
Write-Host "发现运行中的docx_creator进程:" -ForegroundColor Red
$appProcesses | Format-Table -Property Id, ProcessName, Path -AutoSize
if ($Force) {
Write-Host "强制终止docx_creator进程..." -ForegroundColor Red
$appProcesses | Stop-Process -Force
Start-Sleep -Seconds 2
} else {
Write-Host "请手动关闭这些进程,或使用 -Force 参数强制终止" -ForegroundColor Yellow
return
}
}
# 3. 清理虚拟环境缓存
Write-Host "清理虚拟环境缓存..." -ForegroundColor Yellow
if (Test-Path .\.venv) {
try {
# 尝试清理pip缓存
& .\.venv\Scripts\python.exe -m pip cache purge 2>$null
Write-Host "已清理pip缓存" -ForegroundColor Green
} catch {
Write-Host "清理pip缓存失败继续..." -ForegroundColor Yellow
}
}
# 4. 强制清理构建目录
Write-Host "强制清理构建目录..." -ForegroundColor Yellow
$dirsToClean = @("build", "dist", "__pycache__")
foreach ($dir in $dirsToClean) {
if (Test-Path $dir) {
try {
# 先尝试修改权限
icacls $dir /grant Everyone:F /T /Q 2>$null
# 然后删除
Remove-Item $dir -Recurse -Force -ErrorAction Stop
Write-Host "已删除 $dir 目录" -ForegroundColor Green
} catch {
Write-Host "无法删除 $dir 目录: $($_.Exception.Message)" -ForegroundColor Red
# 尝试使用robocopy清空目录
try {
$emptyDir = New-TemporaryFile | % { Remove-Item $_; New-Item -ItemType Directory -Path $_ }
robocopy $emptyDir $dir /MIR /NFL /NDL /NJH /NJS /NC /NS /NP
Remove-Item $emptyDir -Force
Remove-Item $dir -Force
Write-Host "使用robocopy成功清理 $dir" -ForegroundColor Green
} catch {
Write-Host "robocopy清理也失败跳过 $dir" -ForegroundColor Yellow
}
}
}
}
# 5. 重新创建虚拟环境(可选)
if ($Force) {
Write-Host "重新创建虚拟环境..." -ForegroundColor Yellow
if (Test-Path .\.venv) {
try {
Remove-Item .\.venv -Recurse -Force -ErrorAction Stop
Write-Host "已删除旧的虚拟环境" -ForegroundColor Green
} catch {
Write-Host "无法删除旧的虚拟环境,请手动删除" -ForegroundColor Red
return
}
}
# 创建新的虚拟环境
python -m venv .venv
Write-Host "已创建新的虚拟环境" -ForegroundColor Green
}
Write-Host "权限修复完成!" -ForegroundColor Green
Write-Host "现在可以重新运行构建脚本" -ForegroundColor Green