vls_python 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/env bash
  2. # vls_python — stream Windows python.exe output in WSL
  3. # Usage:
  4. # vls_python script.py [args...]
  5. # vls_python -c "print('hi')"
  6. # vls_python -m module [args...]
  7. set -euo pipefail
  8. # Path to Windows python.exe, via WSL mount
  9. PY="/mnt/d/vls-trunk/env-win64/python27/python.exe"
  10. if [[ ! -x "$PY" && ! -f "$PY" ]]; then
  11. echo "Error: python.exe not found at: $PY" >&2
  12. exit 1
  13. fi
  14. if [[ $# -lt 1 ]]; then
  15. echo "Usage: $0 <script.py | -c cmd | -m module | other python flags> [args...]" >&2
  16. exit 1
  17. fi
  18. # If first arg is a python flag (e.g. -c/-m/-V), pass through directly.
  19. if [[ "$1" == -* ]]; then
  20. exec "$PY" -u "$@"
  21. fi
  22. # Otherwise treat first arg as a script path; convert to Windows if it exists in WSL.
  23. SCRIPT="$1"; shift
  24. if [[ -e "$SCRIPT" ]]; then
  25. SCRIPT_WIN="$(wslpath -w "$SCRIPT")"
  26. else
  27. # allow passing a Windows-style path directly
  28. SCRIPT_WIN="$SCRIPT"
  29. fi
  30. # Optional: convert any remaining args that are existing WSL paths if you set
  31. # VLS_PYTHON_CONVERT_ARGS=1 (WINPY_CONVERT_ARGS is still accepted).
  32. ARGS=()
  33. convert_flag="${VLS_PYTHON_CONVERT_ARGS:-${WINPY_CONVERT_ARGS:-0}}"
  34. if [[ "$convert_flag" == "1" ]]; then
  35. for a in "$@"; do
  36. if [[ -e "$a" ]]; then
  37. ARGS+=("$(wslpath -w "$a")")
  38. else
  39. ARGS+=("$a")
  40. fi
  41. done
  42. else
  43. ARGS=("$@")
  44. fi
  45. # Ensure any VLS_* env vars are forwarded to the Windows process by listing
  46. # them in WSLENV.
  47. WSLENV_EXISTING="${WSLENV-}"
  48. WSLENV_ENTRIES=()
  49. if [[ -n "$WSLENV_EXISTING" ]]; then
  50. IFS=':' read -r -a WSLENV_ENTRIES <<<"$WSLENV_EXISTING"
  51. fi
  52. # Build a list of VLS_* vars present in the environment.
  53. VLS_ENV_VARS=()
  54. while IFS='=' read -r name _; do
  55. if [[ "$name" == VLS_* ]]; then
  56. VLS_ENV_VARS+=("$name")
  57. fi
  58. done < <(env)
  59. # Append any missing VLS_* names to WSLENV (keeping existing ordering intact).
  60. if (( ${#VLS_ENV_VARS[@]} > 0 )); then
  61. for var_name in "${VLS_ENV_VARS[@]}"; do
  62. already_present=0
  63. for existing in "${WSLENV_ENTRIES[@]}"; do
  64. if [[ "$existing" == "$var_name" ]]; then
  65. already_present=1
  66. break
  67. fi
  68. done
  69. if (( already_present == 0 )); then
  70. WSLENV_ENTRIES+=("$var_name")
  71. fi
  72. done
  73. WSLENV="$(IFS=:; echo "${WSLENV_ENTRIES[*]}")"
  74. export WSLENV
  75. fi
  76. # -u = unbuffered for immediate streaming
  77. exec "$PY" -u "$SCRIPT_WIN" "${ARGS[@]}"