3 * Bacula common code library interface to Python
5 * Kern Sibbald, November MMIV
12 Copyright (C) 2004-2005 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;
37 #undef _POSIX_C_SOURCE
41 PyObject *bacula_get(PyObject *self, PyObject *args);
42 PyObject *bacula_set(PyObject *self, PyObject *args, PyObject *keyw);
43 int _generate_event(JCR *jcr, const char *event);
45 /* Pull in Bacula entry points */
46 extern PyMethodDef BaculaMethods[];
49 /* Start the interpreter */
50 void init_python_interpreter(const char *progname, const char *scripts)
53 Py_SetProgramName((char *)progname);
56 Py_InitModule("bacula", BaculaMethods);
57 bsnprintf(buf, sizeof(buf), "import sys\n"
58 "sys.path.append('%s')\n", scripts);
59 PyRun_SimpleString(buf);
61 generate_event = _generate_event;
64 void term_python_interpreter()
71 * Generate and process a Bacula event by importing a Python
72 * module and running it.
74 * Returns: 0 if Python not configured or module not found
78 int _generate_event(JCR *jcr, const char *event)
80 PyObject *pName, *pModule, *pDict, *pFunc;
81 PyObject *pArgs, *pValue;
83 Dmsg1(100, "Generate event %s\n", event);
84 pName = PyString_FromString(event);
86 Jmsg(jcr, M_ERROR, 0, "Could not convert \"%s\" to Python string.\n", event);
87 return -1; /* Could not convert string */
90 pModule = PyImport_Import(pName);
91 Py_DECREF(pName); /* release pName */
93 if (pModule != NULL) {
94 pDict = PyModule_GetDict(pModule);
95 /* pDict is a borrowed reference */
97 pFunc = PyDict_GetItemString(pDict, (char *)event);
98 /* pFun: Borrowed reference */
100 if (pFunc && PyCallable_Check(pFunc)) {
101 /* Create JCR argument to send to function */
102 pArgs = PyTuple_New(1);
103 pValue = PyCObject_FromVoidPtr((void *)jcr, NULL);
107 Jmsg(jcr, M_ERROR, 0, "Could not convert JCR to Python CObject.\n");
108 return -1; /* Could not convert JCR to CObject */
110 /* pValue reference stolen here: */
111 PyTuple_SetItem(pArgs, 0, pValue);
113 /* Finally, we call the module here */
114 pValue = PyObject_CallObject(pFunc, pArgs);
116 if (pValue != NULL) {
121 Jmsg(jcr, M_ERROR, 0, "Error running Python module: %s\n", event);
122 return 0; /* error running function */
124 /* pDict and pFunc are borrowed and must not be Py_DECREF-ed */
126 if (PyErr_Occurred()) {
129 Jmsg(jcr, M_ERROR, 0, "Python function \"%s\" not found in module.\n", event);
130 return -1; /* function not found */
134 return 0; /* Module not present */
136 Dmsg0(100, "Generate event OK\n");
143 * No Python configured
146 int _generate_event(JCR *jcr, const char *event) { return 0; }
147 void init_python_interpreter(const char *progname, const char *scripts)
149 generate_event = _generate_event;
151 void term_python_interpreter() { }
154 #endif /* HAVE_PYTHON */