2 Bacula® - The Network Backup Solution
4 Copyright (C) 2005-2008 Free Software Foundation Europe e.V.
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
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.
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
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.
30 * Bacula interface to Python for the File Daemon
32 * Kern Sibbald, March MMV
42 #undef _POSIX_C_SOURCE
45 #include <lib/pythonlib.h>
47 /* Forward referenced functions */
48 static PyObject *set_job_events(PyObject *self, PyObject *arg);
49 static PyObject *job_write(PyObject *self, PyObject *arg);
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 */
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 */
76 /* Writable variables */
77 static struct s_vars setvars[] = {
78 { NT_("JobReport"), "s"},
83 /* Return Job variables */
84 PyObject *job_getattr(PyObject *self, char *attrname)
92 jcr = get_jcr_from_PyObject(self);
94 bstrncpy(errmsg, _("Job pointer not found."), sizeof(errmsg));
97 for (i=0; getvars[i].name; i++) {
98 if (strcmp(getvars[i].name, attrname) == 0) {
104 /* Try our methods */
105 return Py_FindMethod(JobMethods, self, attrname);
108 case 0: /* FD's name */
109 return Py_BuildValue((char *)getvars[i].fmt, my_name);
111 return Py_BuildValue((char *)getvars[i].fmt, job_level_to_str(jcr->getJobLevel()));
113 return Py_BuildValue((char *)getvars[i].fmt, job_type_to_str(jcr->getJobType()));
115 return Py_BuildValue((char *)getvars[i].fmt, jcr->JobId);
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 */
122 buf[0] = jcr->JobStatus;
123 return Py_BuildValue((char *)getvars[i].fmt, buf);
125 bsnprintf(errmsg, sizeof(errmsg), _("Attribute %s not found."), attrname);
127 PyErr_SetString(PyExc_AttributeError, errmsg);
131 int job_setattr(PyObject *self, char *attrname, PyObject *value)
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);
146 jcr = get_jcr_from_PyObject(self);
148 errmsg = _("Job pointer not found.");
152 /* Find attribute name in list */
153 for (i=0; setvars[i].name; i++) {
154 if (strcmp(setvars[i].name, attrname) == 0) {
160 bsnprintf(buf, sizeof(buf), _("Cannot find attribute %s"), attrname);
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"));
172 case 0: /* JobReport */
173 Jmsg(jcr, M_INFO, 0, "%s", strval);
176 bsnprintf(buf, sizeof(buf), _("Cannot find attribute %s"), attrname);
179 PyErr_SetString(PyExc_AttributeError, errmsg);
184 static PyObject *job_write(PyObject *self, PyObject *args)
188 if (!PyArg_ParseTuple(args, "s:write", &text)) {
189 Pmsg0(000, "Parse tuple error in job_write\n");
193 Jmsg(NULL, M_INFO, 0, "%s", text);
200 static PyObject *set_job_events(PyObject *self, PyObject *arg)
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");
210 jcr = get_jcr_from_PyObject(self);
212 PyErr_SetString(PyExc_AttributeError, _("Job pointer not found."));
215 Py_XDECREF((PyObject *)jcr->Python_events); /* release any old events Object */
217 jcr->Python_events = (void *)eObject; /* set new events */
224 int generate_job_event(JCR *jcr, const char *event)
226 PyObject *method = NULL;
227 PyObject *Job = (PyObject *)jcr->Python_job;
228 PyObject *events = (PyObject *)jcr->Python_events;
229 PyObject *result = NULL;
232 if (!Job || !events) {
237 // PyEval_AcquireLock();
239 method = find_method(events, method, event);
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()) {
250 Pmsg1(000, "Error in Python method %s\n", event);
259 // PyEval_ReleaseLock();
266 /* Dummy if Python not configured */
267 int generate_job_event(JCR *jcr, const char *event)
271 #endif /* HAVE_PYTHON */