run-python-in-container.sh 1.7 KB

123456789101112131415161718192021222324252627282930313233343536
  1. #!/usr/bin/env bash
  2. set -Eeuo pipefail
  3. testDir="$(dirname "$BASH_SOURCE")"
  4. testDir="$(readlink -f "$testDir")"
  5. runDir="$(readlink -f "$BASH_SOURCE")"
  6. runDir="$(dirname "$runDir")"
  7. case "$1" in
  8. *windowsservercore* | *nanoserver*)
  9. # https://stackoverflow.com/q/34491463/433558 -- cmd doesn't process past the first newline in the string passed on the command line, even though CreateProcess supports passing newlines??
  10. # https://stackoverflow.com/a/52003129/433558 -- no goto / labels in cmd argument either?? what's even the point??
  11. # -- "And, in any case, remember that the longest command line you can write is 8191 characters long." ...
  12. # cmd /C 'for %x in ( foo bar baz ) do ( echo %x ) & echo hi' runs '( echo %x ) & echo hi' every iteration.........
  13. # so we'll just run twice and use the bash we're in to do the "difficult" work of a fallback when python can't be found... (even though every container has a higher cost on Windows ;.;)
  14. python="$(docker run --rm --entrypoint cmd "$1" /Q /S /C 'for %p in ( pypy3 pypy python3 python ) do ( %p --version >nul 2>&1 && echo %p && exit 0 )' | tr -d '\r')"
  15. python="${python% }" # "echo %p && ..." will print the trailing space because cmd...
  16. if [ -z "$python" ]; then
  17. echo >&2 'error: unable to determine how to run python'
  18. exit 1
  19. fi
  20. source "$runDir/run-in-container.sh" "$testDir" "$1" "$python" container.py
  21. ;;
  22. *)
  23. source "$runDir/run-in-container.sh" "$testDir" "$1" sh -ec '
  24. for c in pypy3 pypy python3 python; do
  25. if [ -x "/usr/local/bin/$c" ]; then
  26. exec "/usr/local/bin/$c" "$@"
  27. fi
  28. done
  29. echo >&2 "error: unable to determine how to run python"
  30. exit 1
  31. ' -- ./container.py
  32. ;;
  33. esac