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.

lodel_admin.py 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #!/usr/bin/env python3
  2. #
  3. # This file is part of Lodel 2 (https://github.com/OpenEdition)
  4. #
  5. # Copyright (C) 2015-2017 Cléo UMS-3287
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as published
  9. # by the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. import sys
  21. import os, os.path
  22. import argparse
  23. ##@brief Dirty hack to avoid problems with simlink to lodel2 lib folder
  24. #
  25. #In instance folder we got a loader.py (the one we want to import here when
  26. #writing "import loader". The problem is that lodel_admin.py is a simlink to
  27. #LODEL2LIB_FOLDER/install/lodel_admin.py . In this folder there is the
  28. #generic loader.py template. And when writing "import loader" its
  29. #LODEL2LIB_FOLDER/install/loader.py that gets imported.
  30. #
  31. #In order to solve this problem the _simlink_hack() function delete the
  32. #LODEL2LIB_FOLDER/install entry from sys.path
  33. #@note This problem will be solved once lodel lib dir will be in
  34. #/usr/lib/python3/lodel
  35. def _simlink_hack():
  36. sys.path[0] = os.getcwd()
  37. ## @brief Utility method to generate python code given an emfile and a
  38. # translator
  39. # @param model_file str : An em file
  40. # @param translator str : a translator name
  41. # @return python code as string
  42. def generate_dyncode(model_file, translator):
  43. from lodel.editorial_model.model import EditorialModel
  44. from lodel.leapi import lefactory
  45. model = EditorialModel.load(translator, filename = model_file)
  46. dyncode = lefactory.dyncode_from_em(model)
  47. return dyncode
  48. ## @brief Utility method to generate a python file representing leapi dyncode
  49. # given an em file and the associated translator name
  50. #
  51. # @param model_file str : An em file
  52. # @param translator str : a translator name
  53. # @param output_filename str : the output file
  54. def create_dyncode(model_file, translator, output_filename):
  55. from lodel import logger
  56. dyncode = generate_dyncode(model_file, translator)
  57. with open(output_filename, 'w+') as out_fd:
  58. out_fd.write(dyncode)
  59. out_fd.close()
  60. logger.info("Dynamic leapi code written in %s", output_filename)
  61. ## @brief Refresh dynamic leapi code from settings
  62. def refresh_dyncode():
  63. import loader
  64. from lodel.settings import Settings
  65. # EditorialModel update/refresh
  66. # TODO
  67. # Dyncode refresh
  68. create_dyncode( Settings.editorialmodel.emfile,
  69. Settings.editorialmodel.emtranslator,
  70. Settings.editorialmodel.dyncode)
  71. def init_all_dbs():
  72. import loader
  73. loader.start()
  74. import leapi_dyncode as dyncode
  75. from lodel.plugin.datasource_plugin import DatasourcePlugin
  76. from lodel.settings.utils import SettingsError
  77. from lodel.leapi.leobject import LeObject
  78. from lodel.plugin import Plugin
  79. from lodel import logger
  80. ds_cls = dict() # EmClass indexed by rw_datasource
  81. for cls in dyncode.dynclasses:
  82. ds = cls._datasource_name
  83. if ds not in ds_cls:
  84. ds_cls[ds] = [cls]
  85. else:
  86. ds_cls[ds].append(cls)
  87. for ds_name in ds_cls:
  88. mh = DatasourcePlugin.init_migration_handler(ds_name)
  89. #Retrieve plugin_name & ds_identifier for logging purpose
  90. plugin_name, ds_identifier = DatasourcePlugin.plugin_name(
  91. ds_name, False)
  92. try:
  93. mh.init_db(ds_cls[ds_name])
  94. except Exception as e:
  95. msg = "Migration failed for datasource %s(%s.%s) when running \
  96. init_db method: %s"
  97. msg %= (ds_name, plugin_name, ds_identifier, e)
  98. logger.info("Database initialisation done for %s(%s.%s)" % (
  99. ds_name, plugin_name, ds_identifier))
  100. def list_registered_hooks():
  101. import loader
  102. loader.start()
  103. from lodel.plugin.hooks import LodelHook
  104. hlist = LodelHook.hook_list()
  105. print("Registered hooks are : ")
  106. for name in sorted(hlist.keys()):
  107. print("\t- %s is registered by : " % name)
  108. for reg_hook in hlist[name]:
  109. hook, priority = reg_hook
  110. msg = "\t\t- {modname}.{funname} with priority : {priority}"
  111. msg = msg.format(
  112. modname = hook.__module__,
  113. funname = hook.__name__,
  114. priority = priority)
  115. print(msg)
  116. print("\n")
  117. ##@brief update plugin's discover cache
  118. #@note impossible to give arguments from a Makefile...
  119. #@todo write a __main__ to be able to run ./lodel_admin
  120. def update_plugin_discover_cache(path_list = None):
  121. os.environ['LODEL2_NO_SETTINGS_LOAD'] = 'True'
  122. import loader
  123. from lodel.plugin.plugins import Plugin
  124. res = Plugin.discover(path_list)
  125. print("Plugin discover result in %s :\n" % res['path_list'])
  126. for pname, pinfos in res['plugins'].items():
  127. print("\t- %s %s -> %s" % (
  128. pname, pinfos['version'], pinfos['path']))
  129. if __name__ == '__main__':
  130. _simlink_hack()
  131. import loader
  132. loader.start()
  133. from lodel.plugin.scripts import main_run
  134. main_run()