freqtrade_origin/setup.ps1

286 lines
9.1 KiB
PowerShell
Raw Normal View History

Clear-Host
$Timestamp = Get-Date -Format "yyyyMMdd_HHmmss"
$Global:LogFilePath = Join-Path $env:TEMP "script_log_$Timestamp.txt"
2024-05-23 10:07:40 +00:00
$RequirementFiles = @("requirements.txt", "requirements-dev.txt", "requirements-hyperopt.txt", "requirements-freqai.txt", "requirements-freqai-rl.txt", "requirements-plot.txt")
$VenvName = ".venv"
2024-05-23 10:07:40 +00:00
$VenvDir = Join-Path $PSScriptRoot $VenvName
function Write-Log {
param (
[string]$Message,
[string]$Level = 'INFO'
)
2024-05-23 10:07:40 +00:00
if (-not (Test-Path -Path $LogFilePath)) {
New-Item -ItemType File -Path $LogFilePath -Force | Out-Null
}
2024-05-30 08:08:41 +00:00
switch ($Level) {
'INFO' { Write-Host $Message -ForegroundColor Green }
'WARNING' { Write-Host $Message -ForegroundColor Yellow }
'ERROR' { Write-Host $Message -ForegroundColor Red }
'PROMPT' { Write-Host $Message -ForegroundColor Cyan }
}
"${Level}: $Message" | Out-File $LogFilePath -Append
}
function Get-UserSelection {
param (
2024-05-23 10:07:40 +00:00
[string]$Prompt,
[string[]]$Options,
[string]$DefaultChoice = 'A',
[bool]$AllowMultipleSelections = $true
)
2024-05-30 08:08:41 +00:00
2024-05-23 10:07:40 +00:00
Write-Log "$Prompt`n" -Level 'PROMPT'
for ($I = 0; $I -lt $Options.Length; $I++) {
Write-Log "$([char](65 + $I)). $($Options[$I])" -Level 'PROMPT'
}
2024-05-30 08:08:41 +00:00
2024-05-23 10:07:40 +00:00
if ($AllowMultipleSelections) {
Write-Log "`nSelect one or more options by typing the corresponding letters, separated by commas." -Level 'PROMPT'
}
else {
Write-Log "`nSelect an option by typing the corresponding letter." -Level 'PROMPT'
}
2024-05-30 08:08:41 +00:00
[string]$UserInput = Read-Host
if ([string]::IsNullOrEmpty($UserInput)) {
$UserInput = $DefaultChoice
}
2024-05-28 11:10:56 +00:00
$UserInput = $UserInput.ToUpper()
2024-05-28 11:10:56 +00:00
if ($AllowMultipleSelections) {
$Selections = $UserInput.Split(',') | ForEach-Object { $_.Trim() }
$SelectedIndices = @()
foreach ($Selection in $Selections) {
if ($Selection -match '^[A-Z]$') {
$Index = [int][char]$Selection - [int][char]'A'
if ($Index -ge 0 -and $Index -lt $Options.Length) {
$SelectedIndices += $Index
2024-05-23 10:07:40 +00:00
}
else {
2024-05-28 11:10:56 +00:00
Write-Log "Invalid input: $Selection. Please enter letters within the valid range of options." -Level 'ERROR'
2024-05-23 10:07:40 +00:00
return -1
}
2024-05-21 20:32:03 +00:00
}
else {
2024-05-28 11:10:56 +00:00
Write-Log "Invalid input: $Selection. Please enter a letter between A and Z." -Level 'ERROR'
2024-05-23 10:07:40 +00:00
return -1
}
}
return $SelectedIndices
2024-05-23 10:07:40 +00:00
}
else {
if ($UserInput -match '^[A-Z]$') {
$SelectedIndex = [int][char]$UserInput - [int][char]'A'
if ($SelectedIndex -ge 0 -and $SelectedIndex -lt $Options.Length) {
return $SelectedIndex
2024-05-23 10:07:40 +00:00
}
else {
Write-Log "Invalid input: $UserInput. Please enter a letter within the valid range of options." -Level 'ERROR'
2024-05-23 10:07:40 +00:00
return -1
2024-05-21 20:32:03 +00:00
}
}
else {
Write-Log "Invalid input: $UserInput. Please enter a letter between A and Z." -Level 'ERROR'
2024-05-23 10:07:40 +00:00
return -1
}
}
}
2024-06-01 18:10:24 +00:00
function Exit-Script {
param (
2024-05-23 10:07:40 +00:00
[int]$ExitCode,
[bool]$WaitForKeypress = $true
)
if ($ExitCode -ne 0) {
Write-Log "Script failed. Would you like to open the log file? (Y/N)" -Level 'PROMPT'
$openLog = Read-Host
if ($openLog -eq 'Y' -or $openLog -eq 'y') {
Start-Process notepad.exe -ArgumentList $LogFilePath
}
}
elseif ($WaitForKeypress) {
2024-05-21 20:32:03 +00:00
Write-Log "Press any key to exit..."
$host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") | Out-Null
}
2024-05-23 10:07:40 +00:00
return $ExitCode
}
2024-05-23 10:07:40 +00:00
function Test-PythonExecutable {
param(
2024-05-23 10:08:10 +00:00
[string]$PythonExecutable
2024-05-23 10:07:40 +00:00
)
2024-05-28 11:10:56 +00:00
$DeactivateVenv = Join-Path $VenvDir "Scripts\Deactivate.bat"
if (Test-Path $DeactivateVenv) {
Write-Host "Deactivating virtual environment..." 2>&1 | Out-File $LogFilePath -Append
2024-05-28 11:10:56 +00:00
& $DeactivateVenv
Write-Host "Virtual environment deactivated." 2>&1 | Out-File $LogFilePath -Append
2024-05-28 11:10:56 +00:00
}
else {
Write-Host "Deactivation script not found: $DeactivateVenv" 2>&1 | Out-File $LogFilePath -Append
2024-05-30 08:08:41 +00:00
}
2024-05-28 11:10:56 +00:00
$PythonCmd = Get-Command $PythonExecutable -ErrorAction SilentlyContinue
if ($PythonCmd) {
2024-05-28 11:10:56 +00:00
$VersionOutput = & $PythonCmd.Source --version 2>&1
2024-05-23 10:08:10 +00:00
if ($LASTEXITCODE -eq 0) {
$Version = $VersionOutput | Select-String -Pattern "Python (\d+\.\d+\.\d+)" | ForEach-Object { $_.Matches.Groups[1].Value }
Write-Log "Python version $Version found using executable '$PythonExecutable'."
2024-05-23 10:08:10 +00:00
return $true
}
else {
Write-Log "Python executable '$PythonExecutable' not working correctly." -Level 'ERROR'
return $false
}
2024-05-23 10:07:40 +00:00
}
else {
2024-05-23 10:08:10 +00:00
Write-Log "Python executable '$PythonExecutable' not found." -Level 'ERROR'
return $false
2024-05-23 10:07:40 +00:00
}
2024-05-21 20:32:03 +00:00
}
2024-05-23 10:07:40 +00:00
function Find-PythonExecutable {
2024-06-01 18:18:57 +00:00
$PythonExecutables = @(
"python",
"python3.12",
"python3.11",
"python3.10",
"python3.9",
"python3",
"C:\Users\$env:USERNAME\AppData\Local\Programs\Python\Python312\python.exe",
"C:\Users\$env:USERNAME\AppData\Local\Programs\Python\Python311\python.exe",
"C:\Users\$env:USERNAME\AppData\Local\Programs\Python\Python310\python.exe",
"C:\Users\$env:USERNAME\AppData\Local\Programs\Python\Python39\python.exe",
2024-06-01 18:19:33 +00:00
"C:\Python312\python.exe",
2024-06-01 18:18:57 +00:00
"C:\Python311\python.exe",
"C:\Python310\python.exe",
"C:\Python39\python.exe",
)
2024-05-23 10:07:40 +00:00
2024-05-30 08:08:41 +00:00
foreach ($Executable in $PythonExecutables) {
if (Test-PythonExecutable -PythonExecutable $Executable) {
return $Executable
2024-05-23 10:08:10 +00:00
}
2024-05-23 10:07:40 +00:00
}
return $null
}
2024-05-23 10:07:40 +00:00
function Main {
"Starting the operations..." | Out-File $LogFilePath -Append
"Current directory: $(Get-Location)" | Out-File $LogFilePath -Append
2024-05-23 10:07:40 +00:00
# Exit on lower versions than Python 3.9 or when Python executable not found
$PythonExecutable = Find-PythonExecutable
if ($null -eq $PythonExecutable) {
Write-Log "No suitable Python executable found. Please ensure that Python 3.9 or higher is installed and available in the system PATH." -Level 'ERROR'
2024-05-23 10:07:40 +00:00
Exit 1
}
2024-05-23 10:07:40 +00:00
# Define the path to the Python executable in the virtual environment
2024-05-28 11:10:56 +00:00
$ActivateVenv = "$VenvDir\Scripts\Activate.ps1"
2024-05-23 10:07:40 +00:00
# Check if the virtual environment exists, if not, create it
2024-05-28 11:10:56 +00:00
if (-Not (Test-Path $ActivateVenv)) {
2024-05-23 10:07:40 +00:00
Write-Log "Virtual environment not found. Creating virtual environment..." -Level 'ERROR'
2024-05-28 11:10:56 +00:00
& $PythonExecutable -m venv $VenvName 2>&1 | Out-File $LogFilePath -Append
if ($LASTEXITCODE -ne 0) {
2024-05-23 10:07:40 +00:00
Write-Log "Failed to create virtual environment." -Level 'ERROR'
Exit-Script -exitCode 1
}
2024-05-28 11:10:56 +00:00
else {
Write-Log "Virtual environment created."
}
2024-05-23 10:07:40 +00:00
}
2024-05-28 11:10:56 +00:00
# Activate the virtual environment and check if it was successful
Write-Log "Virtual environment found. Activating virtual environment..."
& $ActivateVenv 2>&1 | Out-File $LogFilePath -Append
# Check if virtual environment is activated
if ($env:VIRTUAL_ENV) {
Write-Log "Virtual environment is activated at: $($env:VIRTUAL_ENV)"
2024-05-28 11:10:56 +00:00
}
else {
Write-Log "Failed to activate virtual environment." -Level 'ERROR'
Exit-Script -exitCode 1
}
2024-05-30 08:08:41 +00:00
2024-05-28 11:10:56 +00:00
# Ensure pip
python -m ensurepip --default-pip 2>&1 | Out-File $LogFilePath -Append
# Pull latest updates only if the repository state is not dirty
Write-Log "Checking if the repository is clean..."
$Status = & "git" status --porcelain
2024-05-28 11:10:56 +00:00
if ($Status) {
2024-06-01 18:10:24 +00:00
Write-Log "Changes in local git repository. Skipping git pull."
}
else {
Write-Log "Pulling latest updates..."
& "git" pull 2>&1 | Out-File $LogFilePath -Append
if ($LASTEXITCODE -ne 0) {
Write-Log "Failed to pull updates from Git." -Level 'ERROR'
Exit-Script -exitCode 1
}
}
2024-05-23 10:07:40 +00:00
if (-not (Test-Path "$VenvDir\Lib\site-packages\talib")) {
# Install TA-Lib using the virtual environment's pip
Write-Log "Installing TA-Lib using virtual environment's pip..."
2024-05-28 11:10:56 +00:00
python -m pip install --find-links=build_helpers\ --prefer-binary TA-Lib 2>&1 | Out-File $LogFilePath -Append
if ($LASTEXITCODE -ne 0) {
Write-Log "Failed to install TA-Lib." -Level 'ERROR'
Exit-Script -exitCode 1
}
2024-05-23 10:07:40 +00:00
}
2024-05-23 10:07:40 +00:00
# Present options for requirement files
$SelectedIndices = Get-UserSelection -prompt "Select which requirement files to install:" -options $RequirementFiles -defaultChoice 'A'
2024-05-23 10:07:40 +00:00
# Cache the selected requirement files
$SelectedRequirementFiles = @()
2024-05-28 11:10:56 +00:00
$PipInstallArguments = @()
foreach ($Index in $SelectedIndices) {
2024-05-28 11:10:56 +00:00
$RelativePath = $RequirementFiles[$Index]
if (Test-Path $RelativePath) {
$SelectedRequirementFiles += $RelativePath
$PipInstallArguments += "-r", $RelativePath # Add each flag and path as separate elements
2024-05-23 10:07:40 +00:00
}
else {
2024-05-28 11:10:56 +00:00
Write-Log "Requirement file not found: $RelativePath" -Level 'ERROR'
2024-05-23 10:07:40 +00:00
Exit-Script -exitCode 1
}
}
2024-05-28 11:10:56 +00:00
if ($PipInstallArguments.Count -ne 0) {
& pip install @PipInstallArguments # Use array splatting to pass arguments correctly
2024-05-23 10:07:40 +00:00
}
2024-05-23 10:07:40 +00:00
# Install freqtrade from setup using the virtual environment's Python
Write-Log "Installing freqtrade from setup..."
2024-05-28 11:10:56 +00:00
pip install -e . 2>&1 | Out-File $LogFilePath -Append
2024-05-23 10:07:40 +00:00
if ($LASTEXITCODE -ne 0) {
Write-Log "Failed to install freqtrade." -Level 'ERROR'
Exit-Script -exitCode 1
}
Write-Log "Installing freqUI..."
python freqtrade install-ui 2>&1 | Out-File $LogFilePath -Append
if ($LASTEXITCODE -ne 0) {
Write-Log "Failed to install freqUI." -Level 'ERROR'
Exit-Script -exitCode 1
}
2024-05-30 08:08:41 +00:00
Write-Log "Installation/Update complete!"
2024-05-23 10:07:40 +00:00
Exit-Script -exitCode 0
}
2024-05-23 10:07:40 +00:00
# Call the Main function
2024-05-30 08:08:41 +00:00
Main