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 two of the GNU 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 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 John Walker.
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
46 extern JCR *get_jcr_from_PyObject(PyObject *self);
47 extern PyObject *find_method(PyObject *eventsObject, PyObject *method,
50 /* Forward referenced functions */
51 static PyObject *set_job_events(PyObject *self, PyObject *arg);
52 static PyObject *job_write(PyObject *self, PyObject *arg);
54 PyMethodDef JobMethods[] = {
55 {"set_events", set_job_events, METH_VARARGS, "Set Job events"},
56 {"write", job_write, METH_VARARGS, "Write to output"},
57 {NULL, NULL, 0, NULL} /* last item */
66 /* Read-only variables */
67 static struct s_vars getvars[] = {
68 { NT_("FDName"), "s"}, /* 0 */
69 { NT_("Level"), "s"}, /* 1 */
70 { NT_("Type"), "s"}, /* 2 */
71 { NT_("JobId"), "i"}, /* 3 */
72 { NT_("Client"), "s"}, /* 4 */
73 { NT_("JobName"), "s"}, /* 5 */
74 { NT_("JobStatus"), "s"}, /* 6 */
79 /* Writable variables */
80 static struct s_vars setvars[] = {
81 { NT_("JobReport"), "s"},
86 /* Return Job variables */
87 PyObject *job_getattr(PyObject *self, char *attrname)
95 jcr = get_jcr_from_PyObject(self);
97 bstrncpy(errmsg, _("Job pointer not found."), sizeof(errmsg));
100 for (i=0; getvars[i].name; i++) {
101 if (strcmp(getvars[i].name, attrname) == 0) {
107 /* Try our methods */
108 return Py_FindMethod(JobMethods, self, attrname);
111 case 0: /* FD's name */
112 return Py_BuildValue(getvars[i].fmt, my_name);
114 return Py_BuildValue(getvars[i].fmt, job_level_to_str(jcr->JobLevel));
116 return Py_BuildValue(getvars[i].fmt, job_type_to_str(jcr->JobType));
118 return Py_BuildValue(getvars[i].fmt, jcr->JobId);
120 return Py_BuildValue(getvars[i].fmt, jcr->client_name);
121 case 5: /* JobName */
122 return Py_BuildValue(getvars[i].fmt, jcr->Job);
123 case 6: /* JobStatus */
125 buf[0] = jcr->JobStatus;
126 return Py_BuildValue(getvars[i].fmt, buf);
128 bsnprintf(errmsg, sizeof(errmsg), _("Attribute %s not found."), attrname);
130 PyErr_SetString(PyExc_AttributeError, errmsg);
134 int job_setattr(PyObject *self, char *attrname, PyObject *value)
143 Dmsg2(100, "In job_setattr=%s val=%p.\n", attrname, value);
144 if (value == NULL) { /* Cannot delete variables */
145 bsnprintf(buf, sizeof(buf), _("Cannot delete attribute %s"), attrname);
149 jcr = get_jcr_from_PyObject(self);
151 errmsg = _("Job pointer not found.");
155 /* Find attribute name in list */
156 for (i=0; setvars[i].name; i++) {
157 if (strcmp(setvars[i].name, attrname) == 0) {
163 bsnprintf(buf, sizeof(buf), _("Cannot find attribute %s"), attrname);
167 /* Get argument value ***FIXME*** handle other formats */
168 if (setvars[i].fmt != NULL) {
169 if (!PyArg_Parse(value, setvars[i].fmt, &strval)) {
170 PyErr_SetString(PyExc_TypeError, _("Read-only attribute"));
175 case 0: /* JobReport */
176 Jmsg(jcr, M_INFO, 0, "%s", strval);
179 bsnprintf(buf, sizeof(buf), _("Cannot find attribute %s"), attrname);
182 PyErr_SetString(PyExc_AttributeError, errmsg);
187 static PyObject *job_write(PyObject *self, PyObject *args)
191 if (!PyArg_ParseTuple(args, "s:write", &text)) {
192 Pmsg0(000, "Parse tuple error in job_write\n");
196 Jmsg(NULL, M_INFO, 0, "%s", text);
203 static PyObject *set_job_events(PyObject *self, PyObject *arg)
208 Dmsg0(100, "In set_job_events.\n");
209 if (!PyArg_ParseTuple(arg, "O", &eObject)) {
210 Pmsg0(000, "Parse error looking for Object argument\n");
213 jcr = get_jcr_from_PyObject(self);
215 PyErr_SetString(PyExc_AttributeError, _("Job pointer not found."));
218 Py_XDECREF((PyObject *)jcr->Python_events); /* release any old events Object */
220 jcr->Python_events = (void *)eObject; /* set new events */
227 int generate_job_event(JCR *jcr, const char *event)
229 PyObject *method = NULL;
230 PyObject *Job = (PyObject *)jcr->Python_job;
231 PyObject *events = (PyObject *)jcr->Python_events;
232 PyObject *result = NULL;
235 if (!Job || !events) {
240 // PyEval_AcquireLock();
242 method = find_method(events, method, event);
247 bstrncpy(jcr->event, event, sizeof(jcr->event));
248 result = PyObject_CallFunction(method, "O", Job);
249 jcr->event[0] = 0; /* no event in progress */
250 if (result == NULL) {
251 if (PyErr_Occurred()) {
253 Pmsg1(000, "Error in Python method %s\n", event);
262 // PyEval_ReleaseLock();
269 /* Dummy if Python not configured */
270 int generate_job_event(JCR *jcr, const char *event)
274 #endif /* HAVE_PYTHON */