constants.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. """!
  12. Module Documentation:
  13. DccScriptingInterface:: azpy//constants.py
  14. This module is mainly a bunch of commony used constants, and default strings
  15. So we can make an update here once that is used elsewhere.
  16. < To Do: Further document module here >
  17. """
  18. # -------------------------------------------------------------------------
  19. import timeit
  20. _MODULE_START = timeit.default_timer() # start tracking
  21. # standard imports
  22. import os
  23. import sys
  24. import site
  25. from pathlib import Path
  26. from os.path import expanduser
  27. import logging as _logging
  28. # -------------------------------------------------------------------------
  29. # -------------------------------------------------------------------------
  30. # global scope
  31. from DccScriptingInterface.azpy import _PACKAGENAME
  32. _MODULENAME = f'{_PACKAGENAME}.constants'
  33. _LOGGER = _logging.getLogger(_MODULENAME)
  34. _LOGGER.debug(f'Initializing: {_MODULENAME}')
  35. _MODULE_PATH = Path(__file__) # what if frozen?
  36. _LOGGER.debug(f'_MODULE_PATH: {_MODULE_PATH}')
  37. # -------------------------------------------------------------------------
  38. # -------------------------------------------------------------------------
  39. # Ideally this module is standalone with minimal dependancies
  40. # Most logic, such as a search to derive a path, should happen outside of this module.
  41. # To Do: place best defaults here and logical derivations in a config.py
  42. # This sets up basic code access to the DCCsi
  43. from DccScriptingInterface.azpy import PATH_DCCSIG # root DCCsi path
  44. # DCCsi imports
  45. from DccScriptingInterface.azpy.env_bool import env_bool
  46. from azpy.config_utils import return_stub_dir
  47. from azpy.config_utils import get_stub_check_path
  48. # -------------------------------------------------------------------------
  49. # -------------------------------------------------------------------------
  50. # This is the first set of defined constants (and we use them here)
  51. from DccScriptingInterface.constants import ENVAR_DCCSI_GDEBUG
  52. from DccScriptingInterface.constants import ENVAR_DCCSI_DEV_MODE
  53. from DccScriptingInterface.constants import ENVAR_DCCSI_GDEBUGGER
  54. from DccScriptingInterface.constants import ENVAR_DCCSI_LOGLEVEL
  55. from DccScriptingInterface.constants import ENVAR_DCCSI_TESTS
  56. from DccScriptingInterface.globals import *
  57. # -------------------------------------------------------------------------
  58. # -------------------------------------------------------------------------
  59. # utility: constants, like pretty print strings
  60. from DccScriptingInterface.constants import STR_CROSSBAR
  61. from DccScriptingInterface.constants import STR_CROSSBAR_RL
  62. from DccScriptingInterface.constants import STR_CROSSBAR_NL
  63. # Log formating
  64. from DccScriptingInterface.constants import FRMT_LOG_LONG
  65. from DccScriptingInterface.constants import FRMT_LOG_SHRT
  66. # -------------------------------------------------------------------------
  67. # -------------------------------------------------------------------------
  68. # common: constants like os, sys utils
  69. from DccScriptingInterface.constants import SLUG_DIR_REGISTRY
  70. # string literals
  71. BITDEPTH64 = str('64bit')
  72. # some common paths
  73. from DccScriptingInterface.constants import PATH_PROGRAMFILES_X86
  74. from DccScriptingInterface.constants import PATH_PROGRAMFILES_X64
  75. # os.path.expanduser("~") returns different values in py2.7 vs 3
  76. PATH_USER_HOME = expanduser("~")
  77. _LOGGER.debug(f'user home: {PATH_USER_HOME}')
  78. # special case, make sure didn't return <user>\documents
  79. _uh_parts = os.path.split(PATH_USER_HOME)
  80. if str(_uh_parts[1].lower()) == 'documents':
  81. PATH_USER_HOME = _uh_parts[0]
  82. _LOGGER.debug(f'user home CORRECTED: {PATH_USER_HOME}')
  83. STR_USER_O3DE_PATH = str('{home}\\{o3de}')
  84. # -------------------------------------------------------------------------
  85. # -------------------------------------------------------------------------
  86. # O3DE: common
  87. # some common str tags
  88. TAG_DEFAULT_COMPANY = str('Amazon.O3DE')
  89. TAG_DEFAULT_PROJECT = str('DccScriptingInterface')
  90. TAG_DCCSI_NICKNAME = str('DCCsi')
  91. TAG_MOCK_PROJECT = str('MockProject')
  92. TAG_DIR_O3DE_DEV = str('dev')
  93. TAG_DIR_DCCSI_AZPY = str('azpy')
  94. TAG_DIR_DCCSI_TOOLS = str('Tools')
  95. TAG_DIR_O3DE_BUILD_FOLDER = str('build')
  96. TAG_O3DE_FOLDER = str('.o3de')
  97. TAG_O3DE_BOOTSTRAP = str('bootstrap.setreg')
  98. TAG_DCCSI_CONFIG = str('dccsi_configuration.setreg')
  99. from DccScriptingInterface.constants import DCCSI_SETTINGS_LOCAL_FILENAME
  100. # py path string, parts, etc.
  101. TAG_DEFAULT_PY = str('Launch_pyBASE.bat')
  102. # python related
  103. #python and site-dir
  104. TAG_DCCSI_PY_VERSION_MAJOR = str(3)
  105. TAG_DCCSI_PY_VERSION_MINOR = str(10)
  106. TAG_DCCSI_PY_VERSION_RELEASE = str(5)
  107. TAG_PYTHON_EXE = str('python.exe')
  108. TAG_TOOLS_DIR = str('Tools\\Python')
  109. TAG_PLATFORM = str('windows')
  110. TAG_PY_MAJOR = str(sys.version_info.major) # future proof
  111. TAG_PY_MINOR = str(sys.version_info.minor)
  112. # config file stuff
  113. FILENAME_DEFAULT_CONFIG = str('settings.json')
  114. # filesystem markers, stub file names.
  115. STUB_O3DE_DEV = str('engine.json')
  116. STUB_O3DE_BUILD = str('CMakeCache.txt')
  117. STUB_O3DE_ROOT_DCCSI = str('dccsi_stub')
  118. STUB_O3DE_DCCSI_AZPY = str('dccsi_azpy_stub')
  119. STUB_O3DE_DCCSI_TOOLS = str('dccsi_tools_stub')
  120. # -------------------------------------------------------------------------
  121. # -------------------------------------------------------------------------
  122. # JSON: constants used for parsing our dccsi json data
  123. # config string consts, Meta Qualifiers
  124. QUALIFIER_COMMENT = str('_meta_COMMENT')
  125. QUALIFIER_TEMPLATE = str('_meta_{0}_TEMPLATE')
  126. # config string consts, Section Qualifiers
  127. QUALIFIER_EXAMPLE_SECTION = str('_Example_Block_')
  128. QUALIFIER_INFO_SECTION = str('Info_Block')
  129. QUALIFIER_GLOBAL_SECTION = str('Global_Block')
  130. QUALIFIER_DEFAULT_ENV_SECTION = str('Default_Env_Block')
  131. QUALIFIER_SERVICES_SECTION = str('Services_Block')
  132. # config string consts, value, root and flags
  133. QUALIFIER_VALUE = str('value')
  134. QUALIFIER_ROOTPATH = str('rootPath')
  135. QUALIFIER_ENVROOT = str('envRoot')
  136. QUALIFIER_FLAGS = str('flags')
  137. # config action flags
  138. # the presence of a flag descriptor following a value block specifies a use case
  139. FLAG_PATH_ADDPYTHONSITEDIR = str('addPySiteDir')
  140. FLAG_PATH_ADDSYSPATH = str('addSysPath')
  141. FLAG_PATH_SETSYSPATH = str('setSysPath')
  142. FLAG_VAR_ADDENV = str('addEnv')
  143. FLAG_VAR_SETENV = str('setEnv')
  144. FLAG_PATH_ABSOLUTE = str('absolute')
  145. FLAG_PATH_RELATIVE = str('relative')
  146. FLAG_PROJECT_RELATIVE = str('project_relative')
  147. FLAG_PATH_ROOT = str('root')
  148. FLAG_PATH_BLOCKROOT = str('blockRoot')
  149. # -------------------------------------------------------------------------
  150. # -------------------------------------------------------------------------
  151. # ENV: constants, ENVARs, configuration and settings
  152. # O3DE
  153. # base env var key as str
  154. ENVAR_COMPANY = str('COMPANY')
  155. ENVAR_O3DE_BUILD_DIR_NAME = str('O3DE_BUILD_DIR_NAME')
  156. ENVAR_PATH_O3DE_BUILD = str('PATH_O3DE_BUILD')
  157. ENVAR_PATH_O3DE_BIN = str('PATH_O3DE_BIN')
  158. ENVAR_PATH_O3DE_PYTHON_INSTALL = str('PATH_O3DE_PYTHON_INSTALL')
  159. # MOVED to DccScriptingInterface __init__
  160. ENVAR_O3DE_PROJECT = str('O3DE_PROJECT') # project name
  161. ENVAR_PATH_O3DE_PROJECT = str('PATH_O3DE_PROJECT') # path to project
  162. ENVAR_O3DE_DEV = str('O3DE_DEV')
  163. # DCCSI
  164. from DccScriptingInterface.constants import ENVAR_PATH_DCCSIG
  165. ENVAR_DCCSI_AZPY_PATH = str('DCCSI_AZPY_PATH')
  166. ENVAR_PATH_DCCSI_TOOLS = str('PATH_DCCSI_TOOLS')
  167. ENVAR_DCCSI_LOG_PATH = str('DCCSI_LOG_PATH')
  168. ENVAR_DCCSI_LAUNCHERS_PATH = str('DCCSI_LAUNCHERS_PATH')
  169. ENVAR_DCCSI_SYS_PATH = str('DCCSI_SYS_PATH')
  170. ENVAR_DCCSI_PYTHONPATH = str('DCCSI_PYTHONPATH')
  171. # DCCsi Python related
  172. ENVAR_DCCSI_PY_VERSION_MAJOR = str('DCCSI_PY_VERSION_MAJOR')
  173. ENVAR_DCCSI_PY_VERSION_MINOR = str('DCCSI_PY_VERSION_MINOR')
  174. ENVAR_PATH_DCCSI_PYTHON = str('PATH_DCCSI_PYTHON')
  175. from DccScriptingInterface.constants import ENVAR_PATH_DCCSI_PYTHON_LIB
  176. ENVAR_DCCSI_PY_BASE = str('DCCSI_PY_BASE')
  177. ENVAR_DCCSI_PY_DCCSI = str('DCCSI_PY_DCCSI')
  178. ENVAR_DCCSI_PY_DEFAULT = str('DCCSI_PY_DEFAULT')
  179. # Qt / PySide2
  180. ENVAR_QT_PLUGIN_PATH = str('QT_PLUGIN_PATH')
  181. ENVAR_QTFORPYTHON_PATH = str('QTFORPYTHON_PATH')
  182. # -------------------------------------------------------------------------
  183. # -------------------------------------------------------------------------
  184. # dcc:(all) API constants
  185. # -------------------------------------------------------------------------
  186. # -------------------------------------------------------------------------
  187. # material API constants
  188. DCCSI_IMAGE_TYPES = ['.tif', '.tiff', '.png', '.jpg', '.jpeg', '.tga']
  189. # -------------------------------------------------------------------------
  190. # -------------------------------------------------------------------------
  191. # To Do: these really should not be here
  192. from DccScriptingInterface import O3DE_DEV
  193. from DccScriptingInterface import PATH_DCCSIG
  194. PATH_DCCSI_AZPY_PATH = str(return_stub_dir(STUB_O3DE_DCCSI_AZPY))
  195. # -------------------------------------------------------------------------
  196. # -------------------------------------------------------------------------
  197. # Path constructors and paths
  198. PATH_USER_O3DE = str(STR_USER_O3DE_PATH).format(home=PATH_USER_HOME,
  199. o3de=TAG_O3DE_FOLDER)
  200. PATH_DCCSI_TOOLS = str('{0}\\{1}'.format(PATH_DCCSIG, TAG_DIR_DCCSI_TOOLS))
  201. # logging into the user home o3de cache (temporarily)
  202. PATH_DCCSI_LOG_PATH = str('{PATH_O3DE_PROJECT}\\user\\log\{TAG_DCCSI_NICKNAME}')
  203. # dev \ <build> \
  204. STR_CONSTRUCT_PATH_O3DE_BUILD = str('{0}\\{1}')
  205. PATH_O3DE_BUILD = str(STR_CONSTRUCT_PATH_O3DE_BUILD.format(O3DE_DEV,
  206. TAG_DIR_O3DE_BUILD_FOLDER))
  207. # ENVAR_QT_PLUGIN_PATH = TAG_QT_PLUGIN_PATH
  208. STR_QTPLUGIN_DIR = str('{0}\\bin\\profile\\EditorPlugins')
  209. STR_QTFORPYTHON_PATH = str('{0}\\Gems\\QtForPython\\3rdParty\\pyside2\\windows\\release')
  210. STR_PATH_O3DE_BIN = str('{0}\\bin\\profile')
  211. STR_PATH_O3DE_BUILD = str('{0}\\{1}')
  212. PATH_O3DE_BUILD = STR_PATH_O3DE_BUILD.format(O3DE_DEV, TAG_DIR_O3DE_BUILD_FOLDER)
  213. PATH_QTFORPYTHON_PATH = str(STR_QTFORPYTHON_PATH.format(O3DE_DEV))
  214. PATH_QT_PLUGIN_PATH = str(STR_QTPLUGIN_DIR).format(PATH_O3DE_BUILD)
  215. PATH_O3DE_BIN = str(STR_PATH_O3DE_BIN).format(PATH_O3DE_BUILD)
  216. STR_USER_O3DE_REGISTRY_PATH = str('{0}\\{1}')
  217. PATH_USER_O3DE_REGISTRY = str(STR_USER_O3DE_REGISTRY_PATH).format(PATH_USER_O3DE, SLUG_DIR_REGISTRY)
  218. STR_USER_O3DE_BOOTSTRAP_PATH = str('{reg}\\{file}')
  219. PATH_USER_O3DE_BOOTSTRAP = str(STR_USER_O3DE_BOOTSTRAP_PATH).format(reg=PATH_USER_O3DE_REGISTRY,
  220. file=TAG_O3DE_BOOTSTRAP)
  221. STR_CONSTRUCT_PATH_O3DE_PYTHON_INSTALL = str('{0}\\{1}\\{2}.{3}.{4}\\{5}')
  222. PATH_DCCSI_PYTHON = str(STR_CONSTRUCT_PATH_O3DE_PYTHON_INSTALL.format(O3DE_DEV,
  223. TAG_TOOLS_DIR,
  224. TAG_DCCSI_PY_VERSION_MAJOR,
  225. TAG_DCCSI_PY_VERSION_MINOR,
  226. TAG_DCCSI_PY_VERSION_RELEASE,
  227. TAG_PLATFORM))
  228. PATH_DCCSI_PY_BASE = str('{0}\\{1}').format(PATH_DCCSI_PYTHON, TAG_PYTHON_EXE)
  229. PATH_DCCSI_PY_DEFAULT = PATH_DCCSI_PY_BASE
  230. # bootstrap site-packages by version
  231. from DccScriptingInterface import PATH_DCCSI_PYTHON_LIB
  232. PATH_SAT_INSTALL_PATH = str('{0}\\{1}\\{2}\\{3}\\{4}'
  233. ''.format(PATH_PROGRAMFILES_X64,
  234. 'Allegorithmic',
  235. 'Substance Automation Toolkit',
  236. 'Python API',
  237. 'install'))
  238. # -------------------------------------------------------------------------
  239. ###########################################################################
  240. # Main Code Block, runs this script as main (testing)
  241. # -------------------------------------------------------------------------
  242. if __name__ == '__main__':
  243. """Run this file as a standalone script"""
  244. # turn all of these off/on for testing
  245. DCCSI_GDEBUG = False
  246. DCCSI_DEV_MODE = False
  247. _DCCSI_LOGLEVEL = _logging.INFO
  248. DCCSI_GDEBUGGER = 'WING'
  249. # configure basic logger
  250. from azpy.constants import FRMT_LOG_LONG
  251. _logging.basicConfig(level=_DCCSI_LOGLEVEL,
  252. format=FRMT_LOG_LONG,
  253. datefmt='%m-%d %H:%M')
  254. _LOGGER = _logging.getLogger(_MODULENAME)
  255. _LOGGER.info(STR_CROSSBAR)
  256. _LOGGER.info(f'~ {_MODULENAME} ... Running script as __main__')
  257. _LOGGER.info(STR_CROSSBAR)
  258. # this is just a debug developer convenience print (for testing acess)
  259. import pkgutil
  260. _LOGGER.info(f'Current working dir: {os.getcwd()}')
  261. search_path = ['.'] # set to None to see all modules importable from sys.path
  262. all_modules = [x[1] for x in pkgutil.iter_modules(path=search_path)]
  263. _LOGGER.info(f'All Available Modules in working dir: {all_modules}')
  264. # test anything procedurally generated
  265. _LOGGER.info('Testing procedural env paths ...')
  266. from pathlib import Path
  267. _stash_dict = {}
  268. _stash_dict['O3DE_DEV'] = Path(O3DE_DEV)
  269. _stash_dict['PATH_DCCSIG'] = Path(PATH_DCCSIG)
  270. _stash_dict['DCCSI_AZPY_PATH'] = Path(PATH_DCCSI_AZPY_PATH)
  271. _stash_dict['PATH_DCCSI_TOOLS'] = Path(PATH_DCCSI_TOOLS)
  272. _stash_dict['PATH_DCCSI_PYTHON'] = Path(PATH_DCCSI_PYTHON)
  273. _stash_dict['DCCSI_PY_BASE'] = Path(PATH_DCCSI_PY_BASE)
  274. _stash_dict['PATH_DCCSI_PYTHON_LIB'] = Path(PATH_DCCSI_PYTHON_LIB)
  275. _stash_dict['PATH_O3DE_BUILD'] = Path(PATH_O3DE_BUILD)
  276. _stash_dict['PATH_O3DE_BIN'] = Path(PATH_O3DE_BIN)
  277. _stash_dict['QTFORPYTHON_PATH'] = Path(PATH_QTFORPYTHON_PATH)
  278. _stash_dict['QT_PLUGIN_PATH'] = Path(PATH_QT_PLUGIN_PATH)
  279. _stash_dict['SAT_INSTALL_PATH'] = Path(PATH_SAT_INSTALL_PATH)
  280. _stash_dict['PATH_USER_O3DE_BOOTSTRAP'] = Path(PATH_USER_O3DE_BOOTSTRAP)
  281. # ---------------------------------------------------------------------
  282. # py 2 and 3 compatible iter
  283. def get_items(dict_object):
  284. for key in dict_object:
  285. yield key, dict_object[key]
  286. for key, value in get_items(_stash_dict):
  287. # check if path exists
  288. try:
  289. value.exists()
  290. _LOGGER.info(F'{key}: {value}')
  291. except Exception as e:
  292. _LOGGER.warning(f'FAILED PATH: {e}')
  293. # custom prompt
  294. sys.ps1 = f"[{_MODULENAME}]>>"
  295. _LOGGER.debug(f'{_MODULENAME} took: {(timeit.default_timer() - _MODULE_START)} sec')
  296. # --- END -----------------------------------------------------------------