python.sh 4.4 KB

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