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,
36 #undef _POSIX_C_SOURCE
39 bool run_module(const char *module);
41 PyObject *bacula_get(PyObject *self, PyObject *args);
42 PyObject *bacula_set(PyObject *self, PyObject *args, PyObject *keyw);
43 PyObject *bacula_run(PyObject *self, PyObject *args);
45 /* Define Bacula entry points */
46 PyMethodDef BaculaMethods[] = {
47 {"get", bacula_get, METH_VARARGS, "Get Bacula variables."},
48 {"set", (PyCFunction)bacula_set, METH_VARARGS|METH_KEYWORDS,
49 "Set Bacula variables."},
50 {"run", (PyCFunction)bacula_run, METH_VARARGS, "Run a Bacula command."},
51 {NULL, NULL, 0, NULL} /* last item */
60 static struct s_vars vars[] = {
67 { N_("NumVols"), "i"},
69 { N_("Storage"), "s"},
70 { N_("Catalog"), "s"},
71 { N_("MediaType"), "s"},
72 { N_("JobName"), "s"},
73 { N_("JobStatus"), "s"},
78 /* Return Bacula variables */
79 PyObject *bacula_get(PyObject *self, PyObject *args)
88 if (!PyArg_ParseTuple(args, "Os:get", &CObject, &item)) {
91 jcr = (JCR *)PyCObject_AsVoidPtr(CObject);
92 for (i=0; vars[i].name; i++) {
93 if (strcmp(vars[i].name, item) == 0) {
103 return Py_BuildValue(vars[i].fmt, jcr->job->hdr.name);
104 case 1: /* Director's name */
105 return Py_BuildValue(vars[i].fmt, my_name);
107 return Py_BuildValue(vars[i].fmt, job_level_to_str(jcr->JobLevel));
109 return Py_BuildValue(vars[i].fmt, job_type_to_str(jcr->JobType));
111 return Py_BuildValue(vars[i].fmt, jcr->JobId);
113 return Py_BuildValue(vars[i].fmt, jcr->client->hdr.name);
114 case 6: /* NumVols */
115 return Py_BuildValue(vars[i].fmt, jcr->NumVols);
117 return Py_BuildValue(vars[i].fmt, jcr->pool->hdr.name);
118 case 8: /* Storage */
119 return Py_BuildValue(vars[i].fmt, jcr->store->hdr.name);
121 return Py_BuildValue(vars[i].fmt, jcr->catalog->hdr.name);
122 case 10: /* MediaType */
123 return Py_BuildValue(vars[i].fmt, jcr->store->media_type);
124 case 11: /* JobName */
125 return Py_BuildValue(vars[i].fmt, jcr->Job);
126 case 12: /* JobStatus */
128 buf[0] = jcr->JobStatus;
129 return Py_BuildValue(vars[i].fmt, buf);
134 /* Set Bacula variables */
135 PyObject *bacula_set(PyObject *self, PyObject *args, PyObject *keyw)
140 char *VolumeName = NULL;
141 static char *kwlist[] = {"jcr", "JobReport", "VolumeName", NULL};
142 if (!PyArg_ParseTupleAndKeywords(args, keyw, "O|ss:set", kwlist,
143 &CObject, &msg, &VolumeName)) {
146 jcr = (JCR *)PyCObject_AsVoidPtr(CObject);
149 Jmsg(jcr, M_INFO, 0, "%s", msg);
152 if (is_volume_name_legal(NULL, VolumeName)) {
153 pm_strcpy(jcr->VolumeName, VolumeName);
155 return Py_BuildValue("i", 0); /* invalid volume name */
158 return Py_BuildValue("i", 1);
161 /* Run a Bacula command */
162 PyObject *bacula_run(PyObject *self, PyObject *args)
169 if (!PyArg_ParseTuple(args, "Os:get", &CObject, &item)) {
172 jcr = (JCR *)PyCObject_AsVoidPtr(CObject);
173 UAContext *ua = new_ua_context(jcr);
175 pm_strcpy(ua->cmd, item); /* copy command */
176 parse_ua_args(ua); /* parse command */
177 stat = run_cmd(ua, ua->cmd);
179 return Py_BuildValue("i", stat);
183 #endif /* HAVE_PYTHON */