]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/python.c
Apply Preben 'Peppe' Guldberg <peppe@wielders.org>
[bacula/bacula] / bacula / src / dird / python.c
1 /*
2  *
3  * Bacula interface to Python for the Director
4  *
5  * Kern Sibbald, November MMIV
6  *
7  *   Version $Id$
8  *
9  */
10
11 /*
12
13    Copyright (C) 2004 Kern Sibbald
14
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.
19
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.
24
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,
28    MA 02111-1307, USA.
29
30  */
31
32 #include "bacula.h"
33 #include "dird.h"
34
35 #ifdef HAVE_PYTHON
36 #undef _POSIX_C_SOURCE
37 #include <Python.h>
38
39 bool run_module(const char *module);
40
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);
44
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 */
52 };
53
54
55 struct s_vars {
56    const char *name;
57    char *fmt;
58 };
59
60 static struct s_vars vars[] = {
61    { N_("Job"),        "s"},
62    { N_("Dir"),        "s"},
63    { N_("Level"),      "s"},
64    { N_("Type"),       "s"},
65    { N_("JobId"),      "i"},
66    { N_("Client"),     "s"},
67    { N_("NumVols"),    "i"},
68    { N_("Pool"),       "s"},
69    { N_("Storage"),    "s"},
70    { N_("Catalog"),    "s"},
71    { N_("MediaType"),  "s"},
72    { N_("JobName"),    "s"},
73    { N_("JobStatus"),  "s"},
74
75    { NULL,             NULL}
76 };
77
78 /* Return Bacula variables */
79 PyObject *bacula_get(PyObject *self, PyObject *args)
80 {
81    PyObject *CObject;
82    JCR *jcr;
83    char *item;
84    bool found = false;
85    int i;
86    char buf[10];
87
88    if (!PyArg_ParseTuple(args, "Os:get", &CObject, &item)) {
89       return NULL;
90    }
91    jcr = (JCR *)PyCObject_AsVoidPtr(CObject);
92    for (i=0; vars[i].name; i++) {
93       if (strcmp(vars[i].name, item) == 0) {
94          found = true;
95          break;
96       }
97    }
98    if (!found) {
99       return NULL;
100    }
101    switch (i) {
102    case 0:                            /* Job */
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);
106    case 2:                            /* level */
107       return Py_BuildValue(vars[i].fmt, job_level_to_str(jcr->JobLevel));
108    case 3:                            /* type */
109       return Py_BuildValue(vars[i].fmt, job_type_to_str(jcr->JobType));
110    case 4:                            /* JobId */
111       return Py_BuildValue(vars[i].fmt, jcr->JobId);
112    case 5:                            /* Client */
113       return Py_BuildValue(vars[i].fmt, jcr->client->hdr.name);
114    case 6:                            /* NumVols */
115       return Py_BuildValue(vars[i].fmt, jcr->NumVols);
116    case 7:                            /* Pool */
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);
120    case 9:
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 */
127       buf[1] = 0;
128       buf[0] = jcr->JobStatus;
129       return Py_BuildValue(vars[i].fmt, buf);
130    }
131    return NULL;
132 }
133
134 /* Set Bacula variables */
135 PyObject *bacula_set(PyObject *self, PyObject *args, PyObject *keyw)
136 {
137    PyObject *CObject;
138    JCR *jcr;
139    char *msg = NULL;
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)) {
144       return NULL;
145    }
146    jcr = (JCR *)PyCObject_AsVoidPtr(CObject);
147
148    if (msg) {
149       Jmsg(jcr, M_INFO, 0, "%s", msg);
150    }
151    if (VolumeName) {
152       if (is_volume_name_legal(NULL, VolumeName)) {
153          pm_strcpy(jcr->VolumeName, VolumeName);
154       } else {
155          return Py_BuildValue("i", 0);  /* invalid volume name */
156       }
157    }
158    return Py_BuildValue("i", 1);
159 }
160
161 /* Run a Bacula command */
162 PyObject *bacula_run(PyObject *self, PyObject *args)
163 {
164    PyObject *CObject;
165    JCR *jcr;
166    char *item;
167    int stat;
168
169    if (!PyArg_ParseTuple(args, "Os:get", &CObject, &item)) {
170       return NULL;
171    }
172    jcr = (JCR *)PyCObject_AsVoidPtr(CObject);
173    UAContext *ua = new_ua_context(jcr);
174    ua->batch = true;
175    pm_strcpy(ua->cmd, item);          /* copy command */
176    parse_ua_args(ua);                 /* parse command */
177    stat = run_cmd(ua, ua->cmd);
178    free_ua_context(ua);
179    return Py_BuildValue("i", stat);
180 }
181
182
183 #endif /* HAVE_PYTHON */