]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/python.c
- Fix bug 207. jcr use count off by one when manually
[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 #include <Python.h>
37
38 bool run_module(const char *module);
39
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);
43
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 */
51 };
52
53
54 struct s_vars {
55    const char *name;
56    char *fmt;
57 };     
58
59 static struct s_vars vars[] = {
60    { N_("Job"),        "s"},
61    { N_("Dir"),        "s"},
62    { N_("Level"),      "s"},
63    { N_("Type"),       "s"},
64    { N_("JobId"),      "i"},
65    { N_("Client"),     "s"},
66    { N_("NumVols"),    "i"},
67    { N_("Pool"),       "s"},
68    { N_("Storage"),    "s"},
69    { N_("Catalog"),    "s"},
70    { N_("MediaType"),  "s"},
71    { N_("JobName"),    "s"},
72    { N_("JobStatus"),  "s"},
73    
74    { NULL,             NULL}
75 };
76
77 /* Return Bacula variables */
78 PyObject *bacula_get(PyObject *self, PyObject *args)
79 {
80    PyObject *CObject;
81    JCR *jcr;
82    char *item;
83    bool found = false;
84    int i;
85    char buf[10];
86
87    if (!PyArg_ParseTuple(args, "Os:get", &CObject, &item)) {
88       return NULL;
89    }
90    jcr = (JCR *)PyCObject_AsVoidPtr(CObject);
91    for (i=0; vars[i].name; i++) {
92       if (strcmp(vars[i].name, item) == 0) {
93          found = true;
94          break;
95       }
96    }
97    if (!found) {
98       return NULL;
99    }
100    switch (i) {
101    case 0:                            /* Job */
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);
105    case 2:                            /* level */
106       return Py_BuildValue(vars[i].fmt, job_level_to_str(jcr->JobLevel));
107    case 3:                            /* type */
108       return Py_BuildValue(vars[i].fmt, job_type_to_str(jcr->JobType));
109    case 4:                            /* JobId */
110       return Py_BuildValue(vars[i].fmt, jcr->JobId);
111    case 5:                            /* Client */
112       return Py_BuildValue(vars[i].fmt, jcr->client->hdr.name);
113    case 6:                            /* NumVols */
114       return Py_BuildValue(vars[i].fmt, jcr->NumVols);
115    case 7:                            /* Pool */
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);
119    case 9:
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 */
126       buf[1] = 0;
127       buf[0] = jcr->JobStatus;
128       return Py_BuildValue(vars[i].fmt, buf);
129    }
130    return NULL;
131 }
132
133 /* Set Bacula variables */
134 PyObject *bacula_set(PyObject *self, PyObject *args, PyObject *keyw)
135 {
136    PyObject *CObject;
137    JCR *jcr;
138    char *msg = NULL;
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)) {
143       return NULL;
144    }
145    jcr = (JCR *)PyCObject_AsVoidPtr(CObject);
146
147    if (msg) {
148       Jmsg(jcr, M_INFO, 0, "%s", msg);
149    }
150    if (VolumeName) {
151       pm_strcpy(jcr->VolumeName, VolumeName);
152    }
153    return Py_BuildValue("i", 1);
154 }
155
156 /* Run a Bacula command */
157 PyObject *bacula_run(PyObject *self, PyObject *args)
158 {
159    PyObject *CObject;
160    JCR *jcr;
161    char *item;
162    int stat;
163
164    if (!PyArg_ParseTuple(args, "Os:get", &CObject, &item)) {
165       return NULL;
166    }
167    jcr = (JCR *)PyCObject_AsVoidPtr(CObject);
168    UAContext *ua = new_ua_context(jcr);
169    ua->batch = true;
170    pm_strcpy(ua->cmd, item);          /* copy command */
171    parse_ua_args(ua);                 /* parse command */
172    stat = run_cmd(ua, ua->cmd);
173    free_ua_context(ua);
174    return Py_BuildValue("i", stat);
175 }
176
177
178 #endif /* HAVE_PYTHON */