Skip to content

Update Installation Script for Windows (powershell) #312

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 52 commits into from
Jun 29, 2023
Merged
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
ef0c0f0
test only tesseract
Jun 23, 2023
5f62bb8
test only tesseract
Jun 23, 2023
72cf55f
test tesseract only
Jun 23, 2023
a5a49c9
uncomment the last line
Jun 23, 2023
555f62e
try to redirect
Jun 23, 2023
14b604b
change the description for failed message
Jun 23, 2023
12c0c69
add OCR
Jun 23, 2023
32e285d
test python
Jun 23, 2023
d9079a9
format function calling
Jun 23, 2023
bfc950e
Update Description
Jun 23, 2023
ce37a64
prepare for git test
Jun 23, 2023
b6fa29a
test Git
Jun 23, 2023
8348f2d
update the VSCppRedistCMD
Jun 23, 2023
0a913a8
using Write-Host
Jun 23, 2023
c7f47bc
Use ';' for the return command
Jun 23, 2023
2c0f8e8
full test
Jun 23, 2023
61dded1
add description for all arguments
Jun 23, 2023
45e6cc5
test tesseract on pc with out it
Jun 24, 2023
3c8ae35
fix order for the installtion
Jun 24, 2023
a834f26
uncomment the remove-gitinstaller
Jun 24, 2023
a42225b
Stop deletion of Openadapt if the pytest fails
Jun 24, 2023
a7b0ac7
add the Cleanupfailure parameter to the apt comman
Jun 24, 2023
a0a0298
make user oriented messages
Jun 24, 2023
cf38c1a
rewrite comments
Jun 24, 2023
f8da47c
do not exit on pytest fail
Jun 24, 2023
f4f3652
remove VS C++ Redistributable
Jun 24, 2023
88120dc
remove VS C++ installtion consts
Jun 24, 2023
8f51f35
fix pytest fail exit
Jun 24, 2023
45abb80
redirect the output to null
Jun 24, 2023
716ac0d
rename the param
Jun 24, 2023
9edda66
remove unneccessary line
Jun 24, 2023
927294c
try a new powershell window for poetry shell
Jun 24, 2023
adc9b04
add colours to messages
Jun 24, 2023
f007e6f
try to fix NSIS error
Jun 24, 2023
63bfd4f
fix file name
Jun 24, 2023
ac60e41
different approach to fix NSIS
Jun 24, 2023
e2e504a
different approach to fix tesseract
Jun 24, 2023
00a46e1
try to fic NSIS error
Jun 24, 2023
01f4214
try to fix NSIS error
Jun 24, 2023
9d97431
remove line
Jun 24, 2023
827af19
remove uneccssary comment
Jun 24, 2023
0b18cfa
remove unsupported dcostrings
Jun 24, 2023
9f8d729
remove unused arugs
Jun 24, 2023
29daad9
redirect output to null
Jun 24, 2023
91fe887
Merge branch 'MLDSAI:main' into test/install
KrishPatel13 Jun 26, 2023
79da291
ran `poetry lock --no-update` to fix the warning
Jun 26, 2023
3217540
Address comment:
Jun 27, 2023
b5f4a87
Address comment:
Jun 27, 2023
9e67435
Address comment:
Jun 27, 2023
50d06bd
imporve resuability and readability
Jun 27, 2023
8dddfc2
Merge branch 'MLDSAI:main' into test/install
KrishPatel13 Jun 27, 2023
cfb69ab
remove redirection to null
Jun 29, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
255 changes: 126 additions & 129 deletions install/install_openadapt.ps1
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# PowerShell script to pull OpenAdapt and install

################################ PARAMETERS ################################
# Change these if a different version is required

# Change these if a different version of is required
$setupdir = "C:/OpenAdaptSetup"

$tesseractCmd = "tesseract"
$tesseractInstaller = "tesseract-ocr-w64-setup-5.3.1.20230401.exe"
$tesseractInstaller = "tesseract.exe"
$tesseractInstallerLoc = "https://digi.bib.uni-mannheim.de/tesseract/tesseract-ocr-w64-setup-5.3.1.20230401.exe"
$tesseractPath = "C:\Program Files\Tesseract-OCR"

Expand All @@ -18,11 +19,6 @@ $gitCmd = "git"
$gitInstaller = "Git-2.40.1-64-bit.exe"
$gitInstallerLoc = "https://github.com/git-for-windows/git/releases/download/v2.40.1.windows.1/Git-2.40.1-64-bit.exe"
$gitUninstaller = "C:\Program Files\Git\unins000.exe"

