okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge branch 'f-021-iE' into f-021-int3616148bb

main
2.6 KiB57 linesraw
1# Strata Terminal launcher for PowerShell. It finds a Python in a fixed order and hands over to strata.py.
2# It installs nothing and downloads nothing.
3#   1. $env:STRATA_PYTHON                  an explicit override, always wins
4#   2. <root>\python\python.exe            the private runtime shipped in the terminal archive
5#   3. <root>\.venv\Scripts\python.exe     a source checkout set up by SETUP.bat
6#   4. py -3 / python on PATH, 3.10+       a developer convenience, never a requirement
7# If scripts are blocked on this PC, use bin\strata.cmd, or:  powershell -ExecutionPolicy Bypass -File bin\strata.ps1 ...
8$ErrorActionPreference = 'Stop'
9$here = Split-Path -Parent $MyInvocation.MyCommand.Path
10$root = if (Test-Path -LiteralPath (Join-Path $here 'strata.py')) { $here } else { Split-Path -Parent $here }
11$app = Join-Path $root 'strata.py'
12if (-not (Test-Path -LiteralPath $app)) {
13    [Console]::Error.WriteLine("strata: cannot find strata.py next to $here or its parent; the installation looks incomplete")
14    exit 5
15}
16
17function Test-Python([string[]]$cmd) {
18    try {
19        $rest = @($cmd | Select-Object -Skip 1) + @('-c', 'import sys; sys.exit(0 if sys.version_info >= (3, 10) else 1)')
20        & $cmd[0] @rest *> $null
21        return ($LASTEXITCODE -eq 0)
22    } catch { return $false }
23}
24
25$py = $null
26$bundled = Join-Path $root 'python\python.exe'
27$venv = Join-Path $root '.venv\Scripts\python.exe'
28if ($env:STRATA_PYTHON) {
29    if (-not (Test-Path -LiteralPath $env:STRATA_PYTHON)) {
30        [Console]::Error.WriteLine("strata: STRATA_PYTHON=$($env:STRATA_PYTHON) does not exist")
31        exit 5
32    }
33    $py = @($env:STRATA_PYTHON)
34} elseif (Test-Path -LiteralPath $bundled) {
35    # the private runtime: nothing from the user's site-packages or PYTHONHOME/PYTHONPATH may leak in
36    $env:PYTHONNOUSERSITE = '1'
37    Remove-Item Env:PYTHONHOME -ErrorAction SilentlyContinue
38    Remove-Item Env:PYTHONPATH -ErrorAction SilentlyContinue
39    $py = @($bundled)
40} elseif (Test-Path -LiteralPath $venv) {
41    $py = @($venv)
42} elseif (Test-Python @('py', '-3')) {
43    $py = @('py', '-3')
44} elseif (Test-Python @('python')) {
45    $py = @('python')
46}
47if (-not $py) {
48    [Console]::Error.WriteLine("strata: no usable Python found.")
49    [Console]::Error.WriteLine("        This archive carries its own runtime in python\: it is missing from this install.")
50    [Console]::Error.WriteLine("        Reinstall the archive, or set STRATA_PYTHON to a Python 3.10 or newer.")
51    exit 5
52}
53# UTF-8 text in Python, not the Windows code page: folders with Cyrillic or other letters stay intact (#3202)
54$env:PYTHONUTF8 = '1'
55$rest = @($py | Select-Object -Skip 1)
56& $py[0] @rest $app @args
57exit $LASTEXITCODE