3 * Bacula interface to Python for the Director
5 * Kern Sibbald, November MMIV
13 Copyright (C) 2004 Kern Sibbald
15 This program is free software; you can redistribute it and/or
16 modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation; either version 2 of
18 the License, or (at your option) any later version.
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 General Public License for more details.
25 You should have received a copy of the GNU General Public
26 License along with this program; if not, write to the Free
27 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
38 bool run_module(const char *module);
40 PyObject *bacula_get(PyObject *self, PyObject *args);
41 PyObject *bacula_set(PyObject *self, PyObject *args, PyObject *keyw);
42 PyObject *bacula_run(PyObject *self, PyObject *args);
44 /* Define Bacula entry points */
45 PyMethodDef BaculaMethods[] = {
46 {"get", bacula_get, METH_VARARGS, "Get Bacula variables."},
47 {"set", (PyCFunction)bacula_set, METH_VARARGS|METH_KEYWORDS,
48 "Set Bacula variables."},
49 {"run", (PyCFunction)bacula_run, METH_VARARGS, "Run a Bacula command."},
50 {NULL, NULL, 0, NULL} /* last item */
59 static struct s_vars vars[] = {
66 { N_("NumVols"), "i"},
68 { N_("Storage"), "s"},
69 { N_("Catalog"), "s"},
70 { N_("MediaType"), "s"},
71 { N_("JobName"), "s"},
72 { N_("JobStatus"), "s"},
77 /* Return Bacula variables */
78 PyObject *bacula_get(PyObject *self, PyObject *args)
87 if (!PyArg_ParseTuple(args, "Os:get", &CObject, &item)) {
90 jcr = (JCR *)PyCObject_AsVoidPtr(CObject);
91 for (i=0; vars[i].name; i++) {
92 if (strcmp(vars[i].name, item) == 0) {
102 return Py_BuildValue(vars[i].fmt, jcr->job->hdr.name);
103 case 1: /* Director's name */
104 return Py_BuildValue(vars[i].fmt, my_name);
106 return Py_BuildValue(vars[i].fmt, job_level_to_str(jcr->JobLevel));
108 return Py_BuildValue(vars[i].fmt, job_type_to_str(jcr->JobType));
110 return Py_BuildValue(vars[i].fmt, jcr->JobId);
112 return Py_BuildValue(vars[i].fmt, jcr->client->hdr.name);
113 case 6: /* NumVols */
114 return Py_BuildValue(vars[i].fmt, jcr->NumVols);
116 return Py_BuildValue(vars[i].fmt, jcr->pool->hdr.name);
117 case 8: /* Storage */
118 return Py_BuildValue(vars[i].fmt, jcr->store->hdr.name);
120 return Py_BuildValue(vars[i].fmt, jcr->catalog->hdr.name);
121 case 10: /* MediaType */
122 return Py_BuildValue(vars[i].fmt, jcr->store->media_type);
123 case 11: /* JobName */
124 return Py_BuildValue(vars[i].fmt, jcr->Job);
125 case 12: /* JobStatus */
127 buf[0] = jcr->JobStatus;
128 return Py_BuildValue(vars[i].fmt, buf);
133 /* Set Bacula variables */
134 PyObject *bacula_set(PyObject *self, PyObject *args, PyObject *keyw)
139 char *VolumeName = NULL;
140 static char *kwlist[] = {"jcr", "JobReport", "VolumeName", NULL};
141 if (!PyArg_ParseTupleAndKeywords(args, keyw, "O|ss:set", kwlist,
142 &CObject, &msg, &VolumeName)) {
145 jcr = (JCR *)PyCObject_AsVoidPtr(CObject);
148 Jmsg(jcr, M_INFO, 0, "%s", msg);
151 pm_strcpy(jcr->VolumeName, VolumeName);
153 return Py_BuildValue("i", 1);
156 /* Run a Bacula command */
157 PyObject *bacula_run(PyObject *self, PyObject *args)
164 if (!PyArg_ParseTuple(args, "Os:get", &CObject, &item)) {
167 jcr = (JCR *)PyCObject_AsVoidPtr(CObject);
168 UAContext *ua = new_ua_context(jcr);
170 pm_strcpy(ua->cmd, item); /* copy command */
171 parse_ua_args(ua); /* parse command */
172 stat = run_cmd(ua, ua->cmd);
174 return Py_BuildValue("i", stat);
178 #endif /* HAVE_PYTHON */