python.sh 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. elif [[ "$OSTYPE" = "msys" ]];
  22. then
  23. PAL=Windows
  24. ARCH=
  25. else
  26. PAL=Linux
  27. ARCH=$( uname -m )
  28. fi
  29. if ! [ -x "$(command -v cmake)" ]; then
  30. if [ -z ${LY_CMAKE_PATH} ]; then
  31. echo "ERROR: Could not find cmake on the PATH and LY_CMAKE_PATH is not defined, cannot continue."
  32. echo "Please add cmake to your PATH, or define LY_CMAKE_PATH"
  33. exit 1
  34. fi
  35. export PATH=$LY_CMAKE_PATH:$PATH
  36. if ! [ -x "$(command -v cmake)" ]; then
  37. echo "ERROR: Could not find cmake on the PATH or at the known location: $LY_CMAKE_PATH"
  38. echo "Please add cmake to the environment PATH or place it at the above known location."
  39. exit 1
  40. fi
  41. fi
  42. # Calculate the engine ID
  43. CALC_PATH=$DIR/../cmake/CalculateEnginePathId.cmake
  44. LY_ROOT_FOLDER=$DIR/..
  45. ENGINE_ID=$(cmake -P $CALC_PATH $LY_ROOT_FOLDER)
  46. if [ $? -ne 0 ]
  47. then
  48. echo "Unable to calculate engine ID"
  49. exit 1
  50. fi
  51. # Set the expected location of the python venv for this engine and the locations of the critical scripts/executables
  52. # needed to run python within the venv properly
  53. PYTHON_VENV=$HOME/.o3de/Python/venv/$ENGINE_ID
  54. if [[ "$OSTYPE" == "msys" ]]; #git bash on windows
  55. then
  56. PYTHON_VENV_ACTIVATE=$PYTHON_VENV/Scripts/activate
  57. PYTHON_VENV_PYTHON=$PYTHON_VENV/Scripts/python
  58. else
  59. PYTHON_VENV_ACTIVATE=$PYTHON_VENV/bin/activate
  60. PYTHON_VENV_PYTHON=$PYTHON_VENV/bin/python
  61. fi
  62. if [ ! -f $PYTHON_VENV_PYTHON ]
  63. then
  64. echo "Python has not been downloaded/configured yet."
  65. echo "Try running $DIR/get_python.sh first."
  66. exit 1
  67. fi
  68. # Determine the current package from where the current venv was initiated from
  69. PYTHON_VENV_HASH_FILE=$PYTHON_VENV/.hash
  70. if [ ! -f $PYTHON_VENV_HASH_FILE ]
  71. then
  72. echo "Python has not been downloaded/configured yet."
  73. echo "Try running $DIR/get_python.sh first."
  74. exit 1
  75. fi
  76. PYTHON_VENV_HASH=$(cat $PYTHON_VENV_HASH_FILE)
  77. # Calculate the expected hash from the current python package
  78. CURRENT_PYTHON_PACKAGE_HASH=$(cmake -P $DIR/get_python_package_hash.cmake $DIR/.. $PAL $ARCH)
  79. if [ "$PYTHON_VENV_HASH" != "$CURRENT_PYTHON_PACKAGE_HASH" ]
  80. then
  81. echo "Python has been updated since the last time the python command was invoked."
  82. echo "Run $DIR/get_python.sh to update."
  83. exit 1
  84. fi
  85. # Activate the venv environment
  86. source $PYTHON_VENV_ACTIVATE
  87. # Make sure that python shared library that is loaded by the python linked in the venv folder
  88. # is the one that is loaded by injecting the shared lib path before invoking python. Otherwise,
  89. # the shared library may not be found or it could load it from a different location.
  90. PYTHON_LIB_PATH=$PYTHON_VENV/lib
  91. PYTHONNOUSERSITE=1 LD_LIBRARY_PATH="$PYTHON_LIB_PATH:$LD_LIBRARY_PATH" PYTHONPATH= "$PYTHON_VENV_PYTHON" "$@"
  92. exit $?