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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #-*- coding: utf-8 -*-
  2. import sys
  3. import os, os.path
  4. import argparse
  5. ##@brief Dirty hack to avoid problems with simlink to lodel2 lib folder
  6. #
  7. #In instance folder we got a loader.py (the one we want to import here when
  8. #writing "import loader". The problem is that lodel_admin.py is a simlink to
  9. #LODEL2LIB_FOLDER/install/lodel_admin.py . In this folder there is the
  10. #generic loader.py template. And when writing "import loader" its
  11. #LODEL2LIB_FOLDER/install/loader.py that gets imported.
  12. #
  13. #In order to solve this problem the _simlink_hack() function delete the
  14. #LODEL2LIB_FOLDER/install entry from sys.path
  15. #@note This problem will be solved once lodel lib dir will be in
  16. #/usr/lib/python3/lodel
  17. def _simlink_hack():
  18. sys.path[0] = os.getcwd()
  19. ## @brief Utility method to generate python code given an emfile and a
  20. # translator
  21. # @param model_file str : An em file
  22. # @param translator str : a translator name
  23. # @return python code as string
  24. def generate_dyncode(model_file, translator):
  25. from lodel.editorial_model.model import EditorialModel
  26. from lodel.leapi import lefactory
  27. model = EditorialModel.load(translator, filename = model_file)
  28. dyncode = lefactory.dyncode_from_em(model)
  29. return dyncode
  30. ## @brief Utility method to generate a python file representing leapi dyncode
  31. # given an em file and the associated translator name
  32. #
  33. # @param model_file str : An em file
  34. # @param translator str : a translator name
  35. # @param output_filename str : the output file
  36. def create_dyncode(model_file, translator, output_filename):
  37. from lodel import logger
  38. dyncode = generate_dyncode(model_file, translator)
  39. with open(output_filename, 'w+') as out_fd:
  40. out_fd.write(dyncode)
  41. out_fd.close()
  42. logger.info("Dynamic leapi code written in %s", output_filename)
  43. ## @brief Refresh dynamic leapi code from settings
  44. def refresh_dyncode():
  45. import loader
  46. from lodel.settings import Settings
  47. # EditorialModel update/refresh
  48. # TODO
  49. # Dyncode refresh
  50. create_dyncode( Settings.editorialmodel.emfile,
  51. Settings.editorialmodel.emtranslator,
  52. Settings.editorialmodel.dyncode)
  53. def init_all_dbs():
  54. import loader
  55. loader.start()
  56. import leapi_dyncode as dyncode
  57. from lodel.settings.utils import SettingsError
  58. from lodel.leapi.leobject import LeObject
  59. from lodel.plugin import Plugin
  60. from lodel import logger
  61. ds_cls = dict() # EmClass indexed by rw_datasource
  62. for cls in dyncode.dynclasses:
  63. ds = cls._datasource_name
  64. if ds not in ds_cls:
  65. ds_cls[ds] = [cls]
  66. else:
  67. ds_cls[ds].append(cls)
  68. for ds_name in ds_cls:
  69. # Fetching datasource plugin name and datasource connection
  70. # identifier
  71. try:
  72. plugin_name, ds_identifier = LeObject._get_ds_plugin_name(
  73. ds_name, False)
  74. except (NameError, ValueError, RuntimeError):
  75. raise SettingsError("Datasource configuration error")
  76. # Fetching datasource connection option
  77. con_conf=LeObject._get_ds_connection_conf(ds_identifier, plugin_name)
  78. # Fetching migration handler class from plugin
  79. plugin_module = Plugin.get(plugin_name).loader_module()
  80. try:
  81. mh_cls = plugin_module.migration_handler_class()
  82. except NameError as e:
  83. raise RuntimeError("Malformed plugin '%s'. Missing \
  84. migration_handler_class() function in loader file" % ds_name)
  85. #Instanciate the migrationhandler and start db initialisation
  86. if con_conf['read_only'] is True:
  87. raise SettingsError("Trying to instanciate a migration handler \
  88. with a read only datasource")
  89. try:
  90. if 'read_only' in con_conf:
  91. del(con_conf['read_only'])
  92. mh = mh_cls(**con_conf)
  93. except Exception as e:
  94. msg = "Migration failed for datasource %s(%s.%s) at migration \
  95. handler instanciation : %s"
  96. msg %= (ds_name, plugin_name, ds_identifier, e)
  97. raise RuntimeError(msg)
  98. try:
  99. mh.init_db(ds_cls[ds_name])
  100. except Exception as e:
  101. msg = "Migration failed for datasource %s(%s.%s) when running \
  102. init_db method: %s"
  103. msg %= (ds_name, plugin_name, ds_identifier, e)
  104. logger.info("Database initialisation done for %s(%s.%s)" % (
  105. ds_name, plugin_name, ds_identifier))
  106. def list_registered_hooks():
  107. import loader
  108. loader.start()
  109. from lodel.plugin.hooks import LodelHook
  110. hlist = LodelHook.hook_list()
  111. print("Registered hooks are : ")
  112. for name in sorted(hlist.keys()):
  113. print("\t- %s is registered by : " % name)
  114. for reg_hook in hlist[name]:
  115. hook, priority = reg_hook
  116. msg = "\t\t- {modname}.{funname} with priority : {priority}"
  117. msg = msg.format(
  118. modname = hook.__module__,
  119. funname = hook.__name__,
  120. priority = priority)
  121. print(msg)
  122. print("\n")
  123. ##@brief update plugin's discover cache
  124. #@note impossible to give arguments from a Makefile...
  125. #@todo write a __main__ to be able to run ./lodel_admin
  126. def update_plugin_discover_cache(path_list = None):
  127. os.environ['LODEL2_NO_SETTINGS_LOAD'] = 'True'
  128. import loader
  129. from lodel.plugin.plugins import Plugin
  130. res = Plugin.discover(path_list)
  131. print("Plugin discover result in %s :\n" % res['path_list'])
  132. for pname, pinfos in res['plugins'].items():
  133. print("\t- %s %s -> %s" % (
  134. pname, pinfos['version'], pinfos['path']))
  135. if __name__ == '__main__':
  136. _simlink_hack()
  137. import loader
  138. loader.start()
  139. from lodel.plugin.scripts import main_run
  140. main_run()