1 | /** |
---|
2 | * Author : Gérald FENOY |
---|
3 | * |
---|
4 | * Copyright (c) 2009-2011 GeoLabs SARL |
---|
5 | * |
---|
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
7 | * of this software and associated documentation files (the "Software"), to deal |
---|
8 | * in the Software without restriction, including without limitation the rights |
---|
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
10 | * copies of the Software, and to permit persons to whom the Software is |
---|
11 | * furnished to do so, subject to the following conditions: |
---|
12 | * |
---|
13 | * The above copyright notice and this permission notice shall be included in |
---|
14 | * all copies or substantial portions of the Software. |
---|
15 | * |
---|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
22 | * THE SOFTWARE. |
---|
23 | */ |
---|
24 | |
---|
25 | #include "service_internal_python.h" |
---|
26 | |
---|
27 | int zoo_python_support(maps** main_conf,map* request,service* s,maps **real_inputs,maps **real_outputs){ |
---|
28 | maps* m=*main_conf; |
---|
29 | maps* inputs=*real_inputs; |
---|
30 | maps* outputs=*real_outputs; |
---|
31 | char ntmp[1024]; |
---|
32 | getcwd(ntmp,1024); |
---|
33 | map* tmp=NULL; |
---|
34 | tmp=getMapFromMaps(*main_conf,"env","PYTHONPATH"); |
---|
35 | char *python_path; |
---|
36 | #ifdef DEBUG |
---|
37 | fprintf(stderr,"PYTHON SUPPORT \n"); |
---|
38 | #endif |
---|
39 | fflush(stderr); |
---|
40 | if(tmp!=NULL){ |
---|
41 | #ifdef DEBUG |
---|
42 | fprintf(stderr,"PYTHON SUPPORT (%i)\n",strlen(tmp->value)); |
---|
43 | #endif |
---|
44 | python_path=(char*)malloc((strlen(tmp->value))*sizeof(char)); |
---|
45 | sprintf(python_path,"%s",tmp->value); |
---|
46 | } |
---|
47 | else{ |
---|
48 | python_path=strdup("."); |
---|
49 | } |
---|
50 | tmp=NULL; |
---|
51 | tmp=getMap(request,"metapath"); |
---|
52 | char *pythonpath=(char*)malloc((1+strlen(python_path)+2048)*sizeof(char)); |
---|
53 | if(tmp!=NULL && strcmp(tmp->value,"")!=0) |
---|
54 | #ifdef WIN32 |
---|
55 | sprintf(pythonpath,"%s/%s/;%s",ntmp,tmp->value,python_path); |
---|
56 | #else |
---|
57 | sprintf(pythonpath,"%s/%s/:%s",ntmp,tmp->value,python_path); |
---|
58 | #endif |
---|
59 | else |
---|
60 | #ifdef WIN32 |
---|
61 | sprintf(pythonpath,"%s;%s",ntmp,python_path); |
---|
62 | #else |
---|
63 | sprintf(pythonpath,"%s:%s",ntmp,python_path); |
---|
64 | #endif |
---|
65 | #ifdef DEBUG |
---|
66 | fprintf(stderr,"PYTHONPATH=%s\n",pythonpath); |
---|
67 | #endif |
---|
68 | #ifndef WIN32 |
---|
69 | setenv("PYTHONPATH",pythonpath,1); |
---|
70 | #else |
---|
71 | SetEnvironmentVariable("PYTHONPATH",pythonpath); |
---|
72 | #endif |
---|
73 | free(python_path); |
---|
74 | free(pythonpath); |
---|
75 | |
---|
76 | PyThreadState *mainstate; |
---|
77 | PyEval_InitThreads(); |
---|
78 | Py_Initialize(); |
---|
79 | mainstate = PyThreadState_Swap(NULL); |
---|
80 | PyEval_ReleaseLock(); |
---|
81 | PyGILState_STATE gstate; |
---|
82 | gstate = PyGILState_Ensure(); |
---|
83 | PyObject *pName, *pModule, *pFunc; |
---|
84 | tmp=getMap(s->content,"serviceProvider"); |
---|
85 | if(tmp!=NULL) |
---|
86 | pName = PyString_FromString(tmp->value); |
---|
87 | else{ |
---|
88 | map* err=createMap("text","Unable to parse serviceProvider please check your zcfg file."); |
---|
89 | addToMap(err,"code","NoApplicableCode"); |
---|
90 | printExceptionReportResponse(m,err); |
---|
91 | exit(-1); |
---|
92 | } |
---|
93 | pModule = PyImport_Import(pName); |
---|
94 | int res=SERVICE_FAILED; |
---|
95 | if (pModule != NULL) { |
---|
96 | pFunc=PyObject_GetAttrString(pModule,s->name); |
---|
97 | if (pFunc && PyCallable_Check(pFunc)){ |
---|
98 | PyObject *pValue; |
---|
99 | PyDictObject* arg1=PyDict_FromMaps(m); |
---|
100 | PyDictObject* arg2=PyDict_FromMaps(inputs); |
---|
101 | PyDictObject* arg3=PyDict_FromMaps(outputs); |
---|
102 | PyObject *pArgs=PyTuple_New(3); |
---|
103 | if (!pArgs) |
---|
104 | return -1; |
---|
105 | PyTuple_SetItem(pArgs, 0, (PyObject *)arg1); |
---|
106 | PyTuple_SetItem(pArgs, 1, (PyObject *)arg2); |
---|
107 | PyTuple_SetItem(pArgs, 2, (PyObject *)arg3); |
---|
108 | tmp=getMap(request,"storeExecuteResponse"); |
---|
109 | #ifdef DEBUG |
---|
110 | fprintf(stderr,"RUN IN NORMAL MODE \n"); |
---|
111 | fflush(stderr); |
---|
112 | #endif |
---|
113 | pValue = PyObject_CallObject(pFunc, pArgs); |
---|
114 | if (pValue != NULL) { |
---|
115 | res=PyInt_AsLong(pValue); |
---|
116 | freeMaps(real_outputs); |
---|
117 | free(*real_outputs); |
---|
118 | freeMaps(main_conf); |
---|
119 | free(*main_conf); |
---|
120 | *main_conf=mapsFromPyDict(arg1); |
---|
121 | *real_outputs=mapsFromPyDict(arg3); |
---|
122 | #ifdef DEBUG |
---|
123 | fprintf(stderr,"Result of call: %i\n", PyInt_AsLong(pValue)); |
---|
124 | dumpMaps(inputs); |
---|
125 | dumpMaps(outputs); |
---|
126 | #endif |
---|
127 | }else{ |
---|
128 | PyObject *ptype,*pvalue, *ptraceback; |
---|
129 | PyErr_Fetch(&ptype, &pvalue, &ptraceback); |
---|
130 | PyObject *trace=PyObject_Str(pvalue); |
---|
131 | char pbt[10240]; |
---|
132 | if(PyString_Check(trace)) |
---|
133 | sprintf(pbt,"TRACE : %s",PyString_AsString(trace)); |
---|
134 | else |
---|
135 | fprintf(stderr,"EMPTY TRACE ?"); |
---|
136 | trace=NULL; |
---|
137 | trace=PyObject_Str(ptype); |
---|
138 | if(PyString_Check(trace)){ |
---|
139 | char *tpbt=strdup(pbt); |
---|
140 | sprintf(pbt,"%s\n%s\0",tpbt,PyString_AsString(trace)); |
---|
141 | free(tpbt); |
---|
142 | } |
---|
143 | else |
---|
144 | fprintf(stderr,"EMPTY TRACE ?"); |
---|
145 | |
---|
146 | char *tpbt=strdup(pbt); |
---|
147 | pName = PyString_FromString("traceback"); |
---|
148 | pModule = PyImport_Import(pName); |
---|
149 | pArgs = PyTuple_New(1); |
---|
150 | PyTuple_SetItem(pArgs, 0, ptraceback); |
---|
151 | pFunc = PyObject_GetAttrString(pModule,"format_tb"); |
---|
152 | pValue = PyObject_CallObject(pFunc, pArgs); |
---|
153 | trace=NULL; |
---|
154 | trace=PyObject_Str(pValue); |
---|
155 | if(PyString_Check(trace)) |
---|
156 | sprintf(pbt,"%s\nUnable to run your python process properly. Please check the following messages : %s",tpbt,PyString_AsString(trace)); |
---|
157 | else |
---|
158 | sprintf(pbt,"%s \n Unable to run your python process properly. Unable to provide any futher informations. %s",tpbt); |
---|
159 | free(tpbt); |
---|
160 | map* err=createMap("text",pbt); |
---|
161 | addToMap(err,"code","NoApplicableCode"); |
---|
162 | printExceptionReportResponse(m,err); |
---|
163 | res=-1; |
---|
164 | } |
---|
165 | } |
---|
166 | else{ |
---|
167 | char tmpS[1024]; |
---|
168 | sprintf(tmpS, "Cannot find the %s function in the %s file.\n", s->name, tmp->value); |
---|
169 | map* tmps=createMap("text",tmpS); |
---|
170 | printExceptionReportResponse(m,tmps); |
---|
171 | res=-1; |
---|
172 | } |
---|
173 | } else{ |
---|
174 | char tmpS[1024]; |
---|
175 | sprintf(tmpS, "Python module %s cannot be loaded.\n", tmp->value); |
---|
176 | map* tmps=createMap("text",tmpS); |
---|
177 | printExceptionReportResponse(m,tmps); |
---|
178 | if (PyErr_Occurred()) |
---|
179 | PyErr_Print(); |
---|
180 | PyErr_Clear(); |
---|
181 | res=-1; |
---|
182 | //exit(-1); |
---|
183 | } |
---|
184 | PyGILState_Release(gstate); |
---|
185 | PyEval_AcquireLock(); |
---|
186 | PyThreadState_Swap(mainstate); |
---|
187 | Py_Finalize(); |
---|
188 | return res; |
---|
189 | } |
---|
190 | |
---|
191 | PyDictObject* PyDict_FromMaps(maps* t){ |
---|
192 | PyObject* res=PyDict_New( ); |
---|
193 | maps* tmp=t; |
---|
194 | while(tmp!=NULL){ |
---|
195 | PyObject* value=(PyObject*)PyDict_FromMap(tmp->content); |
---|
196 | PyObject* name=PyString_FromString(tmp->name); |
---|
197 | if(PyDict_SetItem(res,name,value)<0){ |
---|
198 | fprintf(stderr,"Unable to set map value ..."); |
---|
199 | return NULL; |
---|
200 | } |
---|
201 | Py_DECREF(name); |
---|
202 | tmp=tmp->next; |
---|
203 | } |
---|
204 | return (PyDictObject*) res; |
---|
205 | } |
---|
206 | |
---|
207 | PyDictObject* PyDict_FromMap(map* t){ |
---|
208 | PyObject* res=PyDict_New( ); |
---|
209 | map* tmp=t; |
---|
210 | map* size=getMap(tmp,"size"); |
---|
211 | while(tmp!=NULL){ |
---|
212 | PyObject* name=PyString_FromString(tmp->name); |
---|
213 | if(strcasecmp(tmp->name,"value")==0){ |
---|
214 | if(size!=NULL){ |
---|
215 | PyObject* value=PyString_FromStringAndSize(tmp->value,atoi(size->value)); |
---|
216 | if(PyDict_SetItem(res,name,value)<0){ |
---|
217 | fprintf(stderr,"Unable to set key value pair..."); |
---|
218 | return NULL; |
---|
219 | } |
---|
220 | } |
---|
221 | else{ |
---|
222 | PyObject* value=PyString_FromString(tmp->value); |
---|
223 | if(PyDict_SetItem(res,name,value)<0){ |
---|
224 | fprintf(stderr,"Unable to set key value pair..."); |
---|
225 | return NULL; |
---|
226 | } |
---|
227 | } |
---|
228 | } |
---|
229 | else{ |
---|
230 | PyObject* value=PyString_FromString(tmp->value); |
---|
231 | if(PyDict_SetItem(res,name,value)<0){ |
---|
232 | fprintf(stderr,"Unable to set key value pair..."); |
---|
233 | return NULL; |
---|
234 | } |
---|
235 | } |
---|
236 | Py_DECREF(name); |
---|
237 | tmp=tmp->next; |
---|
238 | } |
---|
239 | return (PyDictObject*) res; |
---|
240 | } |
---|
241 | |
---|
242 | maps* mapsFromPyDict(PyDictObject* t){ |
---|
243 | maps* res=NULL; |
---|
244 | maps* cursor=res; |
---|
245 | PyObject* list=PyDict_Keys((PyObject*)t); |
---|
246 | int nb=PyList_Size(list); |
---|
247 | int i; |
---|
248 | for(i=0;i<nb;i++){ |
---|
249 | #ifdef DEBUG |
---|
250 | fprintf(stderr,">> parsing maps %d\n",i); |
---|
251 | #endif |
---|
252 | PyObject* key=PyList_GetItem(list,i); |
---|
253 | PyObject* value=PyDict_GetItem((PyObject*)t,key); |
---|
254 | #ifdef DEBUG |
---|
255 | fprintf(stderr,">> DEBUG VALUES : %s => %s\n", |
---|
256 | PyString_AsString(key),PyString_AsString(value)); |
---|
257 | #endif |
---|
258 | cursor=(maps*)malloc(MAPS_SIZE); |
---|
259 | cursor->name=PyString_AsString(key); |
---|
260 | cursor->content=mapFromPyDict((PyDictObject*)value); |
---|
261 | #ifdef DEBUG |
---|
262 | dumpMap(cursor->content); |
---|
263 | #endif |
---|
264 | cursor->next=NULL; |
---|
265 | if(res==NULL) |
---|
266 | res=dupMaps(&cursor); |
---|
267 | else |
---|
268 | addMapsToMaps(&res,cursor); |
---|
269 | freeMap(&cursor->content); |
---|
270 | free(cursor->content); |
---|
271 | free(cursor); |
---|
272 | #ifdef DEBUG |
---|
273 | dumpMaps(res); |
---|
274 | fprintf(stderr,">> parsed maps %d\n",i); |
---|
275 | #endif |
---|
276 | } |
---|
277 | return res; |
---|
278 | } |
---|
279 | |
---|
280 | map* mapFromPyDict(PyDictObject* t){ |
---|
281 | map* res=NULL; |
---|
282 | PyObject* list=PyDict_Keys((PyObject*)t); |
---|
283 | int nb=PyList_Size(list); |
---|
284 | int i; |
---|
285 | for(i=0;i<nb;i++){ |
---|
286 | PyObject* key=PyList_GetItem(list,i); |
---|
287 | PyObject* value=PyDict_GetItem((PyObject*)t,key); |
---|
288 | #ifdef DEBUG |
---|
289 | fprintf(stderr,">> DEBUG VALUES : %s => %s\n", |
---|
290 | PyString_AsString(key),PyString_AsString(value)); |
---|
291 | #endif |
---|
292 | if(strcmp(PyString_AsString(key),"value")==0){ |
---|
293 | char *buffer=NULL; |
---|
294 | Py_ssize_t size; |
---|
295 | PyString_AsStringAndSize(value,&buffer,&size); |
---|
296 | if(res!=NULL){ |
---|
297 | addToMap(res,PyString_AsString(key),""); |
---|
298 | }else{ |
---|
299 | res=createMap(PyString_AsString(key),""); |
---|
300 | } |
---|
301 | map* tmpR=getMap(res,"value"); |
---|
302 | free(tmpR->value); |
---|
303 | tmpR->value=(char*)malloc((size+1)*sizeof(char)); |
---|
304 | memmove(tmpR->value,buffer,size*sizeof(char)); |
---|
305 | tmpR->value[size]=0; |
---|
306 | char sin[1024]; |
---|
307 | sprintf(sin,"%d",size); |
---|
308 | addToMap(res,"size",sin); |
---|
309 | }else{ |
---|
310 | if(res!=NULL) |
---|
311 | addToMap(res,PyString_AsString(key),PyString_AsString(value)); |
---|
312 | else |
---|
313 | res=createMap(PyString_AsString(key),PyString_AsString(value)); |
---|
314 | } |
---|
315 | } |
---|
316 | return res; |
---|
317 | } |
---|