Files
anonymisation/scripts/rebuild_anon.ps1
Domi31tls 003be68ca8 chore(rebuild): script PowerShell robuste — rename + verif timestamp
Après deux rebuilds Windows silencieusement échoués (PermissionError
WinError 5 lors du os.remove par PyInstaller), amélioration du script :

1. Renommer l'ancien Anonymisation.exe en Anonymisation.old-HHMMSS.exe
   AVANT le build (au lieu de laisser PyInstaller faire os.remove qui
   échoue si Defender tient un handle). Move-Item bypass la plupart des
   scanners antivirus.

2. Exclusions Defender sur dist/ et build/ (Add-MpPreference).

3. Retry Remove-Item avec délai 10s × 5 sur build/ en cas de lock.

4. Vérification timestamp APRÈS/AVANT : si l'exe final a le même
   LastWriteTime qu'avant le build, exit code 2 "ÉCHEC CRITIQUE —
   timestamp inchangé". Évite le faux OK quand le build rate mais que
   l'ancien exe subsiste.

5. Encodage UTF-8 BOM nécessaire pour PowerShell Windows (accents
   français dans les messages).

Validé : rebuild v5d a passé — nouveau exe 17:47:40 (vs ancien 17:09:32),
ancien renommé en Anonymisation.old-174023.exe.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-15 17:48:19 +02:00

115 lines
4.6 KiB
PowerShell

# Rebuild script - anonymisation Windows
$ErrorActionPreference = "Continue"
$project = "C:\Users\dom\ai\anonymisation"
$gitea = "http://192.168.1.40:3100/Dom/anonymisation.git"
Write-Host "=== STEP 0 : Désactiver le .git parasite dans C:\Users\dom\ ==="
if (Test-Path "C:\Users\dom\.git") {
if (-not (Test-Path "C:\Users\dom\.git_parasite_disabled")) {
Rename-Item "C:\Users\dom\.git" "C:\Users\dom\.git_parasite_disabled" -ErrorAction SilentlyContinue
Write-Host " .git parasite renommé en .git_parasite_disabled"
} else {
Write-Host " .git_parasite_disabled existe déjà"
}
}
Write-Host "=== STEP 1 : Init/sync repo dans $project ==="
Set-Location $project
if (-not (Test-Path "$project\.git")) {
Write-Host " git init"
git init 2>&1 | Out-Host
Write-Host " git remote add gitea $gitea"
git remote add gitea $gitea 2>&1 | Out-Host
} else {
Write-Host " .git existe déjà"
$existing = git remote 2>&1 | Out-String
if ($existing -notmatch "gitea") {
git remote add gitea $gitea 2>&1 | Out-Host
}
}
Write-Host "=== STEP 2 : Fetch + reset main ==="
git fetch gitea 2>&1 | Out-Host
git checkout -B main 2>&1 | Out-Host
git reset --hard gitea/main 2>&1 | Out-Host
Write-Host "=== STEP 3 : Vérification du dernier commit ==="
git log --oneline -3 2>&1 | Out-Host
# Choix du venv
$venvPython = if (Test-Path "$project\venv\Scripts\python.exe") {
"$project\venv\Scripts\python.exe"
} elseif (Test-Path "$project\.venv\Scripts\python.exe") {
"$project\.venv\Scripts\python.exe"
} else {
Write-Host "ERREUR : aucun venv trouvé (ni venv\, ni .venv\)"
exit 1
}
Write-Host " Venv détecté : $venvPython"
Write-Host "=== STEP 4 : Régénérer finess_numbers.txt (entjur fix) ==="
& $venvPython "$project\scripts\build_finess_gazetteers.py" 2>&1 | Out-Host
Write-Host "=== STEP 4bis : Désactiver scan Defender sur dist/ (évite locks PyInstaller) ==="
try {
Add-MpPreference -ExclusionPath "$project\dist" -ErrorAction SilentlyContinue
Add-MpPreference -ExclusionPath "$project\build" -ErrorAction SilentlyContinue
Write-Host " Exclusions Defender ajoutées (dist, build)"
} catch {
Write-Host " Impossible d'ajouter exclusion Defender : $_"
}
Write-Host "=== STEP 5 : Cleanup build/ et dist/ (contournement lock) ==="
# Stratégie anti-lock : RENOMMER l'ancien EXE au lieu de le supprimer.
# PyInstaller essaie de `os.remove` l'EXE existant avant d'en créer un nouveau.
# Si Defender/antivirus tient un handle sur le fichier, le remove échoue avec
# PermissionError WinError 5. En renommant AVANT (Move-Item bypass le lock de
# la plupart des scanners), PyInstaller voit dist\Anonymisation.exe absent et
# peut créer le nouveau fichier sans conflit.
$exePath = "$project\dist\Anonymisation.exe"
$timestampAvant = if (Test-Path $exePath) { (Get-Item $exePath).LastWriteTime } else { [DateTime]::MinValue }
# Renommer l'ancien EXE avec un suffixe timestamp (évite conflit si multiples retries)
if (Test-Path $exePath) {
$backupName = "Anonymisation.old-$(Get-Date -Format 'HHmmss').exe"
try {
Move-Item $exePath "$project\dist\$backupName" -Force -ErrorAction Stop
Write-Host " Ancien EXE renomme -> $backupName"
} catch {
Write-Host " AVERTISSEMENT : impossible de renommer l'ancien EXE : $_"
# Fallback : essayer de le supprimer quand meme (peut marcher si le lock se libere)
Remove-Item $exePath -Force -ErrorAction SilentlyContinue
}
}
# Nettoyer le dossier build/ (peut toujours etre verrouille mais moins critique)
if (Test-Path "$project\build") {
Remove-Item -Recurse -Force "$project\build" -ErrorAction SilentlyContinue
}
Write-Host "=== STEP 6 : PyInstaller (5-15 min) ==="
$pyinstaller = if (Test-Path "$project\venv\Scripts\pyinstaller.exe") {
"$project\venv\Scripts\pyinstaller.exe"
} else {
"$project\.venv\Scripts\pyinstaller.exe"
}
& $pyinstaller --clean anonymisation_onefile.spec 2>&1 | Out-Host
Write-Host "=== STEP 7 : Vérification (timestamp AVANT/APRÈS) ==="
$exe = "$project\dist\Anonymisation.exe"
if (-not (Test-Path $exe)) {
Write-Host "ÉCHEC CRITIQUE : aucun exe produit"
exit 1
}
$timestampApres = (Get-Item $exe).LastWriteTime
if ($timestampApres -le $timestampAvant) {
$size = [math]::Round((Get-Item $exe).Length / 1MB, 1)
Write-Host "ÉCHEC CRITIQUE : l'exe n'a pas été mis à jour (timestamp inchangé)"
Write-Host " Avant : $timestampAvant"
Write-Host " Après : $timestampApres"
Write-Host " Taille : $size MB (mais c'est probablement l'ancien exe)"
exit 2
}
$size = [math]::Round((Get-Item $exe).Length / 1MB, 1)
Write-Host "OK : $exe ($size MB) - LastWriteTime : $timestampApres"