python.cmd 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. @ECHO OFF
  2. REM
  3. REM Copyright (c) Contributors to the Open 3D Engine Project.
  4. REM For complete copyright and license terms please see the LICENSE at the root of this distribution.
  5. REM
  6. REM SPDX-License-Identifier: Apache-2.0 OR MIT
  7. REM
  8. REM
  9. REM This script provides a single entry point that you can trust is present.
  10. REM Depending on this entry point instead of trying to find a python.exe
  11. REM In a subfolder allows you to keep working if the version of python changes or
  12. REM other environmental requirements change.
  13. REM When the project switches to a new version of Python, this file will be updated.
  14. SETLOCAL
  15. SET CMD_DIR=%~dp0
  16. SET CMD_DIR=%CMD_DIR:~0,-1%
  17. REM Calculate the path to the expected python venv for the current engine located at %CMD_DIR%\..
  18. REM The logic in LYPython will generate a unique ID based on the absolute path of the current engine
  19. REM so that the venv will not collide with any other versions of O3DE installed on the current machine
  20. REM Run the custom cmake command script to calculate the ID based on %CMD_DIR%\..
  21. SET CALC_PATH=%CMD_DIR%\..\cmake\CalculateEnginePathId.cmake
  22. FOR /F %%g IN ('cmake -P %CALC_PATH% %CMD_DIR%\..') DO SET ENGINE_ID=%%g
  23. IF NOT "%ENGINE_ID%" == "" GOTO ENGINE_ID_CALCULATED
  24. echo
  25. echo Unable to calculate engine ID
  26. exit /b 1
  27. :ENGINE_ID_CALCULATED
  28. REM Set the expected location of the python venv for this engine and the locations of the critical scripts/executables
  29. REM needed to run python within the venv properly
  30. REM If the %LY_3RDPARTY_PATH% is not set, then default it to %USERPROFILE%/.o3de/3rdParty
  31. IF "" == "%LY_3RDPARTY_PATH%" (
  32. SET LY_3RDPARTY_PATH=%USERPROFILE%\.o3de\3rdParty
  33. )
  34. SET PYTHON_VENV=%USERPROFILE%\.o3de\Python\venv\%ENGINE_ID%
  35. SET PYTHON_VENV_ACTIVATE=%PYTHON_VENV%\Scripts\activate.bat
  36. SET PYTHON_VENV_DEACTIVATE=%PYTHON_VENV%\Scripts\deactivate.bat
  37. IF [%1] EQU [debug] (
  38. SET PYTHON_VENV_PYTHON=%PYTHON_VENV%\Scripts\python_d.exe
  39. SET PYTHON_ARGS=%*:~6%
  40. ) ELSE (
  41. SET PYTHON_VENV_PYTHON=%PYTHON_VENV%\Scripts\python.exe
  42. SET PYTHON_ARGS=%*
  43. )
  44. IF EXIST %PYTHON_VENV_PYTHON% GOTO PYTHON_VENV_EXISTS
  45. ECHO Python has not been setup completely for O3DE. Missing Python venv %PYTHON_VENV_PYTHON%
  46. ECHO Try running %CMD_DIR%\get_python.bat to setup Python for O3DE.
  47. exit /b 1
  48. :PYTHON_VENV_EXISTS
  49. REM If python venv exists, we still need to validate that it is the current version by getting the
  50. REM package current package hash from 3rd Party
  51. FOR /F %%g IN ('cmake -P %CMD_DIR%\get_python_package_hash.cmake %CMD_DIR%\.. Windows') DO SET CURRENT_PACKAGE_HASH=%%g
  52. IF NOT "%CURRENT_PACKAGE_HASH%" == "" GOTO PACKAGE_HASH_READ
  53. echo
  54. echo Unable to get current python package hash
  55. exit /b 1
  56. :PACKAGE_HASH_READ
  57. REM Make sure there a .hash file that serves as the marker for the source python package the venv is from
  58. SET PYTHON_VENV_HASH=%PYTHON_VENV%\.hash
  59. IF EXIST %PYTHON_VENV_HASH% GOTO PYTHON_VENV_HASH_EXISTS
  60. ECHO Python has not been setup completely for O3DE. Missing venv hash %PYTHON_VENV_HASH%
  61. ECHO Try running %CMD_DIR%\get_python.bat to setup Python for O3DE.
  62. exit /b 1
  63. :PYTHON_VENV_HASH_EXISTS
  64. REM Read in the .hash from the venv to see if we need to update the version of python
  65. SET /p VENV_PACKAGE_HASH=<%PYTHON_VENV_HASH%
  66. IF "%VENV_PACKAGE_HASH%" == "%CURRENT_PACKAGE_HASH%" GOTO PYTHON_VENV_MATCHES
  67. ECHO Python needs to be updated against the current version.
  68. ECHO Try running %CMD_DIR%\get_python.bat to update Python for O3DE.
  69. exit /b 1
  70. :PYTHON_VENV_MATCHES
  71. REM Execute the python call from the arguments within the python venv environment
  72. call %PYTHON_VENV_ACTIVATE%
  73. call "%PYTHON_VENV_PYTHON%" %PYTHON_ARGS%
  74. SET PYTHON_RESULT=%ERRORLEVEL%
  75. call %PYTHON_VENV_DEACTIVATE%
  76. exit /B %PYTHON_RESULT%