$VCRedistInstaller = "vc_redist.x64.exe"
$VCRedistInstallerLoc = "https://aka.ms/vs/17/release/vc_redist.x64.exe"
$VCRedistRegPath = "HKLM:\SOFTWARE\Microsoft\VisualStudio\14.0\VC\Runtimes\X64"

################################ PARAMETERS ################################


Expand All @@ -31,26 +27,35 @@ $VCRedistRegPath = "HKLM:\SOFTWARE\Microsoft\VisualStudio\14.0\VC\Runtimes\X64"
function RunAndCheck {
Param
(
[Parameter(Mandatory = $true)] [string] $Command,
[Parameter(Mandatory = $true)] [string] $Desc
[Parameter(Mandatory = $true)]
[string] $Command,

[Parameter(Mandatory = $true)]
[string] $Desc,

[Parameter(Mandatory = $false)]
[switch] $SkipCleanup = $false
)

Invoke-Expression $Command
if ($LastExitCode) {
Write-Host "Failed: $Desc : $LastExitCode"
Cleanup
exit
Write-Host "Failed: $Desc - Exit code: $LastExitCode" -ForegroundColor Red
if (!$SkipCleanup) {
Cleanup
exit
}
}
else {
Write-Host "Success: $Desc" -ForegroundColor Green
}

Write-Host "Success: $Desc"
}


# Cleanup function to delete the setup directory
function Cleanup {
$exists = Test-Path -Path "..\OpenAdapt"
If ($exists) {
Set-Location ..\
Remove-Item -LiteralPath "OpenAdapt" -Force -Recurse
$exists = Test-Path -Path $setupdir
if ($exists) {
Set-Location $setupdir
Remove-Item -LiteralPath $setupdir -Force -Recurse
}
}

Expand All @@ -63,212 +68,204 @@ function CheckCMDExists {
)

Get-Command $command -errorvariable getErr -erroraction 'silentlycontinue'
If ($getErr -eq $null) {
return $true
if ($getErr -eq $null) {
return $true
}
return $false
}


# Get command for python, install python if required version is unavailable
function GetPythonCMD {
# Use python exe if it exists and is the required version
if (CheckCMDExists($pythonCmd)) {
$res = Invoke-Expression "python -V"
if ($res -like $pythonVerStr) {
return $pythonCmd
}
}

# Install required python version
Write-Host "Downloading python installer"
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest -Uri $pythonInstallerLoc -OutFile $pythonInstaller
$exists = Test-Path -Path $pythonInstaller -PathType Leaf
if (!$exists) {
Write-Host "Failed to download python installer"
Cleanup
exit
}

Write-Host "Installing python, click 'Yes' if prompted for permission"
Start-Process -FilePath $pythonInstaller -Verb runAs -ArgumentList '/quiet', 'InstallAllUsers=0', 'PrependPath=1' -Wait
# Return the current user's PATH variable
function GetUserPath {
$userEnvPath = [System.Environment]::GetEnvironmentVariable("Path", "User")
return $userEnvPath
}

#Refresh Path Environment Variable
$env:Path = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User")

# Make sure python is now available and the right version
if (CheckCMDExists($pythonCmd)) {
$res = Invoke-Expression "python -V"
if ($res -like $pythonVerStr) {
Remove-Item $pythonInstaller
return $pythonCmd
}
}
# Return the system's PATH variable
function GetSystemPath {
$systemEnvPath = [System.Environment]::GetEnvironmentVariable("Path", "Machine")
return $systemEnvPath
}

Write-Host "Error after installing python. Uninstalling, click 'Yes' if prompted for permission"
Start-Process -FilePath $pythonInstaller -Verb runAs -ArgumentList '/quiet', '/uninstall' -Wait
Remove-Item $pythonInstaller

# Stop OpenAdapt install
Cleanup
exit
# Refresh Path Environment Variable for both Curent User and System
function RefreshPathVariables {
$env:Path = GetUserPath + ";" + GetSystemPath
}


# Return true if a command/exe is available
function GetTesseractCMD {
# Use tesseract alias if it exists
if (CheckCMDExists($tesseractCmd)) {
if (CheckCMDExists $tesseractCmd) {
return $tesseractCmd
}

# Check if tesseractPath exists and delete it if it does
if (Test-Path -Path $tesseractPath -PathType Container) {
Write-Host "Found Existing Old TesseractOCR, Deleting existing TesseractOCR folder"
# Delete the whole folder
Remove-Item $tesseractPath -Force -Recurse
}

# Downlaod Tesseract OCR
Write-Host "Downloading Tesseract OCR installer"
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest -Uri $tesseractInstallerLoc -OutFile $tesseractInstaller
$exists = Test-Path -Path $tesseractInstaller -PathType Leaf
if (!$exists) {
Write-Host "Failed to download Tesseract OCR installer"
Write-Host "Failed to download Tesseract OCR installer" -ForegroundColor Red
Cleanup
exit
}

# Install the Tesseract OCR Setup exe (binary file)
Write-Host "Installing Tesseract OCR, click 'Yes' if prompted for permission"
Write-Host "Installing Tesseract OCR..."
Start-Process -FilePath $tesseractInstaller -Verb runAs -ArgumentList "/S" -Wait
Remove-Item $tesseractInstaller

# Check if Tesseract OCR was installed
if (Test-Path -Path $tesseractPath -PathType Container) {
Write-Host "TesseractOCR installation successful."
Write-Host "TesseractOCR installation successful." -ForegroundColor Green
}
else {
Write-Host "TesseractOCR installation failed."
Write-Host "TesseractOCR installation failed." -ForegroundColor Red
Cleanup
exit
}

#Refresh Path Environment Variable
$env:Path = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User")
RefreshPathVariables

# Add Tesseract OCR to the System Path variable
$systemEnvPath = [System.Environment]::GetEnvironmentVariable("Path", "Machine")
$systemEnvPath = GetSystemPath
$updatedSystemPath = "$systemEnvPath;$tesseractPath"
[System.Environment]::SetEnvironmentVariable("Path", $updatedSystemPath, "Machine")

#Refresh Path Environment Variable
$env:Path = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User")
RefreshPathVariables

# Add Tesseract OCR to the User Path variable
$userEnvPath = [System.Environment]::GetEnvironmentVariable("Path", "User")
$userEnvPath = GetUserPath
$updatedUserPath = "$userEnvPath;$tesseractPath"
[System.Environment]::SetEnvironmentVariable("Path", $updatedUserPath, "User")

#Refresh Path Environment Variable
$env:Path = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User")

Write-Host "Added Tesseract OCR to PATH"

Write-Host "Added Tesseract OCR to PATH." -ForegroundColor Green

# Make sure tesseract is now available
if (CheckCMDExists($tesseractCmd)) {
return $tesseractCmd
}

Write-Host "Error after installing Tesseract OCR"
Write-Host "Error after installing Tesseract OCR."
# Stop OpenAdapt install
Cleanup
exit
}


# Check and Istall Python and return the python command
function GetPythonCMD {
# Use python exe if it exists and is the required version
if (CheckCMDExists $pythonCmd) {
$res = Invoke-Expression "python -V"
if ($res -like $pythonVerStr) {
return $pythonCmd
}
}

# Install required python version
Write-Host "Downloading python installer..."
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest -Uri $pythonInstallerLoc -OutFile $pythonInstaller
$exists = Test-Path -Path $pythonInstaller -PathType Leaf
if (!$exists) {
Write-Host "Failed to download python installer" -ForegroundColor Red
Cleanup
exit
}

Write-Host "Installing python..."
Start-Process -FilePath $pythonInstaller -Verb runAs -ArgumentList '/quiet', 'InstallAllUsers=0', 'PrependPath=1' -Wait

RefreshPathVariables

# Make sure python is now available and the right version
if (CheckCMDExists $pythonCmd) {
$res = Invoke-Expression "python -V"
if ($res -like $pythonVerStr) {
Remove-Item $pythonInstaller
return $pythonCmd
}
}

Write-Host "Error after installing python. Uninstalling, click 'Yes' if prompted for permission"
Start-Process -FilePath $pythonInstaller -Verb runAs -ArgumentList '/quiet', '/uninstall' -Wait
Remove-Item $pythonInstaller
# Stop OpenAdapt install
Cleanup
exit
}


# Check and Install Git and return the git command
function GetGitCMD {
$gitExists = CheckCMDExists($gitCmd)
$gitExists = CheckCMDExists $gitCmd
if (!$gitExists) {
# Install git
Write-Host "Downloading git installer"
Write-Host "Downloading git installer..."
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest -Uri $gitInstallerLoc -OutFile $gitInstaller
$exists = Test-Path -Path $gitInstaller -PathType Leaf
if (!$exists) {
Write-Host "Failed to download git installer"
Write-Host "Failed to download git installer" -ForegroundColor Red
exit
}

Write-Host "Installing git, click 'Yes' if prompted for permission"
Write-Host "Installing git..."
Start-Process -FilePath $gitInstaller -Verb runAs -ArgumentList '/VERYSILENT /NORESTART /NOCANCEL /SP- /CLOSEAPPLICATIONS /RESTARTAPPLICATIONS /COMPONENTS="icons,ext\reg\shellhere,assoc,assoc_sh"' -Wait
Remove-Item $gitInstaller

#Refresh Path Environment Variable
$env:Path = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User")
RefreshPathVariables

# Make sure git is now available
$gitExists = CheckCMDExists($gitCmd)
$gitExists = CheckCMDExists $gitCmd
if (!$gitExists) {
Write-Host "Error after installing git. Uninstalling, click 'Yes' if prompted for permission"
Write-Host "Error after installing git. Uninstalling..."
Start-Process -FilePath $gitUninstaller -Verb runAs -ArgumentList '/VERYSILENT', '/SUPPRESSMSGBOXES', '/NORESTART' -Wait
Cleanup
exit
}
}
# Return the git command
return $gitCmd
}


function GetVSCppRedistCMD {
# Check if Visual C++ Redist is installed
$ErrorActionPreference = "SilentlyContinue"
$vcredistExists = Get-ItemPropertyValue -Path $VCRedistRegPath -erroraction 'silentlycontinue' -Name Installed
$ErrorActionPreference = "Continue"

if (!$vcredistExists) {
# Install Visual C++ Redist
Write-Host "Downloading Visual C++ Redist"
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest -Uri $VCRedistInstallerLoc -OutFile $VCRedistInstaller
$exists = Test-Path -Path $VCRedistInstaller -PathType Leaf
if (!$exists) {
Write-Host "Failed to download Visual C++ installer"
Cleanup
exit
}

Write-Host "Installing Visual C++ Redist, click 'Yes' if prompted for permission"
Start-Process -FilePath $VCRedistInstaller -Verb runAs -ArgumentList "/install /q /norestart" -Wait
Remove-Item $VCRedistInstaller

if ($LastExitCode) {
Write-Host "Failed to install Visual C++ Redist: $LastExitCode"
Cleanup
exit
}
}
}

################################ FUNCTIONS ################################


################################ SCRIPT ################################

# Check and Install TesseractOCR -> Python 3.10 -> Git -> VS C++ Redist.
# Create a new directory and run the setup from there
New-Item -ItemType Directory -Path $setupdir -Force
Set-Location -Path $setupdir

# Check and Install the required softwares for OpenAdapt
$tesseract = GetTesseractCMD
RunAndCheck "$tesseract --version" "check tesseract version"
RunAndCheck "$tesseract --version" "check TesseractOCR"

$python = GetPythonCMD
RunAndCheck "$python --version" "check python version"
RunAndCheck "$python --version" "check Python"

$git = GetGitCMD
RunAndCheck "$git --version" "check git version"

GetVSCppRedistCMD
RunAndCheck "$git --version" "check Git"

# OpenAdapt Setup
RunAndCheck "git clone -q https://github.com/MLDSAI/OpenAdapt.git" "clone git repo"
Set-Location .\OpenAdapt
RunAndCheck "pip install poetry" "install poetry"
RunAndCheck "poetry install"
RunAndCheck "poetry shell"
RunAndCheck "alembic upgrade head" "alembic upgrade head"
RunAndCheck "pytest" "run OpenAdapt tests"
Write-Host "OpenAdapt installed successfully"
RunAndCheck "pip install poetry" "Run ``pip install poetry``"
RunAndCheck "poetry install" "Run ``poetry install``"
RunAndCheck "poetry run alembic upgrade head" "Run ``alembic upgrade head``" -SkipCleanup:$true
RunAndCheck "poetry run pytest" "Run ``Pytest``" -SkipCleanup:$true
Write-Host "OpenAdapt installed Successfully!" -ForegroundColor Green
Start-Process powershell -ArgumentList "-Command poetry shell"

################################ SCRIPT ################################