-
Notifications
You must be signed in to change notification settings - Fork 569
/
Copy pathinstall-python.ps1
68 lines (55 loc) · 1.91 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
Param(
[string] $Version = '3.10.2',
[string] $Arch = 'x64'
)
$ErrorActionPreference = 'Stop'
Add-Type -AssemblyName System.IO.Compression.FileSystem
$HOME_DIR = if ($env:HOME) { $env:HOME } else { $env:USERPROFILE }
$destDir = Join-Path "$env:AGENT_TOOLSDIRECTORY" "Python" "$Version" "$Arch"
$completeFile = "$destDir.complete"
if (Test-Path $completeFile) {
Write-Host "Python $Version ($Arch) already installed."
exit 0;
} else {
Write-Host "No matching Python found."
}
Write-Host "Downloading manifest..."
$pythonManifestUri = "https://raw.githubusercontent.com/actions/python-versions/main/versions-manifest.json"
$pythonManifest = Invoke-WebRequest -Uri $pythonManifestUri | ConvertFrom-Json
if ($IsMacOS) {
$platform = "darwin"
} elseif ($IsLinux) {
$platform = "linux"
} else {
$platform = "win32"
}
$manifestFileVersion = $pythonManifest | Where-Object { $_.version -eq $Version } | Select-Object -First 1
$manifestFileItem = $manifestFileVersion.files | Where-Object { $_.platform -eq $platform -and $_.arch -eq $Arch } | Select-Object -First 1
$downloadUrl = $manifestFileItem.download_url
# download
$tempDir = Join-Path "$HOME_DIR" "python-temp"
$archive = Join-Path "$tempDir" (Split-Path $downloadUrl -Leaf)
New-Item -ItemType Directory -Force -Path "$tempDir" | Out-Null
Write-Host "Downloading Python package '$downloadUrl' to '$archive'..."
(New-Object System.Net.WebClient).DownloadFile("$downloadUrl", "$archive")
# extract
Write-Host "Extracting Python to '$tempDir'..."
if ($IsMacOS -or $IsLinux) {
tar -vxzf "$archive" -C "$tempDir"
} else {
[System.IO.Compression.ZipFile]::ExtractToDirectory("$archive", "$tempDir")
}
Write-Host "Extraction complete."
# install
Write-Host "Installing Python..."
try {
Push-Location "$tempDir"
if ($IsMacOS -or $IsLinux) {
./setup.sh
} else {
.\setup.ps1
}
} finally {
Pop-Location
}
exit $LASTEXITCODE