Commit | Line | Data |
---|---|---|
55bb57e0 | 1 | /* |
6fbd4105 | 2 | * python-plugin-provider.c |
55bb57e0 | 3 | * |
6fbd4105 | 4 | * Babeltrace Python plugin provider |
55bb57e0 PP |
5 | * |
6 | * Copyright 2017 Philippe Proulx <pproulx@efficios.com> | |
7 | * | |
8 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
9 | * of this software and associated documentation files (the "Software"), to deal | |
10 | * in the Software without restriction, including without limitation the rights | |
11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
12 | * copies of the Software, and to permit persons to whom the Software is | |
13 | * furnished to do so, subject to the following conditions: | |
14 | * | |
15 | * The above copyright notice and this permission notice shall be included in | |
16 | * all copies or substantial portions of the Software. | |
17 | * | |
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
24 | * SOFTWARE. | |
25 | */ | |
26 | ||
b03487ab | 27 | #define BT_LOG_TAG "LIB/PLUGIN-PY" |
3fe0bf43 | 28 | |
1633ef46 | 29 | #include "lib/logging.h" |
85e7137b | 30 | #include "common/macros.h" |
57952005 | 31 | #include "compat/compiler.h" |
71c5da58 | 32 | #include <babeltrace2/plugin/plugin-const.h> |
57952005 | 33 | #include "lib/plugin/plugin.h" |
71c5da58 | 34 | #include <babeltrace2/graph/component-class.h> |
57952005 | 35 | #include "lib/graph/component-class.h" |
55bb57e0 PP |
36 | #include <stdlib.h> |
37 | #include <signal.h> | |
38 | #include <Python.h> | |
6fbd4105 PP |
39 | #include <glib.h> |
40 | #include <gmodule.h> | |
55bb57e0 PP |
41 | |
42 | #define PYTHON_PLUGIN_FILE_PREFIX "bt_plugin_" | |
43 | #define PYTHON_PLUGIN_FILE_PREFIX_LEN (sizeof(PYTHON_PLUGIN_FILE_PREFIX) - 1) | |
44 | #define PYTHON_PLUGIN_FILE_EXT ".py" | |
45 | #define PYTHON_PLUGIN_FILE_EXT_LEN (sizeof(PYTHON_PLUGIN_FILE_EXT) - 1) | |
46 | ||
47 | enum python_state { | |
48 | /* init_python() not called yet */ | |
49 | PYTHON_STATE_NOT_INITED, | |
50 | ||
51 | /* init_python() called once with success */ | |
52 | PYTHON_STATE_FULLY_INITIALIZED, | |
53 | ||
54 | /* init_python() called once without success */ | |
55 | PYTHON_STATE_CANNOT_INITIALIZE, | |
01f50d54 PP |
56 | |
57 | /* | |
58 | * init_python() called, but environment variable asks the | |
59 | * Python interpreter not to be loaded. | |
60 | */ | |
61 | PYTHON_STATE_WONT_INITIALIZE, | |
55bb57e0 PP |
62 | } python_state = PYTHON_STATE_NOT_INITED; |
63 | ||
64 | static PyObject *py_try_load_plugin_module_func = NULL; | |
7da0994e | 65 | static bool python_was_initialized_by_us; |
55bb57e0 PP |
66 | |
67 | static | |
01f50d54 | 68 | void print_python_traceback(int log_level) |
55bb57e0 | 69 | { |
01f50d54 PP |
70 | if (BT_LOG_ON(log_level) && Py_IsInitialized() && PyErr_Occurred()) { |
71 | BT_LOG_WRITE(log_level, BT_LOG_TAG, | |
72 | "Exception occured: Python traceback: "); | |
55bb57e0 PP |
73 | PyErr_Print(); |
74 | } | |
75 | } | |
76 | ||
77 | static | |
78 | void pyerr_clear(void) | |
79 | { | |
80 | if (Py_IsInitialized()) { | |
81 | PyErr_Clear(); | |
82 | } | |
83 | } | |
84 | ||
85 | static | |
86 | void init_python(void) | |
87 | { | |
88 | PyObject *py_bt2_py_plugin_mod = NULL; | |
89 | const char *dis_python_env; | |
9f0d4ec8 | 90 | #ifndef __MINGW32__ |
a6b0315a | 91 | sig_t old_sigint = signal(SIGINT, SIG_DFL); |
9f0d4ec8 | 92 | #endif |
55bb57e0 PP |
93 | |
94 | if (python_state != PYTHON_STATE_NOT_INITED) { | |
95 | goto end; | |
96 | } | |
97 | ||
98 | /* | |
99 | * User can disable Python plugin support with the | |
100 | * BABELTRACE_DISABLE_PYTHON_PLUGINS environment variable set to | |
101 | * 1. | |
102 | */ | |
103 | dis_python_env = getenv("BABELTRACE_DISABLE_PYTHON_PLUGINS"); | |
9e0bf9b0 PP |
104 | if (dis_python_env && strcmp(dis_python_env, "1") == 0) { |
105 | BT_LOGI_STR("Python plugin support is disabled because `BABELTRACE_DISABLE_PYTHON_PLUGINS=1`."); | |
01f50d54 | 106 | python_state = PYTHON_STATE_WONT_INITIALIZE; |
55bb57e0 PP |
107 | goto end; |
108 | } | |
109 | ||
110 | if (!Py_IsInitialized()) { | |
53264927 | 111 | BT_LOGI_STR("Python interpreter is not initialized: initializing Python interpreter."); |
55bb57e0 | 112 | Py_InitializeEx(0); |
7da0994e | 113 | python_was_initialized_by_us = true; |
9e0bf9b0 PP |
114 | BT_LOGI("Initialized Python interpreter: version=\"%s\"", |
115 | Py_GetVersion()); | |
53264927 PP |
116 | } else { |
117 | BT_LOGI("Python interpreter is already initialized: version=\"%s\"", | |
118 | Py_GetVersion()); | |
55bb57e0 PP |
119 | } |
120 | ||
121 | py_bt2_py_plugin_mod = PyImport_ImportModule("bt2.py_plugin"); | |
122 | if (!py_bt2_py_plugin_mod) { | |
01f50d54 | 123 | BT_LOGW_STR("Cannot import bt2.py_plugin Python module: Python plugin support is disabled."); |
55bb57e0 PP |
124 | python_state = PYTHON_STATE_CANNOT_INITIALIZE; |
125 | goto end; | |
126 | } | |
127 | ||
128 | py_try_load_plugin_module_func = | |
129 | PyObject_GetAttrString(py_bt2_py_plugin_mod, "_try_load_plugin_module"); | |
130 | if (!py_try_load_plugin_module_func) { | |
01f50d54 | 131 | BT_LOGW_STR("Cannot get _try_load_plugin_module attribute from bt2.py_plugin Python module: Python plugin support is disabled."); |
55bb57e0 PP |
132 | python_state = PYTHON_STATE_CANNOT_INITIALIZE; |
133 | goto end; | |
134 | } | |
135 | ||
136 | python_state = PYTHON_STATE_FULLY_INITIALIZED; | |
137 | ||
138 | end: | |
9f0d4ec8 | 139 | #ifndef __MINGW32__ |
55bb57e0 PP |
140 | if (old_sigint != SIG_ERR) { |
141 | (void) signal(SIGINT, old_sigint); | |
142 | } | |
9f0d4ec8 | 143 | #endif |
55bb57e0 | 144 | |
01f50d54 | 145 | print_python_traceback(BT_LOG_WARN); |
55bb57e0 PP |
146 | pyerr_clear(); |
147 | Py_XDECREF(py_bt2_py_plugin_mod); | |
148 | return; | |
149 | } | |
150 | ||
151 | __attribute__((destructor)) static | |
152 | void fini_python(void) { | |
7da0994e | 153 | if (Py_IsInitialized() && python_was_initialized_by_us) { |
55bb57e0 PP |
154 | if (py_try_load_plugin_module_func) { |
155 | Py_DECREF(py_try_load_plugin_module_func); | |
156 | py_try_load_plugin_module_func = NULL; | |
157 | } | |
158 | ||
159 | Py_Finalize(); | |
9e0bf9b0 | 160 | BT_LOGI_STR("Finalized Python interpreter."); |
55bb57e0 PP |
161 | } |
162 | ||
163 | python_state = PYTHON_STATE_NOT_INITED; | |
164 | } | |
165 | ||
166 | static | |
fb25b9e3 | 167 | int bt_plugin_from_python_plugin_info(PyObject *plugin_info, |
01f50d54 | 168 | bool fail_on_load_error, bt_plugin **plugin_out) |
55bb57e0 | 169 | { |
fb25b9e3 | 170 | int status = __BT_FUNC_STATUS_OK; |
55bb57e0 PP |
171 | PyObject *py_name = NULL; |
172 | PyObject *py_author = NULL; | |
173 | PyObject *py_description = NULL; | |
174 | PyObject *py_license = NULL; | |
175 | PyObject *py_version = NULL; | |
176 | PyObject *py_comp_class_addrs = NULL; | |
177 | const char *name = NULL; | |
178 | const char *author = NULL; | |
179 | const char *description = NULL; | |
180 | const char *license = NULL; | |
181 | unsigned int major = 0, minor = 0, patch = 0; | |
182 | const char *version_extra = NULL; | |
55bb57e0 | 183 | |
01f50d54 PP |
184 | BT_ASSERT(plugin_out); |
185 | *plugin_out = NULL; | |
8b45963b PP |
186 | BT_ASSERT(plugin_info); |
187 | BT_ASSERT(python_state == PYTHON_STATE_FULLY_INITIALIZED); | |
55bb57e0 PP |
188 | py_name = PyObject_GetAttrString(plugin_info, "name"); |
189 | if (!py_name) { | |
01f50d54 PP |
190 | BT_LOG_WRITE(fail_on_load_error ? |
191 | BT_LOG_WARN : BT_LOG_INFO, BT_LOG_TAG, | |
192 | "Cannot find `name` attribute in Python plugin info object: " | |
9e0bf9b0 | 193 | "py-plugin-info-addr=%p", plugin_info); |
fb25b9e3 PP |
194 | status = fail_on_load_error ? __BT_FUNC_STATUS_LOADING_ERROR : |
195 | __BT_FUNC_STATUS_NOT_FOUND; | |
55bb57e0 PP |
196 | goto error; |
197 | } | |
198 | ||
199 | py_author = PyObject_GetAttrString(plugin_info, "author"); | |
200 | if (!py_author) { | |
01f50d54 PP |
201 | BT_LOG_WRITE(fail_on_load_error ? |
202 | BT_LOG_WARN : BT_LOG_INFO, BT_LOG_TAG, | |
203 | "Cannot find `author` attribute in Python plugin info object: " | |
9e0bf9b0 | 204 | "py-plugin-info-addr=%p", plugin_info); |
fb25b9e3 PP |
205 | status = fail_on_load_error ? __BT_FUNC_STATUS_LOADING_ERROR : |
206 | __BT_FUNC_STATUS_NOT_FOUND; | |
55bb57e0 PP |
207 | goto error; |
208 | } | |
209 | ||
210 | py_description = PyObject_GetAttrString(plugin_info, "description"); | |
211 | if (!py_description) { | |
01f50d54 PP |
212 | BT_LOG_WRITE(fail_on_load_error ? |
213 | BT_LOG_WARN : BT_LOG_INFO, BT_LOG_TAG, | |
214 | "Cannot find `desciption` attribute in Python plugin info object: " | |
9e0bf9b0 | 215 | "py-plugin-info-addr=%p", plugin_info); |
fb25b9e3 PP |
216 | status = fail_on_load_error ? __BT_FUNC_STATUS_LOADING_ERROR : |
217 | __BT_FUNC_STATUS_NOT_FOUND; | |
55bb57e0 PP |
218 | goto error; |
219 | } | |
220 | ||
221 | py_license = PyObject_GetAttrString(plugin_info, "license"); | |
222 | if (!py_license) { | |
01f50d54 PP |
223 | BT_LOG_WRITE(fail_on_load_error ? |
224 | BT_LOG_WARN : BT_LOG_INFO, BT_LOG_TAG, | |
225 | "Cannot find `license` attribute in Python plugin info object: " | |
9e0bf9b0 | 226 | "py-plugin-info-addr=%p", plugin_info); |
fb25b9e3 PP |
227 | status = fail_on_load_error ? __BT_FUNC_STATUS_LOADING_ERROR : |
228 | __BT_FUNC_STATUS_NOT_FOUND; | |
55bb57e0 PP |
229 | goto error; |
230 | } | |
231 | ||
232 | py_version = PyObject_GetAttrString(plugin_info, "version"); | |
233 | if (!py_version) { | |
01f50d54 PP |
234 | BT_LOG_WRITE(fail_on_load_error ? |
235 | BT_LOG_WARN : BT_LOG_INFO, BT_LOG_TAG, | |
236 | "Cannot find `version` attribute in Python plugin info object: " | |
9e0bf9b0 | 237 | "py-plugin-info-addr=%p", plugin_info); |
fb25b9e3 PP |
238 | status = fail_on_load_error ? __BT_FUNC_STATUS_LOADING_ERROR : |
239 | __BT_FUNC_STATUS_NOT_FOUND; | |
55bb57e0 PP |
240 | goto error; |
241 | } | |
242 | ||
243 | py_comp_class_addrs = PyObject_GetAttrString(plugin_info, | |
244 | "comp_class_addrs"); | |
245 | if (!py_comp_class_addrs) { | |
01f50d54 PP |
246 | BT_LOG_WRITE(fail_on_load_error ? |
247 | BT_LOG_WARN : BT_LOG_INFO, BT_LOG_TAG, | |
248 | "Cannot find `comp_class_addrs` attribute in Python plugin info object: " | |
9e0bf9b0 | 249 | "py-plugin-info-addr=%p", plugin_info); |
fb25b9e3 PP |
250 | status = fail_on_load_error ? __BT_FUNC_STATUS_LOADING_ERROR : |
251 | __BT_FUNC_STATUS_NOT_FOUND; | |
55bb57e0 PP |
252 | goto error; |
253 | } | |
254 | ||
255 | if (PyUnicode_Check(py_name)) { | |
256 | name = PyUnicode_AsUTF8(py_name); | |
257 | if (!name) { | |
01f50d54 PP |
258 | BT_LOG_WRITE(fail_on_load_error ? |
259 | BT_LOG_WARN : BT_LOG_INFO, BT_LOG_TAG, | |
260 | "Cannot decode Python plugin name string: " | |
9e0bf9b0 | 261 | "py-plugin-info-addr=%p", plugin_info); |
01f50d54 | 262 | status = fail_on_load_error ? |
fb25b9e3 PP |
263 | __BT_FUNC_STATUS_LOADING_ERROR : |
264 | __BT_FUNC_STATUS_NOT_FOUND; | |
55bb57e0 PP |
265 | goto error; |
266 | } | |
267 | } else { | |
268 | /* Plugin name is mandatory */ | |
01f50d54 PP |
269 | BT_LOG_WRITE(fail_on_load_error ? |
270 | BT_LOG_WARN : BT_LOG_INFO, BT_LOG_TAG, | |
271 | "Plugin name is not a string: " | |
9e0bf9b0 | 272 | "py-plugin-info-addr=%p", plugin_info); |
fb25b9e3 PP |
273 | status = fail_on_load_error ? __BT_FUNC_STATUS_LOADING_ERROR : |
274 | __BT_FUNC_STATUS_NOT_FOUND; | |
55bb57e0 PP |
275 | goto error; |
276 | } | |
277 | ||
278 | if (PyUnicode_Check(py_author)) { | |
279 | author = PyUnicode_AsUTF8(py_author); | |
280 | if (!author) { | |
01f50d54 PP |
281 | BT_LOG_WRITE(fail_on_load_error ? |
282 | BT_LOG_WARN : BT_LOG_INFO, BT_LOG_TAG, | |
283 | "Cannot decode Python plugin author string: " | |
9e0bf9b0 | 284 | "py-plugin-info-addr=%p", plugin_info); |
01f50d54 | 285 | status = fail_on_load_error ? |
fb25b9e3 PP |
286 | __BT_FUNC_STATUS_LOADING_ERROR : |
287 | __BT_FUNC_STATUS_NOT_FOUND; | |
55bb57e0 PP |
288 | goto error; |
289 | } | |
290 | } | |
291 | ||
292 | if (PyUnicode_Check(py_description)) { | |
293 | description = PyUnicode_AsUTF8(py_description); | |
294 | if (!description) { | |
01f50d54 PP |
295 | BT_LOG_WRITE(fail_on_load_error ? |
296 | BT_LOG_WARN : BT_LOG_INFO, BT_LOG_TAG, | |
297 | "Cannot decode Python plugin description string: " | |
9e0bf9b0 | 298 | "py-plugin-info-addr=%p", plugin_info); |
01f50d54 | 299 | status = fail_on_load_error ? |
fb25b9e3 PP |
300 | __BT_FUNC_STATUS_LOADING_ERROR : |
301 | __BT_FUNC_STATUS_NOT_FOUND; | |
55bb57e0 PP |
302 | goto error; |
303 | } | |
304 | } | |
305 | ||
306 | if (PyUnicode_Check(py_license)) { | |
307 | license = PyUnicode_AsUTF8(py_license); | |
308 | if (!license) { | |
01f50d54 PP |
309 | BT_LOG_WRITE(fail_on_load_error ? |
310 | BT_LOG_WARN : BT_LOG_INFO, BT_LOG_TAG, | |
311 | "Cannot decode Python plugin license string: " | |
9e0bf9b0 | 312 | "py-plugin-info-addr=%p", plugin_info); |
01f50d54 | 313 | status = fail_on_load_error ? |
fb25b9e3 PP |
314 | __BT_FUNC_STATUS_LOADING_ERROR : |
315 | __BT_FUNC_STATUS_NOT_FOUND; | |
55bb57e0 PP |
316 | goto error; |
317 | } | |
318 | } | |
319 | ||
320 | if (PyTuple_Check(py_version)) { | |
321 | if (PyTuple_Size(py_version) >= 3) { | |
322 | PyObject *py_major = PyTuple_GetItem(py_version, 0); | |
323 | PyObject *py_minor = PyTuple_GetItem(py_version, 1); | |
324 | PyObject *py_patch = PyTuple_GetItem(py_version, 2); | |
325 | ||
8b45963b PP |
326 | BT_ASSERT(py_major); |
327 | BT_ASSERT(py_minor); | |
328 | BT_ASSERT(py_patch); | |
55bb57e0 PP |
329 | |
330 | if (PyLong_Check(py_major)) { | |
331 | major = PyLong_AsUnsignedLong(py_major); | |
332 | } | |
333 | ||
334 | if (PyLong_Check(py_minor)) { | |
335 | minor = PyLong_AsUnsignedLong(py_minor); | |
336 | } | |
337 | ||
338 | if (PyLong_Check(py_patch)) { | |
339 | patch = PyLong_AsUnsignedLong(py_patch); | |
340 | } | |
341 | ||
342 | if (PyErr_Occurred()) { | |
343 | /* Overflow error, most probably */ | |
01f50d54 PP |
344 | BT_LOG_WRITE(fail_on_load_error ? |
345 | BT_LOG_WARN : BT_LOG_INFO, BT_LOG_TAG, | |
346 | "Invalid Python plugin version format: " | |
9e0bf9b0 | 347 | "py-plugin-info-addr=%p", plugin_info); |
01f50d54 | 348 | status = fail_on_load_error ? |
fb25b9e3 PP |
349 | __BT_FUNC_STATUS_LOADING_ERROR : |
350 | __BT_FUNC_STATUS_NOT_FOUND; | |
55bb57e0 PP |
351 | goto error; |
352 | } | |
353 | } | |
354 | ||
355 | if (PyTuple_Size(py_version) >= 4) { | |
356 | PyObject *py_extra = PyTuple_GetItem(py_version, 3); | |
357 | ||
8b45963b | 358 | BT_ASSERT(py_extra); |
55bb57e0 PP |
359 | |
360 | if (PyUnicode_Check(py_extra)) { | |
361 | version_extra = PyUnicode_AsUTF8(py_extra); | |
362 | if (!version_extra) { | |
01f50d54 PP |
363 | BT_LOG_WRITE(fail_on_load_error ? |
364 | BT_LOG_WARN : BT_LOG_INFO, BT_LOG_TAG, | |
365 | "Cannot decode Python plugin version's extra string: " | |
366 | "py-plugin-info-addr=%p", plugin_info); | |
367 | status = fail_on_load_error ? | |
fb25b9e3 PP |
368 | __BT_FUNC_STATUS_LOADING_ERROR : |
369 | __BT_FUNC_STATUS_NOT_FOUND; | |
55bb57e0 PP |
370 | goto error; |
371 | } | |
372 | } | |
373 | } | |
374 | } | |
375 | ||
01f50d54 PP |
376 | *plugin_out = bt_plugin_create_empty(BT_PLUGIN_TYPE_PYTHON); |
377 | if (!*plugin_out) { | |
9e0bf9b0 | 378 | BT_LOGE_STR("Cannot create empty plugin object."); |
fb25b9e3 | 379 | status = __BT_FUNC_STATUS_MEMORY_ERROR; |
55bb57e0 PP |
380 | goto error; |
381 | } | |
382 | ||
01f50d54 | 383 | bt_plugin_set_name(*plugin_out, name); |
55bb57e0 PP |
384 | |
385 | if (description) { | |
01f50d54 | 386 | bt_plugin_set_description(*plugin_out, description); |
55bb57e0 PP |
387 | } |
388 | ||
389 | if (author) { | |
01f50d54 | 390 | bt_plugin_set_author(*plugin_out, author); |
55bb57e0 PP |
391 | } |
392 | ||
393 | if (license) { | |
01f50d54 | 394 | bt_plugin_set_license(*plugin_out, license); |
55bb57e0 PP |
395 | } |
396 | ||
01f50d54 | 397 | bt_plugin_set_version(*plugin_out, major, minor, patch, version_extra); |
55bb57e0 PP |
398 | |
399 | if (PyList_Check(py_comp_class_addrs)) { | |
400 | size_t i; | |
401 | ||
402 | for (i = 0; i < PyList_Size(py_comp_class_addrs); i++) { | |
8eee8ea2 | 403 | bt_component_class *comp_class; |
55bb57e0 PP |
404 | PyObject *py_comp_class_addr; |
405 | ||
406 | py_comp_class_addr = | |
407 | PyList_GetItem(py_comp_class_addrs, i); | |
8b45963b | 408 | BT_ASSERT(py_comp_class_addr); |
55bb57e0 | 409 | if (PyLong_Check(py_comp_class_addr)) { |
c240a5d8 | 410 | comp_class = PyLong_AsVoidPtr(py_comp_class_addr); |
55bb57e0 | 411 | } else { |
01f50d54 PP |
412 | BT_LOG_WRITE(fail_on_load_error ? |
413 | BT_LOG_WARN : BT_LOG_INFO, BT_LOG_TAG, | |
414 | "Component class address is not an integer in Python plugin info object: " | |
9e0bf9b0 PP |
415 | "py-plugin-info-addr=%p, index=%zu", |
416 | plugin_info, i); | |
01f50d54 PP |
417 | |
418 | if (fail_on_load_error) { | |
fb25b9e3 | 419 | status = __BT_FUNC_STATUS_LOADING_ERROR; |
01f50d54 PP |
420 | goto error; |
421 | } | |
422 | ||
55bb57e0 PP |
423 | continue; |
424 | } | |
425 | ||
01f50d54 PP |
426 | status = bt_plugin_add_component_class(*plugin_out, |
427 | comp_class); | |
428 | if (status < 0) { | |
9e0bf9b0 PP |
429 | BT_LOGE("Cannot add component class to plugin: " |
430 | "py-plugin-info-addr=%p, " | |
431 | "plugin-addr=%p, plugin-name=\"%s\", " | |
432 | "comp-class-addr=%p, " | |
433 | "comp-class-name=\"%s\", " | |
434 | "comp-class-type=%s", | |
01f50d54 PP |
435 | plugin_info, *plugin_out, |
436 | bt_plugin_get_name(*plugin_out), | |
9e0bf9b0 PP |
437 | comp_class, |
438 | bt_component_class_get_name(comp_class), | |
439 | bt_component_class_type_string( | |
440 | bt_component_class_get_type(comp_class))); | |
01f50d54 | 441 | goto error; |
55bb57e0 PP |
442 | } |
443 | } | |
444 | } | |
445 | ||
55bb57e0 PP |
446 | goto end; |
447 | ||
448 | error: | |
fb25b9e3 | 449 | BT_ASSERT(status != __BT_FUNC_STATUS_OK); |
01f50d54 | 450 | print_python_traceback(fail_on_load_error ? BT_LOG_WARN : BT_LOG_INFO); |
55bb57e0 | 451 | pyerr_clear(); |
01f50d54 | 452 | BT_OBJECT_PUT_REF_AND_RESET(*plugin_out); |
55bb57e0 PP |
453 | |
454 | end: | |
455 | Py_XDECREF(py_name); | |
456 | Py_XDECREF(py_author); | |
457 | Py_XDECREF(py_description); | |
458 | Py_XDECREF(py_license); | |
459 | Py_XDECREF(py_version); | |
460 | Py_XDECREF(py_comp_class_addrs); | |
01f50d54 | 461 | return status; |
55bb57e0 PP |
462 | } |
463 | ||
6fbd4105 | 464 | G_MODULE_EXPORT |
fb25b9e3 | 465 | int bt_plugin_python_create_all_from_file(const char *path, |
01f50d54 | 466 | bool fail_on_load_error, struct bt_plugin_set **plugin_set_out) |
55bb57e0 | 467 | { |
09fc237b | 468 | bt_plugin *plugin = NULL; |
55bb57e0 PP |
469 | PyObject *py_plugin_info = NULL; |
470 | gchar *basename = NULL; | |
471 | size_t path_len; | |
fb25b9e3 | 472 | int status = __BT_FUNC_STATUS_OK; |
55bb57e0 | 473 | |
8b45963b | 474 | BT_ASSERT(path); |
55bb57e0 PP |
475 | |
476 | if (python_state == PYTHON_STATE_CANNOT_INITIALIZE) { | |
477 | /* | |
478 | * We do not even care about the rest of the function | |
479 | * here because we already know Python cannot be fully | |
480 | * initialized. | |
481 | */ | |
fb25b9e3 | 482 | status = __BT_FUNC_STATUS_ERROR; |
01f50d54 PP |
483 | goto error; |
484 | } else if (python_state == PYTHON_STATE_WONT_INITIALIZE) { | |
485 | /* | |
486 | * This is not an error: the environment requires that | |
487 | * Python plugins are disabled, so it's simply not | |
488 | * found. | |
489 | */ | |
fb25b9e3 | 490 | status = __BT_FUNC_STATUS_NOT_FOUND; |
55bb57e0 PP |
491 | goto error; |
492 | } | |
493 | ||
a684a357 PP |
494 | BT_LOGI("Trying to create all Python plugins from file: path=\"%s\"", |
495 | path); | |
55bb57e0 PP |
496 | path_len = strlen(path); |
497 | ||
498 | /* File name ends with `.py` */ | |
499 | if (strncmp(path + path_len - PYTHON_PLUGIN_FILE_EXT_LEN, | |
500 | PYTHON_PLUGIN_FILE_EXT, | |
501 | PYTHON_PLUGIN_FILE_EXT_LEN) != 0) { | |
a684a357 | 502 | BT_LOGI("Skipping non-Python file: path=\"%s\"", path); |
fb25b9e3 | 503 | status = __BT_FUNC_STATUS_NOT_FOUND; |
55bb57e0 PP |
504 | goto error; |
505 | } | |
506 | ||
507 | /* File name starts with `bt_plugin_` */ | |
508 | basename = g_path_get_basename(path); | |
509 | if (!basename) { | |
01f50d54 | 510 | BT_LOGE("Cannot get path's basename: path=\"%s\"", path); |
fb25b9e3 | 511 | status = __BT_FUNC_STATUS_ERROR; |
55bb57e0 PP |
512 | goto error; |
513 | } | |
514 | ||
515 | if (strncmp(basename, PYTHON_PLUGIN_FILE_PREFIX, | |
516 | PYTHON_PLUGIN_FILE_PREFIX_LEN) != 0) { | |
a684a357 | 517 | BT_LOGI("Skipping Python file not starting with `%s`: " |
9e0bf9b0 | 518 | "path=\"%s\"", PYTHON_PLUGIN_FILE_PREFIX, path); |
fb25b9e3 | 519 | status = __BT_FUNC_STATUS_NOT_FOUND; |
55bb57e0 PP |
520 | goto error; |
521 | } | |
522 | ||
523 | /* | |
524 | * Initialize Python now. | |
525 | * | |
526 | * This is not done in the library contructor because the | |
527 | * interpreter is somewhat slow to initialize. If you don't | |
528 | * have any potential Python plugins, you don't need to endure | |
529 | * this waiting time everytime you load the library. | |
530 | */ | |
531 | init_python(); | |
01f50d54 PP |
532 | if (python_state == PYTHON_STATE_WONT_INITIALIZE) { |
533 | /* | |
534 | * This is not an error: the environment requires that | |
535 | * Python plugins are disabled, so it's simply not | |
536 | * found. | |
537 | */ | |
fb25b9e3 | 538 | status = __BT_FUNC_STATUS_NOT_FOUND; |
01f50d54 PP |
539 | goto error; |
540 | } else if (python_state != PYTHON_STATE_FULLY_INITIALIZED) { | |
55bb57e0 PP |
541 | /* |
542 | * For some reason we cannot initialize Python, | |
543 | * import the required modules, and get the required | |
544 | * attributes from them. | |
545 | */ | |
01f50d54 | 546 | BT_LOGE("Failed to initialize Python interpreter."); |
fb25b9e3 | 547 | status = __BT_FUNC_STATUS_ERROR; |
55bb57e0 PP |
548 | goto error; |
549 | } | |
550 | ||
551 | /* | |
552 | * Call bt2.py_plugin._try_load_plugin_module() with this path | |
553 | * to get plugin info if the plugin is loadable and complete. | |
554 | * This function returns None when there's an error, but just in | |
555 | * case we also manually clear the last Python error state. | |
556 | */ | |
9e0bf9b0 | 557 | BT_LOGD_STR("Getting Python plugin info object from Python module."); |
55bb57e0 PP |
558 | py_plugin_info = PyObject_CallFunction(py_try_load_plugin_module_func, |
559 | "(s)", path); | |
560 | if (!py_plugin_info || py_plugin_info == Py_None) { | |
01f50d54 PP |
561 | BT_LOG_WRITE(fail_on_load_error ? |
562 | BT_LOG_WARN : BT_LOG_INFO, BT_LOG_TAG, | |
563 | "Cannot load Python plugin: path=\"%s\"", path); | |
564 | print_python_traceback(fail_on_load_error ? BT_LOG_WARN : | |
565 | BT_LOG_INFO); | |
55bb57e0 | 566 | PyErr_Clear(); |
fb25b9e3 PP |
567 | status = fail_on_load_error ? __BT_FUNC_STATUS_LOADING_ERROR : |
568 | __BT_FUNC_STATUS_NOT_FOUND; | |
55bb57e0 PP |
569 | goto error; |
570 | } | |
571 | ||
572 | /* | |
573 | * Get bt_plugin from plugin info object. | |
55bb57e0 | 574 | */ |
01f50d54 PP |
575 | plugin = NULL; |
576 | status = bt_plugin_from_python_plugin_info(py_plugin_info, | |
577 | fail_on_load_error, &plugin); | |
578 | if (status < 0) { | |
579 | /* | |
580 | * bt_plugin_from_python_plugin_info() handles | |
581 | * `fail_on_load_error`, so this is a "real" error. | |
582 | */ | |
9e0bf9b0 PP |
583 | BT_LOGW("Cannot create plugin object from Python plugin info object: " |
584 | "path=\"%s\", py-plugin-info-addr=%p", | |
585 | path, py_plugin_info); | |
01f50d54 PP |
586 | BT_ASSERT(!plugin); |
587 | goto error; | |
fb25b9e3 | 588 | } else if (status == __BT_FUNC_STATUS_NOT_FOUND) { |
01f50d54 | 589 | BT_ASSERT(!plugin); |
55bb57e0 PP |
590 | goto error; |
591 | } | |
592 | ||
fb25b9e3 | 593 | BT_ASSERT(status == __BT_FUNC_STATUS_OK); |
01f50d54 | 594 | BT_ASSERT(plugin); |
a8ff38ef | 595 | bt_plugin_set_path(plugin, path); |
01f50d54 PP |
596 | *plugin_set_out = bt_plugin_set_create(); |
597 | if (!*plugin_set_out) { | |
9e0bf9b0 | 598 | BT_LOGE_STR("Cannot create empty plugin set."); |
fb25b9e3 | 599 | status = __BT_FUNC_STATUS_MEMORY_ERROR; |
55bb57e0 PP |
600 | goto error; |
601 | } | |
602 | ||
01f50d54 | 603 | bt_plugin_set_add_plugin(*plugin_set_out, plugin); |
9e0bf9b0 PP |
604 | BT_LOGD("Created all Python plugins from file: path=\"%s\", " |
605 | "plugin-addr=%p, plugin-name=\"%s\"", | |
606 | path, plugin, bt_plugin_get_name(plugin)); | |
55bb57e0 PP |
607 | goto end; |
608 | ||
609 | error: | |
fb25b9e3 | 610 | BT_ASSERT(status != __BT_FUNC_STATUS_OK); |
01f50d54 | 611 | BT_OBJECT_PUT_REF_AND_RESET(*plugin_set_out); |
55bb57e0 PP |
612 | |
613 | end: | |
8c6884d9 | 614 | bt_plugin_put_ref(plugin); |
55bb57e0 PP |
615 | Py_XDECREF(py_plugin_info); |
616 | g_free(basename); | |
01f50d54 | 617 | return status; |
55bb57e0 | 618 | } |