run-python-in-container.sh 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. # ensure pip does not complain about a new version being available
  21. # or that a new version will no longer work with this python version
  22. source "$runDir/run-in-container.sh" \
  23. --docker-arg --env=PIP_DISABLE_PIP_VERSION_CHECK=1 \
  24. --docker-arg --env=PIP_NO_PYTHON_VERSION_WARNING=1 \
  25. "$testDir" "$1" "$python" container.py
  26. ;;
  27. *)
  28. # ensure pip does not complain about a new version being available
  29. # or that a new version will no longer work with this python version
  30. source "$runDir/run-in-container.sh" \
  31. --docker-arg --env=PIP_DISABLE_PIP_VERSION_CHECK=1 \
  32. --docker-arg --env=PIP_NO_PYTHON_VERSION_WARNING=1 \
  33. "$testDir" "$1" sh -ec '
  34. for c in pypy3 pypy python3 python; do
  35. if [ -x "/usr/local/bin/$c" ]; then
  36. exec "/usr/local/bin/$c" "$@"
  37. fi
  38. done
  39. echo >&2 "error: unable to determine how to run python"
  40. exit 1
  41. ' -- ./container.py
  42. ;;
  43. esac