forked from freqtrade/freqtrade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.ps1
285 lines (251 loc) · 9.12 KB
/
setup.ps1
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
Clear-Host
$Timestamp = Get-Date -Format "yyyyMMdd_HHmmss"
$Global:LogFilePath = Join-Path $env:TEMP "script_log_$Timestamp.txt"
$RequirementFiles = @("requirements.txt", "requirements-dev.txt", "requirements-hyperopt.txt", "requirements-freqai.txt", "requirements-freqai-rl.txt", "requirements-plot.txt")
$VenvName = ".venv"
$VenvDir = Join-Path $PSScriptRoot $VenvName
function Write-Log {
param (
[string]$Message,
[string]$Level = 'INFO'
)
if (-not (Test-Path -Path $LogFilePath)) {
New-Item -ItemType File -Path $LogFilePath -Force | Out-Null
}
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 (
[string]$Prompt,
[string[]]$Options,
[string]$DefaultChoice = 'A',
[bool]$AllowMultipleSelections = $true
)
Write-Log "$Prompt`n" -Level 'PROMPT'
for ($I = 0; $I -lt $Options.Length; $I++) {
Write-Log "$([char](65 + $I)). $($Options[$I])" -Level 'PROMPT'
}
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'
}
[string]$UserInput = Read-Host
if ([string]::IsNullOrEmpty($UserInput)) {
$UserInput = $DefaultChoice
}
$UserInput = $UserInput.ToUpper()
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
}
else {
Write-Log "Invalid input: $Selection. Please enter letters within the valid range of options." -Level 'ERROR'
return -1
}
}
else {
Write-Log "Invalid input: $Selection. Please enter a letter between A and Z." -Level 'ERROR'
return -1
}
}
return $SelectedIndices
}
else {
if ($UserInput -match '^[A-Z]$') {
$SelectedIndex = [int][char]$UserInput - [int][char]'A'
if ($SelectedIndex -ge 0 -and $SelectedIndex -lt $Options.Length) {
return $SelectedIndex
}
else {
Write-Log "Invalid input: $UserInput. Please enter a letter within the valid range of options." -Level 'ERROR'
return -1
}
}
else {
Write-Log "Invalid input: $UserInput. Please enter a letter between A and Z." -Level 'ERROR'
return -1
}
}
}
function Exit-Script {
param (
[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) {
Write-Log "Press any key to exit..."
$host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") | Out-Null
}
return $ExitCode
}
function Test-PythonExecutable {
param(
[string]$PythonExecutable
)
$DeactivateVenv = Join-Path $VenvDir "Scripts\Deactivate.bat"
if (Test-Path $DeactivateVenv) {
Write-Host "Deactivating virtual environment..." 2>&1 | Out-File $LogFilePath -Append
& $DeactivateVenv
Write-Host "Virtual environment deactivated." 2>&1 | Out-File $LogFilePath -Append
}
else {
Write-Host "Deactivation script not found: $DeactivateVenv" 2>&1 | Out-File $LogFilePath -Append
}
$PythonCmd = Get-Command $PythonExecutable -ErrorAction SilentlyContinue
if ($PythonCmd) {
$VersionOutput = & $PythonCmd.Source --version 2>&1
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'."
return $true
}
else {
Write-Log "Python executable '$PythonExecutable' not working correctly." -Level 'ERROR'
return $false
}
}
else {
Write-Log "Python executable '$PythonExecutable' not found." -Level 'ERROR'
return $false
}
}
function Find-PythonExecutable {
$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",
"C:\Python312\python.exe",
"C:\Python311\python.exe",
"C:\Python310\python.exe",
"C:\Python39\python.exe"
)
foreach ($Executable in $PythonExecutables) {
if (Test-PythonExecutable -PythonExecutable $Executable) {
return $Executable
}
}
return $null
}
function Main {
"Starting the operations..." | Out-File $LogFilePath -Append
"Current directory: $(Get-Location)" | Out-File $LogFilePath -Append
# 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'
Exit 1
}
# Define the path to the Python executable in the virtual environment
$ActivateVenv = "$VenvDir\Scripts\Activate.ps1"
# Check if the virtual environment exists, if not, create it
if (-Not (Test-Path $ActivateVenv)) {
Write-Log "Virtual environment not found. Creating virtual environment..." -Level 'ERROR'
& $PythonExecutable -m venv $VenvName 2>&1 | Out-File $LogFilePath -Append
if ($LASTEXITCODE -ne 0) {
Write-Log "Failed to create virtual environment." -Level 'ERROR'
Exit-Script -exitCode 1
}
else {
Write-Log "Virtual environment created."
}
}
# 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)"
}
else {
Write-Log "Failed to activate virtual environment." -Level 'ERROR'
Exit-Script -exitCode 1
}
# 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
if ($Status) {
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
}
}
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..."
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
}
}
# Present options for requirement files
$SelectedIndices = Get-UserSelection -prompt "Select which requirement files to install:" -options $RequirementFiles -defaultChoice 'A'
# Cache the selected requirement files
$SelectedRequirementFiles = @()
$PipInstallArguments = @()
foreach ($Index in $SelectedIndices) {
$RelativePath = $RequirementFiles[$Index]
if (Test-Path $RelativePath) {
$SelectedRequirementFiles += $RelativePath
$PipInstallArguments += "-r", $RelativePath # Add each flag and path as separate elements
}
else {
Write-Log "Requirement file not found: $RelativePath" -Level 'ERROR'
Exit-Script -exitCode 1
}
}
if ($PipInstallArguments.Count -ne 0) {
& pip install @PipInstallArguments # Use array splatting to pass arguments correctly
}
# Install freqtrade from setup using the virtual environment's Python
Write-Log "Installing freqtrade from setup..."
pip install -e . 2>&1 | Out-File $LogFilePath -Append
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
}
Write-Log "Installation/Update complete!"
Exit-Script -exitCode 0
}
# Call the Main function
Main