python.sh 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/bin/bash
  2. # Copyright (c) Contributors to the Open 3D Engine Project.
  3. # For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. #
  5. # SPDX-License-Identifier: Apache-2.0 OR MIT
  6. #
  7. SOURCE="${BASH_SOURCE[0]}"
  8. # While $SOURCE is a symlink, resolve it
  9. while [[ -h "$SOURCE" ]]; do
  10. DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
  11. SOURCE="$( readlink "$SOURCE" )"
  12. # If $SOURCE was a relative symlink (so no "/" as prefix, need to resolve it relative to the symlink base directory
  13. [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
  14. done
  15. DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
  16. # Locate and make sure cmake is in the path
  17. if [[ "$OSTYPE" = *"darwin"* ]];
  18. then
  19. PAL=Mac
  20. ARCH=
  21. else
  22. PAL=Linux
  23. ARCH=$( uname -m )
  24. fi
  25. if ! [ -x "$(command -v cmake)" ]; then
  26. if [ -z ${LY_CMAKE_PATH} ]; then
  27. echo "ERROR: Could not find cmake on the PATH and LY_CMAKE_PATH is not defined, cannot continue."
  28. echo "Please add cmake to your PATH, or define LY_CMAKE_PATH"
  29. exit 1
  30. fi
  31. export PATH=$LY_CMAKE_PATH:$PATH
  32. if ! [ -x "$(command -v cmake)" ]; then
  33. echo "ERROR: Could not find cmake on the PATH or at the known location: $LY_CMAKE_PATH"
  34. echo "Please add cmake to the environment PATH or place it at the above known location."
  35. exit 1
  36. fi
  37. fi
  38. # Calculate the engine ID
  39. CALC_PATH=$DIR/../cmake/CalculateEnginePathId.cmake
  40. LY_ROOT_FOLDER=$DIR/..
  41. ENGINE_ID=$(cmake -P $CALC_PATH $LY_ROOT_FOLDER)
  42. if [ $? -ne 0 ]
  43. then
  44. echo "Unable to calculate engine ID"
  45. exit 1
  46. fi
  47. # Set the expected location of the python venv for this engine and the locations of the critical scripts/executables
  48. # needed to run python within the venv properly
  49. PYTHON_VENV=$HOME/.o3de/Python/venv/$ENGINE_ID
  50. PYTHON_VENV_ACTIVATE=$PYTHON_VENV/bin/activate
  51. PYTHON_VENV_PYTHON=$PYTHON_VENV/bin/python
  52. if [ ! -f $PYTHON_VENV_PYTHON ]
  53. then
  54. echo "Python has not been downloaded/configured yet."
  55. echo "Try running $DIR/get_python.sh first."
  56. exit 1
  57. fi
  58. # Determine the current package from where the current venv was initiated from
  59. PYTHON_VENV_HASH_FILE=$PYTHON_VENV/.hash
  60. if [ ! -f $PYTHON_VENV_HASH_FILE ]
  61. then
  62. echo "Python has not been downloaded/configured yet."
  63. echo "Try running $DIR/get_python.sh first."
  64. exit 1
  65. fi
  66. PYTHON_VENV_HASH=$(cat $PYTHON_VENV_HASH_FILE)
  67. # Calculate the expected hash from the current python package
  68. CURRENT_PYTHON_PACKAGE_HASH=$(cmake -P $DIR/get_python_package_hash.cmake $DIR/.. $PAL $ARCH)
  69. if [ "$PYTHON_VENV_HASH" != "$CURRENT_PYTHON_PACKAGE_HASH" ]
  70. then
  71. echo "Python has been updated since the last time the python command was invoked."
  72. echo "Run $DIR/get_python.sh to update."
  73. exit 1
  74. fi
  75. # Activate the venv environment
  76. source $PYTHON_VENV_ACTIVATE
  77. # Make sure that python shared library that is loaded by the python linked in the venv folder
  78. # is the one that is loaded by injecting the shared lib path before invoking python. Otherwise,
  79. # the shared library may not be found or it could load it from a different location.
  80. PYTHON_LIB_PATH=$PYTHON_VENV/lib
  81. PYTHONNOUSERSITE=1 LD_LIBRARY_PATH="$PYTHON_LIB_PATH:$LD_LIBRARY_PATH" PYTHONPATH= "$PYTHON_VENV_PYTHON" "$@"
  82. exit $?