3 * Bacula interface to Python for the Storage Daemon
5 * Kern Sibbald, January MMV
12 Copyright (C) 2005 Kern Sibbald
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.
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.
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,
35 #undef _POSIX_C_SOURCE
38 extern JCR *get_jcr_from_PyObject(PyObject *self);
39 extern PyObject *find_method(PyObject *eventsObject, PyObject *method,
42 static int set_job_events(PyObject *self, PyObject *arg);
50 static struct s_vars getvars[] = {
51 { N_("Job"), "s"}, /* 0 */
52 { N_("SDName"), "s"}, /* 1 */
53 { N_("Level"), "s"}, /* 2 */
54 { N_("Type"), "s"}, /* 3 */
55 { N_("JobId"), "i"}, /* 4 */
56 { N_("Client"), "s"}, /* 5 */
57 { N_("Pool"), "s"}, /* 6 */
58 { N_("MediaType"), "s"}, /* 7 */
59 { N_("JobName"), "s"}, /* 8 */
60 { N_("JobStatus"), "s"}, /* 9 */
61 { N_("VolumeName"), "s"}, /* 10 */
62 { N_("Device"), "s"}, /* 11 */
68 /* Writable variables */
69 static struct s_vars setvars[] = {
70 { N_("set_events"), NULL},
71 { N_("JobReport"), "s"},
78 /* Return Job variables */
79 PyObject *job_getattr(PyObject *self, char *attrname)
87 Dmsg1(100, "In job_getattr=%s\n", attrname);
88 jcr = get_jcr_from_PyObject(self);
90 bstrncpy(errmsg, "Job pointer not found.", sizeof(errmsg));
94 for (i=0; getvars[i].name; i++) {
95 if (strcmp(getvars[i].name, attrname) == 0) {
105 return Py_BuildValue(getvars[i].fmt, jcr->job_name); /* Non-unique name */
106 case 1: /* SD's name */
107 return Py_BuildValue(getvars[i].fmt, my_name);
109 return Py_BuildValue(getvars[i].fmt, job_level_to_str(jcr->JobLevel));
111 return Py_BuildValue(getvars[i].fmt, job_type_to_str(jcr->JobType));
113 return Py_BuildValue(getvars[i].fmt, jcr->JobId);
115 return Py_BuildValue(getvars[i].fmt, jcr->client_name);
117 return Py_BuildValue(getvars[i].fmt, jcr->dcr->pool_name);
118 case 7: /* MediaType */
119 return Py_BuildValue(getvars[i].fmt, jcr->dcr->media_type);
120 case 8: /* JobName */
121 return Py_BuildValue(getvars[i].fmt, jcr->Job);
122 case 9: /* JobStatus */
124 buf[0] = jcr->JobStatus;
125 return Py_BuildValue(getvars[i].fmt, buf);
127 return Py_BuildValue(getvars[i].fmt, jcr->dcr->VolumeName);
129 return Py_BuildValue(getvars[i].fmt, jcr->dcr->dev_name);
132 bsnprintf(errmsg, sizeof(errmsg), "Attribute %s not found.", attrname);
134 PyErr_SetString(PyExc_AttributeError, errmsg);
138 int job_setattr(PyObject *self, char *attrname, PyObject *value)
147 Dmsg2(100, "In job_setattr=%s val=%p.\n", attrname, value);
148 if (value == NULL) { /* Cannot delete variables */
149 bsnprintf(buf, sizeof(buf), "Cannot delete attribute %s", attrname);
153 jcr = get_jcr_from_PyObject(self);
155 errmsg = "Job pointer not found.";
159 /* Find attribute name in list */
160 for (i=0; setvars[i].name; i++) {
161 if (strcmp(setvars[i].name, attrname) == 0) {
169 /* Get argument value ***FIXME*** handle other formats */
170 if (setvars[i].fmt != NULL) {
171 if (!PyArg_Parse(value, setvars[i].fmt, &strval)) {
172 PyErr_SetString(PyExc_TypeError, "Read-only attribute");
177 case 0: /* set_events */
178 return set_job_events(self, value);
179 case 1: /* JobReport */
181 Jmsg(jcr, M_INFO, 0, "%s", strval);
185 bsnprintf(buf, sizeof(buf), "Cannot find attribute %s", attrname);
188 PyErr_SetString(PyExc_AttributeError, errmsg);
193 static int set_job_events(PyObject *self, PyObject *arg)
198 Dmsg0(100, "In set_job_events.\n");
199 if (!PyArg_Parse(arg, "O", &eObject)) {
200 Dmsg0(000, "Parse error looking for Object argument\n");
203 jcr = get_jcr_from_PyObject(self);
205 PyErr_SetString(PyExc_AttributeError, "Job pointer not found.");
208 Py_XDECREF((PyObject *)jcr->Python_events); /* release any old events Object */
210 jcr->Python_events = (void *)eObject; /* set new events */
216 int generate_job_event(JCR *jcr, const char *event)
218 PyObject *method = NULL;
219 PyObject *Job = (PyObject *)jcr->Python_job;
220 PyObject *result = NULL;
227 PyEval_AcquireLock();
229 PyObject *events = (PyObject *)jcr->Python_events;
230 method = find_method(events, method, event);
235 bstrncpy(jcr->event, event, sizeof(jcr->event));
236 result = PyObject_CallFunction(method, "O", Job);
237 jcr->event[0] = 0; /* no event in progress */
238 if (result == NULL) {
239 if (PyErr_Occurred()) {
241 Dmsg1(000, "Error in Python method %s\n", event);
249 PyEval_ReleaseLock();
256 /* Dummy if Python not configured */
257 int generate_job_event(JCR *jcr, const char *event)
261 #endif /* HAVE_PYTHON */