# 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 4a : Générer build_info.py (identifiant de build) ===" $commit = (git rev-parse --short HEAD 2>&1).Trim() $branch = (git rev-parse --abbrev-ref HEAD 2>&1).Trim() $buildDate = Get-Date -Format "yyyy-MM-dd HH:mm" $buildInfo = @" """Métadonnées de build - GENERE AUTOMATIQUEMENT par rebuild_anon.ps1. Ne PAS editer a la main - ecrase a chaque rebuild.""" BUILD_DATE = "$buildDate" BUILD_COMMIT = "$commit" BUILD_BRANCH = "$branch" "@ $buildInfo | Set-Content "$project\build_info.py" -Encoding UTF8 Write-Host " build_info.py: $buildDate / $commit / $branch" 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"