Files

71 lines
3.4 KiB
PowerShell

# SPDX-License-Identifier: MPL-2.0
# Fonctions de telechargement communes (install-cuda.ps1, install-cudnn.ps1, get-flatc.ps1).
# ASCII uniquement : compatible Windows PowerShell 5.1 sans BOM.
# Ce fichier n'est pas un script autonome : il se dot-source depuis un autre script.
# Titre d'etape, meme style dans tous les scripts d'installation.
function Write-Step([string]$text) {
Write-Host "==> $text" -ForegroundColor Cyan
}
# Taille distante via une requete HEAD (curl.exe), en suivant les redirections.
function Get-RemoteSize([string]$url) {
$headers = & curl.exe -sIL --fail $url
if ($LASTEXITCODE -ne 0) { throw "Serveur injoignable : $url" }
$length = $headers | Select-String -Pattern '^Content-Length:\s*(\d+)' | Select-Object -Last 1
if (-not $length) { throw "Taille du fichier inconnue (pas de Content-Length)." }
[int64]$length.Matches[0].Groups[1].Value
}
# Empreinte generique via .NET : Get-FileHash manque dans certains Windows PowerShell 5.1
# (PSModulePath pointant vers les modules de PowerShell 7).
function Get-FileDigest([string]$path, [string]$Algorithm = "SHA256") {
switch ($Algorithm.ToUpperInvariant()) {
"SHA256" { $hasher = [System.Security.Cryptography.SHA256]::Create() }
"MD5" { $hasher = [System.Security.Cryptography.MD5]::Create() }
default { throw "Algorithme d'empreinte non gere : $Algorithm (SHA256 ou MD5)." }
}
$stream = [System.IO.File]::OpenRead($path)
try {
([System.BitConverter]::ToString($hasher.ComputeHash($stream))).Replace("-", "")
}
finally {
$hasher.Dispose()
$stream.Dispose()
}
}
# Verifie qu'il reste au moins $Needed octets libres sur le volume de $Path.
function Assert-FreeSpace([string]$Path, [int64]$Needed) {
$drive = Get-PSDrive -Name ([System.IO.Path]::GetPathRoot($Path).Substring(0, 1))
if ($drive.Free -lt $Needed) {
throw ("Espace disque insuffisant sur {0}: ({1:N1} Go libres, {2:N1} Go conseilles)." -f $drive.Name, ($drive.Free / 1GB), ($Needed / 1GB))
}
}
# Telechargement avec reprise : curl -C - repart de la taille du fichier .part.
function Invoke-ResumableDownload([string]$url, [string]$destination, [int64]$expectedSize, [int]$maxAttempts) {
$part = "$destination.part"
for ($attempt = 1; $attempt -le $maxAttempts; $attempt++) {
$have = if (Test-Path $part) { (Get-Item $part).Length } else { 0 }
if ($have -eq $expectedSize) { break }
if ($have -gt $expectedSize) {
Write-Host " Fichier partiel plus grand que prevu : on recommence." -ForegroundColor Yellow
Remove-Item $part -Force
$have = 0
}
$pct = [math]::Round(100.0 * $have / $expectedSize, 1)
Write-Host (" Tentative {0}/{1} : {2:N0} / {3:N0} octets ({4} %)" -f $attempt, $maxAttempts, $have, $expectedSize, $pct)
& curl.exe -L --fail -C - --retry 5 --retry-delay 5 --retry-all-errors --speed-limit 1024 --speed-time 60 -o $part $url
if ($LASTEXITCODE -ne 0) {
Write-Host " curl interrompu (code $LASTEXITCODE), reprise dans 10 s..." -ForegroundColor Yellow
Start-Sleep -Seconds 10
}
}
$final = if (Test-Path $part) { (Get-Item $part).Length } else { 0 }
if ($final -ne $expectedSize) {
throw "Telechargement incomplet apres $maxAttempts tentatives ($final / $expectedSize octets). Relancer le script pour reprendre."
}
Move-Item $part $destination -Force
}