forked from Azure/iotedge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall_python.ps1
98 lines (78 loc) · 2.18 KB
/
install_python.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
<#
Installs python in a Docker image for use in CI
#>
#Requires -RunAsAdministrator
param (
[Parameter(Mandatory = $true)]
[String] $Version
)
$ErrorActionPreference = "Stop"
Set-StrictMode -Version "Latest"
<#
Helper functions
#>
function Invoke-Native {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[String] $Command,
[Switch] $Passthru
)
process {
Write-Verbose "Executing native Windows command '$Command'..."
$out = cmd /c "($Command) 2>&1" 2>&1 | Out-String
Write-Verbose $out
Write-Verbose "Exit code: $LASTEXITCODE"
if ($LASTEXITCODE) {
throw $out
}
elseif ($Passthru) {
$out
}
}
}
<#
Parse version
#>
if (-not ($Version -match "^(\d+)(?:\.(\d+))?(?:\.(\d+)(\w*))?$")) {
throw "$Version is not a valid version string."
}
$Major = $matches[1]
$Minor = $matches[2]
$Patch = $matches[3]
<#
Download python
#>
$PythonUrl = "https://www.python.org/ftp/python/${Major}.${Minor}.${Patch}/python-$Version-embed-amd64.zip"
$PythonArchive = Join-Path $env:TEMP "py.zip"
Write-Host "Downloading python from $PythonUrl"
Invoke-WebRequest -Uri $PythonUrl -OutFile $PythonArchive
<#
Install python
#>
$PythonLibArchive = Join-Path $Env:PYTHONHOME "python$Major$Minor.zip"
$PythonLibDirectory = Join-Path $Env:PYTHONHOME "Lib"
$PythonPathOverride = Join-Path $Env:PYTHONHOME "python$Major$Minor._pth"
Write-Host "Installing python $Version"
try {
Expand-Archive $PythonArchive $Env:PYTHONHOME -Force
Expand-Archive $PythonLibArchive $PythonLibDirectory -Force
Remove-Item $PythonPathOverride -Force -ErrorAction "SilentlyContinue"
}
finally {
Remove-Item @($PythonArchive, $PythonLibArchive) -Force -ErrorAction "SilentlyContinue"
}
<#
Install pip
#>
$PipUrl = "https://bootstrap.pypa.io/get-pip.py"
$PipInstaller = Join-Path $Env:PYTHONHOME "get-dependency-manager.py"
Write-Host "Installing pip"
Invoke-WebRequest -Uri $PipUrl -OutFile $PipInstaller
try {
Invoke-Native "python $PipInstaller"
}
finally {
Remove-Item $PipInstaller -Force -ErrorAction "SilentlyContinue"
}
Write-Host "Done!"