1
0
mirror of https://github.com/djohnlewis/stackdump synced 2024-12-04 06:57:36 +00:00
stackdump/Start-Python.ps1

43 lines
3.1 KiB
PowerShell
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<#
.SYNOPSIS
Starts Python with the Stackdump modules in its path.
.DESCRIPTION
Starts Python, by using the path specified in the PYTHON_CMD file located in
the same directory as this script (create as necessary) or python.exe that
resolves in the current PATH.
The Python instance will be checked to ensure it is the right version before
the command is executed.
All parameters given will be passed directly to Python. If none are given, the
interactive interpreter starts.
.EXAMPLE
Start-Python python/src/stackdump/commands/import_site.py
#>
$ScriptDir = Split-Path $MyInvocation.MyCommand.Path
$PythonCmd = 'python.exe'
if (Test-Path (Join-Path $ScriptDir 'PYTHON_CMD')) {
$PythonCmd = Get-Content (Join-Path $ScriptDir 'PYTHON_CMD')
}
$AbsPythonCmd = @(Get-Command $PythonCmd -ErrorAction SilentlyContinue)[0].Path
if ($AbsPythonCmd -ne $null) {
$PythonVer = "$(& $AbsPythonCmd -V 2>&1)".Trim().Split(' ')[1]
$PythonVerMajor, $PythonVerMinor, $bleh = $PythonVer.Split('.')
if (($PythonVerMajor -eq '2') -and ($PythonVerMinor -ge '5')) {
# this is an appropriate version of Python, run it
Write-Host "Using Python $AbsPythonCmd"
$env:PYTHONPATH="$(Join-Path $ScriptDir 'python/packages');$(Join-Path $ScriptDir 'python/src');$env:PYTHONPATH"
& $AbsPythonCmd @args
}
else {
Write-Error "Python $PythonVer is not compatible with Stackdump. Use Python 2.5 to 2.7."
}
}
else {
Write-Error "Python could not be located. Specify its path using PYTHON_CMD or check the path in PYTHON_CMD."
}