Advertisement
alaestor

penv.ps1

Sep 26th, 2024 (edited)
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. param (
  2.     [Parameter(Mandatory=$true)]
  3.     [ValidateSet('activate', 'init')]
  4.     [string]$Command,
  5.     [string]$VersionString
  6. )
  7.  
  8. function Get-ExistingVirtualEnvironment {
  9.     $directoryNames = '.venv', 'venv', '.env', 'env', '.penv', 'penv'
  10.     foreach ($name in $directoryNames) {
  11.         $activateScript = [IO.Path]::Combine($(Get-Location), $name, 'Scripts', 'Activate.ps1')
  12.         if (Test-Path -LiteralPath $activateScript) {
  13.             return $name
  14.         }
  15.     }
  16.     return $null
  17. }
  18.  
  19. function Initialize-VirtualEnvironment($folderpath) {
  20.     $activateScript = [IO.Path]::Combine($folderpath, 'Scripts', 'Activate.ps1')
  21.  
  22.     # Create virtual environment if it doesn't already exist
  23.     if (Test-Path $activateScript) {
  24.         throw "Environment already exists: $($folderpath)"
  25.     }
  26.  
  27.     python -m venv $folderpath
  28.     if ($LastExitCode -ne 0) {
  29.         throw "Failed to create virtual environment with: 'python -m venv $($folderpath)'"
  30.     }
  31.  
  32.     # Ensure virtual environment's activate script exist
  33.     if (-not (Test-Path $activateScript)) {
  34.         throw 'Virtual environment or activate script not found'
  35.     }
  36.  
  37.     & $activateScript
  38. }
  39.  
  40. # Ensure we have python
  41. if ((Get-Command 'python' -ErrorAction SilentlyContinue) -eq $null) {
  42.     throw 'Python was not found'
  43. }
  44.  
  45. switch ($Command) {
  46.     'activate' {
  47.         $virtualEnvName = Get-ExistingVirtualEnvironment
  48.         if ($virtualEnvName) {
  49.             & $([IO.Path]::Combine($(Get-Location), $virtualEnvName, 'Scripts', 'Activate.ps1'))
  50.         } else {
  51.             throw 'No virtual environment directory found in the current directory.'
  52.         }
  53.     }
  54.     'init' {
  55.         if ($VersionString) {
  56.             pyenv local $version
  57.         } else {
  58.             pyenv local 3.12.2
  59.         }
  60.         $existing = Get-ExistingVirtualEnvironment
  61.         if ($existing) {
  62.             # $confirmation = Read-Host "An existing virtual environment was found. Overwrite? (Y/n)"
  63.             $choice = $Host.UI.PromptForChoice(
  64.                 "An existing virtual environment was found: '$($existing)/'",
  65.                 "Overwrite?",
  66.                 @('&Yes'; '&No'),
  67.                 0
  68.             )
  69.             if ($choice -eq 0) {
  70.                 Remove-Item -LiteralPath $existing -Recurse
  71.             }
  72.             else { exit }
  73.         }
  74.         Initialize-VirtualEnvironment('.venv')
  75.     }
  76. }
  77.  
  78. # Ensure Python is running in a virtual environment
  79. python -c 'import sys;sys.exit(0 if sys.prefix != sys.base_prefix else -1)'
  80. if ($LastExitCode -ne 0) {
  81.     throw 'Python was not virtualized'
  82. }
  83.  
  84. echo ---
  85. echo 'Python Virtual Environment Started'
  86. echo "Running $(python --version)"
  87. echo "Located from $(Get-Command python | Select Source -ExpandProperty Source)"
  88. echo ---
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement