3 * Bacula interface to Python for the File Daemon
5 * Kern Sibbald, March 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 /* External function pointers to be set */
39 extern bool (*python_set_prog)(JCR *jcr, const char *prog);
40 extern int (*python_open)(BFILE *bfd, const char *fname, int flags, mode_t mode);
41 extern int (*python_close)(BFILE *bfd);
42 extern ssize_t (*python_read)(BFILE *bfd, void *buf, size_t count);
45 extern JCR *get_jcr_from_PyObject(PyObject *self);
46 extern PyObject *find_method(PyObject *eventsObject, PyObject *method,
49 /* Forward referenced functions */
50 static int set_job_events(PyObject *self, PyObject *arg);
52 bool my_python_set_prog(JCR *jcr, const char *prog);
53 int my_python_open(BFILE *bfd, const char *fname, int flags, mode_t mode);
54 int my_python_close(BFILE *bfd);
55 ssize_t my_python_read(BFILE *bfd, void *buf, size_t count);
63 /* Read-only variables */
64 static struct s_vars getvars[] = {
65 { N_("FDName"), "s"}, /* 0 */
66 { N_("Level"), "s"}, /* 1 */
67 { N_("Type"), "s"}, /* 2 */
68 { N_("JobId"), "i"}, /* 3 */
69 { N_("Client"), "s"}, /* 4 */
70 { N_("JobName"), "s"}, /* 5 */
71 { N_("JobStatus"), "s"}, /* 6 */
76 /* Writable variables */
77 static struct s_vars setvars[] = {
78 { N_("set_events"), NULL},
79 { N_("JobReport"), "s"},
85 /* Return Job variables */
86 PyObject *job_getattr(PyObject *self, char *attrname)
94 jcr = get_jcr_from_PyObject(self);
96 bstrncpy(errmsg, "Job pointer not found.", sizeof(errmsg));
99 for (i=0; getvars[i].name; i++) {
100 if (strcmp(getvars[i].name, attrname) == 0) {
109 case 0: /* FD's name */
110 return Py_BuildValue(getvars[i].fmt, my_name);
112 return Py_BuildValue(getvars[i].fmt, job_level_to_str(jcr->JobLevel));
114 return Py_BuildValue(getvars[i].fmt, job_type_to_str(jcr->JobType));
116 return Py_BuildValue(getvars[i].fmt, jcr->JobId);
118 return Py_BuildValue(getvars[i].fmt, jcr->client_name);
119 case 5: /* JobName */
120 return Py_BuildValue(getvars[i].fmt, jcr->Job);
121 case 6: /* JobStatus */
123 buf[0] = jcr->JobStatus;
124 return Py_BuildValue(getvars[i].fmt, buf);
127 bsnprintf(errmsg, sizeof(errmsg), "Attribute %s not found.", attrname);
129 PyErr_SetString(PyExc_AttributeError, errmsg);
133 int job_setattr(PyObject *self, char *attrname, PyObject *value)
142 Dmsg2(100, "In job_setattr=%s val=%p.\n", attrname, value);
143 if (value == NULL) { /* Cannot delete variables */
144 bsnprintf(buf, sizeof(buf), "Cannot delete attribute %s", attrname);
148 jcr = get_jcr_from_PyObject(self);
150 errmsg = "Job pointer not found.";
154 /* Find attribute name in list */
155 for (i=0; setvars[i].name; i++) {
156 if (strcmp(setvars[i].name, attrname) == 0) {
162 bsnprintf(buf, sizeof(buf), "Cannot find attribute %s", attrname);
166 /* Get argument value ***FIXME*** handle other formats */
167 if (setvars[i].fmt != NULL) {
168 if (!PyArg_Parse(value, setvars[i].fmt, &strval)) {
169 PyErr_SetString(PyExc_TypeError, "Read-only attribute");
174 case 0: /* set_events */
175 return set_job_events(self, value);
176 case 1: /* JobReport */
178 Jmsg(jcr, M_INFO, 0, "%s", strval);
181 bsnprintf(buf, sizeof(buf), "Cannot find attribute %s", attrname);
184 PyErr_SetString(PyExc_AttributeError, errmsg);
190 static int set_job_events(PyObject *self, PyObject *arg)
195 Dmsg0(100, "In set_job_events.\n");
196 if (!PyArg_Parse(arg, "O", &eObject)) {
197 Dmsg0(000, "Parse error looking for Object argument\n");
200 jcr = get_jcr_from_PyObject(self);
202 PyErr_SetString(PyExc_AttributeError, "Job pointer not found.");
205 Py_XDECREF((PyObject *)jcr->Python_events); /* release any old events Object */
207 jcr->Python_events = (void *)eObject; /* set new events */
209 /* Set function pointers to call here */
210 python_set_prog = my_python_set_prog;
211 python_open = my_python_open;
212 python_close = my_python_close;
213 python_read = my_python_read;
219 int generate_job_event(JCR *jcr, const char *event)
221 PyObject *method = NULL;
222 PyObject *Job = (PyObject *)jcr->Python_job;
223 PyObject *result = NULL;
230 PyEval_AcquireLock();
232 PyObject *events = (PyObject *)jcr->Python_events;
233 method = find_method(events, method, event);
238 bstrncpy(jcr->event, event, sizeof(jcr->event));
239 result = PyObject_CallFunction(method, "O", Job);
240 jcr->event[0] = 0; /* no event in progress */
241 if (result == NULL) {
242 if (PyErr_Occurred()) {
244 Dmsg1(000, "Error in Python method %s\n", event);
252 PyEval_ReleaseLock();
257 bool my_python_set_prog(JCR *jcr, const char *prog)
259 PyObject *events = (PyObject *)jcr->Python_events;
260 BFILE *bfd = &jcr->ff->bfd;
261 char method[MAX_NAME_LENGTH];
266 bstrncpy(method, prog, sizeof(method));
267 bstrncat(method, "_", sizeof(method));
268 bstrncat(method, "open", sizeof(method));
269 bfd->pio.fo = find_method(events, bfd->pio.fo, method);
270 bstrncpy(method, prog, sizeof(method));
271 bstrncat(method, "_", sizeof(method));
272 bstrncat(method, "read", sizeof(method));
273 bfd->pio.fr = find_method(events, bfd->pio.fr, method);
274 bstrncpy(method, prog, sizeof(method));
275 bstrncat(method, "_", sizeof(method));
276 bstrncat(method, "close", sizeof(method));
277 bfd->pio.fc = find_method(events, bfd->pio.fc, method);
278 return bfd->pio.fo && bfd->pio.fr && bfd->pio.fc;
281 int my_python_open(BFILE *bfd, const char *fname, int flags, mode_t mode)
286 int my_python_close(BFILE *bfd)
291 ssize_t my_python_read(BFILE *bfd, void *buf, size_t count)
298 /* Dummy if Python not configured */
299 int generate_job_event(JCR *jcr, const char *event)
303 #endif /* HAVE_PYTHON */