| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE. |
| 4 |
# Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 5 |
# |
| 6 |
# This program is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# 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 General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
"""Common functionality related to the loading and calling of plugins.""" |
| 20 |
|
| 21 |
import os |
| 22 |
from time import time |
| 23 |
|
| 24 |
from cylc.flow import LOG, iter_entry_points |
| 25 |
from cylc.flow.async_util import async_block as _async_block |
| 26 |
from cylc.flow.exceptions import PluginError |
| 27 |
import cylc.flow.flags |
| 28 |
|
| 29 |
|
| 30 |
async def run_plugins_async( |
| 31 |
plugin_namespace, |
| 32 |
*args, |
| 33 |
async_block=False, |
| 34 |
**kwargs |
| 35 |
): |
| 36 |
"""Run all installed plugins for the given namespace. |
| 37 |
|
| 38 |
This runs plugins in series, yielding the results one by one. |
| 39 |
|
| 40 |
Args: |
| 41 |
plugin_namespace: |
| 42 |
The entry point namespace for the plugins to run, |
| 43 |
e.g. "cylc.post_install". |
| 44 |
args: |
| 45 |
Any arguments to call plugins with. |
| 46 |
async_block: |
| 47 |
If True, this will wait for any async tasks started by the plugin |
| 48 |
to complete before moving on to the next plugin. |
| 49 |
kwargs: |
| 50 |
Any kwargs to call plugins with. |
| 51 |
|
| 52 |
Yields: |
| 53 |
(entry_point, plugin_result) |
| 54 |
|
| 55 |
See https://github.com/cylc/cylc-rose/issues/274 |
| 56 |
|
| 57 |
""" |
| 58 |
startpoint = os.getcwd() |
| 59 |
for entry_point in iter_entry_points(plugin_namespace): |
| 60 |
try: |
| 61 |
# measure the import+run time for the plugin (debug mode) |
| 62 |
start_time = time() |
| 63 |
|
| 64 |
# load the plugin |
| 65 |
meth = entry_point.load() |
| 66 |
|
| 67 |
# run the plugin |
| 68 |
if async_block: |
| 69 |
# wait for any async tasks started by the plugin to complete |
| 70 |
async with _async_block(): |
| 71 |
plugin_result = meth(*args, **kwargs) |
| 72 |
else: |
| 73 |
plugin_result = meth(*args, **kwargs) |
| 74 |
|
| 75 |
# log the import+run time (debug mode) |
| 76 |
if cylc.flow.flags.verbosity > 1: |
| 77 |
LOG.debug( |
| 78 |
f'ran {entry_point.name} in {time() - start_time:0.05f}s' |
| 79 |
) |
| 80 |
|
| 81 |
# yield the result to the caller |
| 82 |
yield entry_point, plugin_result |
| 83 |
|
| 84 |
except Exception as exc: # NOTE: except Exception (purposefully vague) |
| 85 |
_raise_plugin_exception(exc, plugin_namespace, entry_point) |
| 86 |
|
| 87 |
finally: |
| 88 |
# ensure the plugin does not change the CWD |
| 89 |
os.chdir(startpoint) |
| 90 |
|
| 91 |
|
| 92 |
def run_plugins(plugin_namespace, *args, **kwargs): |
| 93 |
"""Run all installed plugins for the given namespace. |
| 94 |
|
| 95 |
This runs plugins in series, yielding the results one by one. |
| 96 |
|
| 97 |
Warning: |
| 98 |
Use run_plugins_async for "cylc.post_install" plugins. |
| 99 |
See https://github.com/cylc/cylc-rose/issues/274 |
| 100 |
|
| 101 |
Args: |
| 102 |
plugin_namespace: |
| 103 |
The entry point namespace for the plugins to run, |
| 104 |
e.g. "cylc.post_install". |
| 105 |
args: |
| 106 |
Any arguments to call plugins with. |
| 107 |
kwargs: |
| 108 |
Any kwargs to call plugins with. |
| 109 |
|
| 110 |
Yields: |
| 111 |
(entry_point, plugin_result) |
| 112 |
|
| 113 |
""" |
| 114 |
startpoint = os.getcwd() |
| 115 |
for entry_point in iter_entry_points(plugin_namespace): |
| 116 |
try: |
| 117 |
# measure the import+run time for the plugin (debug mode) |
| 118 |
start_time = time() |
| 119 |
|
| 120 |
# load the plugin |
| 121 |
meth = entry_point.load() |
| 122 |
|
| 123 |
# run the plugin |
| 124 |
plugin_result = meth(*args, **kwargs) |
| 125 |
|
| 126 |
# log the import+run time (debug mode) |
| 127 |
if cylc.flow.flags.verbosity > 1: |
| 128 |
LOG.debug( |
| 129 |
f'ran {entry_point.name} in {time() - start_time:0.05f}s' |
| 130 |
) |
| 131 |
|
| 132 |
# yield the result to the caller |
| 133 |
yield entry_point, plugin_result |
| 134 |
|
| 135 |
except Exception as exc: # NOTE: except Exception (purposefully vague) |
| 136 |
_raise_plugin_exception(exc, plugin_namespace, entry_point) |
| 137 |
|
| 138 |
finally: |
| 139 |
# ensure the plugin does not change the CWD |
| 140 |
os.chdir(startpoint) |
| 141 |
|
| 142 |
|
| 143 |
def _raise_plugin_exception(exc, plugin_namespace, entry_point): |
| 144 |
"""Re-Raise an exception captured from a plugin.""" |
| 145 |
if cylc.flow.flags.verbosity > 1: |
| 146 |
# raise the full exception in debug mode |
| 147 |
# (this helps plugin developers locate the error in their code) |
| 148 |
raise |
| 149 |
# raise a user-friendly exception |
| 150 |
raise PluginError( |
| 151 |
plugin_namespace, |
| 152 |
entry_point.name, |
| 153 |
exc |
| 154 |
) from None |