-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsetup.ps1
More file actions
104 lines (87 loc) · 2.75 KB
/
Copy pathsetup.ps1
File metadata and controls
104 lines (87 loc) · 2.75 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
param(
[string]$VenvDir = "$HOME\Python\serialui"
)
$ErrorActionPreference = "Stop"
function Get-PythonCommand {
if (Get-Command py -ErrorAction SilentlyContinue) {
return @("py", "-3")
}
if (Get-Command python -ErrorAction SilentlyContinue) {
return @("python")
}
throw "Python was not found. Install Python 3 and ensure 'py' or 'python' is on PATH."
}
function Invoke-Python {
param(
[string[]]$PyCmd,
[string[]]$CmdArgs
)
$exe = $PyCmd[0]
$preArgs = @()
if ($PyCmd.Length -gt 1) {
$preArgs = $PyCmd[1..($PyCmd.Length - 1)]
}
& $exe @preArgs @CmdArgs
if ($LASTEXITCODE -ne 0) {
throw "Command failed with exit code ${LASTEXITCODE}: $($PyCmd -join ' ') $($CmdArgs -join ' ')"
}
}
$pythonCmd = Get-PythonCommand
$venvParent = Split-Path -Parent $VenvDir
if (-not (Test-Path -Path $venvParent -PathType Container)) {
New-Item -ItemType Directory -Path $venvParent -Force | Out-Null
}
$venvExisted = Test-Path -Path $VenvDir -PathType Container
if (-not $venvExisted) {
Invoke-Python -PyCmd $pythonCmd -CmdArgs @("-m", "venv", $VenvDir)
}
$activateScript = Join-Path $VenvDir "Scripts\Activate.ps1"
if (-not (Test-Path -Path $activateScript -PathType Leaf)) {
throw "Activation script not found: $activateScript"
}
# Activate in this script scope for package install.
. $activateScript
Invoke-Python -PyCmd @("python") -CmdArgs @("-m", "pip", "install", "--upgrade", "pip")
$commonPackages = @(
"pyqt6",
"pyqtgraph",
"markdown",
"bleak",
"numpy",
"scipy",
"numba",
"fastplotlib",
"PyOpenGL",
"pybind11",
"setuptools",
"cobs",
"tamp"
)
$osPackages = @("wmi")
$installArgs = @("-m", "pip", "install")
if ($venvExisted) {
$installArgs += "--upgrade"
}
$installArgs += $commonPackages + $osPackages
Invoke-Python -PyCmd @("python") -CmdArgs $installArgs
Write-Host ""
Write-Host "Virtual environment ready: $VenvDir"
Write-Host "Python: $((Get-Command python).Source)"
# If dot-sourced, keep activation in current session.
if ($MyInvocation.InvocationName -eq ".") {
Write-Host "Virtual environment activated in current PowerShell session."
return
}
if ($env:VIRTUAL_ENV_DISABLE_PROMPT -and $env:VIRTUAL_ENV_DISABLE_PROMPT -ne "0") {
Write-Host "Note: VIRTUAL_ENV_DISABLE_PROMPT is set; prompt prefix may be hidden."
}
Write-Host "Opening a new PowerShell session with the virtual environment activated..."
$escapedActivate = $activateScript.Replace("'", "''")
$escapedCwd = (Get-Location).Path.Replace("'", "''")
$command = ". '$escapedActivate'; Set-Location '$escapedCwd'"
if (Get-Command pwsh -ErrorAction SilentlyContinue) {
& pwsh -NoExit -Command $command
}
else {
& powershell -NoExit -Command $command
}