diff --git a/install-python.ps1 b/install-python.ps1 deleted file mode 100644 index c4914e1..0000000 --- a/install-python.ps1 +++ /dev/null @@ -1,54 +0,0 @@ -$python = "$PSScriptRoot\python3.7.3.exe" - -function Assert-IsPythonRequired { - $result = Get-WmiObject -Namespace "root/cimv2" -Class Win32_Product -Filter "Name Like 'Python 3.7.3 Executables%'" - return $result.Count -eq 0 -} - -function Cleanup { - Remove-Item $python - Start-Process powershell - Stop-Process $PID -} - -function Download-Python { - $arch ="$env:PROCESSOR_ARCHITECTURE" - $url = "" - - switch($arch) { - "amd64" { - Write-Host "Downloading x64 version of Python." - $url = "https://www.python.org/ftp/python/3.7.3/python-3.7.3-amd64-webinstall.exe" - } - "x86" { - Write-Host "Downloading x86 version of Python." - $url = "https://www.python.org/ftp/python/3.7.3/python-3.7.3-webinstall.exe" - } - default { - Write-Host "Unable to determine the Python download url for processor type: $arch." - } - } - - if (-not ([string]::IsNullOrEmpty($url))) { - $client = New-Object System.Net.WebClient - $client.DownloadFile($url, $python) - return $True - } - - return $False -} - -if (Assert-IsPythonRequired) { - if (Download-Python) { - Write-Host "Silently installing Python. Do not close this window." - $install = Start-Process $python -ArgumentList "/quiet InstallAllUsers=1 PrependPath=1" -PassThru -wait - if ($install.ExitCode -eq 0) { - Write-Host "Installation completed successfully." - } - else { - Write-Host "Installation failed with exit code $install.ExitCode" - } - } -} - -Cleanup diff --git a/scripts/bundled_installer.bat b/scripts/bundled_installer.bat new file mode 100644 index 0000000..9eccd39 --- /dev/null +++ b/scripts/bundled_installer.bat @@ -0,0 +1 @@ +Powershell.exe -executionpolicy remotesigned -File "%~dp0\bundled_installer.ps1" diff --git a/scripts/bundled_installer.ps1 b/scripts/bundled_installer.ps1 new file mode 100644 index 0000000..c2c09fe --- /dev/null +++ b/scripts/bundled_installer.ps1 @@ -0,0 +1,57 @@ +function Write-OutputWithInvertedColors([String] $Message) { + Write-Host $Message -ForegroundColor Black -BackgroundColor White +} + +function Get-HomePath() { + if (Test-Path Env:\USERPROFILE) { + return $env:USERPROFILE + } elseif (Test-Path Env:\HOMEDRIVE -and Env:\HOMEPATH) { + return $env:HOMEDRIVE + $env:HOMEPATH + } elseif (Test-Path Env:\LOCALAPPDATA) { + return $env:LOCALAPPDATA + } elseif (Test-Path Env:\APPDATA) { + return $env:APPDATA + } else { + Write-Host "Cannot determine user's home directory." -ForegroundColor Red + Exit 1 + } +} + +function Exit-OnFailure() { + if ($LASTEXITCODE -ne 0) { + Write-Host "Exiting due to failure" -ForegroundColor Red + exit 1 + } +} + +function Update-UserEnvironmentPath { + $env:Path = + [System.Environment]::GetEnvironmentVariable("Path","Machine") + + ";" + + [System.Environment]::GetEnvironmentVariable("Path","User") +} + +function Write-StepTitle([String] $Message) { + $equals = "=" * 46 + Write-Host "" + Write-OutputWithInvertedColors $equals + Write-OutputWithInvertedColors $Message + Write-OutputWithInvertedColors $equals +} +Write-StepTitle "I. Installing Python " +powershell "$PSScriptRoot\install-python.ps1" +Exit-OnFailure +Update-UserEnvironmentPath + +Write-StepTitle "II. Creating self-contained EBCLI installation" +$HomeDir = Get-HomePath +Write-Host "Installing the EBCLI in $HomeDir\.ebcli-virtual-env" +python "$PSScriptRoot\ebcli_installer.py" --virtualenv-executable "$PSScriptRoot\virtualenv\bin\virtualenv.exe" --hide-export-recommendation --location $HomeDir +Exit-OnFailure + +$PathExporter = "$HomeDir\.ebcli-virtual-env\executables\path_exporter.vbs" +if ([System.IO.File]::Exists($PathExporter)) { + Write-StepTitle "III. Exporting `eb` PATH's " + & "$HomeDir\.ebcli-virtual-env\executables\path_exporter.vbs" + Update-UserEnvironmentPath +} diff --git a/scripts/install-python.ps1 b/scripts/install-python.ps1 new file mode 100644 index 0000000..e715d61 --- /dev/null +++ b/scripts/install-python.ps1 @@ -0,0 +1,83 @@ +$PythonInstaller = "$PSScriptRoot\python3.7.3.exe" +$StepNumber = 1 + +function Write-StepTitle([String] $StepMessage) { + $StepMessage="$Script:StepNumber. $StepMessage" + $MessageLength=$StepMessage.length + $RepeatedStars = "*" * $MessageLength + Write-Host "" + Write-Host $RepeatedStars + Write-Host $StepMessage + Write-Host $RepeatedStars + $Script:StepNumber = $Script:StepNumber + 1 +} + +function Get-PythonExecutable { + Write-StepTitle "Looking for existing Python 3.7.3 installation." + return Get-WmiObject -Namespace "root/cimv2" -Class Win32_Product -Filter "Name Like 'Python 3.7.3 Executables%'" +} + +function Get-PythonInstallationTarget { + if ([Environment]::Is64BitOperatingSystem) { + Write-StepTitle "Downloading x64 version of Python." + return "https://www.python.org/ftp/python/3.7.3/python-3.7.3-amd64-webinstall.exe" + } else { + Write-StepTitle "Downloading x86 version of Python." + return "https://www.python.org/ftp/python/3.7.3/python-3.7.3-webinstall.exe" + } +} + +function Get-PythonMSI { + if ([System.IO.File]::Exists($PythonInstaller)) { + Remove-Item $PythonInstaller + } + $url = Get-PythonInstallationTarget + $client = New-Object System.Net.WebClient + try { + $client.DownloadFile($url, $PythonInstaller) + } catch { + Write-Host "Failed to download Python. The following exception was raised:" -ForegroundColor Red + Write-Host $_.exception -ForegroundColor Red + + Exit 1 + } +} + +function Install-Python { + Write-StepTitle "Installing Python. Do not close this window." + Write-Host "Installing Python. Do not close this window." + $install = Start-Process $PythonInstaller -ArgumentList "InstallAllUsers=1 PrependPath=1" -PassThru -wait + if ($install.ExitCode -eq 0) { + Write-Host "Installation completed successfully." -ForegroundColor Green + } elseif ($install.ExitCode -eq 1602) { + Write-Host "Installer was exited by the user." -ForegroundColor Red + Exit 1 + } else { + Write-Host "Installation failed with exit code $install.ExitCode" -ForegroundColor Red + Exit 1 + } +} + +function Update-UserEnvironmentPath { + Write-StepTitle "Updating Environment PATH of this shell." + $env:Path = + [System.Environment]::GetEnvironmentVariable("Path","Machine") + + ";" + + [System.Environment]::GetEnvironmentVariable("Path","User") +} + +function Install-Virtualenv { + Write-StepTitle "Installing virtualenv" + Invoke-Expression "pip install virtualenv --target $PSScriptRoot\virtualenv --upgrade" +} + +$PythonExecutable = Get-PythonExecutable +if ($PythonExecutable.count -eq 0) { + Get-PythonMSI + Install-Python + Update-UserEnvironmentPath + Remove-Item $PythonInstaller +} else { + Write-Host "Python 3.7.3 is already installed." -ForegroundColor Green +} +Install-Virtualenv