[1] | 1 | /** |
---|
| 2 | * Author : Gérald FENOY |
---|
| 3 | * |
---|
[360] | 4 | * Copyright (c) 2009-2012 GeoLabs SARL |
---|
[1] | 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"); |
---|
[9] | 35 | char *python_path; |
---|
[63] | 36 | #ifdef DEBUG |
---|
[9] | 37 | fprintf(stderr,"PYTHON SUPPORT \n"); |
---|
[63] | 38 | #endif |
---|
[9] | 39 | fflush(stderr); |
---|
[1] | 40 | if(tmp!=NULL){ |
---|
[63] | 41 | #ifdef DEBUG |
---|
[9] | 42 | fprintf(stderr,"PYTHON SUPPORT (%i)\n",strlen(tmp->value)); |
---|
[63] | 43 | #endif |
---|
[9] | 44 | python_path=(char*)malloc((strlen(tmp->value))*sizeof(char)); |
---|
| 45 | sprintf(python_path,"%s",tmp->value); |
---|
[1] | 46 | } |
---|
| 47 | else{ |
---|
[9] | 48 | python_path=strdup("."); |
---|
[1] | 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 |
---|
[9] | 55 | sprintf(pythonpath,"%s/%s/;%s",ntmp,tmp->value,python_path); |
---|
[1] | 56 | #else |
---|
[9] | 57 | sprintf(pythonpath,"%s/%s/:%s",ntmp,tmp->value,python_path); |
---|
[1] | 58 | #endif |
---|
| 59 | else |
---|
| 60 | #ifdef WIN32 |
---|
| 61 | sprintf(pythonpath,"%s;%s",ntmp,python_path); |
---|
| 62 | #else |
---|
[9] | 63 | sprintf(pythonpath,"%s:%s",ntmp,python_path); |
---|
[1] | 64 | #endif |
---|
[67] | 65 | #ifdef DEBUG |
---|
[9] | 66 | fprintf(stderr,"PYTHONPATH=%s\n",pythonpath); |
---|
[67] | 67 | #endif |
---|
[1] | 68 | #ifndef WIN32 |
---|
| 69 | setenv("PYTHONPATH",pythonpath,1); |
---|
| 70 | #else |
---|
| 71 | SetEnvironmentVariable("PYTHONPATH",pythonpath); |
---|
| 72 | #endif |
---|
| 73 | free(python_path); |
---|
| 74 | free(pythonpath); |
---|
[9] | 75 | |
---|
[295] | 76 | PyThreadState *mainstate; |
---|
| 77 | PyEval_InitThreads(); |
---|
[1] | 78 | Py_Initialize(); |
---|
[295] | 79 | mainstate = PyThreadState_Swap(NULL); |
---|
| 80 | PyEval_ReleaseLock(); |
---|
| 81 | PyGILState_STATE gstate; |
---|
| 82 | gstate = PyGILState_Ensure(); |
---|
[1] | 83 | PyObject *pName, *pModule, *pFunc; |
---|
| 84 | tmp=getMap(s->content,"serviceProvider"); |
---|
[9] | 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 | } |
---|
[1] | 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)){ |
---|
[114] | 98 | PyObject *pValue; |
---|
[1] | 99 | PyDictObject* arg1=PyDict_FromMaps(m); |
---|
| 100 | PyDictObject* arg2=PyDict_FromMaps(inputs); |
---|
| 101 | PyDictObject* arg3=PyDict_FromMaps(outputs); |
---|
| 102 | PyObject *pArgs=PyTuple_New(3); |
---|
[114] | 103 | if (!pArgs) |
---|
| 104 | return -1; |
---|
[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"); |
---|
[9] | 111 | fflush(stderr); |
---|
[1] | 112 | #endif |
---|
| 113 | pValue = PyObject_CallObject(pFunc, pArgs); |
---|
| 114 | if (pValue != NULL) { |
---|
| 115 | res=PyInt_AsLong(pValue); |
---|
[9] | 116 | freeMaps(real_outputs); |
---|
| 117 | free(*real_outputs); |
---|
[59] | 118 | freeMaps(main_conf); |
---|
| 119 | free(*main_conf); |
---|
[57] | 120 | *main_conf=mapsFromPyDict(arg1); |
---|
[9] | 121 | *real_outputs=mapsFromPyDict(arg3); |
---|
[1] | 122 | #ifdef DEBUG |
---|
| 123 | fprintf(stderr,"Result of call: %i\n", PyInt_AsLong(pValue)); |
---|
| 124 | dumpMaps(inputs); |
---|
| 125 | dumpMaps(outputs); |
---|
| 126 | #endif |
---|
[9] | 127 | }else{ |
---|
[1] | 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); |
---|
[59] | 138 | if(PyString_Check(trace)){ |
---|
| 139 | char *tpbt=strdup(pbt); |
---|
[337] | 140 | sprintf(pbt,"%s\n%s\0",tpbt,PyString_AsString(trace)); |
---|
[59] | 141 | free(tpbt); |
---|
| 142 | } |
---|
[1] | 143 | else |
---|
| 144 | fprintf(stderr,"EMPTY TRACE ?"); |
---|
[332] | 145 | |
---|
| 146 | char *tpbt=strdup(pbt); |
---|
[1] | 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)) |
---|
[332] | 156 | sprintf(pbt,"%s\nUnable to run your python process properly. Please check the following messages : %s",tpbt,PyString_AsString(trace)); |
---|
[1] | 157 | else |
---|
[332] | 158 | sprintf(pbt,"%s \n Unable to run your python process properly. Unable to provide any futher informations. %s",tpbt); |
---|
| 159 | free(tpbt); |
---|
[1] | 160 | map* err=createMap("text",pbt); |
---|
| 161 | addToMap(err,"code","NoApplicableCode"); |
---|
| 162 | printExceptionReportResponse(m,err); |
---|
[295] | 163 | res=-1; |
---|
[1] | 164 | } |
---|
| 165 | } |
---|
| 166 | else{ |
---|
| 167 | char tmpS[1024]; |
---|
[295] | 168 | sprintf(tmpS, "Cannot find the %s function in the %s file.\n", s->name, tmp->value); |
---|
[1] | 169 | map* tmps=createMap("text",tmpS); |
---|
| 170 | printExceptionReportResponse(m,tmps); |
---|
[295] | 171 | res=-1; |
---|
[1] | 172 | } |
---|
| 173 | } else{ |
---|
| 174 | char tmpS[1024]; |
---|
[9] | 175 | sprintf(tmpS, "Python module %s cannot be loaded.\n", tmp->value); |
---|
[1] | 176 | map* tmps=createMap("text",tmpS); |
---|
| 177 | printExceptionReportResponse(m,tmps); |
---|
| 178 | if (PyErr_Occurred()) |
---|
| 179 | PyErr_Print(); |
---|
[295] | 180 | PyErr_Clear(); |
---|
| 181 | res=-1; |
---|
| 182 | //exit(-1); |
---|
[1] | 183 | } |
---|
[295] | 184 | PyGILState_Release(gstate); |
---|
| 185 | PyEval_AcquireLock(); |
---|
| 186 | PyThreadState_Swap(mainstate); |
---|
[1] | 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){ |
---|
[295] | 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; |
---|
[1] | 200 | } |
---|
[295] | 201 | Py_DECREF(name); |
---|
[1] | 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; |
---|
[360] | 210 | int hasSize=0; |
---|
| 211 | map* isArray=getMap(tmp,"isArray"); |
---|
[58] | 212 | map* size=getMap(tmp,"size"); |
---|
[360] | 213 | map* tmap=getMapType(tmp); |
---|
[1] | 214 | while(tmp!=NULL){ |
---|
[59] | 215 | PyObject* name=PyString_FromString(tmp->name); |
---|
[360] | 216 | if(strcasecmp(tmp->name,"value")==0) { |
---|
| 217 | if(isArray!=NULL){ |
---|
| 218 | map* len=getMap(tmp,"length"); |
---|
| 219 | int cnt=atoi(len->value); |
---|
| 220 | PyObject* value=PyList_New(cnt); |
---|
| 221 | PyObject* mvalue=PyList_New(cnt); |
---|
| 222 | PyObject* svalue=PyList_New(cnt); |
---|
| 223 | |
---|
| 224 | for(int i=0;i<cnt;i++){ |
---|
| 225 | |
---|
| 226 | map* vMap=getMapArray(tmp,"value",i); |
---|
| 227 | map* sMap=getMapArray(tmp,"size",i); |
---|
| 228 | |
---|
| 229 | if(vMap!=NULL){ |
---|
| 230 | |
---|
| 231 | PyObject* lvalue; |
---|
| 232 | PyObject* lsvalue; |
---|
| 233 | if(sMap==NULL){ |
---|
| 234 | lvalue=PyString_FromString(vMap->value); |
---|
| 235 | lsvalue=Py_None; |
---|
| 236 | } |
---|
| 237 | else{ |
---|
| 238 | lvalue=PyString_FromStringAndSize(vMap->value,atoi(sMap->value)); |
---|
| 239 | lsvalue=PyString_FromString(sMap->value); |
---|
| 240 | hasSize=1; |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | if(PyList_SetItem(value,i,lvalue)<0){ |
---|
| 244 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 245 | return NULL; |
---|
| 246 | } |
---|
| 247 | if(PyList_SetItem(svalue,i,lsvalue)<0){ |
---|
| 248 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 249 | return NULL; |
---|
| 250 | } |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | map* mMap=getMapArray(tmp,tmap->name,i); |
---|
| 254 | PyObject* lmvalue; |
---|
| 255 | if(mMap!=NULL){ |
---|
| 256 | lmvalue=PyString_FromString(mMap->value); |
---|
| 257 | }else |
---|
| 258 | lmvalue=Py_None; |
---|
| 259 | |
---|
| 260 | if(PyList_SetItem(mvalue,i,lmvalue)<0){ |
---|
| 261 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 262 | return NULL; |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | if(PyDict_SetItem(res,name,value)<0){ |
---|
| 268 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 269 | return NULL; |
---|
| 270 | } |
---|
| 271 | if(PyDict_SetItem(res,PyString_FromString(tmap->name),mvalue)<0){ |
---|
| 272 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 273 | return NULL; |
---|
| 274 | } |
---|
| 275 | if(hasSize>0) |
---|
| 276 | if(PyDict_SetItem(res,PyString_FromString("size"),svalue)<0){ |
---|
| 277 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 278 | return NULL; |
---|
| 279 | } |
---|
| 280 | } |
---|
| 281 | else if(size!=NULL){ |
---|
[59] | 282 | PyObject* value=PyString_FromStringAndSize(tmp->value,atoi(size->value)); |
---|
| 283 | if(PyDict_SetItem(res,name,value)<0){ |
---|
[295] | 284 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 285 | return NULL; |
---|
[43] | 286 | } |
---|
[58] | 287 | } |
---|
[59] | 288 | else{ |
---|
| 289 | PyObject* value=PyString_FromString(tmp->value); |
---|
| 290 | if(PyDict_SetItem(res,name,value)<0){ |
---|
[295] | 291 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 292 | return NULL; |
---|
[43] | 293 | } |
---|
[59] | 294 | } |
---|
| 295 | } |
---|
| 296 | else{ |
---|
[360] | 297 | if(PyDict_GetItem(res,name)==NULL){ |
---|
| 298 | PyObject* value=PyString_FromString(tmp->value); |
---|
| 299 | if(PyDict_SetItem(res,name,value)<0){ |
---|
| 300 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 301 | return NULL; |
---|
| 302 | } |
---|
[43] | 303 | } |
---|
[59] | 304 | } |
---|
| 305 | Py_DECREF(name); |
---|
[1] | 306 | tmp=tmp->next; |
---|
| 307 | } |
---|
| 308 | return (PyDictObject*) res; |
---|
| 309 | } |
---|
| 310 | |
---|
| 311 | maps* mapsFromPyDict(PyDictObject* t){ |
---|
| 312 | maps* res=NULL; |
---|
| 313 | maps* cursor=res; |
---|
| 314 | PyObject* list=PyDict_Keys((PyObject*)t); |
---|
| 315 | int nb=PyList_Size(list); |
---|
| 316 | int i; |
---|
| 317 | for(i=0;i<nb;i++){ |
---|
| 318 | #ifdef DEBUG |
---|
| 319 | fprintf(stderr,">> parsing maps %d\n",i); |
---|
| 320 | #endif |
---|
| 321 | PyObject* key=PyList_GetItem(list,i); |
---|
| 322 | PyObject* value=PyDict_GetItem((PyObject*)t,key); |
---|
| 323 | #ifdef DEBUG |
---|
| 324 | fprintf(stderr,">> DEBUG VALUES : %s => %s\n", |
---|
| 325 | PyString_AsString(key),PyString_AsString(value)); |
---|
| 326 | #endif |
---|
[9] | 327 | cursor=(maps*)malloc(MAPS_SIZE); |
---|
[1] | 328 | cursor->name=PyString_AsString(key); |
---|
[295] | 329 | cursor->content=mapFromPyDict((PyDictObject*)value); |
---|
[1] | 330 | #ifdef DEBUG |
---|
[295] | 331 | dumpMap(cursor->content); |
---|
[1] | 332 | #endif |
---|
| 333 | cursor->next=NULL; |
---|
| 334 | if(res==NULL) |
---|
[59] | 335 | res=dupMaps(&cursor); |
---|
[1] | 336 | else |
---|
| 337 | addMapsToMaps(&res,cursor); |
---|
[59] | 338 | freeMap(&cursor->content); |
---|
| 339 | free(cursor->content); |
---|
| 340 | free(cursor); |
---|
[1] | 341 | #ifdef DEBUG |
---|
| 342 | dumpMaps(res); |
---|
| 343 | fprintf(stderr,">> parsed maps %d\n",i); |
---|
| 344 | #endif |
---|
| 345 | } |
---|
| 346 | return res; |
---|
| 347 | } |
---|
| 348 | |
---|
| 349 | map* mapFromPyDict(PyDictObject* t){ |
---|
| 350 | map* res=NULL; |
---|
| 351 | PyObject* list=PyDict_Keys((PyObject*)t); |
---|
| 352 | int nb=PyList_Size(list); |
---|
| 353 | int i; |
---|
| 354 | for(i=0;i<nb;i++){ |
---|
| 355 | PyObject* key=PyList_GetItem(list,i); |
---|
| 356 | PyObject* value=PyDict_GetItem((PyObject*)t,key); |
---|
| 357 | #ifdef DEBUG |
---|
| 358 | fprintf(stderr,">> DEBUG VALUES : %s => %s\n", |
---|
| 359 | PyString_AsString(key),PyString_AsString(value)); |
---|
| 360 | #endif |
---|
[100] | 361 | if(strcmp(PyString_AsString(key),"value")==0){ |
---|
| 362 | char *buffer=NULL; |
---|
[67] | 363 | Py_ssize_t size; |
---|
| 364 | PyString_AsStringAndSize(value,&buffer,&size); |
---|
| 365 | if(res!=NULL){ |
---|
| 366 | addToMap(res,PyString_AsString(key),""); |
---|
| 367 | }else{ |
---|
| 368 | res=createMap(PyString_AsString(key),""); |
---|
| 369 | } |
---|
| 370 | map* tmpR=getMap(res,"value"); |
---|
| 371 | free(tmpR->value); |
---|
[100] | 372 | tmpR->value=(char*)malloc((size+1)*sizeof(char)); |
---|
| 373 | memmove(tmpR->value,buffer,size*sizeof(char)); |
---|
| 374 | tmpR->value[size]=0; |
---|
| 375 | char sin[1024]; |
---|
| 376 | sprintf(sin,"%d",size); |
---|
| 377 | addToMap(res,"size",sin); |
---|
[67] | 378 | }else{ |
---|
| 379 | if(res!=NULL) |
---|
| 380 | addToMap(res,PyString_AsString(key),PyString_AsString(value)); |
---|
| 381 | else |
---|
| 382 | res=createMap(PyString_AsString(key),PyString_AsString(value)); |
---|
| 383 | } |
---|
[1] | 384 | } |
---|
| 385 | return res; |
---|
| 386 | } |
---|