Skip to content

Commit fb3ae1e

Browse files
fix(install): improve powershell script (Issue #372) (#409)
* fix: git reinstallation even if it si present * fix: Error Message: Remove-Item : Cannot remove the item at 'C:\Users\Krish Patel\hi' because it is in use. At line:1 char:9 + Remove-Item -LiteralPath $setupdir -Force -Recurse + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [Remove-Item], PSInvalidOperation Exception + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RemoveIt emCommand * fix: issue that poetry shell gives "..runnning scripts is disabled on the command line" * fix: bug #4 issue to support multiple version of python (acc. to OpenAdapt standards) * add start mesage to know why sometimes the installtion command exits * fix the edge case where the new terminal PWD is not set to the OpenAdapt folder that was installed * Update install/install_openadapt.ps1 --------- Co-authored-by: Richard Abrich <[email protected]>
1 parent f470a09 commit fb3ae1e

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

install/install_openadapt.ps1

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ $tesseractInstallerLoc = "https://digi.bib.uni-mannheim.de/tesseract/tesseract-o
1111
$tesseractPath = "C:\Program Files\Tesseract-OCR"
1212

1313
$pythonCmd = "python"
14-
$pythonVerStr = "Python 3.10*"
14+
$pythonMinVersion = "3.10.0" # Change this if a different Lower version are supported by OpenAdapt
15+
$pythonMaxVersion = "3.10.12" # Change this if a different Higher version are supported by OpenAdapt
1516
$pythonInstaller = "python-3.10.11-amd64.exe"
1617
$pythonInstallerLoc = "https://www.python.org/ftp/python/3.10.11/python-3.10.11-amd64.exe"
1718

@@ -55,6 +56,7 @@ function Cleanup {
5556
$exists = Test-Path -Path $setupdir
5657
if ($exists) {
5758
Set-Location $setupdir
59+
Set-Location ../
5860
Remove-Item -LiteralPath $setupdir -Force -Recurse
5961
}
6062
}
@@ -67,11 +69,11 @@ function CheckCMDExists {
6769
[Parameter(Mandatory = $true)] [string] $command
6870
)
6971

70-
Get-Command $command -errorvariable getErr -erroraction 'silentlycontinue'
71-
if ($getErr -eq $null) {
72-
return $true
72+
$result = Get-Command $command -errorvariable getErr -erroraction 'silentlycontinue'
73+
if ($null -eq $result) {
74+
return $false
7375
}
74-
return $false
76+
return $true
7577
}
7678

7779

@@ -163,12 +165,23 @@ function GetTesseractCMD {
163165
}
164166

165167

168+
function ComparePythonVersion($version) {
169+
$v = [version]::new($version)
170+
$min = [version]::new($pythonMinVersion)
171+
$max = [version]::new($pythonMaxVersion)
172+
173+
return $v -ge $min -and $v -le $max
174+
}
175+
176+
166177
# Check and Istall Python and return the python command
167178
function GetPythonCMD {
168-
# Use python exe if it exists and is the required version
179+
# Use python exe if it exists and is within the required version range
169180
if (CheckCMDExists $pythonCmd) {
170181
$res = Invoke-Expression "python -V"
171-
if ($res -like $pythonVerStr) {
182+
$versionString = $res.Split(' ')[-1]
183+
184+
if (ComparePythonVersion $versionString $pythonMaxVersion) {
172185
return $pythonCmd
173186
}
174187
}
@@ -178,6 +191,7 @@ function GetPythonCMD {
178191
$ProgressPreference = 'SilentlyContinue'
179192
Invoke-WebRequest -Uri $pythonInstallerLoc -OutFile $pythonInstaller
180193
$exists = Test-Path -Path $pythonInstaller -PathType Leaf
194+
181195
if (!$exists) {
182196
Write-Host "Failed to download python installer" -ForegroundColor Red
183197
Cleanup
@@ -189,10 +203,12 @@ function GetPythonCMD {
189203

190204
RefreshPathVariables
191205

192-
# Make sure python is now available and the right version
206+
# Make sure python is now available and within the required version range
193207
if (CheckCMDExists $pythonCmd) {
194208
$res = Invoke-Expression "python -V"
195-
if ($res -like $pythonVerStr) {
209+
$versionString = $res.Split(' ')[-1]
210+
211+
if (ComparePythonVersion $versionString $pythonMinVersion $pythonMaxVersion) {
196212
Remove-Item $pythonInstaller
197213
return $pythonCmd
198214
}
@@ -244,9 +260,12 @@ function GetGitCMD {
244260

245261
################################ SCRIPT ################################
246262

263+
Write-Host "Install Script Started..." -ForegroundColor Yellow
264+
247265
# Create a new directory and run the setup from there
248266
New-Item -ItemType Directory -Path $setupdir -Force
249267
Set-Location -Path $setupdir
268+
Set-ExecutionPolicy RemoteSigned -Scope Process -Force
250269

251270
# Check and Install the required softwares for OpenAdapt
252271
$tesseract = GetTesseractCMD
@@ -266,6 +285,6 @@ RunAndCheck "poetry install" "Run ``poetry install``"
266285
RunAndCheck "poetry run alembic upgrade head" "Run ``alembic upgrade head``" -SkipCleanup:$true
267286
RunAndCheck "poetry run pytest" "Run ``Pytest``" -SkipCleanup:$true
268287
Write-Host "OpenAdapt installed Successfully!" -ForegroundColor Green
269-
Start-Process powershell -ArgumentList "-Command poetry shell"
288+
Start-Process powershell -Verb RunAs -ArgumentList "-NoExit", "-Command", "Set-Location -Path '$pwd'; poetry shell"
270289

271290
################################ SCRIPT ################################

0 commit comments

Comments
 (0)