No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

loader.py 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #
  2. # This file is part of Lodel 2 (https://github.com/OpenEdition)
  3. #
  4. # Copyright (C) 2015-2017 Cléo UMS-3287
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as published
  8. # by the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. ##@brief Lodel2 loader script
  20. #
  21. #@note If you want to avoid settings loading you can set the environment
  22. #variable LODEL2_NO_SETTINGS_LOAD (see @ref install.lodel_admin.update_plugin_discover_cache()
  23. #
  24. # @note In tests case, you can pass the path to write results file, context_tests.log
  25. # It has to be at first, otherwise it will not be taken
  26. # and the default one, current directory, will be used.
  27. import sys, os, os.path
  28. #
  29. # Bootstraping
  30. #
  31. LODEL2_LIB_ABS_PATH = None
  32. if LODEL2_LIB_ABS_PATH is not None:
  33. if not os.path.isdir(LODEL2_LIB_ABS_PATH):
  34. print("FATAL ERROR : the LODEL2_LIB_ABS_PATH variable in loader.py is \
  35. not correct : '%s'" % LODEL2_LIB_ABS_PATH, file=sys.stderr)
  36. sys.path.append(LODEL2_LIB_ABS_PATH)
  37. try:
  38. import lodel
  39. except ImportError as e:
  40. print("Unable to load lodel module. exiting...")
  41. print(e)
  42. exit(1)
  43. #Set context to MONOSITE
  44. from lodel.context import LodelContext
  45. LodelContext.init()
  46. if 'LODEL2_NO_SETTINGS_LOAD' not in os.environ:
  47. #
  48. # Loading settings
  49. #
  50. LodelContext.expose_modules(globals(), {
  51. 'lodel.settings.settings': [('Settings', 'settings')]})
  52. if not settings.started():
  53. settings('conf.d')
  54. LodelContext.expose_modules(globals(), {
  55. 'lodel.settings': ['Settings']})
  56. #Starts hooks
  57. LodelContext.expose_modules(globals(), {
  58. 'lodel.plugin': ['LodelHook'],
  59. 'lodel.plugin.core_hooks': 'core_hooks',
  60. 'lodel.plugin.core_scripts': 'core_scripts'})
  61. def start():
  62. #Load plugins
  63. LodelContext.expose_modules(globals(), {
  64. 'lodel.logger': 'logger',
  65. 'lodel.plugin': ['Plugin']})
  66. logger.debug("Loader.start() called")
  67. Plugin.load_all()
  68. LodelHook.call_hook('lodel2_bootstraped', '__main__', None)
  69. if __name__ == '__main__':
  70. start()
  71. if Settings.runtest:
  72. import unittest
  73. import tests
  74. loader = unittest.TestLoader()
  75. test_dir = os.path.join(LODEL2_LIB_ABS_PATH, 'tests')
  76. suite = loader.discover(test_dir, pattern='test*.py')
  77. if ((len(sys.argv) > 1) and (sys.argv[1].startswith('-')) is False):
  78. dpath = sys.argv[1]
  79. else:
  80. dpath = '.'
  81. with open(sys.argv[1]+'/context_tests.log', 'w') as logfile:
  82. tests_res = unittest.TextTestRunner(
  83. logfile,
  84. failfast = '-f' in sys.argv,
  85. verbosity = 2 if '-v' in sys.argv else 1).run(suite)
  86. if tests_res.wasSuccessful():
  87. exit(0)
  88. else:
  89. exit(1)
  90. lodel = LodelContext.get()
  91. import leapi_dyncode as dyncode
  92. lodel.dyncode = dyncode
  93. LodelHook.call_hook('lodel2_dyncode_bootstraped', '__main__', None)
  94. LodelHook.call_hook('lodel2_loader_main', '__main__', None)
  95. #Run interative python
  96. import code
  97. print("""
  98. Running interactive python in Lodel2 %s instance environment
  99. """%Settings.sitename)
  100. code.interact(local=locals())