Skip to content

Commit

Permalink
Hardening OpenCV install script (microsoft#1180)
Browse files Browse the repository at this point in the history
It needs to handle exceptions and retry
  • Loading branch information
wes-b authored Apr 17, 2020
1 parent 48d4384 commit 07ca908
Showing 1 changed file with 39 additions and 4 deletions.
43 changes: 39 additions & 4 deletions scripts/install-opencv.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,52 @@ function Download-ToTemp
$tempDir = [System.IO.Path]::GetTempPath()
$path = Join-Path -Path $tempDir -ChildPath $filename

Write-Host -NoNewline "Downloading $url to $path..."
Write-Host "Downloading $url to $path..."
Invoke-WebRequest -Uri $url -OutFile $path -UserAgent "NativeClient" -MaximumRetryCount 5 -RetryIntervalSec 60
Write-Host "Done"
Write-Host "Downloading Done"

return $path
}

# Download OpenCV
# Total timeout is 20 minutes
$delay_in_seconds = 15
$max_retry_attempts = 80
$url = "https://sourceforge.net/projects/opencvlibrary/files/4.1.1/opencv-4.1.1-vc14_vc15.exe/download"
$filename = "opencv-4.1.1-vc14_vc15.exe"
$opencv_exe = Download-ToTemp -url $url -filename $filename

$retry = 1;
Do
{
Write-Host
Write-Host
Write-Host "Attempting to download OpenCV, try #$retry"

$opencv_exe = "error_Download-ToTemp_did_not_return_a_file" # default value incase of exception
try
{
# Download OpenCV
$opencv_exe = Download-ToTemp -url $url -filename $filename
}
catch
{
Write-Host
Write-Host "An exception was thrown: $($PSItem.ToString())"
}

Write-Host "Processing downloaded file: $opencv_exe"

$retry+=1
if (-not(Test-Path $opencv_exe))
{
if ($retry -gt $max_retry_attempts)
{
Write-Host "ERROR: Retries exhausted!"
exit 1
}
Write-Host "Retry in $delay_in_seconds seconds ..."
Start-Sleep -s $delay_in_seconds
}
}While (-not (Test-Path $opencv_exe))

Start-Process -Wait $opencv_exe -ArgumentList -o"C:\",-y
Write-Host "OpenCV installed."

0 comments on commit 07ca908

Please sign in to comment.