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.

plugins.py 33KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. #-*- coding: utf-8 -*-
  2. import sys
  3. import os.path
  4. import importlib
  5. import copy
  6. import json
  7. from importlib.machinery import SourceFileLoader, SourcelessFileLoader
  8. import plugins
  9. from lodel import logger
  10. from lodel.settings.utils import SettingsError
  11. from .exceptions import *
  12. from lodel.exceptions import *
  13. ## @package lodel.plugins Lodel2 plugins management
  14. #
  15. # Lodel2 plugins are stored in directories
  16. # A typicall lodel2 plugin directory structure looks like :
  17. # - {{__init__.py}}} containing informations like full_name, authors, licence etc.
  18. # - main.py containing hooks registration etc
  19. # - confspec.py containing a configuration specification dictionary named CONFSPEC
  20. ##@brief The package in which we will load plugins modules
  21. VIRTUAL_PACKAGE_NAME = 'lodel.plugins'
  22. ##@brief The temporary package to import python sources
  23. VIRTUAL_TEMP_PACKAGE_NAME = 'lodel.plugin_tmp'
  24. ##@brief Plugin init filename
  25. INIT_FILENAME = '__init__.py' # Loaded with settings
  26. PLUGIN_NAME_VARNAME = '__plugin_name__'
  27. PLUGIN_TYPE_VARNAME = '__plugin_type__'
  28. PLUGIN_VERSION_VARNAME = '__version__'
  29. CONFSPEC_FILENAME_VARNAME = '__confspec__'
  30. CONFSPEC_VARNAME = 'CONFSPEC'
  31. LOADER_FILENAME_VARNAME = '__loader__'
  32. PLUGIN_DEPS_VARNAME = '__plugin_deps__'
  33. ACTIVATE_METHOD_NAME = '_activate'
  34. ##@brief Discover stage cache filename
  35. DISCOVER_CACHE_FILENAME = '.plugin_discover_cache.json'
  36. ##@brief Default & failover value for plugins path list
  37. DEFAULT_PLUGINS_PATH_LIST = ['./plugins']
  38. MANDATORY_VARNAMES = [PLUGIN_NAME_VARNAME, LOADER_FILENAME_VARNAME,
  39. PLUGIN_VERSION_VARNAME]
  40. DEFAULT_PLUGIN_TYPE = 'extension'
  41. PLUGINS_TYPES = [DEFAULT_PLUGIN_TYPE, 'datasource', 'session_handler', 'ui']
  42. ##@brief Describe and handle version numbers
  43. class PluginVersion(object):
  44. PROPERTY_LIST = ['major', 'minor', 'revision' ]
  45. ##@brief Version constructor
  46. #@param *args : You can either give a str that will be splitted on . or you
  47. #can give a iterable containing 3 integer or 3 arguments representing
  48. #major, minor and revision version
  49. def __init__(self, *args):
  50. self.__version = [0 for _ in range(3) ]
  51. if len(args) == 1:
  52. arg = args[0]
  53. if isinstance(arg, str):
  54. spl = arg.split('.')
  55. invalid = False
  56. if len(spl) > 3:
  57. raise PluginError("The string '%s' is not a valid plugin \
  58. version number" % arg)
  59. else:
  60. try:
  61. if len(arg) >= 1:
  62. if len(arg) > 3:
  63. raise PluginError("Expected maximum 3 value to \
  64. create a plugin version number but found '%s' as argument" % arg)
  65. for i, v in enumerate(arg):
  66. self.__version[i] = arg[i]
  67. except TypeError:
  68. raise PluginError("Unable to convert argument into plugin \
  69. version number" % arg)
  70. elif len(args) > 3:
  71. raise PluginError("Expected between 1 and 3 positional arguments \
  72. but %d arguments found" % len(args))
  73. else:
  74. for i,v in enumerate(args):
  75. self.__version[i] = v
  76. @property
  77. def major(self):
  78. return self.__version[0]
  79. @property
  80. def minor(self):
  81. return self.__version[1]
  82. @property
  83. def revision(self):
  84. return self.__version[2]
  85. ##@brief Check and prepare comparisoon argument
  86. #@return A PluginVersion instance
  87. #@throw PluginError if invalid argument provided
  88. def __cmp_check(self, other):
  89. if not isinstance(other, PluginVersion):
  90. try:
  91. if len(other) <= 3 and len(other) > 0:
  92. return PluginVersion(other)
  93. except TypeError:
  94. raise PluginError("Cannot compare argument '%s' with \
  95. a PluginVerison instance" % other)
  96. return other
  97. ##@brief Generic comparison function
  98. #@param other PluginVersion or iterable
  99. #@param cmp_fun_name function : interger comparison function
  100. def __generic_cmp(self, other, cmp_fun_name):
  101. other = self.__cmp_check(other)
  102. try:
  103. cmpfun = getattr(int, cmp_fun_name)
  104. except AttributeError:
  105. raise LodelFatalError("Invalid comparison callback given \
  106. to generic PluginVersion comparison function : '%s'" % cmp_fun_name)
  107. for property_name in self.PROPERTY_LIST:
  108. if not cmpfun(getattr(self, pname), getattr(other, pname)):
  109. return False
  110. return True
  111. def __lt__(self, other):
  112. return self.__generic_cmp(other, '__lt__')
  113. def __le__(self, other):
  114. return self.__generic_cmp(other, '__le__')
  115. def __eq__(self, other):
  116. return self.__generic_cmp(other, '__eq__')
  117. def __ne__(self, other):
  118. return self.__generic_cmp(other, '__ne__')
  119. def __gt__(self, other):
  120. return self.__generic_cmp(other, '__gt__')
  121. def __ge__(self, other):
  122. return self.__generic_cmp(other, '__ge__')
  123. def __str__(self):
  124. return '%d.%d.%d' % tuple(self.__version)
  125. def __repr__(self):
  126. return {'major': self.major, 'minor': self.minor,
  127. 'revision': self.revision}
  128. ##@brief Stores plugin class registered
  129. __all_ptypes = list()
  130. ##@brief Plugin metaclass that allows to "catch" child class
  131. #declaration
  132. #
  133. #Automatic script registration on child class declaration
  134. class MetaPlugType(type):
  135. def __init__(self, name, bases, attrs):
  136. #Here we can store all child classes of Plugin
  137. super().__init__(name, bases, attrs)
  138. if len(bases) == 1 and bases[0] == object:
  139. return
  140. self.__register_types()
  141. #list_name= [cls.__name__ for cls in __all_ptypes]
  142. #if self.name in list_name:
  143. # return
  144. #else:
  145. # plug_type_register(self)
  146. def __register_types(self):
  147. plug_type_register(self)
  148. def plug_type_register(cls):
  149. __all_ptypes.append(cls)
  150. logger.info("New child class registered : %s" % cls.__name__)
  151. def all_types():
  152. return copy.copy(__all_ptypes)
  153. ##@brief Handle plugins
  154. #
  155. # An instance represent a loaded plugin. Class methods allow to load/preload
  156. # plugins.
  157. #
  158. # Typicall Plugins load sequence is :
  159. # 1. Settings call start method to instanciate all plugins found in confs
  160. # 2. Settings fetch all confspecs
  161. # 3. the loader call load_all to register hooks etc
  162. class Plugin(object, metaclass=MetaPlugType):
  163. ##@brief Stores plugin directories paths
  164. _plugin_directories = None
  165. ##@brief Stores Plugin instances indexed by name
  166. _plugin_instances = dict()
  167. ##@brief Attribute used by load_all and load methods to detect circular
  168. #dependencies
  169. _load_called = []
  170. ##@brief Attribute that stores plugins list from discover cache file
  171. _plugin_list = None
  172. ##@brief Store dict representation of discover cache content
  173. _discover_cache = None
  174. #@brief Designed to store, in child classes, the confspec indicating \
  175. #where plugin list is stored
  176. _plist_confspecs = None
  177. ##@brief Plugin class constructor
  178. #
  179. # Called by setting in early stage of lodel2 boot sequence using classmethod
  180. # register
  181. #
  182. # @param plugin_name str : plugin name
  183. # @throw PluginError
  184. def __init__(self, plugin_name):
  185. self.name = plugin_name
  186. self.path = self.plugin_path(plugin_name)
  187. ##@brief Stores the plugin module
  188. self.module = None
  189. ##@breif Stores the plugin loader module
  190. self.__loader_module = None
  191. self.__confspecs = dict()
  192. self.loaded = False
  193. # Importing __init__.py infos in it
  194. plugin_module = '%s.%s' % (VIRTUAL_PACKAGE_NAME,
  195. plugin_name)
  196. init_source = os.path.join(self.path, INIT_FILENAME)
  197. try:
  198. loader = SourceFileLoader(plugin_module, init_source)
  199. self.module = loader.load_module()
  200. except (ImportError,FileNotFoundError) as e:
  201. raise PluginError("Failed to load plugin '%s'. It seems that the plugin name is not valid or the plugin do not exists" % plugin_name)
  202. # loading confspecs
  203. try:
  204. # Loading confspec directly from __init__.py
  205. self.__confspecs = getattr(self.module, CONFSPEC_VARNAME)
  206. except AttributeError:
  207. # Loading file in __confspec__ var in __init__.py
  208. try:
  209. module = self._import_from_init_var(CONFSPEC_FILENAME_VARNAME)
  210. except AttributeError:
  211. msg = "Malformed plugin {plugin} . No {varname} not {filevar} found in __init__.py"
  212. msg = msg.format(
  213. plugin = self.name,
  214. varname = CONFSPEC_VARNAME,
  215. filevar = CONFSPEC_FILENAME_VARNAME)
  216. raise PluginError(msg)
  217. except ImportError as e:
  218. msg = "Broken plugin {plugin} : {expt}"
  219. msg = msg.format(
  220. plugin = self.name,
  221. expt = str(e))
  222. raise PluginError(msg)
  223. try:
  224. # loading confpsecs from file
  225. self.__confspecs = getattr(module, CONFSPEC_VARNAME)
  226. except AttributeError:
  227. msg = "Broken plugin. {varname} not found in '{filename}'"
  228. msg = msg.format(
  229. varname = CONFSPEC_VARNAME,
  230. filename = confspec_filename)
  231. raise PluginError(msg)
  232. # loading plugin version
  233. try:
  234. #this try block should be useless. The existance of
  235. #PLUGIN_VERSION_VARNAME in init file is mandatory
  236. self.__version = getattr(self.module, PLUGIN_VERSION_VARNAME)
  237. except AttributeError:
  238. msg = "Error that should not append while loading plugin '%s': no \
  239. %s found in plugin init file. Malformed plugin"
  240. msg %= (plugin_name, PLUGIN_VERSION_VARNAME)
  241. raise LodelFatalError(msg)
  242. # Load plugin type
  243. try:
  244. self.__type = getattr(self.module, PLUGIN_TYPE_VARNAME)
  245. except AttributeError:
  246. self.__type = DEFAULT_PLUGIN_TYPE
  247. self.__type = str(self.__type).lower()
  248. if self.__type not in PLUGINS_TYPES:
  249. raise PluginError("Unknown plugin type '%s'" % self.__type)
  250. # Load plugin name from init file (just for checking)
  251. try:
  252. #this try block should be useless. The existance of
  253. #PLUGIN_NAME_VARNAME in init file is mandatory
  254. pname = getattr(self.module, PLUGIN_NAME_VARNAME)
  255. except AttributeError:
  256. msg = "Error that should not append : no %s found in plugin \
  257. init file. Malformed plugin"
  258. msg %= PLUGIN_NAME_VARNAME
  259. raise LodelFatalError(msg)
  260. if pname != plugin_name:
  261. msg = "Plugin's discover cache inconsistency detected ! Cached \
  262. name differ from the one found in plugin's init file"
  263. raise PluginError(msg)
  264. ##@brief Try to import a file from a variable in __init__.py
  265. #@param varname str : The variable name
  266. #@return loaded module
  267. #@throw AttributeError if varname not found
  268. #@throw ImportError if the file fails to be imported
  269. #@throw PluginError if the filename was not valid
  270. def _import_from_init_var(self, varname):
  271. # Read varname
  272. try:
  273. filename = getattr(self.module, varname)
  274. except AttributeError:
  275. msg = "Malformed plugin {plugin}. No {varname} found in __init__.py"
  276. msg = msg.format(
  277. plugin = self.name,
  278. varname = LOADER_FILENAME_VARNAME)
  279. raise PluginError(msg)
  280. #Path are not allowed
  281. if filename != os.path.basename(filename):
  282. msg = "Invalid {varname} content : '{fname}' for plugin {name}"
  283. msg = msg.format(
  284. varname = varname,
  285. fname = filename,
  286. name = self.name)
  287. raise PluginError(msg)
  288. # importing the file in varname
  289. module_name = self.module.__name__+"."+varname
  290. filename = os.path.join(self.path, filename)
  291. loader = SourceFileLoader(module_name, filename)
  292. return loader.load_module()
  293. ##@brief Check dependencies of plugin
  294. #@return A list of plugin name to be loaded before
  295. def check_deps(self):
  296. try:
  297. res = getattr(self.module, PLUGIN_DEPS_VARNAME)
  298. except AttributeError:
  299. return list()
  300. result = list()
  301. errors = list()
  302. for plugin_name in res:
  303. try:
  304. result.append(self.get(plugin_name))
  305. except PluginError:
  306. errors.append(plugin_name)
  307. if len(errors) > 0:
  308. raise PluginError( "Bad dependencie for '%s' :"%self.name,
  309. ', '.join(errors))
  310. return result
  311. ##@brief Check if the plugin should be activated
  312. #
  313. #Try to fetch a function called @ref ACTIVATE_METHOD_NAME in __init__.py
  314. #of a plugin. If none found assert that the plugin can be loaded, else
  315. #the method is called. If it returns anything else that True, the plugin
  316. #is noted as not activable
  317. #
  318. # @note Maybe we have to exit everything if a plugin cannot be loaded...
  319. def activable(self):
  320. from lodel import logger
  321. try:
  322. test_fun = getattr(self.module, ACTIVATE_METHOD_NAME)
  323. except AttributeError:
  324. msg = "No %s method found for plugin %s. Assuming plugin is ready to be loaded"
  325. msg %= (ACTIVATE_METHOD_NAME, self.name)
  326. logger.debug(msg)
  327. test_fun = lambda:True
  328. return test_fun()
  329. ##@brief Load a plugin
  330. #
  331. #Loading a plugin means importing a file. The filename is defined in the
  332. #plugin's __init__.py file in a LOADER_FILENAME_VARNAME variable.
  333. #
  334. #The loading process has to take care of other things :
  335. #- loading dependencies (other plugins)
  336. #- check that the plugin can be activated using Plugin.activate() method
  337. #- avoid circular dependencies infinite loop
  338. def _load(self):
  339. if self.loaded:
  340. return
  341. from lodel import logger
  342. #Test that plugin "wants" to be activated
  343. activable = self.activable()
  344. if not(activable is True):
  345. msg = "Plugin %s is not activable : %s"
  346. msg %= (self.name, activable)
  347. raise PluginError(activable)
  348. #Circular dependencie detection
  349. if self.name in self._load_called:
  350. raise PluginError("Circular dependencie in Plugin detected. Abording")
  351. else:
  352. self._load_called.append(self.name)
  353. #Dependencie load
  354. for dependencie in self.check_deps():
  355. activable = dependencie.activable()
  356. if activable is True:
  357. dependencie._load()
  358. else:
  359. msg = "Plugin {plugin_name} not activable because it depends on plugin {dep_name} that is not activable : {reason}"
  360. msg = msg.format(
  361. plugin_name = self.name,
  362. dep_name = dependencie.name,
  363. reason = activable)
  364. #Loading the plugin
  365. try:
  366. self.__loader_module = self._import_from_init_var(LOADER_FILENAME_VARNAME)
  367. except PluginError as e:
  368. raise e
  369. except ImportError as e:
  370. msg = "Broken plugin {plugin} : {expt}"
  371. msg = msg.format(
  372. plugin = self.name,
  373. expt = str(e))
  374. raise PluginError(msg)
  375. logger.debug("Plugin '%s' loaded" % self.name)
  376. self.loaded = True
  377. def loader_module(self):
  378. if not self.loaded:
  379. raise RuntimeError("Plugin %s not loaded yet."%self.name)
  380. return self.__loader_module
  381. def __str__(self):
  382. return "<LodelPlugin '%s' version %s>" % (self.name, self.__version)
  383. ##@brief Call load method on every pre-loaded plugins
  384. #
  385. # Called by loader to trigger hooks registration.
  386. # This method have to avoid circular dependencies infinite loops. For this
  387. # purpose a class attribute _load_called exists.
  388. # @throw PluginError
  389. @classmethod
  390. def load_all(cls):
  391. errors = dict()
  392. cls._load_called = []
  393. for name, plugin in cls._plugin_instances.items():
  394. try:
  395. plugin._load()
  396. except PluginError as e:
  397. errors[name] = e
  398. if len(errors) > 0:
  399. msg = "Errors while loading plugins :"
  400. for name, e in errors.items():
  401. msg += "\n\t%20s : %s" % (name,e)
  402. msg += "\n"
  403. raise PluginError(msg)
  404. from lodel.plugin.hooks import LodelHook
  405. LodelHook.call_hook(
  406. "lodel2_plugins_loaded", cls, cls._plugin_instances)
  407. ##@return a copy of __confspecs attr
  408. @property
  409. def confspecs(self):
  410. return copy.copy(self.__confspecs)
  411. @classmethod
  412. def plist_confspecs(cls):
  413. if cls._plist_confspecs is None:
  414. raise LodelFatalError('Unitialized _plist_confspecs attribute for \
  415. %s' % cls.__name__)
  416. return copy.copy(cls._plist_confspecs)
  417. ##@brief Retrieves plugin list confspecs
  418. #
  419. #This method ask for each Plugin child class the confspecs specifying where
  420. #the wanted plugin list is stored. (For example DatasourcePlugin expect
  421. #that a list of ds plugin to load stored in lodel2 section, datasources key
  422. # etc...
  423. @classmethod
  424. def plugin_list_confspec(cls):
  425. from lodel.settings.validator import confspec_append
  426. res = dict()
  427. for pcls in cls.plugin_types():
  428. plcs = pcls.plist_confspec()
  429. confspec_append(res, plcs)
  430. return res
  431. ##@brief Attempt to read plugin discover cache
  432. #@note If no cache yet make a discover with default plugin directory
  433. #@return a dict (see @ref _discover() )
  434. @classmethod
  435. def plugin_cache(cls):
  436. if cls._discover_cache is None:
  437. if not os.path.isfile(DISCOVER_CACHE_FILENAME):
  438. cls.discover()
  439. with open(DISCOVER_CACHE_FILENAME) as pdcache_fd:
  440. res = json.load(pdcache_fd)
  441. #Check consistency of loaded cache
  442. if 'path_list' not in res:
  443. raise LodelFatalError("Malformed plugin's discover cache file \
  444. : '%s'. Unable to find plugin's paths list." % DISCOVER_CACHE_FILENAME)
  445. expected_keys = ['type', 'path', 'version']
  446. for pname in res['plugins']:
  447. for ekey in expected_keys:
  448. if ekey not in res['plugins'][pname]:
  449. #Bad cache !
  450. logger.warning("Malformed plugin's discover cache \
  451. file : '%s'. Running discover again..." % DISCOVER_CACHE_FILENAME)
  452. cls._discover_cache = cls.discover(res['path_list'])
  453. break
  454. else:
  455. #The cache we just read was OK
  456. cls._discover_cache = res
  457. return cls._discover_cache
  458. ##@brief Register a new plugin
  459. #
  460. #@param plugin_name str : The plugin name
  461. #@return a Plugin instance
  462. #@throw PluginError
  463. @classmethod
  464. def register(cls, plugin_name):
  465. from .datasource_plugin import DatasourcePlugin
  466. if plugin_name in cls._plugin_instances:
  467. msg = "Plugin allready registered with same name %s"
  468. msg %= plugin_name
  469. raise PluginError(msg)
  470. #Here we check that previous discover found a plugin with that name
  471. pdcache = cls.plugin_cache()
  472. if plugin_name not in pdcache['plugins']:
  473. raise PluginError("No plugin named %s found" % plugin_name)
  474. pinfos = pdcache['plugins'][plugin_name]
  475. ptype = pinfos['type']
  476. if ptype == 'datasource':
  477. pcls = DatasourcePlugin
  478. else:
  479. pcls = cls
  480. plugin = pcls(plugin_name)
  481. cls._plugin_instances[plugin_name] = plugin
  482. logger.debug("Plugin %s available." % plugin)
  483. return plugin
  484. ##@brief Plugins instances accessor
  485. #
  486. #@param plugin_name str: The plugin name
  487. #@return a Plugin instance
  488. #@throw PluginError if plugin not found
  489. @classmethod
  490. def get(cls, plugin_name):
  491. try:
  492. return cls._plugin_instances[plugin_name]
  493. except KeyError:
  494. msg = "No plugin named '%s' loaded"
  495. msg %= plugin_name
  496. raise PluginError(msg)
  497. ##@brief Given a plugin name returns the plugin path
  498. # @param plugin_name str : The plugin name
  499. # @return the plugin directory path
  500. @classmethod
  501. def plugin_path(cls, plugin_name):
  502. plist = cls.plugin_list()
  503. if plugin_name not in plist:
  504. raise PluginError("No plugin named '%s' found" % plugin_name)
  505. try:
  506. return cls.get(plugin_name).path
  507. except PluginError:
  508. pass
  509. return plist[plugin_name]['path']
  510. @classmethod
  511. def plugin_module_name(cls, plugin_name):
  512. return "%s.%s" % (VIRTUAL_PACKAGE_NAME, plugin_name)
  513. ##@brief Start the Plugin class
  514. #
  515. # Called by Settings.__bootstrap()
  516. #
  517. # This method load path and preload plugins
  518. @classmethod
  519. def start(cls, plugins):
  520. for plugin_name in plugins:
  521. cls.register(plugin_name)
  522. @classmethod
  523. def clear(cls):
  524. if cls._plugin_directories is not None:
  525. cls._plugin_directories = None
  526. if cls._plugin_instances != dict():
  527. cls._plugin_instances = dict()
  528. if cls._load_called != []:
  529. cls._load_called = []
  530. ##@brief Reccursively walk throught paths to find plugin, then stores
  531. #found plugin in a file...
  532. #@param paths list : list of directory paths
  533. #@param no_cache bool : if true only return a list of found plugins
  534. #without modifying the cache file
  535. #@return a dict {'path_list': [...], 'plugins': { see @ref _discover }}
  536. #@todo add max_depth and symlink following options
  537. @classmethod
  538. def discover(cls, paths = None, no_cache = False):
  539. logger.info("Running plugin discover")
  540. if paths is None:
  541. paths = DEFAULT_PLUGINS_PATH_LIST
  542. tmp_res = []
  543. for path in paths:
  544. tmp_res += cls._discover(path)
  545. #Formating and dedoubloning result
  546. result = dict()
  547. for pinfos in tmp_res:
  548. pname = pinfos['name']
  549. if ( pname in result
  550. and pinfos['version'] > result[pname]['version'])\
  551. or pname not in result:
  552. result[pname] = pinfos
  553. else:
  554. #dropped
  555. pass
  556. result = {'path_list': paths, 'plugins': result}
  557. #Writing to cache
  558. if not no_cache:
  559. with open(DISCOVER_CACHE_FILENAME, 'w+') as pdcache:
  560. pdcache.write(json.dumps(result))
  561. return result
  562. ##@brief Return discover result
  563. #@param refresh bool : if true invalidate all plugin list cache
  564. #@note If discover cache file not found run discover first
  565. #@note if refresh is set to True discover MUST have been run at least
  566. #one time. In fact refresh action load the list of path to explore
  567. #from the plugin's discover cache
  568. @classmethod
  569. def plugin_list(cls, refresh = False):
  570. try:
  571. infos = cls._load_discover_cache()
  572. path_list = infos['path_list']
  573. except PluginError:
  574. refresh = True
  575. path_list = DEFAULT_PLUGINS_PATH_LIST
  576. if cls._plugin_list is None or refresh:
  577. if not os.path.isfile(DISCOVER_CACHE_FILENAME) or refresh:
  578. infos = cls.discover(path_list)
  579. cls._plugin_list = infos['plugins']
  580. return cls._plugin_list
  581. ##@brief Return a list of child Class Plugin
  582. @classmethod
  583. def plugin_types(cls):
  584. return all_types()
  585. ##@brief Attempt to open and load plugin discover cache
  586. #@return discover cache
  587. #@throw PluginError when open or load fails
  588. @classmethod
  589. def _load_discover_cache(cls):
  590. try:
  591. pdcache = open(DISCOVER_CACHE_FILENAME, 'r')
  592. except Exception as e:
  593. msg = "Unable to open discover cache : %s"
  594. msg %= e
  595. raise PluginError(msg)
  596. try:
  597. res = json.load(pdcache)
  598. except Exception as e:
  599. msg = "Unable to load discover cache : %s"
  600. msg %= e
  601. raise PluginError(msg)
  602. pdcache.close()
  603. return res
  604. ##@brief Check if a directory is a plugin module
  605. #@param path str : path to check
  606. #@return a dict with name, version and path if path is a plugin module, else False
  607. @classmethod
  608. def dir_is_plugin(cls, path):
  609. log_msg = "%s is not a plugin directory because : " % path
  610. #Checks that path exists
  611. if not os.path.isdir(path):
  612. raise ValueError(
  613. "Expected path to be a directory, but '%s' found" % path)
  614. #Checks that path contains plugin's init file
  615. initfile = os.path.join(path, INIT_FILENAME)
  616. if not os.path.isfile(initfile):
  617. log_msg += "'%s' not found" % (INIT_FILENAME)
  618. logger.debug(log_msg)
  619. return False
  620. #Importing plugin's init file to check contained datas
  621. try:
  622. initmod, modname = cls.import_init(path)
  623. except PluginError as e:
  624. log_msg += "unable to load '%s'. Exception raised : %s"
  625. log_msg %= (INIT_FILENAME, e)
  626. logger.debug(log_msg)
  627. return False
  628. #Checking mandatory init module variables
  629. for attr_name in MANDATORY_VARNAMES:
  630. if not hasattr(initmod,attr_name):
  631. log_msg += " mandatory variable '%s' not found in '%s'"
  632. log_msg %= (attr_name, INIT_FILENAME)
  633. logger.debug(log_msg)
  634. return False
  635. #Fetching plugin's version
  636. try:
  637. pversion = getattr(initmod, PLUGIN_VERSION_VARNAME)
  638. except (NameError, AttributeError) as e:
  639. msg = "Invalid plugin version found in %s : %s"
  640. msg %= (path, e)
  641. raise PluginError(msg)
  642. #Fetching plugin's type
  643. try:
  644. ptype = getattr(initmod, PLUGIN_TYPE_VARNAME)
  645. except (NameError, AttributeError) as e:
  646. ptype = DEFAULT_PLUGIN_TYPE
  647. pname = getattr(initmod, PLUGIN_NAME_VARNAME)
  648. return {'name': pname,
  649. 'version': pversion,
  650. 'path': path,
  651. 'type': ptype}
  652. ##@brief Import init file from a plugin path
  653. #@param path str : Directory path
  654. #@return a tuple (init_module, module_name)
  655. @classmethod
  656. def import_init(self, path):
  657. init_source = os.path.join(path, INIT_FILENAME)
  658. temp_module = '%s.%s.%s' % (
  659. VIRTUAL_TEMP_PACKAGE_NAME, os.path.basename(os.path.dirname(path)),
  660. 'test_init')
  661. try:
  662. loader = SourceFileLoader(temp_module, init_source)
  663. except (ImportError, FileNotFoundError) as e:
  664. raise PluginError("Unable to import init file from '%s' : %s" % (
  665. temp_module, e))
  666. try:
  667. res_module = loader.load_module()
  668. except Exception as e:
  669. raise PluginError("Unable to import initfile")
  670. return (res_module, temp_module)
  671. ##@brief Reccursiv plugin discover given a path
  672. #@param path str : the path to walk through
  673. #@return A dict with plugin_name as key and {'path':..., 'version':...} as value
  674. @classmethod
  675. def _discover(cls, path):
  676. res = []
  677. to_explore = [path]
  678. while len(to_explore) > 0:
  679. cur_path = to_explore.pop()
  680. for f in os.listdir(cur_path):
  681. f_path = os.path.join(cur_path, f)
  682. if f not in ['.', '..'] and os.path.isdir(f_path):
  683. #Check if it is a plugin directory
  684. test_result = cls.dir_is_plugin(f_path)
  685. if not (test_result is False):
  686. logger.info("Plugin found in %s" % f_path)
  687. res.append(test_result)
  688. else:
  689. to_explore.append(f_path)
  690. return res
  691. ##@brief Decorator class designed to allow plugins to add custom methods
  692. #to LeObject childs (dyncode objects)
  693. #
  694. class CustomMethod(object):
  695. ##@brief Stores registered custom methods
  696. #
  697. #Key = LeObject child class name
  698. #Value = CustomMethod instance
  699. _custom_methods = dict()
  700. INSTANCE_METHOD = 0
  701. CLASS_METHOD = 1
  702. STATIC_METHOD = 2
  703. ##@brief Decorator constructor
  704. #@param component_name str : the name of the component to enhance
  705. #@param method_name str : the name of the method to inject (if None given
  706. #@param method_type int : take value in one of
  707. #CustomMethod::INSTANCE_METHOD CustomMethod::CLASS_METHOD or
  708. #CustomMethod::STATIC_METHOD
  709. #use the function name
  710. def __init__(self, component_name, method_name = None, method_type=0):
  711. ##@brief The targeted LeObject child class
  712. self._comp_name = component_name
  713. ##@brief The method name
  714. self._method_name = method_name
  715. ##@brief The function (that will be the injected method)
  716. self._fun = None
  717. ##@brief Stores the type of method (instance, class or static)
  718. self._type = int(method_type)
  719. if self._type not in (self.INSTANCE_METHOD, self.CLASS_METHOD,\
  720. self.STATIC_METHOD):
  721. raise ValueError("Excepted value for method_type was one of \
  722. CustomMethod::INSTANCE_METHOD CustomMethod::CLASS_METHOD or \
  723. CustomMethod::STATIC_METHOD, but got %s" % self._type)
  724. ##@brief called just after __init__
  725. #@param fun function : the decorated function
  726. #@param return the function
  727. def __call__(self, fun):
  728. if self._method_name is None:
  729. self._method_name = fun.__name__
  730. if self._comp_name not in self._custom_methods:
  731. self._custom_methods[self._comp_name] = list()
  732. if self._method_name in [ scm._method_name for scm in self._custom_methods[self._comp_name]]:
  733. raise RuntimeError("A method named %s allready registered by \
  734. another plugin : %s" % (
  735. self._method_name,
  736. self._custom_methods[self._comp_name].__module__))
  737. self._fun = fun
  738. self._custom_methods[self._comp_name].append(self)
  739. ##@brief Textual representation
  740. #@return textual representation of the CustomMethod instance
  741. def __repr__(self):
  742. res = "<CustomMethod name={method_name} target={classname} \
  743. source={module_name}.{fun_name}>"
  744. return res.format(
  745. method_name = self._method_name,
  746. classname = self._comp_name,
  747. module_name = self._fun.__module__,
  748. fun_name = self._fun.__name__)
  749. ##@brief Return a well formed method
  750. #
  751. #@note the type of method depends on the _type attribute
  752. #@return a method directly injectable in the target class
  753. def __get_method(self):
  754. if self._type == self.INSTANCE_METHOD:
  755. def custom__get__(self, obj, objtype = None):
  756. return types.MethodType(self, obj, objtype)
  757. setattr(self._fun, '__get__', custom__get__)
  758. return self._fun
  759. elif self._type == self.CLASS_METHOD:
  760. return classmethod(self._fun)
  761. elif self._type == self.STATIC_METHOD:
  762. return staticmethod(self._fun)
  763. else:
  764. raise RuntimeError("Attribute _type is not one of \
  765. CustomMethod::INSTANCE_METHOD CustomMethod::CLASS_METHOD \
  766. CustomMethod::STATIC_METHOD")
  767. ##@brief Handle custom method dynamic injection in LeAPI dyncode
  768. #
  769. #Called by lodel2_dyncode_loaded hook defined at
  770. #lodel.plugin.core_hooks.lodel2_plugin_custom_methods()
  771. #
  772. #@param cls
  773. #@param dynclasses LeObject child classes : List of dynamically generated
  774. #LeObject child classes
  775. @classmethod
  776. def set_registered(cls, dynclasses):
  777. from lodel import logger
  778. dyn_cls_dict = { dc.__name__:dc for dc in dynclasses}
  779. for cls_name, custom_methods in cls._custom_methods.items():
  780. for custom_method in custom_methods:
  781. if cls_name not in dyn_cls_dict:
  782. logger.error("Custom method %s adding fails : No dynamic \
  783. LeAPI objects named %s." % (custom_method, cls_name))
  784. elif custom_method._method_name in dir(dyn_cls_dict[cls_name]):
  785. logger.warning("Overriding existing method '%s' on target \
  786. with %s" % (custom_method._method_name, custom_method))
  787. else:
  788. setattr(
  789. dyn_cls_dict[cls_name],
  790. custom_method._method_name,
  791. custom_method.__get_method())
  792. logger.debug(
  793. "Custom method %s added to target" % custom_method)