ya.bat 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. @echo off
  2. rem Ya Simple Windows launcher
  3. setlocal
  4. call :dbg Ya Simple Windows Launcher (Debug)
  5. call :find_ya
  6. if ERRORLEVEL 1 exit /b 1
  7. call :dbg Ya: %YA_BAT_REAL%
  8. call :fix_env
  9. call :find_python
  10. if ERRORLEVEL 1 exit /b 1
  11. call :dbg Python: "%YA_BAT_PYTHON%"
  12. call "%YA_BAT_PYTHON%" %YA_PY_KNOB% "%YA_BAT_REAL%" %*
  13. exit /b %ERRORLEVEL%
  14. :find_ya
  15. call :dbg Searching for ya near ya.bat...
  16. set YA_BAT_REAL=%~dp0ya
  17. if exist "%YA_BAT_REAL%" exit /b 0
  18. call :err Ya not found
  19. exit /b 1
  20. :fix_env
  21. call :dbg Fixing environment...
  22. for /f "delims=: tokens=2" %%F in ('chcp') do (
  23. if "%%F" == " 65001" (
  24. call :dbg -- Forcing I/O encoding for python: utf-8
  25. set PYTHONIOENCODING=utf-8
  26. set PYTHONUTF8=1
  27. )
  28. )
  29. exit /b 0
  30. :find_python
  31. call :dbg Searching for python in PATH...
  32. for /f "delims=" %%F in ('where python 2^>nul') do (
  33. call :test_python %%~sF
  34. if not ERRORLEVEL 1 (
  35. set YA_BAT_PYTHON=%%F
  36. if /I "%%F" == "C:\Windows\py.exe" (
  37. set YA_PY_KNOB=-3
  38. )
  39. exit /b 0
  40. )
  41. )
  42. call :dbg Searching for python in ftypes...
  43. for /f delims^=^=^"^ tokens^=2 %%F in ('ftype Python.File 2^>nul') do (
  44. call :test_python %%F
  45. if not ERRORLEVEL 1 (
  46. set YA_BAT_PYTHON=%%F
  47. if /I "%%F" == "C:\Windows\py.exe" (
  48. set YA_PY_KNOB=-3
  49. )
  50. exit /b 0
  51. )
  52. )
  53. call :dbg Searching for python with Get-Command...
  54. for /f "delims=" %%i in ('powershell -Command "Get-Command python | Select-Object -ExpandProperty Source"') do (
  55. call :test_python %%i
  56. if not ERRORLEVEL 1 (
  57. set YA_BAT_PYTHON=%%i
  58. exit /b 0
  59. )
  60. )
  61. call :err Python not found
  62. exit /b 1
  63. :test_python
  64. call :dbg -- Checking python: %1
  65. if not exist %1 (
  66. call :dbg ---- Not found
  67. exit /b 1
  68. )
  69. for /f %%P in ('%1 -c "import os, sys; sys.stdout.write(os.name + '\n')" 2^>nul') do set YA_BAT_PYTHON_PLATFORM=%%P
  70. if not defined YA_BAT_PYTHON_PLATFORM (
  71. call :dbg ---- Not runnable
  72. exit /b 2
  73. )
  74. if not "%YA_BAT_PYTHON_PLATFORM%"=="nt" (
  75. call :dbg ---- Non-windows: %YA_BAT_PYTHON_PLATFORM%
  76. exit /b 3
  77. )
  78. exit /b 0
  79. :dbg
  80. if defined YA_BAT_DEBUG echo [ya.bat] %* 1>&2
  81. exit /b 0
  82. :err
  83. echo [ya.bat] Error: %* 1>&2
  84. exit /b 0