-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpython_importer.c
More file actions
78 lines (63 loc) · 2.02 KB
/
python_importer.c
File metadata and controls
78 lines (63 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "python_importer.h"
PyObject *py_handler;
bool always_import = false;
void debug(const char *text)
{
FILE *fp = fopen("/tmp/samba.log", "a");
{
fprintf(fp, "SAMBA (%s): %s\n", getenv("USER"), text);
fclose(fp);
}
}
const char *get_conf(vfs_handle_struct *handle, const char *name)
{
return lp_parm_const_string(SNUM(handle->conn), "python", name, NULL);
}
struct PyObject *get_py_mod(const char *script_path)
{
PyObject *py_mod;
PyObject *py_imp_str, *py_imp_handle, *py_imp_dict;
PyObject *py_imp_load_source, *py_args_tuple;
Py_Initialize();
py_imp_str = PyString_FromString("imp");
py_imp_handle = PyImport_Import(py_imp_str);
py_imp_dict = PyModule_GetDict(py_imp_handle);
py_imp_load_source = PyDict_GetItemString(py_imp_dict, "load_source");
py_args_tuple = PyTuple_New(2);
PyTuple_SetItem(py_args_tuple, 0, PyString_FromString("handler"));
PyTuple_SetItem(py_args_tuple, 1, PyString_FromString(script_path));
py_mod = PyObject_CallObject(py_imp_load_source, py_args_tuple);
Py_DECREF(py_args_tuple);
Py_DECREF(py_imp_str);
Py_DECREF(py_imp_handle);
Py_DECREF(py_imp_dict);
Py_DECREF(py_imp_load_source);
return py_mod;
}
struct PyObject *get_py_func(PyObject *py_mod, const char *func_name)
{
PyObject *py_func_name = PyString_FromString(func_name);
PyObject *py_func = PyObject_GetAttr(py_mod, py_func_name);
Py_DECREF(py_func_name);
if (py_func != NULL)
{
if (PyCallable_Check(py_func) == 1)
{
return py_func;
}
}
debug("something went wrong get_py_func.");
return NULL;
}
struct PyObject *get_func(vfs_handle_struct *handle,
const char *func_name)
{
debug("get_func");
if ((py_handler == NULL) || (always_import == true))
{
debug("importing module.");
const char *script_path = get_conf(handle, "script");
py_handler = get_py_mod(script_path);
}
return get_py_func(py_handler, func_name);
}