-
-
Notifications
You must be signed in to change notification settings - Fork 263
Expand file tree
/
Copy pathinstall_openadapt.ps1
More file actions
371 lines (297 loc) · 12 KB
/
Copy pathinstall_openadapt.ps1
File metadata and controls
371 lines (297 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# PowerShell script to pull OpenAdapt and install
################################ PARAMETERS ################################
# Change these if a different version is required
$setupdir = "C:/OpenAdaptSetup"
$tesseractCmd = "tesseract"
$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"
$pythonCmd = "python"
$pythonMinVersion = "3.10.0" # Change this if a different Lower version are supported by OpenAdapt
$pythonMaxVersion = "3.10.12" # Change this if a different Higher version are supported by OpenAdapt
$pythonInstaller = "python-3.10.11-amd64.exe"
$pythonInstallerLoc = "https://www.python.org/ftp/python/3.10.11/python-3.10.11-amd64.exe"
$nvmCmd = "nvm"
$nvmInstaller = "nvm-setup.exe"
$nvmInstallerLoc = "https://github.com/coreybutler/nvm-windows/releases/download/1.1.12/nvm-setup.exe"
$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 ################################
################################ FUNCTIONS ################################
# Run a command and ensure it did not fail
function RunAndCheck {
Param
(
[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 - Exit code: $LastExitCode" -ForegroundColor Red
if (!$SkipCleanup) {
Cleanup
exit
}
}
else {
Write-Host "Success: $Desc" -ForegroundColor Green
}
}
# Cleanup function to delete the setup directory
function Cleanup {
$exists = Test-Path -Path $setupdir
if ($exists) {
Set-Location $setupdir
Set-Location ../
Remove-Item -LiteralPath $setupdir -Force -Recurse
}
}
# Return true if a command/exe is available
function CheckCMDExists {
Param
(
[Parameter(Mandatory = $true)] [string] $command
)
$result = Get-Command $command -errorvariable getErr -erroraction 'silentlycontinue'
if ($null -eq $result) {
return $false
}
return $true
}
# Return the current user's PATH variable
function GetUserPath {
$userEnvPath = [System.Environment]::GetEnvironmentVariable("Path", "User")
return $userEnvPath
}
# Return the system's PATH variable
function GetSystemPath {
$systemEnvPath = [System.Environment]::GetEnvironmentVariable("Path", "Machine")
return $systemEnvPath
}
# 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) {
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" -ForegroundColor Red
Cleanup
exit
}
# Install the Tesseract OCR Setup exe (binary file)
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." -ForegroundColor Green
}
else {
Write-Host "TesseractOCR installation failed." -ForegroundColor Red
Cleanup
exit
}
RefreshPathVariables
# Add Tesseract OCR to the System Path variable
$systemEnvPath = GetSystemPath
$updatedSystemPath = "$systemEnvPath;$tesseractPath"
[System.Environment]::SetEnvironmentVariable("Path", $updatedSystemPath, "Machine")
RefreshPathVariables
# Add Tesseract OCR to the User Path variable
$userEnvPath = GetUserPath
$updatedUserPath = "$userEnvPath;$tesseractPath"
[System.Environment]::SetEnvironmentVariable("Path", $updatedUserPath, "User")
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."
# Stop OpenAdapt install
Cleanup
exit
}
function ComparePythonVersion($version) {
$v = [version]::new($version)
$min = [version]::new($pythonMinVersion)
$max = [version]::new($pythonMaxVersion)
return $v -ge $min -and $v -le $max
}
# Check and Istall Python and return the python command
function GetPythonCMD {
# Use python exe if it exists and is within the required version range
if (CheckCMDExists $pythonCmd) {
$res = Invoke-Expression "python -V"
$versionString = $res.Split(' ')[-1]
if (ComparePythonVersion $versionString $pythonMaxVersion) {
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 within the required version range
if (CheckCMDExists $pythonCmd) {
$res = Invoke-Expression "python -V"
$versionString = $res.Split(' ')[-1]
if (ComparePythonVersion $versionString $pythonMinVersion $pythonMaxVersion) {
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 NVM and return the nvm command
function GetNVMCMD {
$nvmExists = CheckCMDExists $nvmCmd
if (!$nvmExists) {
# Install NVM
Write-Host "Downloading NVM installer..."
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest -Uri $nvmInstallerLoc -OutFile $nvmInstaller
$exists = Test-Path -Path $nvmInstaller -PathType Leaf
if (!$exists) {
Write-Host "Failed to download NVM installer" -ForegroundColor Red
exit
}
Write-Host "Installing NVM..."
Start-Process -FilePath $nvmInstaller -Verb runAs -ArgumentList '/VERYSILENT /NORESTART /NOCANCEL /SP-' -Wait
Remove-Item $nvmInstaller
RefreshPathVariables
# Make sure NVM is now available
$nvmExists = CheckCMDExists $nvmCmd
if (!$nvmExists) {
Write-Host "Error after installing NVM. Uninstalling..."
Start-Process -FilePath $nvmInstaller -Verb runAs -ArgumentList '/VERYSILENT /NORESTART /NOCANCEL /SP-' -Wait
Cleanup
exit
}
}
# Return the nvm command
return $nvmCmd
}
# Check and Install Git and return the git command
function GetGitCMD {
$gitExists = CheckCMDExists $gitCmd
if (!$gitExists) {
# Install git
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" -ForegroundColor Red
exit
}
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
RefreshPathVariables
# Make sure git is now available
$gitExists = CheckCMDExists $gitCmd
if (!$gitExists) {
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 Install-VCRedist {
# Check if Visual C++ Redist is installed
try {
$vcredistExists = (Get-ItemPropertyValue -Path $VCRedistRegPath -Name Installed -ErrorAction Stop)
}
catch {
$vcredistExists = $false
}
if (!$vcredistExists) {
# Install Visual C++ Redist
Write-Host "Downloading Visual C++ Redist"
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest -Uri $VCRedistInstallerLoc -OutFile $VCRedistInstaller
if (-Not (Test-Path -Path $VCRedistInstaller -PathType Leaf)) {
Write-Host "Failed to download Visual C++ installer"
return
}
Write-Host "Installing Visual C++ Redist, click 'Yes' if prompted for permission"
Start-Process -FilePath $VCRedistInstaller -Verb runAs -ArgumentList "/install /quiet /norestart" -Wait
Remove-Item $VCRedistInstaller
if ($LastExitCode) {
Write-Host "Failed to install Visual C++ Redist: $LastExitCode"
return
}
}
}
################################ FUNCTIONS ################################
################################ SCRIPT ###################################
Write-Host "Install Script Started..." -ForegroundColor Yellow
# Create a new directory and run the setup from there
New-Item -ItemType Directory -Path $setupdir -Force
Set-Location -Path $setupdir
Set-ExecutionPolicy RemoteSigned -Scope Process -Force
# Check and Install the required softwares for OpenAdapt
$tesseract = GetTesseractCMD
RunAndCheck "$tesseract --version" "check TesseractOCR"
$python = GetPythonCMD
RunAndCheck "$python --version" "check Python"
$nvm = GetNVMCMD
RunAndCheck "$nvm --version" "check NVM"
$git = GetGitCMD
RunAndCheck "$git --version" "check Git"
Install-VCRedist
# OpenAdapt Setup
RunAndCheck "git clone -q https://github.com/MLDSAI/OpenAdapt.git" "clone git repo"
Set-Location .\OpenAdapt
RunAndCheck "pip install uv" "Run ``pip install uv``"
RunAndCheck "uv venv" "Run ``uv venv``"
RunAndCheck "uv run python scripts/postinstall.py" "Install other dependencies" -SkipCleanup:$true
RunAndCheck "cd openadapt"
RunAndCheck "uv run alembic upgrade head" "Run ``alembic upgrade head``" -SkipCleanup:$true
RunAndCheck "cd .."
RunAndCheck "uv run pytest" "Run ``Pytest``" -SkipCleanup:$true
Write-Host "OpenAdapt installed Successfully!" -ForegroundColor Green
Start-Process powershell -Verb RunAs -ArgumentList "-NoExit", "-Command", "Set-Location -Path '$pwd';"
################################ SCRIPT ###################################