-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap-windows.ps1
More file actions
278 lines (228 loc) · 9.4 KB
/
bootstrap-windows.ps1
File metadata and controls
278 lines (228 loc) · 9.4 KB
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
<#
.SYNOPSIS
Bootstrap Python without administrator access on Windows.
.DESCRIPTION
Downloads and installs Python using the official embeddable package,
then configures pip and environment variables at the user level.
No administrator privileges are required.
.NOTES
Repository: Python-NoAdmin
Requires: PowerShell 5.1+, Internet access
#>
param(
[string]$InstallDir = "$env:USERPROFILE\.python-nonadmin",
[switch]$Force,
[switch]$SkipPathUpdate
)
$ErrorActionPreference = "Stop"
# ============================================================================
# Configuration
# ============================================================================
$ConfigFile = Join-Path $PSScriptRoot "config.json"
if (Test-Path $ConfigFile) {
$Config = Get-Content $ConfigFile -Raw | ConvertFrom-Json
$PythonVersion = $Config.python_version
$EmbeddableUrl = $Config.windows.embeddable_url
$GetPipUrl = $Config.windows.get_pip_url
} else {
# Fallback defaults
$PythonVersion = "3.12.8"
$EmbeddableUrl = "https://www.python.org/ftp/python/3.12.8/python-3.12.8-embed-amd64.zip"
$GetPipUrl = "https://bootstrap.pypa.io/get-pip.py"
}
$PythonMajorMinor = ($PythonVersion -split '\.')[0..1] -join ''
$PthFile = "python$PythonMajorMinor._pth"
# ============================================================================
# Functions
# ============================================================================
function Write-Status {
param([string]$Message, [string]$Type = "INFO")
$colors = @{
"INFO" = "Cyan"
"SUCCESS" = "Green"
"WARNING" = "Yellow"
"ERROR" = "Red"
}
Write-Host "[$Type] " -ForegroundColor $colors[$Type] -NoNewline
Write-Host $Message
}
function Test-InternetConnection {
try {
$null = Invoke-WebRequest -Uri "https://www.python.org" -Method Head -TimeoutSec 5 -UseBasicParsing
return $true
} catch {
return $false
}
}
function Get-FileFromUrl {
param(
[string]$Url,
[string]$OutFile
)
Write-Status "Downloading: $Url"
# Use BITS for better reliability, fall back to Invoke-WebRequest
try {
Start-BitsTransfer -Source $Url -Destination $OutFile -ErrorAction Stop
} catch {
Write-Status "BITS transfer failed, using Invoke-WebRequest..." "WARNING"
Invoke-WebRequest -Uri $Url -OutFile $OutFile -UseBasicParsing
}
}
function Add-ToUserPath {
param([string]$PathToAdd)
$currentPath = [Environment]::GetEnvironmentVariable("PATH", "User")
if ($currentPath -notlike "*$PathToAdd*") {
$newPath = "$PathToAdd;$currentPath"
[Environment]::SetEnvironmentVariable("PATH", $newPath, "User")
Write-Status "Added to user PATH: $PathToAdd" "SUCCESS"
return $true
} else {
Write-Status "Already in user PATH: $PathToAdd" "INFO"
return $false
}
}
# ============================================================================
# Pre-flight Checks
# ============================================================================
Write-Host ""
Write-Host "============================================" -ForegroundColor Magenta
Write-Host " Python-NoAdmin Bootstrap (Windows)" -ForegroundColor Magenta
Write-Host " Python $PythonVersion" -ForegroundColor Magenta
Write-Host "============================================" -ForegroundColor Magenta
Write-Host ""
# Check if already installed
if ((Test-Path $InstallDir) -and (-not $Force)) {
Write-Status "Python already installed at: $InstallDir" "WARNING"
Write-Status "Use -Force to reinstall" "INFO"
$pythonExe = Join-Path $InstallDir "python.exe"
if (Test-Path $pythonExe) {
Write-Status "Existing version: $((& $pythonExe --version) 2>&1)" "INFO"
}
exit 0
}
# Check internet
Write-Status "Checking internet connection..."
if (-not (Test-InternetConnection)) {
Write-Status "No internet connection. Cannot download Python." "ERROR"
exit 1
}
Write-Status "Internet connection OK" "SUCCESS"
# ============================================================================
# Download and Extract Python
# ============================================================================
$TempDir = Join-Path $env:TEMP "python-nonadmin-setup"
$ZipFile = Join-Path $TempDir "python-embed.zip"
# Clean up any previous temp files
if (Test-Path $TempDir) {
Remove-Item -Recurse -Force $TempDir
}
New-Item -ItemType Directory -Path $TempDir -Force | Out-Null
# Download embeddable package
Write-Status "Downloading Python $PythonVersion embeddable package..."
Get-FileFromUrl -Url $EmbeddableUrl -OutFile $ZipFile
# Create install directory
if (Test-Path $InstallDir) {
Write-Status "Removing existing installation..." "WARNING"
Remove-Item -Recurse -Force $InstallDir
}
New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null
# Extract
Write-Status "Extracting to: $InstallDir"
Expand-Archive -Path $ZipFile -DestinationPath $InstallDir -Force
# ============================================================================
# Configure Python for pip
# ============================================================================
# The embeddable package disables site-packages by default via the ._pth file
# We need to uncomment "import site" to enable pip
$PthFilePath = Join-Path $InstallDir $PthFile
if (Test-Path $PthFilePath) {
Write-Status "Configuring $PthFile for pip support..."
$pthContent = Get-Content $PthFilePath -Raw
# Uncomment "import site" if it's commented
if ($pthContent -match '#\s*import site') {
$pthContent = $pthContent -replace '#\s*import site', 'import site'
Set-Content -Path $PthFilePath -Value $pthContent -NoNewline
Write-Status "Enabled 'import site' in $PthFile" "SUCCESS"
} elseif ($pthContent -notmatch 'import site') {
# Add it if it doesn't exist at all
Add-Content -Path $PthFilePath -Value "`nimport site"
Write-Status "Added 'import site' to $PthFile" "SUCCESS"
} else {
Write-Status "'import site' already enabled" "INFO"
}
} else {
Write-Status "Warning: $PthFile not found" "WARNING"
}
# ============================================================================
# Install pip
# ============================================================================
$GetPipFile = Join-Path $TempDir "get-pip.py"
$PythonExe = Join-Path $InstallDir "python.exe"
Write-Status "Downloading get-pip.py..."
Get-FileFromUrl -Url $GetPipUrl -OutFile $GetPipFile
Write-Status "Installing pip..."
& $PythonExe $GetPipFile --no-warn-script-location 2>&1 | ForEach-Object { Write-Host " $_" }
if ($LASTEXITCODE -ne 0) {
Write-Status "Failed to install pip" "ERROR"
exit 1
}
Write-Status "pip installed successfully" "SUCCESS"
# ============================================================================
# Create Scripts directory and add to PATH
# ============================================================================
$ScriptsDir = Join-Path $InstallDir "Scripts"
if (-not (Test-Path $ScriptsDir)) {
New-Item -ItemType Directory -Path $ScriptsDir -Force | Out-Null
}
# ============================================================================
# Update User PATH
# ============================================================================
if (-not $SkipPathUpdate) {
Write-Status "Updating user PATH environment variable..."
$pathUpdated1 = Add-ToUserPath -PathToAdd $InstallDir
$pathUpdated2 = Add-ToUserPath -PathToAdd $ScriptsDir
if ($pathUpdated1 -or $pathUpdated2) {
Write-Status "PATH updated. Restart your terminal for changes to take effect." "WARNING"
}
# Update current session
$env:PATH = "$InstallDir;$ScriptsDir;$env:PATH"
}
# ============================================================================
# Clean up
# ============================================================================
Write-Status "Cleaning up temporary files..."
Remove-Item -Recurse -Force $TempDir -ErrorAction SilentlyContinue
# ============================================================================
# Verification
# ============================================================================
Write-Host ""
Write-Status "Verifying installation..."
$pythonVersion = & $PythonExe --version 2>&1
Write-Status "Python: $pythonVersion" "SUCCESS"
$pipExe = Join-Path $ScriptsDir "pip.exe"
if (Test-Path $pipExe) {
$pipVersion = & $pipExe --version 2>&1
Write-Status "pip: $pipVersion" "SUCCESS"
} else {
# Try pip via python -m pip
$pipVersion = & $PythonExe -m pip --version 2>&1
Write-Status "pip: $pipVersion" "SUCCESS"
}
# ============================================================================
# Summary
# ============================================================================
Write-Host ""
Write-Host "============================================" -ForegroundColor Green
Write-Host " Installation Complete!" -ForegroundColor Green
Write-Host "============================================" -ForegroundColor Green
Write-Host ""
Write-Host " Install location: $InstallDir" -ForegroundColor White
Write-Host " Python executable: $PythonExe" -ForegroundColor White
Write-Host ""
Write-Host " To activate in current session:" -ForegroundColor Cyan
Write-Host " . .\scripts\activate.ps1" -ForegroundColor Yellow
Write-Host ""
Write-Host " Or restart your terminal to use 'python' and 'pip' directly." -ForegroundColor Cyan
Write-Host ""
# Return success
exit 0