3 * Bacula common code library interface to Python
5 * Kern Sibbald, November MMIV
12 Copyright (C) 2004 Kern Sibbald
14 This program is free software; you can redistribute it and/or
15 modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation; either version 2 of
17 the License, or (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
24 You should have received a copy of the GNU General Public
25 License along with this program; if not, write to the Free
26 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
33 EVENT_HANDLER *generate_event;
36 #undef _POSIX_C_SOURCE
40 PyObject *bacula_get(PyObject *self, PyObject *args);
41 PyObject *bacula_set(PyObject *self, PyObject *args, PyObject *keyw);
42 int _generate_event(JCR *jcr, const char *event);
44 /* Pull in Bacula entry points */
45 extern PyMethodDef BaculaMethods[];
48 /* Start the interpreter */
49 void init_python_interpreter(const char *progname, const char *scripts)
52 Py_SetProgramName((char *)progname);
55 Py_InitModule("bacula", BaculaMethods);
56 bsnprintf(buf, sizeof(buf), "import sys\n"
57 "sys.path.append('%s')\n", scripts);
58 PyRun_SimpleString(buf);
60 generate_event = _generate_event;
63 void term_python_interpreter()
70 * Generate and process a Bacula event by importing a Python
71 * module and running it.
73 * Returns: 0 if Python not configured or module not found
77 int _generate_event(JCR *jcr, const char *event)
79 PyObject *pName, *pModule, *pDict, *pFunc;
80 PyObject *pArgs, *pValue;
82 pName = PyString_FromString(event);
84 Jmsg(jcr, M_ERROR, 0, "Could not convert \"%s\" to Python string.\n", event);
85 return -1; /* Could not convert string */
88 pModule = PyImport_Import(pName);
89 Py_DECREF(pName); /* release pName */
91 if (pModule != NULL) {
92 pDict = PyModule_GetDict(pModule);
93 /* pDict is a borrowed reference */
95 pFunc = PyDict_GetItemString(pDict, (char *)event);
96 /* pFun: Borrowed reference */
98 if (pFunc && PyCallable_Check(pFunc)) {
99 /* Create JCR argument to send to function */
100 pArgs = PyTuple_New(1);
101 pValue = PyCObject_FromVoidPtr((void *)jcr, NULL);
105 Jmsg(jcr, M_ERROR, 0, "Could not convert JCR to Python CObject.\n");
106 return -1; /* Could not convert JCR to CObject */
108 /* pValue reference stolen here: */
109 PyTuple_SetItem(pArgs, 0, pValue);
111 /* Finally, we call the module here */
112 pValue = PyObject_CallObject(pFunc, pArgs);
114 if (pValue != NULL) {
119 Jmsg(jcr, M_ERROR, 0, "Error running Python module: %s\n", event);
120 return 0; /* error running function */
122 /* pDict and pFunc are borrowed and must not be Py_DECREF-ed */
124 if (PyErr_Occurred()) {
127 Jmsg(jcr, M_ERROR, 0, "Python function \"%s\" not found in module.\n", event);
128 return -1; /* function not found */
132 return 0; /* Module not present */
140 * No Python configured
143 int _generate_event(JCR *jcr, const char *event) { return 0; }
144 void init_python_interpreter(const char *progname, const char *scripts)
146 generate_event = _generate_event;
148 void term_python_interpreter() { }
151 #endif /* HAVE_PYTHON */