- Updated the project structure to store configuration files (.env, models.yaml, extensions.yaml) in the project root instead of the user's home directory. - Enhanced the setup process to automatically copy example files to the project root on first run. - Implemented a migration function to transfer legacy configuration files from the user's home directory to the new project structure. - Revised documentation to reflect changes in file locations and setup instructions. - Improved code readability and maintainability by refactoring path management functions.
98 lines
3.2 KiB
PowerShell
98 lines
3.2 KiB
PowerShell
#Requires -Version 5.1
|
||
# Windows launcher: venv + pip + python -m gpu_rent. Call via gpu-rent.bat if ExecutionPolicy blocks this file.
|
||
Set-StrictMode -Version Latest
|
||
$ErrorActionPreference = "Stop"
|
||
|
||
$Root = Split-Path -Parent $MyInvocation.MyCommand.Path
|
||
Set-Location -LiteralPath $Root
|
||
try {
|
||
$utf8 = New-Object System.Text.UTF8Encoding $false
|
||
[Console]::OutputEncoding = $utf8
|
||
$script:OutputEncoding = $utf8
|
||
} catch {
|
||
}
|
||
|
||
function Test-Python311 {
|
||
param(
|
||
[Parameter(Mandatory = $true)][string]$Exe,
|
||
[string[]]$Prefix = @()
|
||
)
|
||
$cmd = Get-Command $Exe -ErrorAction SilentlyContinue
|
||
if (-not $cmd) {
|
||
return $false
|
||
}
|
||
try {
|
||
& $Exe @Prefix -c "import sys; raise SystemExit(0 if sys.version_info >= (3, 11) else 1)" 2>$null | Out-Null
|
||
return ($LASTEXITCODE -eq 0)
|
||
} catch {
|
||
return $false
|
||
}
|
||
}
|
||
|
||
$PyExe = $null
|
||
$PyPrefix = @()
|
||
$found = $false
|
||
foreach ($row in @(
|
||
@{ Exe = "py"; Prefix = @("-3.12") },
|
||
@{ Exe = "py"; Prefix = @("-3.11") },
|
||
@{ Exe = "py"; Prefix = @("-3") },
|
||
@{ Exe = "python3"; Prefix = @() },
|
||
@{ Exe = "python"; Prefix = @() }
|
||
)) {
|
||
if (Test-Python311 -Exe $row.Exe -Prefix $row.Prefix) {
|
||
$PyExe = $row.Exe
|
||
$PyPrefix = $row.Prefix
|
||
$found = $true
|
||
break
|
||
}
|
||
}
|
||
|
||
if (-not $found) {
|
||
Write-Host "gpu-rent: нужен Python 3.11+. Поставь с python.org и включи Add python.exe to PATH."
|
||
exit 1
|
||
}
|
||
|
||
$VenvPy = Join-Path $Root ".venv\Scripts\python.exe"
|
||
if (-not (Test-Path -LiteralPath $VenvPy)) {
|
||
Write-Host "gpu-rent: создаю .venv"
|
||
& $PyExe @PyPrefix -m venv (Join-Path $Root ".venv")
|
||
}
|
||
|
||
$Stamp = Join-Path $Root ".venv\.gpu-rent-installed"
|
||
$Pyproject = Join-Path $Root "pyproject.toml"
|
||
$needInstall = -not (Test-Path -LiteralPath $Stamp)
|
||
if (-not $needInstall -and (Test-Path -LiteralPath $Pyproject)) {
|
||
$needInstall = (Get-Item -LiteralPath $Pyproject).LastWriteTime -gt (Get-Item -LiteralPath $Stamp).LastWriteTime
|
||
}
|
||
if ($needInstall) {
|
||
Write-Host "gpu-rent: ставлю пакет в .venv"
|
||
& $VenvPy -m pip install -U pip
|
||
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
||
& $VenvPy -m pip install -e $Root
|
||
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
||
Get-Date -Format o | Set-Content -LiteralPath $Stamp -Encoding ascii
|
||
}
|
||
|
||
$EnvFile = Join-Path $Root ".env"
|
||
$Example = Join-Path $Root "env.example"
|
||
if (-not (Test-Path -LiteralPath $EnvFile)) {
|
||
if (Test-Path -LiteralPath $Example) {
|
||
Copy-Item -LiteralPath $Example -Destination $EnvFile
|
||
Write-Host "gpu-rent: created .env - fill OS_* (docs/setup.md)"
|
||
}
|
||
}
|
||
|
||
function Copy-IfMissing {
|
||
param([string]$Src, [string]$Dst, [string]$Label)
|
||
if ((Test-Path -LiteralPath $Src) -and -not (Test-Path -LiteralPath $Dst)) {
|
||
Copy-Item -LiteralPath $Src -Destination $Dst
|
||
Write-Host "gpu-rent: created $Label"
|
||
}
|
||
}
|
||
Copy-IfMissing (Join-Path $Root "models.example.yaml") (Join-Path $Root "models.yaml") "models.yaml"
|
||
Copy-IfMissing (Join-Path $Root "extensions.example.yaml") (Join-Path $Root "extensions.yaml") "extensions.yaml"
|
||
|
||
$ErrorActionPreference = "Continue"
|
||
& $VenvPy -m gpu_rent @args
|
||
exit $LASTEXITCODE
|