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