-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwork.ps1
226 lines (200 loc) · 7.16 KB
/
work.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#New-Venv [name] (no name => .venv)
#Enter-Venv [name] (no name => .venv) # Run a shell in the given venv.
#Enter-TempVenv
#Get-Venv # List venvs
#Remove-Venv # Delete a venv
#Reset-Venv # ?
#Use-Venv { scriptblock }
$env_location = Resolve-Path ~/.virtualenvs
function Resolve-VenvName {
[CmdletBinding()]
param([String]$env)
if (!$env) {
$env = ".venv"
} else {
$try = Join-Path $env_location $env
if (Test-Path $try -PathType Container) {
$env = $try
}
}
# Returns PathInfo. Could just reuse $env and return string
$envpath = Resolve-Path -ErrorAction Ignore $env
if ($null -eq $envpath) {
Write-Error "Invalid environment: $env"
return $null
}
$python = Join-Path $envpath "Scripts" "python.exe"
if (!(Test-Path $python -PathType Leaf)) {
Write-Error "$env does not contain a Python interpreter"
return $null
}
$cfg = Join-Path $envpath "pyvenv.cfg"
if (!(Test-Path $cfg -PathType Leaf)) {
Write-Error "$env does not contain pyvenv.cfg"
return $null
}
# TODO: Move the fllowing to a separate function
# Double the backslashes as ConvertFrom-StringData treats them
# as escape characters, but pyvenv.cfg uses raw backslashes in paths
$raw_config = ((Get-Content $cfg) -Replace '\\', '\\' | ConvertFrom-StringData)
$TextInfo = (Get-Culture).TextInfo
$config = @{}
foreach ($k in $raw_config.keys) {
# Format keys in Powershell InitCapsFormat
$newkey = ($TextInfo.ToTitleCase($k) -replace '[-_]','');
$config[$newkey] = $raw_config.$k
}
# Virtualenv sets VersionInfo but not Version
if ($null -eq $config.Version) {
$config.Version = ($config.VersionInfo -split '\.')[0..2] -join '.'
}
[PSCustomObject]$config
}
function is_pyarg ([String]$arg) {
$arg -match "^-[23](.\d+)?(-(32|64))?$"
}
# pip install (fn_that_generates_requirements)
# pip install $array
function New-Venv ([String]$Name, [Switch]$NoActivate) {
# If $Name isn't a path
$Name = Join-Path $env_location $Name
# TODO: Have a local copy of virtualenv?
# TODO: Support -Python pyver
# TODO: Support -Install r1,r2,...
# TODO: Support -Requirements reqfile
virtualenv $Name
if (!$NoActivate) {
Use-Venv $Name
}
}
function Use-Venv ($Name, $ScriptBlock) {
# If we're only passed a scriptblock, fix the arguments up
if (($null -eq $ScriptBlock) -and ($Name -is [ScriptBlock])) {
$ScriptBlock = $Name
$Name = $null
}
$env = Resolve-VenvName $Name
$scripts = Join-Path $env "Scripts"
if ($ScriptBlock) {
# Save the environment, activate the virtualenv, and run the script block
$oldpath = $env:PATH
$oldvenv = $env:VIRTUAL_ENV
try {
$env:PATH = $scripts + ';' + $env:PATH
$env:VIRTUAL_ENV = $Name
& $ScriptBlock
}
finally {
$env:PATH = $oldpath
$env:VIRTUAL_ENV = $oldenv
}
} else {
& (Get-Process -Id $pid).Path -NoExit {
param([string]$env)
Write-Host -ForegroundColor Cyan "Launching nested prompt in virtual environment. Type 'exit' to return."
& (Join-Path $env "Scripts" "activate.ps1")
} -args $env
}
}
function New-TempVenv {
$Name = Join-Path $env_location (New-Guid)
virtualenv $Name
& (Get-Process -Id $pid).Path -NoExit {
Write-Host -ForegroundColor Cyan "Launching nested prompt in virtual environment. Type 'exit' to return."
Write-Host -ForegroundColor Cyan "This is a temporary environment and will be deleted on exit."
$name = $args[0]
& (Join-Path $name "Scripts" "activate.ps1")
Register-EngineEvent PowerShell.Exiting { Remove-Item -Recurse $name } | Out-Null
} -args $name
}
function ve_create ([String]$Path, [String]$Python, [String[]]$Install, [String[]]$Requirements) {
if ($Python) {
$virtualenvargs = ("-p", $Python)
}
virtualenv $Path @virtualenvargs
$pipargs = $Install
if ($Requirements) {
$pipargs = $pipargs, ($Requirements | Foreach-Object { "-r", $_ })
}
if ($pipargs) {
$pip = Join-Path $Path "Scripts" "pip.exe"
if (Test-Path $pip -PathType Leaf) {
& $pip install @pipargs
} else {
Write-Error "Cannot install packages as venv does not include pip"
}
}
}
function ve_path ([String]$name) {
# Get a full path for a venv
if ($name -eq "") {
$venv = "./.venv"
} elseif ((Split-Path -Leaf $name) -eq $name) {
# If just a leaf is given, assume the venv directory
# Put it in the first valid location on $env_location
$root = ((Resolve-Path -ErrorAction SilentlyContinue $env_location) | Select-Object -First 1)
$venv = Join-Path $root $name
} else {
$venv = $name
}
$venv
}
function ve_select ([String]$pattern) {
# Get full venv path(s) from an env name/pattern
# ve_select (no args) - do we want it to work like *? What about .venv?
# No, caller can do "if no pattern, use *" if that's what they want.
if ($pattern -eq "") {
if ($env:VIRTUAL_ENV) {
$venv = $env:VIRTUAL_ENV
} else {
$venv = "./.venv"
}
} elseif ((Split-Path -Leaf $pattern) -eq $pattern) {
# If just a leaf is given, assume the venv directory
$venv = Join-Path $env_location $pattern
} else {
$venv = $pattern
}
# -ErrorAction SilentlyContinue ignores any paths that don't exist.
# Could return more than one value ($env_location could be a list)
(Resolve-Path -ErrorAction SilentlyContinue $venv).Path
}
function ve_data ([String]$venv) {
# Get a PS object representing the env
$cfg = Join-Path $venv "pyvenv.cfg"
if (!(Test-Path $cfg -PathType Leaf)) {
Write-Error "$env does not contain pyvenv.cfg"
return $null
}
# Double the backslashes as ConvertFrom-StringData treats them
# as escape characters, but pyvenv.cfg uses raw backslashes in paths
# The first string is a regex, so \\ matches \. The second is a literal,
# so the overall effect is to replace \ with \\...
$raw_config = ((Get-Content $cfg) -Replace '\\', '\\' | ConvertFrom-StringData)
$TextInfo = (Get-Culture).TextInfo
$config = @{}
foreach ($k in $raw_config.keys) {
# Format keys in Powershell InitCapsFormat rather than dash-separated
$newkey = ($TextInfo.ToTitleCase($k) -replace '[-_]','');
$config[$newkey] = $raw_config.$k
}
# Virtualenv sets VersionInfo but not Version
if ($null -eq $config.Version) {
$config.Version = ($config.VersionInfo -split '\.')[0..2] -join '.'
}
[PSCustomObject]$config
}
function Get-Venv {
[CmdletBinding()]
param([String]$Name)
ve_select $Name | Where-Object { Test-Path (Join-Path $_ "Scripts" "python.exe") -PathType Leaf }
}
function veCommand ([string]$cmd) {
switch ($cmd) {
"ls" { Get-Venv $args }
"path" { Resolve-VenvName $args }
"new" { New-Venv $args }
"temp" { New-TempVenv $args }
"run" { Use-Venv $args }
}
}