]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/pythonfd.c
As it seems Linux doesn't comply to its own manpages.
[bacula/bacula] / bacula / src / filed / pythonfd.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2005-2008 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version three of the GNU Affero General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU Affero General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of Kern Sibbald.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28 /*
29  *
30  * Bacula interface to Python for the File Daemon
31  *
32  * Kern Sibbald, March MMV
33  *
34  *   Version $Id$
35  *
36  */
37
38 #include "bacula.h"
39 #include "filed.h"
40
41 #ifdef HAVE_PYTHON
42 #undef _POSIX_C_SOURCE
43 #include <Python.h>
44
45 #include <lib/pythonlib.h>
46
47 /* Forward referenced functions */
48 static PyObject *set_job_events(PyObject *self, PyObject *arg);
49 static PyObject *job_write(PyObject *self, PyObject *arg);
50
51 PyMethodDef JobMethods[] = {
52     {"set_events", set_job_events, METH_VARARGS, "Set Job events"},
53     {"write", job_write, METH_VARARGS, "Write to output"},
54     {NULL, NULL, 0, NULL}             /* last item */
55 };
56
57
58 struct s_vars {
59    const char *name;
60    const char *fmt;
61 };
62
63 /* Read-only variables */
64 static struct s_vars getvars[] = {
65    { NT_("FDName"),     "s"},          /* 0 */
66    { NT_("Level"),      "s"},          /* 1 */
67    { NT_("Type"),       "s"},          /* 2 */
68    { NT_("JobId"),      "i"},          /* 3 */
69    { NT_("Client"),     "s"},          /* 4 */
70    { NT_("JobName"),    "s"},          /* 5 */
71    { NT_("JobStatus"),  "s"},          /* 6 */
72
73    { NULL,             NULL}
74 };
75
76 /* Writable variables */
77 static struct s_vars setvars[] = {
78    { NT_("JobReport"),   "s"},
79
80    { NULL,             NULL}
81 };
82
83 /* Return Job variables */
84 PyObject *job_getattr(PyObject *self, char *attrname)
85 {
86    JCR *jcr;
87    bool found = false;
88    int i;
89    char buf[10];
90    char errmsg[200];
91
92    jcr = get_jcr_from_PyObject(self);
93    if (!jcr) {
94       bstrncpy(errmsg, _("Job pointer not found."), sizeof(errmsg));
95       goto bail_out;
96    }
97    for (i=0; getvars[i].name; i++) {
98       if (strcmp(getvars[i].name, attrname) == 0) {
99          found = true;
100          break;
101       }
102    }
103    if (!found) {
104       /* Try our methods */
105       return Py_FindMethod(JobMethods, self, attrname);
106    }
107    switch (i) {
108    case 0:                            /* FD's name */
109       return Py_BuildValue((char *)getvars[i].fmt, my_name);
110    case 1:                            /* level */
111       return Py_BuildValue((char *)getvars[i].fmt, job_level_to_str(jcr->getJobLevel()));
112    case 2:                            /* type */
113       return Py_BuildValue((char *)getvars[i].fmt, job_type_to_str(jcr->getJobType()));
114    case 3:                            /* JobId */
115       return Py_BuildValue((char *)getvars[i].fmt, jcr->JobId);
116    case 4:                            /* Client */
117       return Py_BuildValue((char *)getvars[i].fmt, jcr->client_name);
118    case 5:                            /* JobName */
119       return Py_BuildValue((char *)getvars[i].fmt, jcr->Job);
120    case 6:                            /* JobStatus */
121       buf[1] = 0;
122       buf[0] = jcr->JobStatus;
123       return Py_BuildValue((char *)getvars[i].fmt, buf);
124    }
125    bsnprintf(errmsg, sizeof(errmsg), _("Attribute %s not found."), attrname);
126 bail_out:
127    PyErr_SetString(PyExc_AttributeError, errmsg);
128    return NULL;
129 }
130
131 int job_setattr(PyObject *self, char *attrname, PyObject *value)
132 {
133   JCR *jcr;
134    bool found = false;
135    char *strval = NULL;
136    char buf[200];
137    char *errmsg;
138    int i;
139
140    Dmsg2(100, "In job_setattr=%s val=%p.\n", attrname, value);
141    if (value == NULL) {                /* Cannot delete variables */
142       bsnprintf(buf, sizeof(buf), _("Cannot delete attribute %s"), attrname);
143       errmsg = buf;
144       goto bail_out;
145    }
146    jcr = get_jcr_from_PyObject(self);
147    if (!jcr) {
148       errmsg = _("Job pointer not found.");
149       goto bail_out;
150    }
151
152    /* Find attribute name in list */
153    for (i=0; setvars[i].name; i++) {
154       if (strcmp(setvars[i].name, attrname) == 0) {
155          found = true;
156          break;
157       }
158    }
159    if (!found) {
160       bsnprintf(buf, sizeof(buf), _("Cannot find attribute %s"), attrname);
161       errmsg = buf;
162       goto bail_out;
163    }
164    /* Get argument value ***FIXME*** handle other formats */
165    if (setvars[i].fmt != NULL) {
166       if (!PyArg_Parse(value, (char *)setvars[i].fmt, &strval)) {
167          PyErr_SetString(PyExc_TypeError, _("Read-only attribute"));
168          return -1;
169       }
170    }   
171    switch (i) {
172    case 0:                            /* JobReport */
173       Jmsg(jcr, M_INFO, 0, "%s", strval);
174       return 0;
175    }
176    bsnprintf(buf, sizeof(buf), _("Cannot find attribute %s"), attrname);
177    errmsg = buf;
178 bail_out:
179    PyErr_SetString(PyExc_AttributeError, errmsg);
180    return -1;
181 }
182
183
184 static PyObject *job_write(PyObject *self, PyObject *args)
185 {
186    char *text = NULL;
187
188    if (!PyArg_ParseTuple(args, "s:write", &text)) {
189       Pmsg0(000, "Parse tuple error in job_write\n");
190       return NULL;
191    }
192    if (text) {
193       Jmsg(NULL, M_INFO, 0, "%s", text);
194    }
195    Py_INCREF(Py_None);
196    return Py_None;
197 }
198
199
200 static PyObject *set_job_events(PyObject *self, PyObject *arg)
201 {
202    PyObject *eObject;
203    JCR *jcr;
204
205    Dmsg0(100, "In set_job_events.\n");
206    if (!PyArg_ParseTuple(arg, "O", &eObject)) {
207       Pmsg0(000, "Parse error looking for Object argument\n");
208       return NULL;
209    }
210    jcr = get_jcr_from_PyObject(self);
211    if (!jcr) {
212       PyErr_SetString(PyExc_AttributeError, _("Job pointer not found."));
213       return NULL;
214    }
215    Py_XDECREF((PyObject *)jcr->Python_events);  /* release any old events Object */
216    Py_INCREF(eObject);
217    jcr->Python_events = (void *)eObject;        /* set new events */
218
219    Py_INCREF(Py_None);
220    return Py_None;
221 }
222
223
224 int generate_job_event(JCR *jcr, const char *event)
225 {
226    PyObject *method = NULL;
227    PyObject *Job = (PyObject *)jcr->Python_job;
228    PyObject *events = (PyObject *)jcr->Python_events;
229    PyObject *result = NULL;
230    int stat = 0;
231
232    if (!Job || !events) {
233       return 0;
234    }
235
236    lock_python();
237 // PyEval_AcquireLock();
238
239    method = find_method(events, method, event);
240    if (!method) {
241       goto bail_out;
242    }
243
244    bstrncpy(jcr->event, event, sizeof(jcr->event));
245    result = PyObject_CallFunction(method, (char *)"O", Job);
246    jcr->event[0] = 0;             /* no event in progress */
247    if (result == NULL) {
248       if (PyErr_Occurred()) {
249          PyErr_Print();
250          Pmsg1(000, "Error in Python method %s\n", event);
251       }
252    } else {
253       stat = 1;
254    }
255    Py_XDECREF(result);
256
257 bail_out:
258    unlock_python();
259 // PyEval_ReleaseLock();
260    return stat;
261 }
262
263
264 #else
265
266 /* Dummy if Python not configured */
267 int generate_job_event(JCR *jcr, const char *event)
268 { return 1; }
269
270
271 #endif /* HAVE_PYTHON */