vls_python 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. # -u = unbuffered for immediate streaming
  46. exec "$PY" -u "$SCRIPT_WIN" "${ARGS[@]}"