foxygit / RPA-Remisser Log in
commits tags

/install_win.ps1 · 3.86 KB

raw
<#
    Sätter upp projektet på Windows: skapar virtuell miljö (.venv),
    installerar Python-paketen i requirements.txt samt Playwrights
    Chromium-browser.

    Körs en gång (eller på nytt om requirements.txt ändras):
        powershell -ExecutionPolicy Bypass -File .\install_win.ps1

    Starta sedan med:
        .venv\Scripts\Activate.ps1
        python run_webui.py        # webui:t på http://127.0.0.1:8765
        python main.py             # fristående 1177-exempel
#>

$ErrorActionPreference = "Stop"
Set-Location -Path $PSScriptRoot

$VenvDir = ".venv"

# Hitta en python-tolk. py-launchern föredras på Windows. Microsoft Store-
# aliaset i ...\Microsoft\WindowsApps\ ignoreras (det är bara en stub som
# öppnar Store och inte en riktig Python).
function Test-PythonExe([string]$exe, [string[]]$prefix) {
    try {
        $out = & $exe @($prefix + "--version") 2>&1
        return ($LASTEXITCODE -eq 0 -and "$out" -match "Python 3")
    } catch { return $false }
}

function Get-PythonCmd {
    # 1. py-launchern (finns oftast i PATH även när python.exe inte gör det)
    $pyCmd = Get-Command py -ErrorAction SilentlyContinue
    if ($pyCmd -and (Test-PythonExe $pyCmd.Source @("-3"))) {
        return @{ Exe = $pyCmd.Source; Args = @("-3") }
    }

    # 2. python / python3 i PATH, men hoppa över WindowsApps-stubben
    foreach ($name in "python", "python3") {
        foreach ($cmd in @(Get-Command $name -All -ErrorAction SilentlyContinue)) {
            if ($cmd.Source -like "*\Microsoft\WindowsApps\*") { continue }
            if (Test-PythonExe $cmd.Source @()) {
                return @{ Exe = $cmd.Source; Args = @() }
            }
        }
    }

    # 3. Vanliga installationsplatser (t.ex. om PATH inte lästs om ännu)
    $roots = @(
        "$env:LOCALAPPDATA\Programs\Python",
        "$env:PROGRAMFILES\Python*",
        "${env:ProgramFiles(x86)}\Python*",
        "C:\Python3*"
    )
    foreach ($root in $roots) {
        $found = Get-ChildItem -Path $root -Filter python.exe -Recurse -Depth 1 -ErrorAction SilentlyContinue |
            Sort-Object FullName -Descending | Select-Object -First 1
        if ($found -and (Test-PythonExe $found.FullName @())) {
            return @{ Exe = $found.FullName; Args = @() }
        }
    }

    throw @"
Hittar ingen riktig Python-installation.

Installera Python 3 fran https://www.python.org/downloads/ och kryssa i
'Add python.exe to PATH' i installeraren. Oppna sedan ett NYTT
terminalfonster (sa att PATH last om) och kor det har skriptet igen.

Tips: kor du 'python' och det oppnar Microsoft Store ar det bara ett
alias - stang av det under Installningar > Appar > Appkortkommandon for
apputforande, eller installera fran python.org enligt ovan.
"@
}

$py = Get-PythonCmd

if (-not (Test-Path $VenvDir)) {
    Write-Host "Skapar virtuell miljo i .\$VenvDir ..."
    & $py.Exe @($py.Args + @("-m", "venv", $VenvDir))
    if ($LASTEXITCODE -ne 0) { throw "Kunde inte skapa virtuell miljo." }
} else {
    Write-Host "Virtuell miljo .\$VenvDir finns redan, ateranvander den."
}

$VenvPython = Join-Path $VenvDir "Scripts\python.exe"
if (-not (Test-Path $VenvPython)) { throw "Hittar inte $VenvPython efter venv-skapande." }

Write-Host "Uppdaterar pip..."
& $VenvPython -m pip install --upgrade pip
if ($LASTEXITCODE -ne 0) { throw "pip-uppgradering misslyckades." }

Write-Host "Installerar Python-paket fran requirements.txt..."
& $VenvPython -m pip install -r requirements.txt
if ($LASTEXITCODE -ne 0) { throw "Paketinstallation misslyckades." }

Write-Host "Installerar Playwright Chromium..."
& $VenvPython -m playwright install chromium
if ($LASTEXITCODE -ne 0) { throw "Playwright-installation misslyckades." }

Write-Host ""
Write-Host "Klart! Starta med:"
Write-Host "    .venv\Scripts\Activate.ps1"
Write-Host "    python run_webui.py     # webui pa http://127.0.0.1:8765"
Write-Host "    python main.py          # fristaende 1177-exempel"