1
0

setup-python-runtime-windows.ps1 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # Prepare a job-private Python 3.10 toolchain without Windows installer or registry writes.
  2. $ErrorActionPreference = 'Stop'
  3. $root = Join-Path $env:RUNNER_TEMP ("python-runtime-" + [guid]::NewGuid().ToString('N'))
  4. New-Item -ItemType Directory -Path $root | Out-Null
  5. "root=$root" >> $env:GITHUB_OUTPUT
  6. $privateEnvironment = @{
  7. TMP = $root
  8. TEMP = $root
  9. UV_CACHE_DIR = (Join-Path $root 'uv-cache')
  10. UV_PYTHON_INSTALL_DIR = (Join-Path $root 'python')
  11. UV_PYTHON_INSTALL_BIN = '0'
  12. UV_PYTHON_INSTALL_REGISTRY = '0'
  13. UV_NO_CONFIG = '1'
  14. PIP_CACHE_DIR = (Join-Path $root 'pip-cache')
  15. npm_config_cache = (Join-Path $root 'npm-cache')
  16. npm_config_devdir = (Join-Path $root 'node-gyp')
  17. PNPM_CONFIG_PACKAGE_IMPORT_METHOD = 'copy'
  18. PKG_CACHE_PATH = (Join-Path $root 'pkg-cache')
  19. PNPM_CONFIG_STORE_DIR = (Join-Path $root 'pnpm-store')
  20. NODE_COMPILE_CACHE = (Join-Path $root 'node-compile-cache')
  21. }
  22. foreach ($entry in $privateEnvironment.GetEnumerator()) {
  23. [Environment]::SetEnvironmentVariable($entry.Key, $entry.Value, 'Process')
  24. "$($entry.Key)=$($entry.Value)" >> $env:GITHUB_ENV
  25. }
  26. if ([Runtime.InteropServices.RuntimeInformation]::OSArchitecture -ne 'X64') {
  27. throw 'Python runtime CI requires a native x64 Windows host.'
  28. }
  29. $devMode = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\AppModelUnlock' -Name AllowDevelopmentWithoutDevLicense
  30. if ($devMode.AllowDevelopmentWithoutDevLicense -ne 1) {
  31. throw 'The self-hosted Windows image must enable Developer Mode before CI.'
  32. }
  33. $bootstrap = Join-Path $root 'bootstrap'
  34. python -m venv $bootstrap
  35. if ($LASTEXITCODE -ne 0) { throw 'The self-hosted Windows image requires Python with venv and ensurepip.' }
  36. $bootstrapScripts = Join-Path $bootstrap 'Scripts'
  37. & (Join-Path $bootstrapScripts 'python.exe') -m pip --isolated --disable-pip-version-check --no-cache-dir install uv==0.11.23
  38. if ($LASTEXITCODE -ne 0) { throw 'Job-private uv installation failed.' }
  39. $uv = Join-Path $bootstrapScripts 'uv.exe'
  40. & $uv python install --install-dir $env:UV_PYTHON_INSTALL_DIR --no-bin --no-registry 3.10
  41. if ($LASTEXITCODE -ne 0) { throw 'Job-private Python 3.10 download failed.' }
  42. $tooling = Join-Path $root 'tooling'
  43. & $uv venv --python 3.10 --managed-python --no-python-downloads --seed $tooling
  44. if ($LASTEXITCODE -ne 0) { throw 'Job-private Python 3.10 environment creation failed.' }
  45. $toolingScripts = Join-Path $tooling 'Scripts'
  46. $python = Join-Path $toolingScripts 'python.exe'
  47. & $python -c 'import platform, sys; assert sys.version_info[:2] == (3, 10); assert platform.machine() == "AMD64"; print(sys.version); print(sys.executable)'
  48. if ($LASTEXITCODE -ne 0) { throw 'Job-private Python version or architecture is incorrect.' }
  49. $bootstrapScripts >> $env:GITHUB_PATH
  50. $toolingScripts >> $env:GITHUB_PATH