multirun_with_pyenv.sh 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/env bash
  2. #
  3. # Tool for running scripts with several versions of Python by the use of pyenv (versions must be installed before in
  4. # the docker image)
  5. #
  6. # Examples:
  7. # ./multirun_with_pyenv.sh ./exec.sh # Run ./exec.h with ALL installed versions of Python
  8. # ./multirun_with_pyenv.sh ./exec.sh arg1 arg2 # Run ./exec.h with arguments (and ALL installed versions of Python)
  9. # ./multirun_with_pyenv.sh -p 2.7.15 ./exec.sh # Run ./exec.h with Python 2.7.15 (-p must be the first argument)
  10. # ./multirun_with_pyenv.sh -p 3.4.8,2.7.15 ./exec.sh # Run ./exec.h with Python 3.4.8 and 2.7.15 (versions must be
  11. # # separated by coma and be without a space)
  12. PY_VERSIONS=""
  13. { source /opt/pyenv/activate; } || { echo 'Pyenv activation has failed!' ; exit 1; }
  14. if [ "$1" = "-p" ]; then
  15. if [ "$#" -ge 2 ]; then
  16. IFS=',' read -a PY_VERSIONS <<< "$2"
  17. shift #remove -p
  18. shift #remove argument after -p
  19. else
  20. echo 'No value (Python version) is given for argument -p!'
  21. exit 1
  22. fi
  23. else
  24. PY_VERSIONS=$(pyenv versions --bare)
  25. fi
  26. if [ "$#" -lt 1 ]; then
  27. echo 'No executable was passed to the runner!'
  28. exit 1
  29. fi
  30. for ver in ${PY_VERSIONS[@]}
  31. do
  32. echo 'Switching to Python' $ver
  33. $(pyenv global $ver) || exit 1
  34. echo 'Running' $@
  35. $@ || {
  36. echo 'Run failed! Switching back to the system version of the Python interpreter.';
  37. pyenv global system;
  38. exit 1;
  39. }
  40. done
  41. echo 'Switching back to the system version of Python'
  42. { pyenv global system; } || { echo 'Restoring the system version of the Python interpreter has failed!' ; exit 1; }