-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython.ps1
More file actions
178 lines (150 loc) · 5.13 KB
/
python.ps1
File metadata and controls
178 lines (150 loc) · 5.13 KB
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
param(
[Parameter(ValueFromRemainingArguments = $true)]
[string[]]$Args
)
$ProxyDir = Split-Path -Parent $MyInvocation.MyCommand.Path
# --------------------------------------------------
# Recarrega variáveis de ambiente no terminal atual
# --------------------------------------------------
function Refresh-CurrentEnv {
$env:PATH = (
[Environment]::GetEnvironmentVariable("PATH", "User"),
[Environment]::GetEnvironmentVariable("PATH", "Machine")
) -join ";"
}
# --------------------------------------------------
# Python local (prioridade máxima)
# --------------------------------------------------
function Get-LocalPython {
$venv = Join-Path (Get-Location) ".venv\Scripts\python.exe"
if (Test-Path $venv) { return $venv }
$local = Join-Path (Get-Location) "python.exe"
if (Test-Path $local) { return $local }
return $null
}
# --------------------------------------------------
# Python global existente no PATH (exceto o proxy)
# --------------------------------------------------
function Get-GlobalPython {
try {
$cmd = Get-Command python.exe -ErrorAction Stop
if ($cmd.Source -ne $MyInvocation.MyCommand.Path) {
return $cmd.Source
}
} catch {}
return $null
}
# --------------------------------------------------
# Pythons instalados via uv (somente instalados)
# --------------------------------------------------
function Get-UvPythons {
$output = uv python list 2>$null
if (-not $output) { return @() }
$output |
Where-Object {
$_ -notmatch "<download available>" -and
$_ -match "uv\\python"
} |
ForEach-Object {
$parts = $_ -split "\s{2,}"
if ($parts.Count -ge 2 -and (Test-Path $parts[1])) {
[PSCustomObject]@{
Name = $parts[0]
Path = $parts[1]
}
}
} |
Sort-Object Name -Unique
}
# --------------------------------------------------
# Seleção interativa
# --------------------------------------------------
function Select-UvPython {
$versions = Get-UvPythons
if (-not $versions -or $versions.Count -eq 0) {
Write-Error "Nenhuma versão do Python instalada via uv."
exit 1
}
Write-Host "Selecione uma versão do Python (uv):"
for ($i = 0; $i -lt $versions.Count; $i++) {
Write-Host "[$i] $($versions[$i].Name)"
}
$choice = Read-Host "Número"
$selected = $versions[$choice]
if (-not $selected) {
Write-Error "Seleção inválida."
exit 1
}
return $selected.Path
}
# --------------------------------------------------
# Atualiza PATH do usuário + terminal atual
# --------------------------------------------------
function Set-GlobalPythonPath {
param([string]$PythonExe)
# Garante caminho absoluto
$PythonExe = [System.IO.Path]::GetFullPath($PythonExe)
$PythonDir = Split-Path -Parent $PythonExe
$userPath = [Environment]::GetEnvironmentVariable("PATH", "User") -split ";"
# remove pythons antigos do uv
$userPath = $userPath | Where-Object { $_ -notmatch "\\uv\\python\\" }
# garante proxy no topo
$userPath = $userPath | Where-Object { $_ -ne $ProxyDir }
$userPath = @($ProxyDir, $PythonDir) + $userPath
$newPath = ($userPath | Select-Object -Unique) -join ";"
[Environment]::SetEnvironmentVariable("PATH", $newPath, "User")
# atualiza o shell atual
Refresh-CurrentEnv
}
# --------------------------------------------------
# Ativa venv do projeto atual
# --------------------------------------------------
function Activate-ProjectVenv {
$venvScript = Join-Path (Get-Location) ".venv\Scripts\Activate.ps1"
if (Test-Path $venvScript) {
& $venvScript
Write-Host "Venv ativado: $(Get-Location)\.venv use deactivate para desativar"
return $true
}
return $false
}
# --------------------------------------------------
# Comando especial: python change version
# --------------------------------------------------
if ($Args.Count -ge 2 -and $Args[0] -eq "change" -and $Args[1] -eq "version") {
$pythonExe = Select-UvPython
Set-GlobalPythonPath $pythonExe
Write-Host "Python global atualizado."
exit 0
}
# --------------------------------------------------
# Comando especial: python activate venv
# --------------------------------------------------
if ($Args.Count -ge 2 -and $Args[0] -eq "activate" -and $Args[1] -eq "venv") {
if (Activate-ProjectVenv) {
exit 0
} else {
Write-Error "Nenhum venv encontrado no diretório atual (.venv\Scripts\Activate.ps1)."
exit 1
}
}
# --------------------------------------------------
# Fluxo principal
# --------------------------------------------------
# 1️⃣ Python local
$python = Get-LocalPython
if ($python) {
& $python @Args
exit $LASTEXITCODE
}
# 2️⃣ Python global existente
$python = Get-GlobalPython
if ($python) {
& $python @Args
exit $LASTEXITCODE
}
# 3️⃣ Nenhum encontrado → selecionar uv + setar global
$pythonExe = Select-UvPython
Set-GlobalPythonPath $pythonExe
& $pythonExe @Args
exit $LASTEXITCODE