-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyrpl_install.ps1
232 lines (194 loc) · 10.3 KB
/
pyrpl_install.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
function Install-Software {
param (
[string]$Name,
[string]$WingetId
)
if (!(Get-Command $Name -ErrorAction SilentlyContinue)) {
Write-Host "Installing $Name..." -ForegroundColor Yellow
winget install --id $WingetId --silent --accept-package-agreements
} else {
Write-Host "$Name is already installed." -ForegroundColor Yellow
}
}
# Function to create and display SSH key
function Setup-SSHKey {
if (-not (Test-Path "~/.ssh/id_rsa.pub")) {
Write-Host "Generating a new SSH key..." -ForegroundColor Yellow
Run-GitBashCommand "ssh-keygen -t rsa -f ~/.ssh/id_rsa -N ''"
} else {
Write-Host "SSH key already exists." -ForegroundColor Green
}
Write-Host "Public Key:" -ForegroundColor Cyan
Run-GitBashCommand "cat ~/.ssh/id_rsa.pub" # This line displays the public key
}
# Helper function to run a command in Git Bash
function Run-GitBashCommand {
param ([string]$Command)
$gitBashExe = "$($Env:ProgramFiles)\Git\bin\bash.exe"
if (-not (Test-Path $gitBashExe)) {
Write-Host "Git Bash executable not found!" -ForegroundColor Red
Read-Host -Prompt "Press enter to exit..."
exit 1
}
& $gitBashExe -c $Command
}
# Helper function to confirm user action
function Confirm-Action {
param ([string]$Message)
$response = Read-Host "$Message (y/n)"
return $response -match '^(y|Y)$'
}
# Function to create a folder
function Create-Folder {
param ([string]$DefaultPath)
$customPath = Read-Host "Default folder: $DefaultPath. Press Enter to accept or specify an alternative path"
$targetPath = if ($customPath -eq "") { $DefaultPath } else { $customPath }
if (-not (Test-Path $targetPath)) {
Write-Host "Creating folder: $targetPath" -ForegroundColor Yellow
New-Item -ItemType Directory -Path $targetPath -Force | Out-Null
} else {
Write-Host "Folder already exists: $targetPath" -ForegroundColor Green
}
return $targetPath
}
# Function to clone a Git repository
function Clone-Repo {
param ([string]$RepoUrl, [string]$TargetPath)
Write-Host "Cloning repository into: $TargetPath"
& git clone $RepoUrl $TargetPath
if ($LASTEXITCODE -ne 0) {
Write-Host "Git clone failed! Check SSH key and repository access. In case the local folder already exists under the specified path, please delete it manually and retry." -ForegroundColor Red
Read-Host -Prompt "Press enter to continue or terminate the script with 'Ctrl+C'..."
} else {
Write-Host "Repository cloned successfully!" -ForegroundColor Green
}
}
# Function to create and activate a conda environment
function Create-CondaEnv {
param ([string]$EnvName)
$condaPath = "$HOME\AppData\Local\miniconda3\shell\condabin\conda-hook.ps1"
if (-not (Test-Path $condaPath)) {
Write-Host "Conda not found at $condaPath" -ForegroundColor Red
exit 1
}
Write-Host "Activating Conda hook from: $condaPath" -ForegroundColor Yellow
& $condaPath
Write-Host "Creating Conda environment: $EnvName" -ForegroundColor Yellow
& conda create -n $EnvName python=3.13 -y
if ($LASTEXITCODE -ne 0) {
Write-Host "Failed to create the Conda environment." -ForegroundColor Red
Read-Host -Prompt "Press enter to exit..."
exit 1
} else {
Write-Host "Conda '$EnvName' environment created successfully!" -ForegroundColor Green
}
Write-Host "Activating Conda environment: $EnvName" -ForegroundColor Yellow
& conda activate $EnvName
if ($LASTEXITCODE -ne 0) {
Write-Host "Failed to activate the Conda environment $EnvName." -ForegroundColor Red
Read-Host -Prompt "Press enter to exit..."
exit 1
}
}
function Build-AndInstall-Pyrpl {
param ([string]$RepoPath)
Set-Location $RepoPath
Write-Host "Running python setup.py bdist_wheel" -ForegroundColor Yellow
& python setup.py bdist_wheel
if ($LASTEXITCODE -ne 0) {
Write-Host "Failed to build the Pyrpl package." -ForegroundColor Red
Read-Host -Prompt "Press enter to exit..."
exit 1
}
$whlFile = Get-ChildItem -Path "$RepoPath\dist" -Filter "pyrpl-*.whl" | Sort-Object LastWriteTime -Descending | Select-Object -First 1
if ($whlFile) {
$whlPath = $whlFile.FullName
Write-Host "Installing Pyrpl package from: $whlPath" -ForegroundColor Yellow
& pip install $whlPath
} else {
Write-Host "Pyrpl wheel file not found in the dist folder." -ForegroundColor Red
}
# This Read-Host is now outside the if block
if ($LASTEXITCODE -ne 0) {
Write-Host "Failed to install Pyrpl." -ForegroundColor Red
Read-Host -Prompt "Press enter to exit..."
exit 1
}
}
# Function to perform complete installation
function Complete-Installation {
# Install conda and git using winget
Write-Host "`r`n=======================================================`r`nStep 1: Installing conda and git..." -ForegroundColor Cyan
Install-Software -Name "conda" -WingetId "Anaconda.Miniconda3"
Install-Software -Name "git" -WingetId "Git.Git"
Write-Host "`r`n=======================================================`r`nStep 2: Generating or displaying SSH key..." -ForegroundColor Cyan
Setup-SSHKey
Write-Host "`r`n=======================================================`r`nStep 3: Adding SSH key to GitHub..." -ForegroundColor Cyan
Write-Host "To allow access to the repository, you need to add your public SSH key to your GitHub account." -ForegroundColor Yellow
Write-Host "1. Go to your GitHub settings: https://github.com/settings/keys"
Write-Host "2. Click 'New SSH key'."
Write-Host "3. Give your key a title (e.g., 'My Laptop')."
Write-Host "4. Paste the public key (displayed above) into the 'Key' field."
Write-Host "5. Click 'Add SSH key'."
# Ensure the user is a member of the organization with access to the repository
Write-Host "`nPlease also ensure that your GitHub account is a member of the 'qpit' organization and has access to the 'pyrpl' repository." -ForegroundColor Yellow
Read-Host -Prompt "Press Enter to continue after adding the SSH key and confirming organization membership..."
Write-Host "`r`n=======================================================`r`nStep 4: Setting up project..." -ForegroundColor Cyan
Write-Host "If you are being asked if you want to continue connecting, answer 'yes'." -ForegroundColor Yellow
$defaultPath = [System.IO.Path]::Combine($HOME, "software")
$softwareFolder = Create-Folder -DefaultPath $defaultPath
$repoUrl = "[email protected]:qpit/pyrpl.git"
$repoPath = Join-Path -Path $softwareFolder -ChildPath "pyrpl"
Clone-Repo -RepoUrl $repoUrl -TargetPath $repoPath
Write-Host "`r`n=======================================================`r`nStep 5: Setting up Conda environment..." -ForegroundColor Cyan
$envName = Read-Host "Enter the name for the Conda environment (default: rp): "
if ($envName -eq "") { $envName = "rp" }
Create-CondaEnv -EnvName $envName
Write-Host "`r`n=======================================================`r`nStep 6: Building and installing Pyrpl..." -ForegroundColor Cyan
Build-AndInstall-Pyrpl -RepoPath $repoPath
}
# Function to install pyrpl in current environment
function Install-InCurrentEnv {
Write-Host "`r`n=======================================================`r`nStep 1: Generating or displaying SSH key..." -ForegroundColor Cyan
Setup-SSHKey
Write-Host "`r`n=======================================================`r`nStep 3: Adding SSH key to GitHub..." -ForegroundColor Cyan
Write-Host "To allow access to the repository, you need to add your public SSH key to your GitHub account." -ForegroundColor Yellow
Write-Host "1. Go to your GitHub settings: https://github.com/settings/keys"
Write-Host "2. Click 'New SSH key'."
Write-Host "3. Give your key a title (e.g., 'My Laptop')."
Write-Host "4. Paste the public key (displayed above) into the 'Key' field."
Write-Host "5. Click 'Add SSH key'."
# Ensure the user is a member of the organization with access to the repository
Write-Host "`nPlease also ensure that your GitHub account is a member of the 'qpit' organization and has access to the 'pyrpl' repository." -ForegroundColor Yellow
Read-Host -Prompt "Press Enter to continue after adding the SSH key and confirming organization membership..."
Write-Host "`r`n=======================================================`r`nStep 3: Setting up project..." -ForegroundColor Cyan
Write-Host "If you are being asked if you want to continue connecting, answer 'yes'." -ForegroundColor Yellow
$defaultPath = [System.IO.Path]::Combine($HOME, "software")
$softwareFolder = Create-Folder -DefaultPath $defaultPath
$repoUrl = "[email protected]:qpit/pyrpl.git"
$repoPath = Join-Path -Path $softwareFolder -ChildPath "pyrpl"
Clone-Repo -RepoUrl $repoUrl -TargetPath $repoPath
Write-Host "`r`n=======================================================`r`nStep 4: Checking for pip..." -ForegroundColor Cyan
if (!(Get-Command pip -ErrorAction SilentlyContinue)) {
Write-Host "pip is not available in the current environment." -ForegroundColor Red
Read-Host -Prompt "Press enter to exit..."
exit 1
}
Write-Host "`r`n=======================================================`r`nStep 5: Building and installing Pyrpl..." -ForegroundColor Cyan
Build-AndInstall-Pyrpl -RepoPath $repoPath
}
# Main script workflow
# Ask the user for installation type
Write-Host "Choose installation type:"
Write-Host "1. Complete Installation (includes miniconda, git, and new environment) [Default]" -ForegroundColor Green
Write-Host "2. Install in current environment (not recommended)" -ForegroundColor Yellow
$installationType = Read-Host "Enter your choice (1 or 2)"
switch ($installationType) {
"2" { # Only if the user explicitly enters '2'
Install-InCurrentEnv
}
default { # This will include an empty input or any other input besides '2'
Complete-Installation
}
}
Write-Host "`r`n=======================================================`r`nSetup complete! Repository is ready, and environment is activated and configured. Remember that this environment needs to be active for pyrpl to work." -ForegroundColor Green