Skip to content

Commit ddb7d78

Browse files
authored
feat(install): install tesseract, remove vcredist (windows)
* test only tesseract * test only tesseract * test tesseract only * uncomment the last line * try to redirect * change the description for failed message * add OCR * test python * format function calling * Update Description * prepare for git test * test Git * update the VSCppRedistCMD * using Write-Host * Use ';' for the return command * full test * add description for all arguments * test tesseract on pc with out it * fix order for the installtion * uncomment the remove-gitinstaller * Stop deletion of Openadapt if the pytest fails * add the Cleanupfailure parameter to the apt comman * make user oriented messages * rewrite comments * do not exit on pytest fail * remove VS C++ Redistributable * remove VS C++ installtion consts * fix pytest fail exit * redirect the output to null * rename the param * remove unneccessary line * try a new powershell window for poetry shell * add colours to messages * try to fix NSIS error * fix file name * different approach to fix NSIS * different approach to fix tesseract * try to fic NSIS error * try to fix NSIS error * remove line * remove uneccssary comment * remove unsupported dcostrings * remove unused arugs * redirect output to null * ran `poetry lock --no-update` to fix the warning * Address comment: #312 (comment) * Address comment: #312 (comment) * Address comment: #312 (comment) * imporve resuability and readability * remove redirection to null
1 parent 5ab0e01 commit ddb7d78

File tree

1 file changed

+126
-129
lines changed

1 file changed

+126
-129
lines changed

install/install_openadapt.ps1

Lines changed: 126 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
# PowerShell script to pull OpenAdapt and install
22

33
################################ PARAMETERS ################################
4+
# Change these if a different version is required
45

5-
# Change these if a different version of is required
6+
$setupdir = "C:/OpenAdaptSetup"
67

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

@@ -18,11 +19,6 @@ $gitCmd = "git"
1819
$gitInstaller = "Git-2.40.1-64-bit.exe"
1920
$gitInstallerLoc = "https://github.com/git-for-windows/git/releases/download/v2.40.1.windows.1/Git-2.40.1-64-bit.exe"
2021
$gitUninstaller = "C:\Program Files\Git\unins000.exe"
21-
22-
$VCRedistInstaller = "vc_redist.x64.exe"
23-
$VCRedistInstallerLoc = "https://aka.ms/vs/17/release/vc_redist.x64.exe"
24-
$VCRedistRegPath = "HKLM:\SOFTWARE\Microsoft\VisualStudio\14.0\VC\Runtimes\X64"
25-
2622
################################ PARAMETERS ################################
2723

2824

@@ -31,26 +27,35 @@ $VCRedistRegPath = "HKLM:\SOFTWARE\Microsoft\VisualStudio\14.0\VC\Runtimes\X64"
3127
function RunAndCheck {
3228
Param
3329
(
34-
[Parameter(Mandatory = $true)] [string] $Command,
35-
[Parameter(Mandatory = $true)] [string] $Desc
30+
[Parameter(Mandatory = $true)]
31+
[string] $Command,
32+
33+
[Parameter(Mandatory = $true)]
34+
[string] $Desc,
35+
36+
[Parameter(Mandatory = $false)]
37+
[switch] $SkipCleanup = $false
3638
)
3739

3840
Invoke-Expression $Command
3941
if ($LastExitCode) {
40-
Write-Host "Failed: $Desc : $LastExitCode"
41-
Cleanup
42-
exit
42+
Write-Host "Failed: $Desc - Exit code: $LastExitCode" -ForegroundColor Red
43+
if (!$SkipCleanup) {
44+
Cleanup
45+
exit
46+
}
47+
}
48+
else {
49+
Write-Host "Success: $Desc" -ForegroundColor Green
4350
}
44-
45-
Write-Host "Success: $Desc"
4651
}
4752

48-
53+
# Cleanup function to delete the setup directory
4954
function Cleanup {
50-
$exists = Test-Path -Path "..\OpenAdapt"
51-
If ($exists) {
52-
Set-Location ..\
53-
Remove-Item -LiteralPath "OpenAdapt" -Force -Recurse
55+
$exists = Test-Path -Path $setupdir
56+
if ($exists) {
57+
Set-Location $setupdir
58+
Remove-Item -LiteralPath $setupdir -Force -Recurse
5459
}
5560
}
5661

@@ -63,212 +68,204 @@ function CheckCMDExists {
6368
)
6469

6570
Get-Command $command -errorvariable getErr -erroraction 'silentlycontinue'
66-
If ($getErr -eq $null) {
67-
return $true
71+
if ($getErr -eq $null) {
72+
return $true
6873
}
6974
return $false
7075
}
7176

7277

73-
# Get command for python, install python if required version is unavailable
74-
function GetPythonCMD {
75-
# Use python exe if it exists and is the required version
76-
if (CheckCMDExists($pythonCmd)) {
77-
$res = Invoke-Expression "python -V"
78-
if ($res -like $pythonVerStr) {
79-
return $pythonCmd
80-
}
81-
}
82-
83-
# Install required python version
84-
Write-Host "Downloading python installer"
85-
$ProgressPreference = 'SilentlyContinue'
86-
Invoke-WebRequest -Uri $pythonInstallerLoc -OutFile $pythonInstaller
87-
$exists = Test-Path -Path $pythonInstaller -PathType Leaf
88-
if (!$exists) {
89-
Write-Host "Failed to download python installer"
90-
Cleanup
91-
exit
92-
}
93-
94-
Write-Host "Installing python, click 'Yes' if prompted for permission"
95-
Start-Process -FilePath $pythonInstaller -Verb runAs -ArgumentList '/quiet', 'InstallAllUsers=0', 'PrependPath=1' -Wait
78+
# Return the current user's PATH variable
79+
function GetUserPath {
80+
$userEnvPath = [System.Environment]::GetEnvironmentVariable("Path", "User")
81+
return $userEnvPath
82+
}
9683

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

100-
# Make sure python is now available and the right version
101-
if (CheckCMDExists($pythonCmd)) {
102-
$res = Invoke-Expression "python -V"
103-
if ($res -like $pythonVerStr) {
104-
Remove-Item $pythonInstaller
105-
return $pythonCmd
106-
}
107-
}
85+
# Return the system's PATH variable
86+
function GetSystemPath {
87+
$systemEnvPath = [System.Environment]::GetEnvironmentVariable("Path", "Machine")
88+
return $systemEnvPath
89+
}
10890

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

113-
# Stop OpenAdapt install
114-
Cleanup
115-
exit
92+
# Refresh Path Environment Variable for both Curent User and System
93+
function RefreshPathVariables {
94+
$env:Path = GetUserPath + ";" + GetSystemPath
11695
}
11796

97+
98+
# Return true if a command/exe is available
11899
function GetTesseractCMD {
119100
# Use tesseract alias if it exists
120-
if (CheckCMDExists($tesseractCmd)) {
101+
if (CheckCMDExists $tesseractCmd) {
121102
return $tesseractCmd
122103
}
123104

105+
# Check if tesseractPath exists and delete it if it does
106+
if (Test-Path -Path $tesseractPath -PathType Container) {
107+
Write-Host "Found Existing Old TesseractOCR, Deleting existing TesseractOCR folder"
108+
# Delete the whole folder
109+
Remove-Item $tesseractPath -Force -Recurse
110+
}
111+
124112
# Downlaod Tesseract OCR
125113
Write-Host "Downloading Tesseract OCR installer"
126114
$ProgressPreference = 'SilentlyContinue'
127115
Invoke-WebRequest -Uri $tesseractInstallerLoc -OutFile $tesseractInstaller
128116
$exists = Test-Path -Path $tesseractInstaller -PathType Leaf
129117
if (!$exists) {
130-
Write-Host "Failed to download Tesseract OCR installer"
118+
Write-Host "Failed to download Tesseract OCR installer" -ForegroundColor Red
131119
Cleanup
132120
exit
133121
}
134122

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

140128
# Check if Tesseract OCR was installed
141129
if (Test-Path -Path $tesseractPath -PathType Container) {
142-
Write-Host "TesseractOCR installation successful."
130+
Write-Host "TesseractOCR installation successful." -ForegroundColor Green
143131
}
144132
else {
145-
Write-Host "TesseractOCR installation failed."
133+
Write-Host "TesseractOCR installation failed." -ForegroundColor Red
146134
Cleanup
147135
exit
148136
}
149137

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

153140
# Add Tesseract OCR to the System Path variable
154-
$systemEnvPath = [System.Environment]::GetEnvironmentVariable("Path", "Machine")
141+
$systemEnvPath = GetSystemPath
155142
$updatedSystemPath = "$systemEnvPath;$tesseractPath"
156143
[System.Environment]::SetEnvironmentVariable("Path", $updatedSystemPath, "Machine")
157144

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

161147
# Add Tesseract OCR to the User Path variable
162-
$userEnvPath = [System.Environment]::GetEnvironmentVariable("Path", "User")
148+
$userEnvPath = GetUserPath
163149
$updatedUserPath = "$userEnvPath;$tesseractPath"
164150
[System.Environment]::SetEnvironmentVariable("Path", $updatedUserPath, "User")
165151

166-
#Refresh Path Environment Variable
167-
$env:Path = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User")
168-
169-
Write-Host "Added Tesseract OCR to PATH"
170-
152+
Write-Host "Added Tesseract OCR to PATH." -ForegroundColor Green
171153

172154
# Make sure tesseract is now available
173155
if (CheckCMDExists($tesseractCmd)) {
174156
return $tesseractCmd
175157
}
176158

177-
Write-Host "Error after installing Tesseract OCR"
159+
Write-Host "Error after installing Tesseract OCR."
178160
# Stop OpenAdapt install
179161
Cleanup
180162
exit
181163
}
182164

183165

166+
# Check and Istall Python and return the python command
167+
function GetPythonCMD {
168+
# Use python exe if it exists and is the required version
169+
if (CheckCMDExists $pythonCmd) {
170+
$res = Invoke-Expression "python -V"
171+
if ($res -like $pythonVerStr) {
172+
return $pythonCmd
173+
}
174+
}
175+
176+
# Install required python version
177+
Write-Host "Downloading python installer..."
178+
$ProgressPreference = 'SilentlyContinue'
179+
Invoke-WebRequest -Uri $pythonInstallerLoc -OutFile $pythonInstaller
180+
$exists = Test-Path -Path $pythonInstaller -PathType Leaf
181+
if (!$exists) {
182+
Write-Host "Failed to download python installer" -ForegroundColor Red
183+
Cleanup
184+
exit
185+
}
186+
187+
Write-Host "Installing python..."
188+
Start-Process -FilePath $pythonInstaller -Verb runAs -ArgumentList '/quiet', 'InstallAllUsers=0', 'PrependPath=1' -Wait
189+
190+
RefreshPathVariables
191+
192+
# Make sure python is now available and the right version
193+
if (CheckCMDExists $pythonCmd) {
194+
$res = Invoke-Expression "python -V"
195+
if ($res -like $pythonVerStr) {
196+
Remove-Item $pythonInstaller
197+
return $pythonCmd
198+
}
199+
}
200+
201+
Write-Host "Error after installing python. Uninstalling, click 'Yes' if prompted for permission"
202+
Start-Process -FilePath $pythonInstaller -Verb runAs -ArgumentList '/quiet', '/uninstall' -Wait
203+
Remove-Item $pythonInstaller
204+
# Stop OpenAdapt install
205+
Cleanup
206+
exit
207+
}
208+
209+
210+
# Check and Install Git and return the git command
184211
function GetGitCMD {
185-
$gitExists = CheckCMDExists($gitCmd)
212+
$gitExists = CheckCMDExists $gitCmd
186213
if (!$gitExists) {
187214
# Install git
188-
Write-Host "Downloading git installer"
215+
Write-Host "Downloading git installer..."
189216
$ProgressPreference = 'SilentlyContinue'
190217
Invoke-WebRequest -Uri $gitInstallerLoc -OutFile $gitInstaller
191218
$exists = Test-Path -Path $gitInstaller -PathType Leaf
192219
if (!$exists) {
193-
Write-Host "Failed to download git installer"
220+
Write-Host "Failed to download git installer" -ForegroundColor Red
194221
exit
195222
}
196223

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

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

204230
# Make sure git is now available
205-
$gitExists = CheckCMDExists($gitCmd)
231+
$gitExists = CheckCMDExists $gitCmd
206232
if (!$gitExists) {
207-
Write-Host "Error after installing git. Uninstalling, click 'Yes' if prompted for permission"
233+
Write-Host "Error after installing git. Uninstalling..."
208234
Start-Process -FilePath $gitUninstaller -Verb runAs -ArgumentList '/VERYSILENT', '/SUPPRESSMSGBOXES', '/NORESTART' -Wait
235+
Cleanup
209236
exit
210237
}
211238
}
212239
# Return the git command
213240
return $gitCmd
214241
}
215-
216-
217-
function GetVSCppRedistCMD {
218-
# Check if Visual C++ Redist is installed
219-
$ErrorActionPreference = "SilentlyContinue"
220-
$vcredistExists = Get-ItemPropertyValue -Path $VCRedistRegPath -erroraction 'silentlycontinue' -Name Installed
221-
$ErrorActionPreference = "Continue"
222-
223-
if (!$vcredistExists) {
224-
# Install Visual C++ Redist
225-
Write-Host "Downloading Visual C++ Redist"
226-
$ProgressPreference = 'SilentlyContinue'
227-
Invoke-WebRequest -Uri $VCRedistInstallerLoc -OutFile $VCRedistInstaller
228-
$exists = Test-Path -Path $VCRedistInstaller -PathType Leaf
229-
if (!$exists) {
230-
Write-Host "Failed to download Visual C++ installer"
231-
Cleanup
232-
exit
233-
}
234-
235-
Write-Host "Installing Visual C++ Redist, click 'Yes' if prompted for permission"
236-
Start-Process -FilePath $VCRedistInstaller -Verb runAs -ArgumentList "/install /q /norestart" -Wait
237-
Remove-Item $VCRedistInstaller
238-
239-
if ($LastExitCode) {
240-
Write-Host "Failed to install Visual C++ Redist: $LastExitCode"
241-
Cleanup
242-
exit
243-
}
244-
}
245-
}
246-
247242
################################ FUNCTIONS ################################
248243

249244

250245
################################ SCRIPT ################################
251246

252-
# Check and Install TesseractOCR -> Python 3.10 -> Git -> VS C++ Redist.
247+
# Create a new directory and run the setup from there
248+
New-Item -ItemType Directory -Path $setupdir -Force
249+
Set-Location -Path $setupdir
250+
251+
# Check and Install the required softwares for OpenAdapt
253252
$tesseract = GetTesseractCMD
254-
RunAndCheck "$tesseract --version" "check tesseract version"
253+
RunAndCheck "$tesseract --version" "check TesseractOCR"
255254

256255
$python = GetPythonCMD
257-
RunAndCheck "$python --version" "check python version"
256+
RunAndCheck "$python --version" "check Python"
258257

259258
$git = GetGitCMD
260-
RunAndCheck "$git --version" "check git version"
261-
262-
GetVSCppRedistCMD
259+
RunAndCheck "$git --version" "check Git"
263260

264261
# OpenAdapt Setup
265262
RunAndCheck "git clone -q https://github.com/MLDSAI/OpenAdapt.git" "clone git repo"
266263
Set-Location .\OpenAdapt
267-
RunAndCheck "pip install poetry" "install poetry"
268-
RunAndCheck "poetry install"
269-
RunAndCheck "poetry shell"
270-
RunAndCheck "alembic upgrade head" "alembic upgrade head"
271-
RunAndCheck "pytest" "run OpenAdapt tests"
272-
Write-Host "OpenAdapt installed successfully"
264+
RunAndCheck "pip install poetry" "Run ``pip install poetry``"
265+
RunAndCheck "poetry install" "Run ``poetry install``"
266+
RunAndCheck "poetry run alembic upgrade head" "Run ``alembic upgrade head``" -SkipCleanup:$true
267+
RunAndCheck "poetry run pytest" "Run ``Pytest``" -SkipCleanup:$true
268+
Write-Host "OpenAdapt installed Successfully!" -ForegroundColor Green
269+
Start-Process powershell -ArgumentList "-Command poetry shell"
273270

274271
################################ SCRIPT ################################

0 commit comments

Comments
 (0)