env_base.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. # -- This line is 75 characters -------------------------------------------
  11. from __future__ import unicode_literals
  12. '''
  13. Module: <DCCsi>\\azpy\\shared\\common\\base_env.py
  14. This module packs the most basic set of environment variables.
  15. Easy access IF you know the environment is setup.
  16. We assume these are already set up in the envionment.
  17. The str() tag for each ENVAR is defined in azpy.shared.common.constants
  18. Allowing those str('tag') to easily be changed in a single location.
  19. If they are paths for code acess we assume they were put on the sys path.
  20. '''
  21. # -------------------------------------------------------------------------
  22. # built in's
  23. import os
  24. import sys
  25. import json
  26. import logging as _logging
  27. from collections import OrderedDict
  28. # 3rd Party
  29. from pathlib import Path
  30. # Lumberyard extensions
  31. from azpy.constants import *
  32. from azpy.shared.common.core_utils import return_stub
  33. from azpy.shared.common.core_utils import get_stub_check_path
  34. from azpy.shared.common.envar_utils import get_envar_default
  35. from azpy.shared.common.envar_utils import set_envar_defaults
  36. from azpy.shared.common.envar_utils import Validate_Envar
  37. from azpy.env_bool import env_bool
  38. from azpy.constants import ENVAR_DCCSI_GDEBUG
  39. from azpy.constants import ENVAR_DCCSI_DEV_MODE
  40. # -------------------------------------------------------------------------
  41. # -------------------------------------------------------------------------
  42. _PACKAGENAME = 'azpy.env_base'
  43. _logging.basicConfig(level=_logging.INFO,
  44. format=FRMT_LOG_LONG,
  45. datefmt='%m-%d %H:%M')
  46. _LOGGER = _logging.getLogger(_PACKAGENAME)
  47. _LOGGER.debug('Initializing: {0}.'.format({_PACKAGENAME}))
  48. # global space
  49. _DCCSI_GDEBUG = env_bool(ENVAR_DCCSI_GDEBUG, False)
  50. _DCCSI_DEV_MODE = env_bool(ENVAR_DCCSI_DEV_MODE, False)
  51. # set up base totally non-functional defauls (denoted with $<ENVAR>)
  52. # if something hasn't been set, it will stay '$<envar>'
  53. _BASE_ENVVAR_DICT = OrderedDict()
  54. # project tag
  55. _BASE_ENVVAR_DICT[ENVAR_O3DE_PROJECT] = '${0}'.format(ENVAR_O3DE_PROJECT)
  56. # paths
  57. _BASE_ENVVAR_DICT[ENVAR_O3DE_DEV] = '${0}'.format(ENVAR_O3DE_DEV)
  58. _BASE_ENVVAR_DICT[ENVAR_PATH_O3DE_PROJECT] = '${0}'.format(ENVAR_PATH_O3DE_PROJECT)
  59. _BASE_ENVVAR_DICT[ENVAR_PATH_DCCSIG] = '${0}'.format(ENVAR_PATH_DCCSIG)
  60. _BASE_ENVVAR_DICT[ENVAR_DCCSI_LOG_PATH] = '${0}'.format(ENVAR_DCCSI_LOG_PATH)
  61. _BASE_ENVVAR_DICT[ENVAR_DCCSI_AZPY_PATH] = '${0}'.format(ENVAR_DCCSI_AZPY_PATH)
  62. _BASE_ENVVAR_DICT[ENVAR_PATH_DCCSI_TOOLS] = '${0}'.format(ENVAR_PATH_DCCSI_TOOLS)
  63. # dev env flags
  64. _BASE_ENVVAR_DICT[ENVAR_DCCSI_GDEBUG] = '${0}'.format(ENVAR_DCCSI_GDEBUG)
  65. _BASE_ENVVAR_DICT[ENVAR_DCCSI_DEV_MODE] = '${0}'.format(ENVAR_DCCSI_DEV_MODE)
  66. _BASE_ENVVAR_DICT[ENVAR_DCCSI_GDEBUGGER] = '${0}'.format(ENVAR_DCCSI_GDEBUGGER)
  67. # default python dist
  68. _BASE_ENVVAR_DICT[ENVAR_DCCSI_PY_VERSION_MAJOR] = '${0}'.format(ENVAR_DCCSI_PY_VERSION_MAJOR)
  69. _BASE_ENVVAR_DICT[ENVAR_DCCSI_PY_VERSION_MINOR] = '${0}'.format(ENVAR_DCCSI_PY_VERSION_MINOR)
  70. _BASE_ENVVAR_DICT[ENVAR_PATH_DCCSI_PYTHON] = '${0}'.format(ENVAR_PATH_DCCSI_PYTHON)
  71. _BASE_ENVVAR_DICT[ENVAR_PATH_DCCSI_PYTHON_LIB] = '${0}'.format(ENVAR_PATH_DCCSI_PYTHON_LIB)
  72. # try to fetch and set the base values from the environment
  73. # this makes sure all envars set, are resolved on import
  74. _BASE_ENVVAR_DICT = set_envar_defaults(_BASE_ENVVAR_DICT)
  75. # If they are not set in the environment they should reamin the default
  76. # value assigned above in the pattern $<SOME_ENVAR>
  77. # -------------------------------------------------------------------------
  78. ###########################################################################
  79. # Main Code Block, runs this script as main (testing)
  80. # -------------------------------------------------------------------------
  81. if __name__ == '__main__':
  82. # srun simple tests?
  83. test = True
  84. # happy print
  85. _LOGGER.info("# {0} #".format('-' * 72))
  86. _LOGGER.info('~ config_utils.py ... Running script as __main__')
  87. _LOGGER.info("# {0} #\r".format('-' * 72))
  88. # print(setEnvarDefaults(), '\r') #<-- not necissary, already called
  89. # print(BASE_ENVVAR_VALUES, '\r')
  90. _LOGGER.info('Pretty print: _BASE_ENVVAR_DICT')
  91. _LOGGER.debug(json.dumps(_BASE_ENVVAR_DICT,
  92. indent=4, sort_keys=False,
  93. ensure_ascii=False), '\r')
  94. # retreive a Path type key from the Box
  95. foo = _BASE_ENVVAR_DICT[ENVAR_O3DE_DEV]
  96. _LOGGER.info('~ foo is: {0}'.format(type(foo), foo))
  97. # simple tests
  98. _ENV_TAG = 'O3DE_DEV'
  99. foo = get_envar_default(_ENV_TAG)
  100. _LOGGER.info("~ Results of getVar on tag, '{0}':'{1}'\r".format(_ENV_TAG, foo))
  101. envar_value = Validate_Envar(envar=_ENV_TAG)
  102. _LOGGER.info('~ Repr is: {0}\r'.format(str(repr(envar_value))))
  103. _LOGGER.info("~ Results of ValidateEnvar(envar='{0}')=='{1}'\r".format(_ENV_TAG, envar_value))
  104. # custom prompt
  105. sys.ps1 = "[azpy]>>"