python.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. # Special Case: If we are using the export-project script in a ROS2 enabled environment, then we cannot use the O3DE embedded python
  39. # for the export scripts because the ROS2 projects may require ros-specific python modules to exist for validation to build the project
  40. # which is installed as part of the ROS2 system packages. These packages are not available in the embedded O3DE python so in this
  41. # case we must call the system installed python3
  42. if [[ $ROS_DISTRO != "" && ( $1 == "export-project" || $2 == "export-project" ) ]]
  43. then
  44. which python3 > /dev/null 2>&1
  45. if [ $? -eq 0 ]
  46. then
  47. # Make sure the required 'resolvelib' is installed which is required for project export
  48. python3 -m pip install resolvelib || true
  49. # Run the python command through the ROS-installed python3
  50. python3 "$@"
  51. exit $?
  52. else
  53. echo "Warning. Detected ROS but cannot locate python3 for ROS, this may cause issues with O3DE."
  54. fi
  55. fi
  56. # Calculate the engine ID
  57. CALC_PATH=$DIR/../cmake/CalculateEnginePathId.cmake
  58. LY_ROOT_FOLDER=$DIR/..
  59. ENGINE_ID=$(cmake -P $CALC_PATH $LY_ROOT_FOLDER)
  60. if [ $? -ne 0 ]
  61. then
  62. echo "Unable to calculate engine ID"
  63. exit 1
  64. fi
  65. # Set the expected location of the python venv for this engine and the locations of the critical scripts/executables
  66. # needed to run python within the venv properly
  67. PYTHON_VENV=$HOME/.o3de/Python/venv/$ENGINE_ID
  68. PYTHON_VENV_ACTIVATE=$PYTHON_VENV/bin/activate
  69. PYTHON_VENV_PYTHON=$PYTHON_VENV/bin/python
  70. if [ ! -f $PYTHON_VENV_PYTHON ]
  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. # Determine the current package from where the current venv was initiated from
  77. PYTHON_VENV_HASH_FILE=$PYTHON_VENV/.hash
  78. if [ ! -f $PYTHON_VENV_HASH_FILE ]
  79. then
  80. echo "Python has not been downloaded/configured yet."
  81. echo "Try running $DIR/get_python.sh first."
  82. exit 1
  83. fi
  84. PYTHON_VENV_HASH=$(cat $PYTHON_VENV_HASH_FILE)
  85. # Calculate the expected hash from the current python package
  86. CURRENT_PYTHON_PACKAGE_HASH=$(cmake -P $DIR/get_python_package_hash.cmake $DIR/.. $PAL $ARCH)
  87. if [ "$PYTHON_VENV_HASH" != "$CURRENT_PYTHON_PACKAGE_HASH" ]
  88. then
  89. echo "Python has been updated since the last time the python command was invoked."
  90. echo "Run $DIR/get_python.sh to update."
  91. exit 1
  92. fi
  93. # Activate the venv environment
  94. source "$PYTHON_VENV_ACTIVATE"
  95. # Make sure that python shared library that is loaded by the python linked in the venv folder
  96. # is the one that is loaded by injecting the shared lib path before invoking python. Otherwise,
  97. # the shared library may not be found or it could load it from a different location.
  98. PYTHON_LIB_PATH=$PYTHON_VENV/lib
  99. PYTHONNOUSERSITE=1 LD_LIBRARY_PATH="$PYTHON_LIB_PATH:$LD_LIBRARY_PATH" PYTHONPATH= "$PYTHON_VENV_PYTHON" -B "$@"
  100. exit $?