]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/python.c
- While using the rescue CDROM after my computer would not
[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 #ifdef HAVE_PYTHON
34 #include <Python.h>
35
36
37 PyObject *bacula_get(PyObject *self, PyObject *args);
38 PyObject *bacula_set(PyObject *self, PyObject *args, PyObject *keyw);
39
40 /* Pull in Bacula entry points */
41 extern PyMethodDef BaculaMethods[];
42
43
44 /* Start the interpreter */
45 void init_python_interpreter(const char *progname, const char *scripts)
46 {
47    char buf[MAXSTRING];
48    Py_SetProgramName((char *)progname);
49    Py_Initialize();
50    PyEval_InitThreads();
51    Py_InitModule("bacula", BaculaMethods);
52    bsnprintf(buf, sizeof(buf), "import sys\n"
53             "sys.path.append('%s')\n", scripts);
54    PyRun_SimpleString(buf);
55    PyEval_ReleaseLock();
56 }
57
58 void term_python_interpreter()
59 {
60    Py_Finalize();
61 }
62
63
64 /* 
65  * Generate and process a Bacula event by importing a Python
66  *  module and running it.
67  *
68  *  Returns: 0 if Python not configured or module not found
69  *          -1 on Python error
70  *           1 OK
71  */
72 int generate_event(JCR *jcr, const char *event)
73 {
74    PyObject *pName, *pModule, *pDict, *pFunc;
75    PyObject *pArgs, *pValue;
76
77    pName = PyString_FromString(event);
78    if (!pName) {
79       Jmsg(jcr, M_ERROR, 0, "Could not convert \"%s\" to Python string.\n", event);
80       return -1;                      /* Could not convert string */
81    }
82
83    pModule = PyImport_Import(pName);
84    Py_DECREF(pName);                  /* release pName */
85
86    if (pModule != NULL) {
87       pDict = PyModule_GetDict(pModule);
88       /* pDict is a borrowed reference */
89
90       pFunc = PyDict_GetItemString(pDict, (char *)event);
91       /* pFun: Borrowed reference */
92
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);
97           if (!pValue) {
98              Py_DECREF(pArgs);
99              Py_DECREF(pModule);
100              Jmsg(jcr, M_ERROR, 0, "Could not convert JCR to Python CObject.\n");
101              return -1;               /* Could not convert JCR to CObject */
102           }
103           /* pValue reference stolen here: */
104           PyTuple_SetItem(pArgs, 0, pValue);
105
106           /* Finally, we call the module here */
107           pValue = PyObject_CallObject(pFunc, pArgs);
108           Py_DECREF(pArgs);
109           if (pValue != NULL) {
110              Py_DECREF(pValue);
111           } else {
112              Py_DECREF(pModule);
113              PyErr_Print();
114              Jmsg(jcr, M_ERROR, 0, "Error running Python module: %s\n", event);
115              return 0;                /* error running function */
116           }
117           /* pDict and pFunc are borrowed and must not be Py_DECREF-ed */
118       } else {
119          if (PyErr_Occurred()) {
120             PyErr_Print();
121          }
122          Jmsg(jcr, M_ERROR, 0, "Python function \"%s\" not found in module.\n", event);
123          return -1;                   /* function not found */ 
124       }
125       Py_DECREF(pModule);
126    } else {
127       return 0;                       /* Module not present */
128    }
129    return 1;
130 }
131
132 #else
133
134 /*
135  *  No Python configured
136  */
137
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() { }
141
142
143 #endif /* HAVE_PYTHON */