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,
37 PyObject *bacula_get(PyObject *self, PyObject *args);
38 PyObject *bacula_set(PyObject *self, PyObject *args, PyObject *keyw);
40 /* Pull in Bacula entry points */
41 extern PyMethodDef BaculaMethods[];
44 /* Start the interpreter */
45 void init_python_interpreter(const char *progname, const char *scripts)
48 Py_SetProgramName((char *)progname);
51 Py_InitModule("bacula", BaculaMethods);
52 bsnprintf(buf, sizeof(buf), "import sys\n"
53 "sys.path.append('%s')\n", scripts);
54 PyRun_SimpleString(buf);
58 void term_python_interpreter()
65 * Generate and process a Bacula event by importing a Python
66 * module and running it.
68 * Returns: 0 if Python not configured or module not found
72 int generate_event(JCR *jcr, const char *event)
74 PyObject *pName, *pModule, *pDict, *pFunc;
75 PyObject *pArgs, *pValue;
77 pName = PyString_FromString(event);
79 Jmsg(jcr, M_ERROR, 0, "Could not convert \"%s\" to Python string.\n", event);
80 return -1; /* Could not convert string */
83 pModule = PyImport_Import(pName);
84 Py_DECREF(pName); /* release pName */
86 if (pModule != NULL) {
87 pDict = PyModule_GetDict(pModule);
88 /* pDict is a borrowed reference */
90 pFunc = PyDict_GetItemString(pDict, (char *)event);
91 /* pFun: Borrowed reference */
93 if (pFunc && PyCallable_Check(pFunc)) {
94 /* Create JCR argument to send to function */
95 pArgs = PyTuple_New(1);
96 pValue = PyCObject_FromVoidPtr((void *)jcr, NULL);
100 Jmsg(jcr, M_ERROR, 0, "Could not convert JCR to Python CObject.\n");
101 return -1; /* Could not convert JCR to CObject */
103 /* pValue reference stolen here: */
104 PyTuple_SetItem(pArgs, 0, pValue);
106 /* Finally, we call the module here */
107 pValue = PyObject_CallObject(pFunc, pArgs);
109 if (pValue != NULL) {
114 Jmsg(jcr, M_ERROR, 0, "Error running Python module: %s\n", event);
115 return 0; /* error running function */
117 /* pDict and pFunc are borrowed and must not be Py_DECREF-ed */
119 if (PyErr_Occurred()) {
122 Jmsg(jcr, M_ERROR, 0, "Python function \"%s\" not found in module.\n", event);
123 return -1; /* function not found */
127 return 0; /* Module not present */
135 * No Python configured
138 int generate_event(JCR *jcr, const char *event) { return 0; }
139 void init_python_interpreter(const char *progname, const char *scripts) { }
140 void term_python_interpreter() { }
143 #endif /* HAVE_PYTHON */