entry_test.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #
  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. #
  8. # -------------------------------------------------------------------------
  9. """! This module is an early temporary way to attach wing ide as debugger"""
  10. # TO DO: this whole file needs a refactor!!!
  11. # -------------------------------------------------------------------------
  12. # standard imports
  13. from __future__ import unicode_literals
  14. import os
  15. import sys
  16. import site
  17. import importlib
  18. import logging as _logging
  19. from pathlib import Path
  20. # -------------------------------------------------------------------------
  21. # -------------------------------------------------------------------------
  22. _BOOT_CHECK = False # set true to test breakpoint in this module directly
  23. # global scope
  24. from DccScriptingInterface import _PACKAGENAME
  25. _MODULENAME = f'{_PACKAGENAME}.entry_test'
  26. _LOGGER = _logging.getLogger(_MODULENAME)
  27. _LOGGER.debug('Initializing: {0}.'.format({_MODULENAME}))
  28. _MODULE_PATH = Path(__file__)
  29. _LOGGER.debug(f'_MODULE_PATH: {_MODULE_PATH.as_posix()}')
  30. # local dccsi imports
  31. # this accesses common global state, e.g. DCCSI_GDEBUG (is True or False)
  32. from DccScriptingInterface.globals import *
  33. # this module needs to be lightweight and avoid cyclical imports
  34. # so refactoring this out, a more fully featured dev module with
  35. # support for multiple IDEs can come later
  36. # from DccScriptingInterface.Tools.IDE.Wing.config import wing_config
  37. # settings = wing_config.get_settings()
  38. from DccScriptingInterface import PATH_WINGHOME
  39. WINGHOME = Path(PATH_WINGHOME).resolve()
  40. from DccScriptingInterface import PATH_WING_APPDATA
  41. WING_APPDATA = Path(PATH_WINGHOME).resolve()
  42. # -------------------------------------------------------------------------
  43. # -------------------------------------------------------------------------
  44. def import_module_path(module_name, module_path):
  45. """! Imports a module from abs path."""
  46. from importlib import util
  47. spec = util.spec_from_file_location(module_name, module_path)
  48. module = util.module_from_spec(spec)
  49. spec.loader.exec_module(module)
  50. return module
  51. # -------------------------------------------------------------------------
  52. # -------------------------------------------------------------------------
  53. def main(verbose=DCCSI_GDEBUG, connect_debugger=True):
  54. if verbose:
  55. _LOGGER.info(f"{'-' * 74}")
  56. _LOGGER.info('entry_test.main()')
  57. _LOGGER.info('Root test import successful:')
  58. _LOGGER.info('~ {}'.format(__file__))
  59. if connect_debugger:
  60. status = connect_wing()
  61. _LOGGER.info(status)
  62. # -------------------------------------------------------------------------
  63. # -------------------------------------------------------------------------
  64. def connect_wing():
  65. _LOGGER.info(f"{'-' * 74}")
  66. _LOGGER.info('entry_test.connect_wing()')
  67. try:
  68. Path(PATH_WINGHOME).exists()
  69. _LOGGER.info(f'~ WINGHOME: {PATH_WINGHOME}')
  70. except Exception as e:
  71. _LOGGER.error(e)
  72. _LOGGER.error(f'WINGHOME does not exist')
  73. # this line temp until future refactor
  74. _LOGGER.warning(f'Check to ensure Wing Pro 8, is installed')
  75. _LOGGER.warning(f'C:\Program Files (x86)\Wing Pro 8')
  76. return None
  77. try:
  78. _wing_appdata = Path(WING_APPDATA)
  79. _wing_appdata.exists()
  80. _LOGGER.info(f'~ WING_APPDATA: {WING_APPDATA}')
  81. wDBstub = Path(_wing_appdata, 'wingdbstub.py').resolve(strict=True)
  82. _LOGGER.info(f'~ Wing debugger: {wDBstub}')
  83. except Exception as e:
  84. _LOGGER.error(e)
  85. _LOGGER.warning(f'{WING_APPDATA}\wingdbstub.py does not exist')
  86. _LOGGER.warning(f'This is required to attach wing as debugger')
  87. _LOGGER.warning(f'Copy the file: {PATH_WINGHOME}\\wingdbstub.py')
  88. _LOGGER.warning(f'To the dir: {WING_APPDATA}\\wingdbstub.py')
  89. _LOGGER.warning(f'Then open the file: {WING_APPDATA}\\wingdbstub.py')
  90. _LOGGER.warning(f"Modify line 96 to 'kEmbedded = 1'")
  91. return None
  92. try:
  93. wingdbstub = import_module_path("wingdbstub", wDBstub.as_posix())
  94. _LOGGER.info('~ Success: imported wingdbstub')
  95. except Exception as e:
  96. _LOGGER.warning(e)
  97. _LOGGER.warning('warning: import wingdbstub.py, FAILED')
  98. pass
  99. # try: # hail mary, random import attempt
  100. # import wingdbstub
  101. # except Exception as e:
  102. # _LOGGER.warning('warning: import wingdbstub.py, FAILED')
  103. # return None
  104. try:
  105. wingdbstub.Ensure(require_connection=1, require_debugger=1)
  106. _LOGGER.info('~ Success: wingdbstub.Ensure()')
  107. except Exception as e:
  108. _LOGGER.error(e)
  109. _LOGGER.warning('Error: wingdbstub.Ensure()')
  110. _LOGGER.warning('Check that wing ide is running')
  111. try:
  112. wingdbstub.debugger.StartDebug()
  113. # leave a tag, so another boostrap can check if already set
  114. os.environ["DCCSI_DEBUGGER_ATTACHED"] = 'True'
  115. _LOGGER.info('~ Success: wingdbstub.debugger.StartDebug()')
  116. except Exception as e:
  117. _LOGGER.warning(e)
  118. _LOGGER.warning('Error: wingdbstub.debugger.StartDebug()')
  119. _LOGGER.warning('Check that wing ide is running, and not attached to another process')
  120. _LOGGER.info('{0}'.format('-' * 74))
  121. # a hack to allow a place to drop a breakpoint on boot
  122. while _BOOT_CHECK:
  123. _LOGGER.debug(str('SPAM'))
  124. return
  125. # -------------------------------------------------------------------------
  126. ###########################################################################
  127. # Main Code Block, runs this script as main (testing)
  128. # -------------------------------------------------------------------------
  129. if __name__ == '__main__':
  130. """Run in debug perform local tests from IDE or CLI"""
  131. # default loglevel to info unless set
  132. DCCSI_LOGLEVEL = int(env_bool(ENVAR_DCCSI_LOGLEVEL, _logging.INFO))
  133. if DCCSI_GDEBUG:
  134. # override loglevel if runnign debug
  135. DCCSI_LOGLEVEL = _logging.DEBUG
  136. # configure basic logger
  137. # note: not using a common logger to reduce cyclical imports
  138. _logging.basicConfig(level=DCCSI_LOGLEVEL,
  139. format=FRMT_LOG_LONG,
  140. datefmt='%m-%d %H:%M')
  141. _LOGGER = _logging.getLogger(_MODULENAME)
  142. _LOGGER.setLevel(_logging.DEBUG)
  143. _debugger = connect_wing()
  144. _LOGGER.debug('_DCCSI_GDEBUG: {}'.format(DCCSI_GDEBUG))
  145. _LOGGER.debug('_DCCSI_DEV_MODE: {}'.format(DCCSI_DEV_MODE))
  146. _LOGGER.debug('_DCCSI_LOGLEVEL: {}'.format(DCCSI_LOGLEVEL))
  147. # -------------------------------------------------------------------------