python.sh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. if [ "$LY_3RDPARTY_PATH" == "" ]
  48. then
  49. LY_3RDPARTY_PATH=$HOME/.o3de/3rdParty
  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=$LY_3RDPARTY_PATH/venv/$ENGINE_ID
  54. PYTHON_VENV_ACTIVATE=$PYTHON_VENV/bin/activate
  55. PYTHON_VENV_PYTHON=$PYTHON_VENV/bin/python
  56. if [ ! -f $PYTHON_VENV_PYTHON ]
  57. then
  58. echo "Python has not been downloaded/configured yet."
  59. echo "Try running $LY_ROOT_FOLDER/python/get_python.sh first."
  60. exit 1
  61. fi
  62. # Determine the current package from where the current venv was initiated from
  63. PYTHON_VENV_HASH_FILE=$PYTHON_VENV/.hash
  64. if [ ! -f $PYTHON_VENV_HASH_FILE ]
  65. then
  66. echo "Python has not been downloaded/configured yet."
  67. echo "Try running $LY_ROOT_FOLDER/python/get_python.sh first."
  68. exit 1
  69. fi
  70. PYTHON_VENV_HASH=$(cat $PYTHON_VENV_HASH_FILE)
  71. # Calculate the expected hash from the current python package
  72. CURRENT_PYTHON_PACKAGE_HASH=$(cmake -P $DIR/../../../../python/get_python_package_hash.cmake $DIR/.. $PAL $ARCH)
  73. if [ "$PYTHON_VENV_HASH" != "$CURRENT_PYTHON_PACKAGE_HASH" ]
  74. then
  75. echo "Python has been updated since the last time the python command was invoked."
  76. echo "Run $LY_ROOT_FOLDER/python/get_python.sh to update."
  77. exit 1
  78. fi
  79. # The python in the venv environment is a symlink which will cause issues with loading the python shared
  80. # object that is relative to the original python lib shared library in the package.
  81. PYTHON_LIB_PATH=$(awk -F ' = ' '/home/ {print $2}' $LY_3RDPARTY_PATH/venv/$ENGINE_ID/pyvenv.cfg | sed 's/python\/bin/python\/lib/g')
  82. source $PYTHON_VENV_ACTIVATE
  83. PYTHONNOUSERSITE=1 LD_LIBRARY_PATH="$PYTHON_LIB_PATH:$LD_LIBRARY_PATH" "$PYTHON_VENV_PYTHON" "$@"
  84. exit $?