install-python.ps1 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # Licensed to the Apache Software Foundation (ASF) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The ASF licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. # The code lines below are derived from -
  17. # https://github.com/docker-library/python/blob/105d6f34e7d70aad6f8c3e249b8208efa591916a/3.11/windows/windowsservercore-ltsc2022/Dockerfile
  18. $url = ('https://www.python.org/ftp/python/{0}/python-{1}-amd64.exe' -f ($Env:PYTHON_VERSION -replace '[a-z]+[0-9]*$', ''), $Env:PYTHON_VERSION)
  19. Write-Host ('Downloading {0} ...' -f $url)
  20. [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
  21. Invoke-WebRequest -Uri $url -OutFile 'python.exe'
  22. Write-Host 'Installing ...'
  23. $exitCode = (Start-Process python.exe -Wait -NoNewWindow -PassThru `
  24. -ArgumentList @(
  25. '/quiet',
  26. 'InstallAllUsers=1',
  27. 'TargetDir=C:\Python',
  28. 'PrependPath=1',
  29. 'Shortcuts=0',
  30. 'Include_doc=0',
  31. 'Include_pip=0',
  32. 'Include_test=0'
  33. )
  34. ).ExitCode
  35. if ($exitCode -ne 0) {
  36. Write-Host ('Running python installer failed with exit code: {0}' -f $exitCode)
  37. Get-ChildItem $Env:TEMP | Sort-Object -Descending -Property LastWriteTime | Select-Object -First 1 | Get-Content
  38. exit $exitCode
  39. }
  40. # the installer updated PATH, so we should refresh our local value
  41. $Env:PATH = [Environment]::GetEnvironmentVariable('PATH', [EnvironmentVariableTarget]::Machine)
  42. Write-Host 'Verifying install ...'
  43. Write-Host "python --version $(python --version)"
  44. Write-Host 'Removing ...'
  45. Remove-Item python.exe -Force
  46. Remove-Item $Env:TEMP\Python*.log -Force
  47. Write-Host 'Complete.'