SXXXXXXX_PyUCC/tools/run_with_venv.ps1
2025-11-25 12:18:55 +01:00

63 lines
2.0 KiB
PowerShell

<#
.\tools\run_with_venv.ps1 -Install # create/use .venv and install requirements
.\tools\run_with_venv.ps1 -Run -Module pyucc -- -h # run module inside .venv and pass args
The script prefers an existing `.venv` directory at the repository root. If missing
it will attempt to create it using the system `python` on PATH.
#>
param(
[switch]$Install,
[switch]$Run,
[string]$Module = "pyucc",
[Parameter(ValueFromRemainingArguments=$true)]
[string[]]$Args
)
function Write-Info($s){ Write-Host $s }
$scriptRoot = Split-Path -Parent $MyInvocation.MyCommand.Path
$venvPath = Join-Path $scriptRoot ".venv"
if (-not (Test-Path $venvPath)) {
Write-Info ".venv not found at $venvPath. Creating virtual environment..."
& python -m venv $venvPath
}
$pythonExe = Join-Path $venvPath "Scripts\python.exe"
if (-not (Test-Path $pythonExe)) {
Write-Info "No python.exe found in .venv; attempting to create virtualenv using current python..."
& python -m venv $venvPath
}
if (-not (Test-Path $pythonExe)) {
Write-Error "Failed to find or create a Python executable in .venv. Ensure Python is installed and on PATH."
exit 1
}
if ($Install) {
Write-Info "Installing/Updating requirements using $pythonExe"
& $pythonExe -m pip install --upgrade pip
$req = Join-Path $scriptRoot "requirements.txt"
if (Test-Path $req) {
& $pythonExe -m pip install -r $req
} else {
Write-Info "requirements.txt not found at repo root. Skipping requirements install."
}
}
if ($Run) {
Write-Info "Running module: $Module"
if ($Args) {
& $pythonExe -m $Module @Args
} else {
& $pythonExe -m $Module
}
exit $LASTEXITCODE
}
Write-Info "No action requested. Helpful commands:"
Write-Info " Install requirements: .\tools\run_with_venv.ps1 -Install"
Write-Info " Run app: .\tools\run_with_venv.ps1 -Run -Module pyucc -- -h"
Write-Info " Example: .\tools\run_with_venv.ps1 -Install; .\tools\run_with_venv.ps1 -Run -Module pyucc -- --gui"