From 8bef21fdee6699c26ecc7919c271d41a7d6b3a6f Mon Sep 17 00:00:00 2001 From: Kern Sibbald Date: Mon, 3 Jan 2005 21:19:58 +0000 Subject: [PATCH] Add Python.c git-svn-id: https://bacula.svn.sourceforge.net/svnroot/bacula/trunk@1786 91ce42f0-d328-0410-95d8-f526ca767f89 --- bacula/src/stored/python.c | 146 +++++++++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 bacula/src/stored/python.c diff --git a/bacula/src/stored/python.c b/bacula/src/stored/python.c new file mode 100644 index 0000000000..e2ff88acb3 --- /dev/null +++ b/bacula/src/stored/python.c @@ -0,0 +1,146 @@ +/* + * + * Bacula interface to Python for the Storage Daemon + * + * Kern Sibbald, January MMV + * + * Version $Id$ + * + */ + +/* + + Copyright (C) 2005 Kern Sibbald + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of + the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public + License along with this program; if not, write to the Free + Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, + MA 02111-1307, USA. + + */ + +#include "bacula.h" +#include "stored.h" + +#ifdef HAVE_PYTHON +#undef _POSIX_C_SOURCE +#include + +PyObject *bacula_get(PyObject *self, PyObject *args); +PyObject *bacula_set(PyObject *self, PyObject *args, PyObject *keyw); + +/* Define Bacula entry points */ +PyMethodDef BaculaMethods[] = { + {"get", bacula_get, METH_VARARGS, "Get Bacula variables."}, + {"set", (PyCFunction)bacula_set, METH_VARARGS|METH_KEYWORDS, + "Set Bacula variables."}, + {NULL, NULL, 0, NULL} /* last item */ +}; + + +struct s_vars { + const char *name; + char *fmt; +}; + +static struct s_vars vars[] = { + { N_("Job"), "s"}, /* 0 */ + { N_("SDName"), "s"}, /* 1 */ + { N_("Level"), "s"}, /* 2 */ + { N_("Type"), "s"}, /* 3 */ + { N_("JobId"), "i"}, /* 4 */ + { N_("Client"), "s"}, /* 5 */ + { N_("Pool"), "s"}, /* 6 */ + { N_("MediaType"), "s"}, /* 7 */ + { N_("JobName"), "s"}, /* 8 */ + { N_("JobStatus"), "s"}, /* 9 */ + { N_("VolumeName"), "s"}, /* 10 */ + { N_("Device"), "s"}, /* 11 */ + + { NULL, NULL} +}; + +/* Return Bacula variables */ +PyObject *bacula_get(PyObject *self, PyObject *args) +{ + PyObject *CObject; + JCR *jcr; + char *item; + bool found = false; + int i; + char buf[10]; + + if (!PyArg_ParseTuple(args, "Os:get", &CObject, &item)) { + return NULL; + } + jcr = (JCR *)PyCObject_AsVoidPtr(CObject); + for (i=0; vars[i].name; i++) { + if (strcmp(vars[i].name, item) == 0) { + found = true; + break; + } + } + if (!found) { + return NULL; + } + switch (i) { + case 0: /* Job */ + return Py_BuildValue(vars[i].fmt, jcr->job_name); /* Non-unique name */ + case 1: /* SD's name */ + return Py_BuildValue(vars[i].fmt, my_name); + case 2: /* level */ + return Py_BuildValue(vars[i].fmt, job_level_to_str(jcr->JobLevel)); + case 3: /* type */ + return Py_BuildValue(vars[i].fmt, job_type_to_str(jcr->JobType)); + case 4: /* JobId */ + return Py_BuildValue(vars[i].fmt, jcr->JobId); + case 5: /* Client */ + return Py_BuildValue(vars[i].fmt, jcr->client_name); + case 6: /* Pool */ + return Py_BuildValue(vars[i].fmt, jcr->dcr->pool_name); + case 7: /* MediaType */ + return Py_BuildValue(vars[i].fmt, jcr->dcr->media_type); + case 8: /* JobName */ + return Py_BuildValue(vars[i].fmt, jcr->Job); + case 9: /* JobStatus */ + buf[1] = 0; + buf[0] = jcr->JobStatus; + return Py_BuildValue(vars[i].fmt, buf); + case 10: + return Py_BuildValue(vars[i].fmt, jcr->dcr->VolumeName); + case 11: + return Py_BuildValue(vars[i].fmt, jcr->dcr->dev_name); + } + return NULL; +} + +/* Set Bacula variables */ +PyObject *bacula_set(PyObject *self, PyObject *args, PyObject *keyw) +{ + PyObject *CObject; + JCR *jcr; + char *msg = NULL; + static char *kwlist[] = {"jcr", "JobReport", NULL}; + if (!PyArg_ParseTupleAndKeywords(args, keyw, "O|ss:set", kwlist, + &CObject, &msg)) { + return NULL; + } + jcr = (JCR *)PyCObject_AsVoidPtr(CObject); + + if (msg) { + Jmsg(jcr, M_INFO, 0, "%s", msg); + } + return Py_BuildValue("i", 1); +} + +#endif /* HAVE_PYTHON */ -- 2.39.5