mit-samba: Allow nesting on the event context
[idra/samba.git] / source4 / scripting / python / modules.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Samba utility functions
4    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <Python.h>
21 #include "includes.h"
22 #include "scripting/python/modules.h"
23 #include "dynconfig/dynconfig.h"
24
25 static bool PySys_PathPrepend(PyObject *list, const char *path)
26 {
27         PyObject *py_path = PyString_FromString(path);
28         if (py_path == NULL)
29                 return false;
30
31         return (PyList_Insert(list, 0, py_path) == 0);
32 }
33
34 bool py_update_path(const char *bindir)
35 {
36         char *newpath;
37         PyObject *mod_sys, *py_path;
38
39         mod_sys = PyImport_ImportModule("sys");
40         if (mod_sys == NULL) {
41                 return false;
42         }
43
44         py_path = PyObject_GetAttrString(mod_sys, "path");
45         if (py_path == NULL) {
46                 return false;
47         }       
48
49         if (!PyList_Check(py_path)) {
50                 return false;
51         }
52
53         if (!PySys_PathPrepend(py_path, dyn_PYTHONARCHDIR)) {
54                 return false;
55         }
56
57         if (!PySys_PathPrepend(py_path, dyn_PYTHONDIR)) {
58                 return false;
59         }
60
61         if (asprintf(&newpath, "%s/../scripting/python", bindir) < 0) {
62                 return false;
63         }
64         if (!PySys_PathPrepend(py_path, newpath)) {
65                 free(newpath);
66                 return false;
67         }
68         free(newpath);
69
70         if (asprintf(&newpath, "%s/python", bindir) < 0) {
71                 return false;
72         }
73         if (!PySys_PathPrepend(py_path, newpath)) {
74                 free(newpath);
75                 return false;
76         }
77         free(newpath);
78
79         return true;
80 }