1 | /** |
---|
2 | * Author : Gérald FENOY |
---|
3 | * |
---|
4 | * Copyright (c) 2009-2010 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 python_support(maps* m,service* s,int argc,char** argv,map *inputs,map *outputs){ |
---|
28 | Py_Initialize(); |
---|
29 | PyObject *pName, *pModule, *pFunc; |
---|
30 | pName = PyString_FromString(argv[2]); |
---|
31 | pModule = PyImport_Import(pName); |
---|
32 | int i; |
---|
33 | if (pModule != NULL) { |
---|
34 | pFunc=PyObject_GetAttrString(pModule,argv[4]); |
---|
35 | if (pFunc && PyCallable_Check(pFunc)){ |
---|
36 | PyDictObject* arg1=PyDict_FromMap(inputs); |
---|
37 | PyDictObject* arg2=PyDict_FromMap(outputs); |
---|
38 | PyObject *pArgs=PyTuple_New(2); |
---|
39 | PyObject *pValue; |
---|
40 | PyTuple_SetItem(pArgs, 0, (PyObject *)arg1); |
---|
41 | PyTuple_SetItem(pArgs, 1, (PyObject *)arg2); |
---|
42 | pValue = PyString_FromString(argv[argc-1]); |
---|
43 | #ifdef DEBUG |
---|
44 | fprintf(stderr,"Function successfully loaded\n"); |
---|
45 | #endif |
---|
46 | /** |
---|
47 | * Need to check if we need to fork to load a status enabled |
---|
48 | */ |
---|
49 | if(strcmp(PyString_AsString(pValue),"bg")!=0){ |
---|
50 | #ifdef DEBUG |
---|
51 | fprintf(stderr,"RUN IN NORMAL MODE \n"); |
---|
52 | #endif |
---|
53 | pValue = PyObject_CallObject(pFunc, pArgs); |
---|
54 | if (pValue != NULL) { |
---|
55 | inputs=mapFromPyDict(arg1); |
---|
56 | outputs=mapFromPyDict(arg2); |
---|
57 | #ifdef DEBUG |
---|
58 | fprintf(stderr,"Result of call: %i\n", PyInt_AsLong(pValue)); |
---|
59 | dumpMap(inputs); |
---|
60 | dumpMap(outputs); |
---|
61 | #ifndef WIN32 |
---|
62 | fprintf(stderr,"printProcessResponse(%i,\"%s\",%i,inputs,outputs);",getpid(),argv[1],PyInt_AsLong(pValue)); |
---|
63 | #endif |
---|
64 | #endif |
---|
65 | printProcessResponse(m,getpid(),s,argv[2],PyInt_AsLong(pValue),inputs,outputs); |
---|
66 | } |
---|
67 | } |
---|
68 | else{ |
---|
69 | pid_t pid; |
---|
70 | #ifdef WIN32 |
---|
71 | pid = 0; |
---|
72 | #else |
---|
73 | pid = fork (); |
---|
74 | #endif |
---|
75 | if (pid > 0) { |
---|
76 | /* dady */ |
---|
77 | printProcessResponse(m,pid,s,argv[2],SERVICE_STARTED,inputs,outputs); |
---|
78 | }else if (pid == 0) { |
---|
79 | /* son */ |
---|
80 | #ifdef DEBUG |
---|
81 | fprintf(stderr,"RUN IN BACKGROUND MODE \n"); |
---|
82 | #endif |
---|
83 | char tmp1[256]; |
---|
84 | sprintf(tmp1,"service/temp/%s_%d.xml",argv[2],getpid()); |
---|
85 | #ifndef WIN32 |
---|
86 | stdout = |
---|
87 | #endif |
---|
88 | freopen(tmp1 , "w+" , stdout); |
---|
89 | printProcessResponse(m,getpid(),s,argv[2],SERVICE_STARTED,inputs,outputs); |
---|
90 | fflush(stdout); |
---|
91 | pValue = PyObject_CallObject(pFunc, pArgs); |
---|
92 | if (pValue != NULL) { |
---|
93 | rewind(stdout); |
---|
94 | inputs=mapFromPyDict(arg1); |
---|
95 | outputs=mapFromPyDict(arg2); |
---|
96 | #ifdef DEBUG |
---|
97 | fprintf(stderr,"Result of call: %ld\n", PyInt_AsLong(pValue)); |
---|
98 | dumpMap(inputs); |
---|
99 | dumpMap(outputs); |
---|
100 | fprintf(stderr,"printProcessResponse(%i,%s,%i,inputs,outputs);",s,getpid(),argv[2],PyInt_AsLong(pValue)); |
---|
101 | #endif |
---|
102 | // sleep(6); |
---|
103 | printProcessResponse(m,getpid(),s,argv[2],PyInt_AsLong(pValue),inputs,outputs); |
---|
104 | } |
---|
105 | exit(1); |
---|
106 | } else { |
---|
107 | /* error */ |
---|
108 | } |
---|
109 | } |
---|
110 | Py_DECREF(pArgs); |
---|
111 | Py_DECREF(pValue); |
---|
112 | #ifdef DEBUG |
---|
113 | fprintf(stderr,"Function ran successfully\n"); |
---|
114 | #endif |
---|
115 | } |
---|
116 | else{ |
---|
117 | char tmpS[1024]; |
---|
118 | sprintf(tmpS, "Cannot find function %s \n", argv[4]); |
---|
119 | map* tmps=createMap("text",tmpS); |
---|
120 | printExceptionReportResponse(m,tmps); |
---|
121 | //fprintf(stderr, "Cannot find function %s \n", argv[2]); |
---|
122 | exit(-1); |
---|
123 | } |
---|
124 | }else{ |
---|
125 | char tmpS[1024]; |
---|
126 | sprintf(tmpS, "Cannot find function %s \n", argv[4]); |
---|
127 | map* tmps=createMap("text",tmpS); |
---|
128 | printExceptionReportResponse(m,tmps); |
---|
129 | //fprintf(stderr, "Cannot find function %s \n", argv[4]); |
---|
130 | if (PyErr_Occurred()) |
---|
131 | PyErr_Print(); |
---|
132 | |
---|
133 | } |
---|
134 | Py_Finalize(); |
---|
135 | return 2; |
---|
136 | } |
---|
137 | |
---|
138 | int zoo_python_support(maps** main_conf,map* request,service* s,maps **real_inputs,maps **real_outputs){ |
---|
139 | maps* m=*main_conf; |
---|
140 | maps* inputs=*real_inputs; |
---|
141 | maps* outputs=*real_outputs; |
---|
142 | char ntmp[1024]; |
---|
143 | getcwd(ntmp,1024); |
---|
144 | map* tmp=NULL; |
---|
145 | tmp=getMapFromMaps(*main_conf,"env","PYTHONPATH"); |
---|
146 | char *python_path=NULL; |
---|
147 | if(tmp!=NULL){ |
---|
148 | python_path=(char*)malloc((1+strlen(tmp->value))*sizeof(char)); |
---|
149 | sprintf(python_path,"%s",tmp->value); |
---|
150 | } |
---|
151 | else{ |
---|
152 | python_path=(char*)malloc(2*sizeof(char)); |
---|
153 | python_path="."; |
---|
154 | } |
---|
155 | tmp=NULL; |
---|
156 | tmp=getMap(request,"metapath"); |
---|
157 | char *pythonpath=(char*)malloc((1+strlen(python_path)+2048)*sizeof(char)); |
---|
158 | if(tmp!=NULL && strcmp(tmp->value,"")!=0) |
---|
159 | #ifdef WIN32 |
---|
160 | sprintf(pythonpath,"%s/%s/;%s",ntmp,tmp->value,python_path); |
---|
161 | #else |
---|
162 | sprintf(pythonpath,"%s/%s/:%s",ntmp,tmp->value,python_path); |
---|
163 | #endif |
---|
164 | else |
---|
165 | #ifdef WIN32 |
---|
166 | sprintf(pythonpath,"%s;%s",ntmp,python_path); |
---|
167 | #else |
---|
168 | sprintf(pythonpath,"%s:%s",ntmp,python_path); |
---|
169 | #endif |
---|
170 | #ifdef DEBUG |
---|
171 | fprintf(stderr,"PYTHONPATH=%s\n",pythonpath); |
---|
172 | #endif |
---|
173 | #ifndef WIN32 |
---|
174 | setenv("PYTHONPATH",pythonpath,1); |
---|
175 | #else |
---|
176 | SetEnvironmentVariable("PYTHONPATH",pythonpath); |
---|
177 | #endif |
---|
178 | free(python_path); |
---|
179 | free(pythonpath); |
---|
180 | // |
---|
181 | Py_Initialize(); |
---|
182 | PyObject *pName, *pModule, *pFunc; |
---|
183 | tmp=getMap(s->content,"serviceProvider"); |
---|
184 | //if(tmp!=NULL) |
---|
185 | pName = PyString_FromString(tmp->value); |
---|
186 | pModule = PyImport_Import(pName); |
---|
187 | int i; |
---|
188 | int res=SERVICE_FAILED; |
---|
189 | int cpid=getpid(); |
---|
190 | if (pModule != NULL) { |
---|
191 | pFunc=PyObject_GetAttrString(pModule,s->name); |
---|
192 | if (pFunc && PyCallable_Check(pFunc)){ |
---|
193 | PyDictObject* arg1=PyDict_FromMaps(m); |
---|
194 | PyDictObject* arg2=PyDict_FromMaps(inputs); |
---|
195 | PyDictObject* arg3=PyDict_FromMaps(outputs); |
---|
196 | PyObject *pArgs=PyTuple_New(3); |
---|
197 | PyObject *pValue; |
---|
198 | PyTuple_SetItem(pArgs, 0, (PyObject *)arg1); |
---|
199 | PyTuple_SetItem(pArgs, 1, (PyObject *)arg2); |
---|
200 | PyTuple_SetItem(pArgs, 2, (PyObject *)arg3); |
---|
201 | tmp=getMap(request,"storeExecuteResponse"); |
---|
202 | #ifdef DEBUG |
---|
203 | fprintf(stderr,"RUN IN NORMAL MODE \n"); |
---|
204 | //fflush(stderr); |
---|
205 | #endif |
---|
206 | pValue = PyObject_CallObject(pFunc, pArgs); |
---|
207 | if (pValue != NULL) { |
---|
208 | res=PyInt_AsLong(pValue); |
---|
209 | /*inputs=mapsFromPyDict(arg2); |
---|
210 | dumpMaps(inputs);*/ |
---|
211 | outputs=mapsFromPyDict(arg3); |
---|
212 | *real_outputs=outputs; |
---|
213 | |
---|
214 | #ifdef DEBUG |
---|
215 | fprintf(stderr,"Result of call: %i\n", PyInt_AsLong(pValue)); |
---|
216 | dumpMaps(inputs); |
---|
217 | dumpMaps(outputs); |
---|
218 | fprintf(stderr,"printProcessResponse(%i,\"%s\",%i,inputs,outputs);", |
---|
219 | getpid(),tmp->value,PyInt_AsLong(pValue)); |
---|
220 | dumpMaps(outputs); |
---|
221 | #endif |
---|
222 | Py_DECREF(pValue); |
---|
223 | Py_XDECREF(pFunc); |
---|
224 | Py_DECREF(pArgs); |
---|
225 | Py_DECREF(pModule); |
---|
226 | }else{ |
---|
227 | |
---|
228 | PyObject *ptype,*pvalue, *ptraceback; |
---|
229 | PyErr_Fetch(&ptype, &pvalue, &ptraceback); |
---|
230 | PyObject *trace=PyObject_Str(pvalue); |
---|
231 | char tb[1024]; |
---|
232 | char pbt[10240]; |
---|
233 | if(PyString_Check(trace)) |
---|
234 | sprintf(pbt,"TRACE : %s",PyString_AsString(trace)); |
---|
235 | else |
---|
236 | fprintf(stderr,"EMPTY TRACE ?"); |
---|
237 | trace=NULL; |
---|
238 | trace=PyObject_Str(ptype); |
---|
239 | if(PyString_Check(trace)) |
---|
240 | sprintf(pbt,"%s\nTRACE : %s",strdup(pbt),PyString_AsString(trace)); |
---|
241 | else |
---|
242 | fprintf(stderr,"EMPTY TRACE ?"); |
---|
243 | PyObject *t; |
---|
244 | pName = PyString_FromString("traceback"); |
---|
245 | pModule = PyImport_Import(pName); |
---|
246 | pArgs = PyTuple_New(1); |
---|
247 | PyTuple_SetItem(pArgs, 0, ptraceback); |
---|
248 | pFunc = PyObject_GetAttrString(pModule,"format_tb"); |
---|
249 | pValue = PyObject_CallObject(pFunc, pArgs); |
---|
250 | trace=NULL; |
---|
251 | trace=PyObject_Str(pValue); |
---|
252 | if(PyString_Check(trace)) |
---|
253 | sprintf(pbt,"%s\nUnable to run your python process properly. Please check the following messages : %s",strdup(pbt),PyString_AsString(trace)); |
---|
254 | else |
---|
255 | sprintf(pbt,"%s \n Unable to run your python process properly. Unable to provide any futher informations.",strdup(pbt)); |
---|
256 | map* err=createMap("text",pbt); |
---|
257 | addToMap(err,"code","NoApplicableCode"); |
---|
258 | printExceptionReportResponse(m,err); |
---|
259 | Py_XDECREF(pFunc); |
---|
260 | Py_DECREF(pArgs); |
---|
261 | Py_DECREF(pModule); |
---|
262 | exit(-1); |
---|
263 | } |
---|
264 | } |
---|
265 | else{ |
---|
266 | char tmpS[1024]; |
---|
267 | sprintf(tmpS, "Cannot find the %s function int the %s file.\n", s->name, tmp->value); |
---|
268 | map* tmps=createMap("text",tmpS); |
---|
269 | printExceptionReportResponse(m,tmps); |
---|
270 | //fprintf(stderr, "Cannot find function %s \n", argv[2]); |
---|
271 | Py_XDECREF(pFunc); |
---|
272 | Py_DECREF(pModule); |
---|
273 | exit(-1); |
---|
274 | } |
---|
275 | } else{ |
---|
276 | char tmpS[1024]; |
---|
277 | sprintf(tmpS, "Cannot find function %s \n", tmp->value); |
---|
278 | map* tmps=createMap("text",tmpS); |
---|
279 | printExceptionReportResponse(m,tmps); |
---|
280 | //fprintf(stderr, "Cannot find function %s \n", argv[4]); |
---|
281 | if (PyErr_Occurred()) |
---|
282 | PyErr_Print(); |
---|
283 | exit(-1); |
---|
284 | } |
---|
285 | Py_Finalize(); |
---|
286 | return res; |
---|
287 | } |
---|
288 | |
---|
289 | PyDictObject* PyDict_FromMaps(maps* t){ |
---|
290 | PyObject* res=PyDict_New( ); |
---|
291 | maps* tmp=t; |
---|
292 | while(tmp!=NULL){ |
---|
293 | if(PyDict_SetItem(res,PyString_FromString(tmp->name),(PyObject*)PyDict_FromMap(tmp->content))<0){ |
---|
294 | fprintf(stderr,"Unable to parse params..."); |
---|
295 | exit(1); |
---|
296 | } |
---|
297 | tmp=tmp->next; |
---|
298 | } |
---|
299 | return (PyDictObject*) res; |
---|
300 | } |
---|
301 | |
---|
302 | PyDictObject* PyDict_FromMap(map* t){ |
---|
303 | PyObject* res=PyDict_New( ); |
---|
304 | map* tmp=t; |
---|
305 | while(tmp!=NULL){ |
---|
306 | if(PyDict_SetItem(res,PyString_FromString(tmp->name),PyString_FromString(tmp->value))<0){ |
---|
307 | fprintf(stderr,"Unable to parse params..."); |
---|
308 | exit(1); |
---|
309 | } |
---|
310 | tmp=tmp->next; |
---|
311 | } |
---|
312 | return (PyDictObject*) res; |
---|
313 | } |
---|
314 | |
---|
315 | maps* mapsFromPyDict(PyDictObject* t){ |
---|
316 | maps* res=NULL; |
---|
317 | maps* cursor=res; |
---|
318 | PyObject* list=PyDict_Keys((PyObject*)t); |
---|
319 | int nb=PyList_Size(list); |
---|
320 | int i; |
---|
321 | for(i=0;i<nb;i++){ |
---|
322 | #ifdef DEBUG |
---|
323 | fprintf(stderr,">> parsing maps %d\n",i); |
---|
324 | #endif |
---|
325 | PyObject* key=PyList_GetItem(list,i); |
---|
326 | PyObject* value=PyDict_GetItem((PyObject*)t,key); |
---|
327 | #ifdef DEBUG |
---|
328 | fprintf(stderr,">> DEBUG VALUES : %s => %s\n", |
---|
329 | PyString_AsString(key),PyString_AsString(value)); |
---|
330 | #endif |
---|
331 | if(cursor!=NULL){ |
---|
332 | cursor=cursor->next; |
---|
333 | } |
---|
334 | cursor=(maps*)malloc(sizeof(maps*)); |
---|
335 | cursor->name=PyString_AsString(key); |
---|
336 | #ifdef DEBUG |
---|
337 | dumpMap(mapFromPyDict((PyDictObject*)value)); |
---|
338 | #endif |
---|
339 | cursor->content=mapFromPyDict((PyDictObject*)value); |
---|
340 | cursor->next=NULL; |
---|
341 | if(res==NULL) |
---|
342 | res=cursor; |
---|
343 | else |
---|
344 | addMapsToMaps(&res,cursor); |
---|
345 | #ifdef DEBUG |
---|
346 | dumpMaps(res); |
---|
347 | fprintf(stderr,">> parsed maps %d\n",i); |
---|
348 | #endif |
---|
349 | } |
---|
350 | return res; |
---|
351 | } |
---|
352 | |
---|
353 | map* mapFromPyDict(PyDictObject* t){ |
---|
354 | map* res=NULL; |
---|
355 | PyObject* list=PyDict_Keys((PyObject*)t); |
---|
356 | int nb=PyList_Size(list); |
---|
357 | int i; |
---|
358 | for(i=0;i<nb;i++){ |
---|
359 | PyObject* key=PyList_GetItem(list,i); |
---|
360 | PyObject* value=PyDict_GetItem((PyObject*)t,key); |
---|
361 | #ifdef DEBUG |
---|
362 | fprintf(stderr,">> DEBUG VALUES : %s => %s\n", |
---|
363 | PyString_AsString(key),PyString_AsString(value)); |
---|
364 | #endif |
---|
365 | if(res!=NULL) |
---|
366 | addToMap(res,PyString_AsString(key),PyString_AsString(value)); |
---|
367 | else |
---|
368 | res=createMap(PyString_AsString(key),PyString_AsString(value)); |
---|
369 | } |
---|
370 | return res; |
---|
371 | } |
---|