bootstrap.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. # coding:utf-8
  2. #!/usr/bin/python
  3. #
  4. # Copyright (c) Contributors to the Open 3D Engine Project.
  5. # For complete copyright and license terms please see the LICENSE at the root of this distribution.
  6. #
  7. # SPDX-License-Identifier: Apache-2.0 OR MIT
  8. #
  9. #
  10. # -------------------------------------------------------------------------
  11. """This module is for use in boostrapping the DccScriptingInterface Gem
  12. with O3DE. Note: this boostrap is only designed fo be py3 compatible.
  13. If you need DCCsi access in py27 (Autodesk Maya for instance) you may need
  14. to implement your own boostrapper module. Currently this is boostrapped
  15. from add_dccsi.py, as a temporty measure related to this Jira:
  16. SPEC-2581"""
  17. # standard imports
  18. import sys
  19. import os
  20. import site
  21. import importlib.util
  22. from pathlib import Path
  23. import logging as _logging
  24. # -------------------------------------------------------------------------
  25. # -------------------------------------------------------------------------
  26. _O3DE_RUNNING=None
  27. try:
  28. import azlmbr
  29. _O3DE_RUNNING=True
  30. except:
  31. _O3DE_RUNNING=False
  32. # -------------------------------------------------------------------------
  33. # -------------------------------------------------------------------------
  34. # we don't use dynaconf setting here as we might not yet have access
  35. # to that site-dir.
  36. _MODULENAME = __name__
  37. if _MODULENAME is '__main__':
  38. _MODULENAME = 'O3DE.DCCsi.bootstrap'
  39. # set up module logging
  40. for handler in _logging.root.handlers[:]:
  41. _logging.root.removeHandler(handler)
  42. _LOGGER = _logging.getLogger(_MODULENAME)
  43. # we need to set up basic access to the DCCsi
  44. _MODULE_PATH = os.path.realpath(__file__) # To Do: what if frozen?
  45. _DCCSI_PATH = os.path.normpath(os.path.join(_MODULE_PATH, '../../..'))
  46. _DCCSI_PATH = os.getenv('DCCSI_PATH', _DCCSI_PATH)
  47. site.addsitedir(_DCCSI_PATH)
  48. # now we have azpy api access
  49. from azpy.env_bool import env_bool
  50. from azpy.constants import ENVAR_DCCSI_GDEBUG
  51. from azpy.constants import ENVAR_DCCSI_DEV_MODE
  52. from azpy.constants import ENVAR_DCCSI_LOGLEVEL
  53. from azpy.constants import FRMT_LOG_LONG
  54. # set up global space, logging etc.
  55. # set these true if you want them set globally for debugging
  56. _DCCSI_GDEBUG = env_bool(ENVAR_DCCSI_GDEBUG, False)
  57. _DCCSI_DEV_MODE = env_bool(ENVAR_DCCSI_DEV_MODE, False)
  58. _DCCSI_LOGLEVEL = int(env_bool(ENVAR_DCCSI_LOGLEVEL, int(20)))
  59. if _DCCSI_GDEBUG:
  60. _DCCSI_LOGLEVEL = int(10)
  61. _logging.basicConfig(format=FRMT_LOG_LONG, level=_DCCSI_LOGLEVEL)
  62. _LOGGER.debug('Initializing: {0}.'.format({_MODULENAME}))
  63. # -------------------------------------------------------------------------
  64. # -------------------------------------------------------------------------
  65. # _settings.setenv() # doing this will add the additional DYNACONF_ envars
  66. def get_dccsi_config(DCCSI_PATH=_DCCSI_PATH):
  67. """Convenience method to set and retreive settings directly from module."""
  68. # we can go ahead and just make sure the the DCCsi env is set
  69. # _config is SO generic this ensures we are importing a specific one
  70. _spec_dccsi_config = importlib.util.spec_from_file_location("dccsi._config",
  71. Path(DCCSI_PATH,
  72. "config.py"))
  73. _dccsi_config = importlib.util.module_from_spec(_spec_dccsi_config)
  74. _spec_dccsi_config.loader.exec_module(_dccsi_config)
  75. return _dccsi_config
  76. # -------------------------------------------------------------------------
  77. # set and retreive the base env context/_settings on import
  78. _config = get_dccsi_config()
  79. _settings = _config.get_config_settings()
  80. if _DCCSI_DEV_MODE:
  81. _config.attach_debugger() # attempts to start debugger
  82. # done with basic setup
  83. # --- END -----------------------------------------------------------------
  84. ###########################################################################
  85. # Main Code Block, runs this script as main (testing)
  86. # -------------------------------------------------------------------------
  87. if __name__ == '__main__':
  88. """Run this file as main"""
  89. # -------------------------------------------------------------------------
  90. _O3DE_RUNNING=None
  91. try:
  92. import azlmbr
  93. _O3DE_RUNNING=True
  94. except:
  95. _O3DE_RUNNING=False
  96. # -------------------------------------------------------------------------
  97. _MODULENAME = __name__
  98. if _MODULENAME is '__main__':
  99. _MODULENAME = 'O3DE.DCCsi.bootstrap'
  100. from azpy.constants import STR_CROSSBAR
  101. # module internal debugging flags
  102. while 0: # temp internal debug flag
  103. _DCCSI_GDEBUG = True
  104. break
  105. # overide logger for standalone to be more verbose and log to file
  106. import azpy
  107. _LOGGER = azpy.initialize_logger(_MODULENAME,
  108. log_to_file=_DCCSI_GDEBUG,
  109. default_log_level=_DCCSI_LOGLEVEL)
  110. # happy print
  111. _LOGGER.info(STR_CROSSBAR)
  112. _LOGGER.info('~ constants.py ... Running script as __main__')
  113. _LOGGER.info(STR_CROSSBAR)
  114. # parse the command line args
  115. import argparse
  116. parser = argparse.ArgumentParser(
  117. description='O3DE DCCsi Boostrap (Test)',
  118. epilog="Will externally test the DCCsi boostrap")
  119. _config = get_dccsi_config()
  120. _settings = _config.get_config_settings(enable_o3de_python=True,
  121. enable_o3de_pyside2=True)
  122. parser.add_argument('-gd', '--global-debug',
  123. type=bool,
  124. required=False,
  125. help='Enables global debug flag.')
  126. parser.add_argument('-dm', '--developer-mode',
  127. type=bool,
  128. required=False,
  129. help='Enables dev mode for early auto attaching debugger.')
  130. parser.add_argument('-tp', '--test-pyside2',
  131. type=bool,
  132. required=False,
  133. help='Runs Qt/PySide2 tests and reports.')
  134. args = parser.parse_args()
  135. # easy overrides
  136. if args.global_debug:
  137. _DCCSI_GDEBUG = True
  138. if args.developer_mode:
  139. _DCCSI_DEV_MODE = True
  140. _config.attach_debugger() # attempts to start debugger
  141. if _DCCSI_GDEBUG:
  142. _LOGGER.info(f'DCCSI_PATH: {_settings.DCCSI_PATH}')
  143. _LOGGER.info(f'DCCSI_G_DEBUG: {_settings.DCCSI_GDEBUG}')
  144. _LOGGER.info(f'DCCSI_DEV_MODE: {_settings.DCCSI_DEV_MODE}')
  145. _LOGGER.info(f'DCCSI_OS_FOLDER: {_settings.DCCSI_OS_FOLDER}')
  146. _LOGGER.info(f'O3DE_PROJECT: {_settings.O3DE_PROJECT}')
  147. _LOGGER.info(f'O3DE_PROJECT_PATH: {_settings.O3DE_PROJECT_PATH}')
  148. _LOGGER.info(f'O3DE_DEV: {_settings.O3DE_DEV}')
  149. _LOGGER.info(f'O3DE_BUILD_PATH: {_settings.O3DE_BUILD_PATH}')
  150. _LOGGER.info(f'O3DE_BIN_PATH: {_settings.O3DE_BIN_PATH}')
  151. _LOGGER.info(f'DCCSI_PATH: {_settings.DCCSI_PATH}')
  152. _LOGGER.info(f'DCCSI_PYTHON_LIB_PATH: {_settings.DCCSI_PYTHON_LIB_PATH}')
  153. _LOGGER.info(f'DCCSI_PY_BASE: {_settings.DCCSI_PY_BASE}')
  154. if _DCCSI_GDEBUG or args.test_pyside2:
  155. try:
  156. import PySide2
  157. except:
  158. # set up Qt/PySide2 access and test
  159. _settings = _config.get_config_settings(enable_o3de_pyside2=True)
  160. import PySide2
  161. _LOGGER.info(f'PySide2: {PySide2}')
  162. _LOGGER.info(f'O3DE_BIN_PATH: {_settings.O3DE_BIN_PATH}')
  163. _LOGGER.info(f'QT_PLUGIN_PATH: {_settings.QT_PLUGIN_PATH}')
  164. _LOGGER.info(f'QT_QPA_PLATFORM_PLUGIN_PATH: {_settings.QT_QPA_PLATFORM_PLUGIN_PATH}')
  165. _config.test_pyside2()
  166. if not _O3DE_RUNNING:
  167. # return
  168. sys.exit()
  169. # --- END -----------------------------------------------------------------