win_python 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/env bash
  2. # winpy.sh — stream Windows python.exe output in WSL
  3. # Usage:
  4. # winpy.sh script.py [args...]
  5. # winpy.sh -c "print('hi')"
  6. # winpy.sh -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. # WINPY_CONVERT_ARGS=1 in your environment.
  32. ARGS=()
  33. if [[ "${WINPY_CONVERT_ARGS:-0}" == "1" ]]; then
  34. for a in "$@"; do
  35. if [[ -e "$a" ]]; then
  36. ARGS+=("$(wslpath -w "$a")")
  37. else
  38. ARGS+=("$a")
  39. fi
  40. done
  41. else
  42. ARGS=("$@")
  43. fi
  44. # -u = unbuffered for immediate streaming
  45. exec "$PY" -u "$SCRIPT_WIN" "${ARGS[@]}"