Created
February 5, 2016 10:15
-
-
Save qbikez/6ad07e495bb97fb20bde to your computer and use it in GitHub Desktop.
python version manager - a simple version manager with hardcoded paths that can server as a template for future use, Can be used from PS or CMD.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
(echo REM pyvm.cmd) > "%TEMP%\_env.cmd" | |
powershell %~dp0\pyvm.ps1 %* | |
call "%TEMP%\_env.cmd" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[cmdletbinding()] | |
param ($command, [Alias("p")][switch][bool]$persistent, [switch][bool]$verbose) | |
write-host "pyvm - python version manager" | |
write-host "command=$command args=$args" | |
function where-is($wally) { | |
cmd /c "where $wally" | |
#Get-Command $wally | |
} | |
function add-topath($path, [switch][bool] $persistent) { | |
$p = $env:Path.Split(';') | |
$p = $p | % { $_.trimend("\") } | |
$paths = @($path) | |
$paths | % { | |
$path = $_ | |
write-verbose "adding $path to PATH" | |
$p += $path.trimend("\") | |
} | |
$env:path = [string]::Join(";",$p) | |
[System.Environment]::SetEnvironmentVariable("PATH", $env:Path, [System.EnvironmentVariableTarget]::Process); | |
if ($persistent) { | |
write-warning "saving global PATH" | |
[System.Environment]::SetEnvironmentVariable("PATH", $env:Path, [System.EnvironmentVariableTarget]::Machine); | |
} | |
} | |
function remove-frompath($path, [switch][bool] $persistent) { | |
$paths = @($path) | |
$p = $env:Path.Split(';') | |
$p = $p | % { $_.trimend("\") } | |
$paths | % { | |
$path = $_ | |
$found = $p | ? { $_ -ieq $path } | |
if ($found -ne $null) { | |
write-verbose "found $($found.count) matches" | |
$p = $p | ? { !($_ -ieq $path) } | |
} | |
} | |
$env:path = [string]::Join(";",$p) | |
[System.Environment]::SetEnvironmentVariable("PATH", $env:Path, [System.EnvironmentVariableTarget]::Process); | |
if ($persistent) { | |
write-warning "saving global PATH" | |
[System.Environment]::SetEnvironmentVariable("PATH", $env:Path, [System.EnvironmentVariableTarget]::Machine); | |
} | |
} | |
function contains-path($path) { | |
$paths = @($path) | |
$p = $env:Path.Split(';') | |
$p = $p | % { $_.trimend("\") } | |
$r = $true | |
$paths | % { | |
$path = $_ | |
$found = $p | ? { $_ -ieq $path } | |
if (@($found).Count -le 0) { | |
write-verbose "$path not found in PATH" | |
$r = $false | |
} | |
else { | |
write-verbose "$path found in PATH" | |
} | |
} | |
return $r | |
} | |
$vp = $VerbosePreference | |
try { | |
if ($verbose) { $VerbosePreference = "Continue" } | |
$paths = @{ | |
python3 = "c:\tools\python3","C:\tools\python3\scripts" | |
python2 = "c:\tools\python2","C:\tools\python2\scripts" | |
} | |
switch($command) { | |
"list" { | |
write-host "Available python versions" | |
$paths.GetEnumerator() | % { | |
$active = contains-path $_.Value | |
$msg = "" | |
if ($active) { $msg += " * " } | |
else { $msg += " " } | |
$msg += $_.Key | |
$msg += " " + $_.Value | |
write-host $msg | |
} | |
} | |
"use" { | |
if ($args.Length -eq 0) { | |
throw "must specify a version"` | |
} | |
$ver = $args[0] | |
switch($ver) { | |
{ @("3", "python3") -contains $_ } { | |
write-host "using $($paths.python3)" | |
$paths.values | % { | |
write-verbose "removing $_ from path" | |
remove-frompath $_ | |
} | |
add-topath $paths.python3 -persistent:$persistent | |
} | |
{ @("2", "python2") -contains $_ } { | |
write-host "using $($paths.python2)" | |
$paths.values | % { | |
write-verbose "removing $_ from path" | |
remove-frompath $_ | |
} | |
add-topath $paths.python2 -persistent:$persistent | |
} | |
{ @("none", "no") -contains $_ } { | |
write-host "disabling python" | |
$paths.values | % { | |
write-verbose "removing $_ from path" | |
remove-frompath $_ -persistent:$persistent | |
} | |
} | |
default { | |
throw "unrecognized version" | |
} | |
} | |
} | |
default { | |
write-verbose "usage: " | |
write-verbose " pyvm list" | |
write-verbose " pyvm use" | |
} | |
} | |
write-verbose "PATH=" | |
write-verbose "`r`n$env:path" | |
} finally { | |
$VerbosePreference = $vp | |
} | |
"set PATH=$env:PATH" | out-file "$env:TEMP\_env.cmd" -Encoding ascii |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment