]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/python.c
Apply Preben 'Peppe' Guldberg <peppe@wielders.org>
[bacula/bacula] / bacula / src / lib / python.c
1 /*
2  *
3  * Bacula common code library interface to Python
4  *
5  * Kern Sibbald, November MMIV
6  *
7  *   Version $Id$
8  *
9  */
10
11 /*
12    Copyright (C) 2004 Kern Sibbald
13
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.
18
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.
23
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,
27    MA 02111-1307, USA.
28
29  */
30
31 #include "bacula.h"
32
33 EVENT_HANDLER *generate_event;
34
35 #ifdef HAVE_PYTHON
36 #undef _POSIX_C_SOURCE
37 #include <Python.h>
38
39
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);
43
44 /* Pull in Bacula entry points */
45 extern PyMethodDef BaculaMethods[];
46
47
48 /* Start the interpreter */
49 void init_python_interpreter(const char *progname, const char *scripts)
50 {
51    char buf[MAXSTRING];
52    Py_SetProgramName((char *)progname);
53    Py_Initialize();
54    PyEval_InitThreads();
55    Py_InitModule("bacula", BaculaMethods);
56    bsnprintf(buf, sizeof(buf), "import sys\n"
57             "sys.path.append('%s')\n", scripts);
58    PyRun_SimpleString(buf);
59    PyEval_ReleaseLock();
60    generate_event = _generate_event;
61 }
62
63 void term_python_interpreter()
64 {
65    Py_Finalize();
66 }
67
68
69 /*
70  * Generate and process a Bacula event by importing a Python
71  *  module and running it.
72  *
73  *  Returns: 0 if Python not configured or module not found
74  *          -1 on Python error
75  *           1 OK
76  */
77 int _generate_event(JCR *jcr, const char *event)
78 {
79    PyObject *pName, *pModule, *pDict, *pFunc;
80    PyObject *pArgs, *pValue;
81
82    pName = PyString_FromString(event);
83    if (!pName) {
84       Jmsg(jcr, M_ERROR, 0, "Could not convert \"%s\" to Python string.\n", event);
85       return -1;                      /* Could not convert string */
86    }
87
88    pModule = PyImport_Import(pName);
89    Py_DECREF(pName);                  /* release pName */
90
91    if (pModule != NULL) {
92       pDict = PyModule_GetDict(pModule);
93       /* pDict is a borrowed reference */
94
95       pFunc = PyDict_GetItemString(pDict, (char *)event);
96       /* pFun: Borrowed reference */
97
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);
102           if (!pValue) {
103              Py_DECREF(pArgs);
104              Py_DECREF(pModule);
105              Jmsg(jcr, M_ERROR, 0, "Could not convert JCR to Python CObject.\n");
106              return -1;               /* Could not convert JCR to CObject */
107           }
108           /* pValue reference stolen here: */
109           PyTuple_SetItem(pArgs, 0, pValue);
110
111           /* Finally, we call the module here */
112           pValue = PyObject_CallObject(pFunc, pArgs);
113           Py_DECREF(pArgs);
114           if (pValue != NULL) {
115              Py_DECREF(pValue);
116           } else {
117              Py_DECREF(pModule);
118              PyErr_Print();
119              Jmsg(jcr, M_ERROR, 0, "Error running Python module: %s\n", event);
120              return 0;                /* error running function */
121           }
122           /* pDict and pFunc are borrowed and must not be Py_DECREF-ed */
123       } else {
124          if (PyErr_Occurred()) {
125             PyErr_Print();
126          }
127          Jmsg(jcr, M_ERROR, 0, "Python function \"%s\" not found in module.\n", event);
128          return -1;                   /* function not found */
129       }
130       Py_DECREF(pModule);
131    } else {
132       return 0;                       /* Module not present */
133    }
134    return 1;
135 }
136
137 #else
138
139 /*
140  *  No Python configured
141  */
142
143 int _generate_event(JCR *jcr, const char *event) { return 0; }
144 void init_python_interpreter(const char *progname, const char *scripts)
145 {
146    generate_event = _generate_event;   
147 }
148 void term_python_interpreter() { }
149
150
151 #endif /* HAVE_PYTHON */