Skip to content

fix(install): improve powershell script (Issue #372) #409

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 9 commits into from
Jul 25, 2023
Merged
Changes from 4 commits
Commits
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
37 changes: 27 additions & 10 deletions install/install_openadapt.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ $tesseractInstallerLoc = "https://digi.bib.uni-mannheim.de/tesseract/tesseract-o
$tesseractPath = "C:\Program Files\Tesseract-OCR"

$pythonCmd = "python"
$pythonVerStr = "Python 3.10*"
$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"

Expand Down Expand Up @@ -55,6 +56,7 @@ function Cleanup {
$exists = Test-Path -Path $setupdir
if ($exists) {
Set-Location $setupdir
Set-Location ../
Remove-Item -LiteralPath $setupdir -Force -Recurse
}
}
Expand All @@ -67,11 +69,11 @@ function CheckCMDExists {
[Parameter(Mandatory = $true)] [string] $command
)

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


Expand Down Expand Up @@ -163,12 +165,23 @@ function GetTesseractCMD {
}


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 the required version
# Use python exe if it exists and is within the required version range
if (CheckCMDExists $pythonCmd) {
$res = Invoke-Expression "python -V"
if ($res -like $pythonVerStr) {
$versionString = $res.Split(' ')[-1]

if (ComparePythonVersion $versionString $pythonMaxVersion) {
return $pythonCmd
}
}
Expand All @@ -178,6 +191,7 @@ function GetPythonCMD {
$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
Expand All @@ -189,10 +203,12 @@ function GetPythonCMD {

RefreshPathVariables

# Make sure python is now available and the right version
# Make sure python is now available and within the required version range
if (CheckCMDExists $pythonCmd) {
$res = Invoke-Expression "python -V"
if ($res -like $pythonVerStr) {
$versionString = $res.Split(' ')[-1]

if (ComparePythonVersion $versionString $pythonMinVersion $pythonMaxVersion) {
Remove-Item $pythonInstaller
return $pythonCmd
}
Expand Down Expand Up @@ -247,6 +263,7 @@ function GetGitCMD {
# 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
Expand All @@ -266,6 +283,6 @@ 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"
Start-Process powershell -ArgumentList "-ExecutionPolicy Bypass", "-NoExit", "-Command", "poetry shell"

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