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.

run.py 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. import os
  20. import os.path
  21. import warnings
  22. #This file expose common function to process a wsgi request and the
  23. #uWSGI application callback
  24. #preloading all instances
  25. FAST_APP_EXPOSAL_CACHE = dict()
  26. LODEL2_INSTANCES_DIR = '.'
  27. EXCLUDE_DIR = {'conf.d', '__pycache__'}
  28. try:
  29. from lodel.context import LodelContext
  30. except ImportError:
  31. LODEL_BASE_DIR = os.path.dirname(
  32. os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  33. from lodel.context import LodelContext, ContextError
  34. LodelContext.init(LodelContext.MULTISITE)
  35. LodelContext.set(None) #Loading context creation
  36. #Multisite instance settings loading
  37. CONFDIR = os.path.join(os.getcwd(), 'conf.d')
  38. if not os.path.isdir(CONFDIR):
  39. warnings.warn('%s do not exists, default settings used' % CONFDIR)
  40. LodelContext.expose_modules(globals(), {
  41. 'lodel.settings.settings': [('Settings', 'settings')],
  42. 'lodel.plugins.multisite.confspecs': 'multisite_confspecs'})
  43. if not settings.started():
  44. settings('./conf.d', multisite_confspecs.LODEL2_CONFSPECS)
  45. #Fetching insrtance list from subdirectories
  46. lodelsites_list = [ os.path.realpath(os.path.join(LODEL2_INSTANCES_DIR,sitename))
  47. for sitename in os.listdir(LODEL2_INSTANCES_DIR)
  48. if os.path.isdir(sitename) and sitename not in EXCLUDE_DIR]
  49. #Bootstraping instances
  50. for lodelsite_path in lodelsites_list:
  51. ctx_name = LodelContext.from_path(lodelsite_path)
  52. #Switch to new context
  53. LodelContext.set(ctx_name)
  54. os.chdir(lodelsite_path)
  55. # Loading settings
  56. LodelContext.expose_modules(globals(), {
  57. 'lodel.settings.settings': [('Settings', 'settings')]})
  58. if not settings.started():
  59. settings('./conf.d')
  60. LodelContext.expose_modules(globals(), {'lodel.settings': ['Settings']})
  61. # Loading hooks & plugins
  62. LodelContext.expose_modules(globals(), {
  63. 'lodel.plugin': ['LodelHook'],
  64. 'lodel.plugin.core_hooks': 'core_hooks',
  65. 'lodel.plugin.core_scripts': 'core_scripts'
  66. })
  67. #Load plugins
  68. LodelContext.expose_modules(globals(), {
  69. 'lodel.logger': 'logger',
  70. 'lodel.plugin': ['Plugin']})
  71. logger.debug("Loader.start() called")
  72. Plugin.load_all()
  73. #Import & expose dyncode
  74. LodelContext.expose_dyncode(globals())
  75. #Next hook triggers dyncode datasource instanciations
  76. LodelHook.call_hook('lodel2_plugins_loaded', '__main__', None)
  77. #Next hook triggers call of interface's main loop
  78. LodelHook.call_hook('lodel2_bootstraped', '__main__', None)
  79. #FAST_APP_EXPOSAL_CACHE populate
  80. FAST_APP_EXPOSAL_CACHE[ctx_name] = LodelContext.module(
  81. 'lodel.plugins.webui.run')
  82. LodelContext
  83. #a dirty & quick attempt to fix context unwanted exite via
  84. #hooks
  85. for name in ( 'LodelHook', 'core_hooks', 'core_scripts',
  86. 'Settings', 'settings', 'logger', 'Plugin'):
  87. del(globals()[name])
  88. #switch back to loader context
  89. LodelContext.set(None)
  90. #
  91. # From here lodel2 multisite instances are loaded and ready to run
  92. #
  93. ##@brief Utility function to return quickly an error
  94. def http_error(env, start_response, status = '500 internal server error', \
  95. extra = None):
  96. headers = [('Content-type', 'text/plain; charset=utf-8')]
  97. start_response(status, headers)
  98. msg = status
  99. if extra is not None:
  100. msg = extra
  101. return [msg.encode('utf-8')]
  102. ##@brief utility function to extract site id from an url
  103. #@param url str :
  104. def site_id_from_url(url):
  105. res = ''
  106. for c in url[1:]:
  107. if c == '/':
  108. break
  109. res += c
  110. if len(res) == 0:
  111. return None
  112. return res
  113. ##@brief This method is run in a child process by the handler
  114. def application(env, start_response):
  115. #Attempt to load a context
  116. site_id = site_id_from_url(env['PATH_INFO'])
  117. if site_id is None:
  118. #It can be nice to provide a list of instances here
  119. return http_error(env, start_response, '404 Not Found')
  120. try:
  121. LodelContext.set(site_id)
  122. #We are in the good context
  123. except ContextError as e:
  124. print(e)
  125. return http_error(env, start_response, '404 Not found',
  126. "No site named '%s'" % site_id)
  127. #Calling webui
  128. return FAST_APP_EXPOSAL_CACHE[site_id].application(env, start_response)
  129. #LodelContext.expose_modules(globals(), {
  130. # 'lodel.plugins.webui.run': ['application']})
  131. #return application(env, start_response)