-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWinPython.ps1
More file actions
307 lines (279 loc) · 14.4 KB
/
WinPython.ps1
File metadata and controls
307 lines (279 loc) · 14.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#Requires -Version 5
[CmdletBinding()]
Param(
[ValidateSet('3.7','3.8','3.9','3.10','3.11','3.12','3.13')]
[String]$WinPythonVersion = '3.13',
[ValidateSet('unmarked','dot','cod','PyPy','dotPyPy','post1')]
[String]$WinPythonType = 'dot',
[Switch]$InstallPwsh7SDK,
[Switch]$InstallDotnetInteractive,
[Switch]$InstallPortableGit,
[Switch]$AddStartMenu,
[String]$WinPythonPath = (Join-Path $env:LOCALAPPDATA 'Programs\WinPython'),
[String]$NodePath = (Join-Path $WinPythonPath 'node'),
[String]$PortableGitPath = (Join-Path $WinPythonPath 'PortableGit'),
[Switch]$CleanupDownloadFiles,
[String]$WorkingFolder = $PSScriptRoot
)
$ErrorActionPreference = 'Stop'
Push-Location $WorkingFolder
chcp 65001
$OutputEncoding = [System.Text.Encoding]::GetEncoding('utf-8')
$osBits = ([System.IntPtr]::Size*8).ToString()
if (-not([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
if ($InstallDotnetInteractive) {
Write-Error 'Require admin privileges for installation of .Net Interactive.'
exit
}
}
if ($ProgressPreference -ne 'SilentlyContinue') {
Write-Verbose 'Change $ProgressPreference to SilentlyContinue.'
$exProgressPreference = $ProgressPreference
$ProgressPreference = 'SilentlyContinue'
}
Write-Host 'Downloading latest WinPython...'
if ($WinPythonType -eq 'unmarked') {
$pattern = $osBits + '-' + $WinPythonVersion + '.*\d\.exe'
}
else {
$pattern = $osBits + '-' + $WinPythonVersion + '.*' + $WinPythonType + '\.exe'
}
$releaseURI = 'https://github.com/winpython/winpython/releases'
$latestRelease = (Invoke-WebRequest -Uri "$releaseURI/latest" -UseBasicParsing -Headers @{'Accept'='application/json'}| ConvertFrom-Json).update_url
$versionString = $latestRelease -replace '.*tag/(.*)', '$1'
$links = (Invoke-WebRequest -Uri "$releaseURI/expanded_assets/$($versionString)" -UseBasicParsing).Links.href
Write-Verbose ('$pattern = ' + $pattern)
Try {
$fileUri = ($links | Select-String -Pattern $pattern | Get-Unique).Tostring().Trim()
}
Catch {
throw "Does not exist WinPython$osBits version $WinPythonVersion $WinPythonType in latest release."
}
Invoke-WebRequest -uri "https://github.com$($fileUri)" -OutFile (Join-Path $WorkingFolder 'Winpython.exe') -Verbose
$wpVer = $fileUri -replace ".*-((\d+\.)?(\d+\.)?(\d+\.)?(\*|\d+)).*\.exe",'$1'
Write-Host 'Downloading Node.js...'
$releaseURI = 'https://nodejs.org/download/release/latest-v22.x'
$links = (Invoke-WebRequest -uri $releaseURI -UseBasicParsing).Links.href
$pattern = "win-x$osBits.*\.zip"
$fileUri = 'https://nodejs.org' + ($links | Select-String -Pattern $pattern | Get-Unique).Tostring().Trim()
Invoke-WebRequest -Uri $fileUri -OutFile (Join-Path $WorkingFolder '\node.zip')
if ($InstallPortableGit) {
Write-Host 'Downloading latest PortableGit...'
$releaseURI = 'https://github.com/git-for-windows/git/releases'
$latestRelease = (Invoke-WebRequest -Uri "$releaseURI/latest" -UseBasicParsing -Headers @{'Accept'='application/json'}| ConvertFrom-Json).update_url
$versionString = $latestRelease -replace '.*tag/(.*)', '$1'
$links = (Invoke-WebRequest -Uri "$releaseURI/expanded_assets/$($versionString)" -UseBasicParsing).Links.href
$fileUri = ($links | Select-String -Pattern ".*PortableGit.*($osBits)-bit.*\.exe" | Get-Unique).Tostring().Trim()
Invoke-WebRequest -uri "https://github.com$($fileUri)" -OutFile (Join-Path $WorkingFolder 'PortableGit.exe') -Verbose
}
if ($InstallDotnetInteractive) {
Write-Host 'Downloading latest .NET Core SDK...'
$links = (Invoke-WebRequest -uri 'https://dotnet.microsoft.com/download' -UseBasicParsing).Links.href
$latestVer = (($links | Select-String -Pattern '.*sdk.*windows-x64-installer') -replace '.*sdk-(([0-9]+\.){1}[0-9]+(\.[0-9]+)?)-.*', '$1' | Measure-Object -Maximum).Maximum
$latestUri = 'https://dotnet.microsoft.com' + ($links | Select-String -Pattern ".*sdk-$latestVer-windows-x64-installer" | Get-Unique).Tostring().Trim()
$fileUri = ((Invoke-WebRequest -uri $latestUri -UseBasicParsing).Links.href | Select-String -Pattern '.*\.exe' | Get-Unique).Tostring().Trim()
Invoke-WebRequest -uri $fileUri -UseBasicParsing -OutFile (Join-Path $WorkingFolder 'dotnet.exe') -Verbose
}
elseif ($InstallPwsh7SDK) {
Write-Host 'Downloading latest .NET Runtime...'
$links = (Invoke-WebRequest -Uri 'https://dotnet.microsoft.com/en-us/download/dotnet/8.0/runtime' -UseBasicParsing).Links.href
$latestVer = (($links | Select-String -Pattern '.*runtime.*windows-x64-installer') -replace '.*runtime-(([0-9]+\.){1}[0-9]+(\.[0-9]+)?)-.*', '$1' | Measure-Object -Maximum).Maximum
$latestUri = 'https://dotnet.microsoft.com' + ($links | Select-String -Pattern ".*runtime-$latestVer-windows-x64-installer" | Get-Unique).Tostring().Trim()
$fileUri = ((Invoke-WebRequest -Uri $latestUri -UseBasicParsing).Links.href | Select-String -Pattern '.*\.exe' | Get-Unique).Tostring().Trim()
Invoke-WebRequest -Uri $fileUri -UseBasicParsing -OutFile (Join-Path $WorkingFolder 'dotnet.exe') -Verbose
}
$releaseURI = 'https://github.com/sakaztk/Jupyter-PowerShellSDK/releases'
$latestRelease = (Invoke-WebRequest -Uri "$releaseURI/latest" -UseBasicParsing -Headers @{'Accept'='application/json'}| ConvertFrom-Json).update_url
$versionString = $latestRelease -replace '.*tag/(.*)', '$1'
$links = (Invoke-WebRequest -Uri "$releaseURI/expanded_assets/$($versionString)" -UseBasicParsing).Links.href
Write-Host 'Downloading latest DeepAQ pwsh5 Kernel...'
$fileUri = 'https://github.com' + ( $links | Select-String -Pattern '.*PowerShell5.zip' | Get-Unique).Tostring().Trim()
Invoke-WebRequest -uri $fileUri -UseBasicParsing -OutFile (Join-Path $WorkingFolder 'PowerShell5.zip') -Verbose
Write-Host 'Downloading latest DeepAQ pwshSDK Kernel...'
$fileUri = 'https://github.com' + ( $links | Select-String -Pattern 'Jupyter-PowerShellSDK-7.*\.zip' | Get-Unique).Tostring().Trim()
Invoke-WebRequest -uri $fileUri -UseBasicParsing -OutFile (Join-Path $WorkingFolder 'PowerShellSDK.zip') -Verbose
if ($null -ne $exProgressPreference) {
Write-Verbose "Restore $ProgressPreference to $exProgressPreference"
$ProgressPreference = $exProgressPreference
}
Write-Host 'Installing WinPython...'
New-Item -Path $WinPythonPath -ItemType Directory -Force
Start-Process -FilePath (Join-Path $WorkingFolder 'Winpython.exe') -ArgumentList ('-y -o"' + $WinPythonPath + '"') -wait
$wpRoot = (Join-Path $WinPythonPath "WPy$osBits-$($wpVer -replace('\.',''))")
$kernelPath = Join-Path $wpRoot '\settings\kernels'
if ($CleanupDownloadFiles) {
Start-Sleep -Seconds 5
Remove-Item (Join-Path $WorkingFolder 'Winpython.exe') -Force
}
Write-Host 'Installing Node.js...'
New-Item -Path $NodePath -ItemType Directory -Force
Expand-Archive -Path (Join-Path $WorkingFolder '\node.zip') -DestinationPath $NodePath -Force
[Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem')
$zipFile = [IO.Compression.ZipFile]::OpenRead("$(Join-Path $WorkingFolder '\node.zip')")
$nodeFolder = $zipFile.Entries[0].FullName -replace('/','')
$zipFile.Dispose()
$nodeEnvPath = join-path $nodePath $nodeFolder
. (join-path $nodeEnvPath "nodevars.bat")
$env:Path += (';' + $nodeEnvPath)
$nodeEnvPath = '%WINPYDIRBASE%\..\node\' + $nodeFolder
@"
set NODEPATH=$nodeEnvPath
echo ";%PATH%;" | %FINDDIR%\find.exe /C /I ";%NODEPATH%\;" >nul
if %ERRORLEVEL% NEQ 0 (
set "PATH=%PATH%;%NODEPATH%\;"
)
"@ | Add-Content -Path "$wpRoot\scripts\env.bat"
if ($CleanupDownloadFiles) {
Remove-Item (Join-Path $WorkingFolder '\node.zip') -Force
}
if ($InstallPortableGit) {
Write-Host 'Installing PortableGit...'
New-Item -Path $PortableGitPath -ItemType Directory -Force
Start-Process -FilePath (Join-Path $WorkingFolder 'PortableGit.exe') -ArgumentList ('-y -o"' + $PortableGitPath + '"') -wait
$gitEnvPath = Join-Path $PortableGitPath 'cmd'
if ($CleanupDownloadFiles) {
Start-Sleep -Seconds 5
Remove-Item (Join-Path $WorkingFolder 'PortableGit.exe') -Force
}
$env:Path += ";$gitEnvPath"
if ($PortableGitPath -eq (Join-Path $WinPythonPath 'PortableGit')) {
$gitEnvPath = '%WINPYDIRBASE%\..\PortableGit\cmd'
}
@"
set GITPATH=$gitEnvPath
echo ";%PATH%;" | %FINDDIR%\find.exe /C /I ";%GITPATH%\;" >nul
if %ERRORLEVEL% NEQ 0 (
set "PATH=%PATH%;%GITPATH%\;"
)
"@ | Add-Content -Path "$wpRoot\scripts\env.bat"
}
Write-Host 'Installing Jupyter...'
Get-Content "$wpRoot\scripts\WinPython_PS_Prompt.ps1" | ForEach-Object {
if ($_ -match '^\s*\$host\.ui\.RawUI\.(BackgroundColor|ForegroundColor)\s*=') {
"# $_"
} else {
$_
}
} | Set-Content "$wpRoot\scripts\WinPython_PS_Prompt_temp.ps1"
& "$wpRoot\scripts\env_for_icons.bat"
. "$wpRoot\scripts\WinPython_PS_Prompt_temp.ps1"
Remove-Item "$wpRoot\scripts\WinPython_PS_Prompt_temp.ps1" -Force
python -m pip install --upgrade pip
python -m pip install --upgrade wheel
python -m pip install jupyter
python -m pip install notebook
python -m pip install jupyterlab
$packagePath = Join-Path $wpRoot (Get-ChildItem $wpRoot -Filter "python-$WinPythonVersion*" -Name) | Join-Path -ChildPath '\python\Lib\site-packages'
Write-Host 'Installing DeepAQ pwsh5 Kernel...'
$installPath = Join-Path $packagePath 'powershell5_kernel'
Expand-Archive -Path (Join-Path $WorkingFolder 'PowerShell5.zip') -DestinationPath $installPath -Force
New-Item -ItemType Directory -Path (Join-Path $kernelPath '\powershell5\') -Force
Invoke-WebRequest -UseBasicParsing -Verbose -Uri 'https://raw.githubusercontent.com/PowerShell/PowerShell/master/assets/Powershell_64.png' -OutFile (Join-Path $kernelPath '\powershell5\logo-64x64.png')
Add-Type -AssemblyName System.Drawing
$image = [System.Drawing.Image]::FromFile((Join-Path $kernelPath '\powershell5\logo-64x64.png'))
$bitmap32 = New-Object System.Drawing.Bitmap(32, 32)
[System.Drawing.Graphics]::FromImage($bitmap32).DrawImage($image, 0, 0, 32, 32)
$bitmap32.Save((Join-Path $kernelPath '\powershell5\logo-32x32.png'), [System.Drawing.Imaging.ImageFormat]::Png)
@"
set PS5KPATH=%WINPYDIR%\Lib\site-packages\powershell5_kernel
echo ";%PATH%;" | %FINDDIR%\find.exe /C /I ";%PS5KPATH%\;" >nul
if %ERRORLEVEL% NEQ 0 (
set "PATH=%PATH%;%PS5KPATH%\;"
)
"@ | Add-Content -Path "$wpRoot\scripts\env.bat"
@"
{
"argv": [
"Jupyter_PowerShell5.exe",
"{connection_file}"
],
"display_name": "PowerShell 5",
"language": "Powershell"
}
"@ | Set-Content -Path (Join-Path $kernelPath '\powershell5\kernel.json')
Move-Item -Path (Join-Path $installPath '*.png') -Destination (Join-Path $kernelPath '\powershell5\') -Force
if ($CleanupDownloadFiles) {
Remove-Item (Join-Path $WorkingFolder 'PowerShell5.zip') -Force
}
if ($InstallPwsh7SDK) {
$packagePath = Join-Path $wpRoot (Get-ChildItem $wpRoot -Filter "python-$WinPythonVersion*" -Name) | Join-Path -ChildPath '\python\Lib\site-packages'
Write-Host 'Installing DeepAQ pwshSDK Kernel...'
$installPath = Join-Path $packagePath 'powershellSDK_kernel'
Expand-Archive -Path (Join-Path $WorkingFolder 'PowerShellSDK.zip') -DestinationPath $installPath -Force
New-Item -ItemType Directory -Path (Join-Path $kernelPath '\powershellSDK\') -Force
Invoke-WebRequest -UseBasicParsing -Verbose -Uri 'https://raw.githubusercontent.com/PowerShell/PowerShell/master/assets/Powershell_black_64.png' -OutFile (Join-Path $kernelPath '\powershellSDK\logo-64x64.png')
Add-Type -AssemblyName System.Drawing
$image = [System.Drawing.Image]::FromFile((Join-Path $kernelPath '\powershellSDK\logo-64x64.png'))
$bitmap32 = New-Object System.Drawing.Bitmap(32, 32)
[System.Drawing.Graphics]::FromImage($bitmap32).DrawImage($image, 0, 0, 32, 32)
$bitmap32.Save((Join-Path $kernelPath '\powershellSDK\logo-32x32.png'), [System.Drawing.Imaging.ImageFormat]::Png)
@"
set PS7KPATH=%WINPYDIR%\Lib\site-packages\powershellSDK_kernel
echo ";%PATH%;" | %FINDDIR%\find.exe /C /I ";%PS7KPATH%\;" >nul
if %ERRORLEVEL% NEQ 0 (
set "PATH=%PATH%;%PS7KPATH%\;"
)
"@ | Add-Content -Path "$wpRoot\scripts\env.bat"
@"
{
"argv": [
"Jupyter_PowerShellSDK.exe",
"{connection_file}"
],
"display_name": "PowerShell 7 (SDK)",
"language": "Powershell"
}
"@ | Set-Content -Path (Join-Path $kernelPath '\powershellSDK\kernel.json')
Move-Item -Path (Join-Path $installPath '*.png') -Destination (Join-Path $kernelPath '\powershellSDK\') -Force
if ($CleanupDownloadFiles) {
Remove-Item (Join-Path $WorkingFolder 'PowerShellSDK.zip') -Force
}
}
if ($InstallDotnetInteractive) {
Write-Host 'Installing .NET SDK...'
Start-Process -FilePath (Join-Path $WorkingFolder 'dotnet.exe') -ArgumentList '/install /passive /norestart' -Wait
Write-Output 'Installing .NET Interactive...'
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
dotnet tool install -g --add-source "https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools/nuget/v3/index.json" Microsoft.dotnet-interactive
dotnet interactive jupyter install --path "$kernelPath"
if ($CleanupDownloadFiles) {
Start-Sleep -Seconds 5
Remove-Item (Join-Path $WorkingFolder 'dotnet.exe') -Force
}
}
elseif ($InstallPwsh7SDK) {
Write-Host 'Installing .NET Runtime...'
Start-Process -FilePath (Join-Path $WorkingFolder 'dotnet.exe') -ArgumentList '/install /passive /norestart' -Wait
if ($CleanupDownloadFiles) {
Start-Sleep -Seconds 5
Remove-Item (Join-Path $WorkingFolder 'dotnet.exe') -Force
}
}
if ($AddStartMenu) {
$shortcutPath = join-path $env:APPDATA '\Microsoft\Windows\Start Menu\Programs\WinPython'
New-Item -Path $shortcutPath -ItemType Directory -Force
$wshShell = New-Object -comObject WScript.Shell
Get-ChildItem -Path $wpRoot -Filter '*.exe' | ForEach-Object {
$Shortcut = $WshShell.CreateShortcut("$shortcutPath\$($_.Name -replace '.exe','').lnk")
$Shortcut.TargetPath = $_.FullName
$Shortcut.Save()
}
}
@(
"$wpRoot\scripts\env.bat"
) | ForEach-Object {
$fileContent = Get-Content $_ -Raw
if ($fileContent -notcontains "chcp 65001") {
$fileContent = $filecontent -replace "^@echo off", "$&`nchcp 65001"
$filecontent | Set-Content $_
}
}
@'
@echo off
pushd %~dp0
icacls ..\..\ /grant Everyone:F /T /C
popd
pause
'@ | Set-Content -Path "$wpRoot\scripts\sakaztk-everyonefull.bat"
Pop-Location
Write-Host 'Done, It may require reboot to some function(s).' -ForegroundColor Green