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 extern JCR *get_jcr_from_PyObject(PyObject *self);
39 extern PyObject *find_method(PyObject *eventsObject, PyObject *method,
42 static PyObject *jcr_get(PyObject *self, PyObject *args);
43 static PyObject *jcr_write(PyObject *self, PyObject *args);
44 static PyObject *jcr_set(PyObject *self, PyObject *args, PyObject *keyw);
45 static PyObject *set_jcr_events(PyObject *self, PyObject *args);
48 /* Define Job entry points */
49 PyMethodDef JobMethods[] = {
50 {"get", jcr_get, METH_VARARGS, "Get Job variables."},
51 {"set", (PyCFunction)jcr_set, METH_VARARGS|METH_KEYWORDS,
52 "Set Job variables."},
53 {"set_events", set_jcr_events, METH_VARARGS, "Define Job events."},
54 {"write", jcr_write, METH_VARARGS, "Write output."},
55 {NULL, NULL, 0, NULL} /* last item */
59 static PyObject *open_method = NULL;
60 static PyObject *read_method = NULL;
61 static PyObject *close_method = NULL;
69 /* Read-only variables */
70 static struct s_vars vars[] = {
71 { N_("FDName"), "s"}, /* 0 */
72 { N_("Level"), "s"}, /* 1 */
73 { N_("Type"), "s"}, /* 2 */
74 { N_("JobId"), "i"}, /* 3 */
75 { N_("Client"), "s"}, /* 4 */
76 { N_("JobName"), "s"}, /* 5 */
77 { N_("JobStatus"), "s"}, /* 6 */
82 /* Return Job variables */
83 PyObject *jcr_get(PyObject *self, PyObject *args)
91 if (!PyArg_ParseTuple(args, "s:get", &item)) {
94 jcr = get_jcr_from_PyObject(self);
95 for (i=0; vars[i].name; i++) {
96 if (strcmp(vars[i].name, item) == 0) {
105 case 0: /* FD's name */
106 return Py_BuildValue(vars[i].fmt, my_name);
108 return Py_BuildValue(vars[i].fmt, job_level_to_str(jcr->JobLevel));
110 return Py_BuildValue(vars[i].fmt, job_type_to_str(jcr->JobType));
112 return Py_BuildValue(vars[i].fmt, jcr->JobId);
114 return Py_BuildValue(vars[i].fmt, jcr->client_name);
115 case 5: /* JobName */
116 return Py_BuildValue(vars[i].fmt, jcr->Job);
117 case 6: /* JobStatus */
119 buf[0] = jcr->JobStatus;
120 return Py_BuildValue(vars[i].fmt, buf);
125 /* Set Job variables */
126 PyObject *jcr_set(PyObject *self, PyObject *args, PyObject *keyw)
130 PyObject *fo = NULL, *fr = NULL, *fc = NULL;
131 static char *kwlist[] = {"JobReport",
132 "FileOpen", "FileRead", "FileClose",
135 if (!PyArg_ParseTupleAndKeywords(args, keyw, "|sOOO:set", kwlist,
136 &msg, &fo, &fr, &fc)) {
139 jcr = get_jcr_from_PyObject(self);
142 Jmsg(jcr, M_INFO, 0, "%s", msg);
144 if (strcmp("Reader", jcr->event) == 0) {
146 if (PyCallable_Check(fo)) {
147 jcr->ff->bfd.pio.fo = fo;
149 Jmsg(jcr, M_ERROR, 0, _("Python FileOpen object not callable.\n"));
153 if (PyCallable_Check(fo)) {
154 jcr->ff->bfd.pio.fr = fr;
156 Jmsg(jcr, M_ERROR, 0, _("Python FileRead object not callable.\n"));
160 if (PyCallable_Check(fc)) {
161 jcr->ff->bfd.pio.fc = fc;
163 Jmsg(jcr, M_ERROR, 0, _("Python FileClose object not callable.\n"));
167 return Py_BuildValue("i", 1);
171 static PyObject *set_jcr_events(PyObject *self, PyObject *args)
175 if (!PyArg_ParseTuple(args, "O:set_events_hook", &eObject)) {
179 jcr = get_jcr_from_PyObject(self);
180 jcr->ff->bfd.pio.fo = find_method(eObject, open_method, "open");
181 jcr->ff->bfd.pio.fr = find_method(eObject, read_method, "read");
182 jcr->ff->bfd.pio.fc = find_method(eObject, close_method, "close");
187 /* Write text to job output */
188 static PyObject *jcr_write(PyObject *self, PyObject *args)
191 if (!PyArg_ParseTuple(args, "s:write", &text)) {
195 JCR *jcr = get_jcr_from_PyObject(self);
196 Jmsg(jcr, M_INFO, 0, "%s", text);
203 int generate_job_event(JCR *jcr, const char *event)
205 PyEval_AcquireLock();
207 PyObject *result = PyObject_CallFunction(open_method, "s", "m.py");
208 if (result == NULL) {
214 PyEval_ReleaseLock();
220 /* Dummy if Python not configured */
221 int generate_job_event(JCR *jcr, const char *event)
225 #endif /* HAVE_PYTHON */