[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); |
---|
[364] | 72 | char* toto=(char*)malloc((strlen(pythonpath)+12)*sizeof(char)); |
---|
| 73 | sprintf(toto,"PYTHONPATH=%s",pythonpath); |
---|
| 74 | putenv(toto); |
---|
[1] | 75 | #endif |
---|
| 76 | free(python_path); |
---|
| 77 | free(pythonpath); |
---|
[9] | 78 | |
---|
[295] | 79 | PyThreadState *mainstate; |
---|
| 80 | PyEval_InitThreads(); |
---|
[1] | 81 | Py_Initialize(); |
---|
[295] | 82 | mainstate = PyThreadState_Swap(NULL); |
---|
| 83 | PyEval_ReleaseLock(); |
---|
| 84 | PyGILState_STATE gstate; |
---|
| 85 | gstate = PyGILState_Ensure(); |
---|
[1] | 86 | PyObject *pName, *pModule, *pFunc; |
---|
| 87 | tmp=getMap(s->content,"serviceProvider"); |
---|
[9] | 88 | if(tmp!=NULL) |
---|
| 89 | pName = PyString_FromString(tmp->value); |
---|
| 90 | else{ |
---|
| 91 | map* err=createMap("text","Unable to parse serviceProvider please check your zcfg file."); |
---|
| 92 | addToMap(err,"code","NoApplicableCode"); |
---|
| 93 | printExceptionReportResponse(m,err); |
---|
| 94 | exit(-1); |
---|
| 95 | } |
---|
[1] | 96 | pModule = PyImport_Import(pName); |
---|
| 97 | int res=SERVICE_FAILED; |
---|
| 98 | if (pModule != NULL) { |
---|
| 99 | pFunc=PyObject_GetAttrString(pModule,s->name); |
---|
| 100 | if (pFunc && PyCallable_Check(pFunc)){ |
---|
[114] | 101 | PyObject *pValue; |
---|
[1] | 102 | PyDictObject* arg1=PyDict_FromMaps(m); |
---|
| 103 | PyDictObject* arg2=PyDict_FromMaps(inputs); |
---|
| 104 | PyDictObject* arg3=PyDict_FromMaps(outputs); |
---|
| 105 | PyObject *pArgs=PyTuple_New(3); |
---|
[114] | 106 | if (!pArgs) |
---|
| 107 | return -1; |
---|
[1] | 108 | PyTuple_SetItem(pArgs, 0, (PyObject *)arg1); |
---|
| 109 | PyTuple_SetItem(pArgs, 1, (PyObject *)arg2); |
---|
| 110 | PyTuple_SetItem(pArgs, 2, (PyObject *)arg3); |
---|
| 111 | tmp=getMap(request,"storeExecuteResponse"); |
---|
| 112 | #ifdef DEBUG |
---|
| 113 | fprintf(stderr,"RUN IN NORMAL MODE \n"); |
---|
[9] | 114 | fflush(stderr); |
---|
[1] | 115 | #endif |
---|
| 116 | pValue = PyObject_CallObject(pFunc, pArgs); |
---|
| 117 | if (pValue != NULL) { |
---|
| 118 | res=PyInt_AsLong(pValue); |
---|
[9] | 119 | freeMaps(real_outputs); |
---|
| 120 | free(*real_outputs); |
---|
[59] | 121 | freeMaps(main_conf); |
---|
| 122 | free(*main_conf); |
---|
[57] | 123 | *main_conf=mapsFromPyDict(arg1); |
---|
[9] | 124 | *real_outputs=mapsFromPyDict(arg3); |
---|
[1] | 125 | #ifdef DEBUG |
---|
| 126 | fprintf(stderr,"Result of call: %i\n", PyInt_AsLong(pValue)); |
---|
| 127 | dumpMaps(inputs); |
---|
[364] | 128 | dumpMaps(*real_outputs); |
---|
[1] | 129 | #endif |
---|
[9] | 130 | }else{ |
---|
[1] | 131 | PyObject *ptype,*pvalue, *ptraceback; |
---|
| 132 | PyErr_Fetch(&ptype, &pvalue, &ptraceback); |
---|
| 133 | PyObject *trace=PyObject_Str(pvalue); |
---|
| 134 | char pbt[10240]; |
---|
| 135 | if(PyString_Check(trace)) |
---|
| 136 | sprintf(pbt,"TRACE : %s",PyString_AsString(trace)); |
---|
| 137 | else |
---|
| 138 | fprintf(stderr,"EMPTY TRACE ?"); |
---|
| 139 | trace=NULL; |
---|
| 140 | trace=PyObject_Str(ptype); |
---|
[59] | 141 | if(PyString_Check(trace)){ |
---|
| 142 | char *tpbt=strdup(pbt); |
---|
[337] | 143 | sprintf(pbt,"%s\n%s\0",tpbt,PyString_AsString(trace)); |
---|
[59] | 144 | free(tpbt); |
---|
| 145 | } |
---|
[1] | 146 | else |
---|
| 147 | fprintf(stderr,"EMPTY TRACE ?"); |
---|
[332] | 148 | |
---|
| 149 | char *tpbt=strdup(pbt); |
---|
[1] | 150 | pName = PyString_FromString("traceback"); |
---|
| 151 | pModule = PyImport_Import(pName); |
---|
| 152 | pArgs = PyTuple_New(1); |
---|
| 153 | PyTuple_SetItem(pArgs, 0, ptraceback); |
---|
| 154 | pFunc = PyObject_GetAttrString(pModule,"format_tb"); |
---|
| 155 | pValue = PyObject_CallObject(pFunc, pArgs); |
---|
| 156 | trace=NULL; |
---|
| 157 | trace=PyObject_Str(pValue); |
---|
| 158 | if(PyString_Check(trace)) |
---|
[332] | 159 | sprintf(pbt,"%s\nUnable to run your python process properly. Please check the following messages : %s",tpbt,PyString_AsString(trace)); |
---|
[1] | 160 | else |
---|
[332] | 161 | sprintf(pbt,"%s \n Unable to run your python process properly. Unable to provide any futher informations. %s",tpbt); |
---|
| 162 | free(tpbt); |
---|
[1] | 163 | map* err=createMap("text",pbt); |
---|
| 164 | addToMap(err,"code","NoApplicableCode"); |
---|
| 165 | printExceptionReportResponse(m,err); |
---|
[295] | 166 | res=-1; |
---|
[1] | 167 | } |
---|
| 168 | } |
---|
| 169 | else{ |
---|
| 170 | char tmpS[1024]; |
---|
[295] | 171 | sprintf(tmpS, "Cannot find the %s function in the %s file.\n", s->name, tmp->value); |
---|
[1] | 172 | map* tmps=createMap("text",tmpS); |
---|
| 173 | printExceptionReportResponse(m,tmps); |
---|
[295] | 174 | res=-1; |
---|
[1] | 175 | } |
---|
| 176 | } else{ |
---|
| 177 | char tmpS[1024]; |
---|
[9] | 178 | sprintf(tmpS, "Python module %s cannot be loaded.\n", tmp->value); |
---|
[1] | 179 | map* tmps=createMap("text",tmpS); |
---|
| 180 | printExceptionReportResponse(m,tmps); |
---|
| 181 | if (PyErr_Occurred()) |
---|
| 182 | PyErr_Print(); |
---|
[295] | 183 | PyErr_Clear(); |
---|
| 184 | res=-1; |
---|
| 185 | //exit(-1); |
---|
[1] | 186 | } |
---|
[295] | 187 | PyGILState_Release(gstate); |
---|
| 188 | PyEval_AcquireLock(); |
---|
| 189 | PyThreadState_Swap(mainstate); |
---|
[1] | 190 | Py_Finalize(); |
---|
| 191 | return res; |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | PyDictObject* PyDict_FromMaps(maps* t){ |
---|
| 195 | PyObject* res=PyDict_New( ); |
---|
| 196 | maps* tmp=t; |
---|
| 197 | while(tmp!=NULL){ |
---|
[295] | 198 | PyObject* value=(PyObject*)PyDict_FromMap(tmp->content); |
---|
| 199 | PyObject* name=PyString_FromString(tmp->name); |
---|
| 200 | if(PyDict_SetItem(res,name,value)<0){ |
---|
| 201 | fprintf(stderr,"Unable to set map value ..."); |
---|
| 202 | return NULL; |
---|
[1] | 203 | } |
---|
[295] | 204 | Py_DECREF(name); |
---|
[1] | 205 | tmp=tmp->next; |
---|
| 206 | } |
---|
| 207 | return (PyDictObject*) res; |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | PyDictObject* PyDict_FromMap(map* t){ |
---|
| 211 | PyObject* res=PyDict_New( ); |
---|
| 212 | map* tmp=t; |
---|
[360] | 213 | int hasSize=0; |
---|
| 214 | map* isArray=getMap(tmp,"isArray"); |
---|
[58] | 215 | map* size=getMap(tmp,"size"); |
---|
[360] | 216 | map* tmap=getMapType(tmp); |
---|
[1] | 217 | while(tmp!=NULL){ |
---|
[59] | 218 | PyObject* name=PyString_FromString(tmp->name); |
---|
[360] | 219 | if(strcasecmp(tmp->name,"value")==0) { |
---|
| 220 | if(isArray!=NULL){ |
---|
| 221 | map* len=getMap(tmp,"length"); |
---|
| 222 | int cnt=atoi(len->value); |
---|
| 223 | PyObject* value=PyList_New(cnt); |
---|
| 224 | PyObject* mvalue=PyList_New(cnt); |
---|
| 225 | PyObject* svalue=PyList_New(cnt); |
---|
| 226 | |
---|
| 227 | for(int i=0;i<cnt;i++){ |
---|
| 228 | |
---|
| 229 | map* vMap=getMapArray(tmp,"value",i); |
---|
| 230 | map* sMap=getMapArray(tmp,"size",i); |
---|
| 231 | |
---|
| 232 | if(vMap!=NULL){ |
---|
| 233 | |
---|
| 234 | PyObject* lvalue; |
---|
| 235 | PyObject* lsvalue; |
---|
| 236 | if(sMap==NULL){ |
---|
| 237 | lvalue=PyString_FromString(vMap->value); |
---|
| 238 | lsvalue=Py_None; |
---|
| 239 | } |
---|
| 240 | else{ |
---|
| 241 | lvalue=PyString_FromStringAndSize(vMap->value,atoi(sMap->value)); |
---|
| 242 | lsvalue=PyString_FromString(sMap->value); |
---|
| 243 | hasSize=1; |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | if(PyList_SetItem(value,i,lvalue)<0){ |
---|
| 247 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 248 | return NULL; |
---|
| 249 | } |
---|
| 250 | if(PyList_SetItem(svalue,i,lsvalue)<0){ |
---|
| 251 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 252 | return NULL; |
---|
| 253 | } |
---|
| 254 | } |
---|
| 255 | |
---|
| 256 | map* mMap=getMapArray(tmp,tmap->name,i); |
---|
| 257 | PyObject* lmvalue; |
---|
| 258 | if(mMap!=NULL){ |
---|
| 259 | lmvalue=PyString_FromString(mMap->value); |
---|
| 260 | }else |
---|
| 261 | lmvalue=Py_None; |
---|
| 262 | |
---|
| 263 | if(PyList_SetItem(mvalue,i,lmvalue)<0){ |
---|
| 264 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 265 | return NULL; |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | } |
---|
| 269 | |
---|
| 270 | if(PyDict_SetItem(res,name,value)<0){ |
---|
| 271 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 272 | return NULL; |
---|
| 273 | } |
---|
| 274 | if(PyDict_SetItem(res,PyString_FromString(tmap->name),mvalue)<0){ |
---|
| 275 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 276 | return NULL; |
---|
| 277 | } |
---|
| 278 | if(hasSize>0) |
---|
| 279 | if(PyDict_SetItem(res,PyString_FromString("size"),svalue)<0){ |
---|
| 280 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 281 | return NULL; |
---|
| 282 | } |
---|
| 283 | } |
---|
| 284 | else if(size!=NULL){ |
---|
[59] | 285 | PyObject* value=PyString_FromStringAndSize(tmp->value,atoi(size->value)); |
---|
| 286 | if(PyDict_SetItem(res,name,value)<0){ |
---|
[295] | 287 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 288 | return NULL; |
---|
[43] | 289 | } |
---|
[58] | 290 | } |
---|
[59] | 291 | else{ |
---|
| 292 | PyObject* value=PyString_FromString(tmp->value); |
---|
| 293 | if(PyDict_SetItem(res,name,value)<0){ |
---|
[295] | 294 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 295 | return NULL; |
---|
[43] | 296 | } |
---|
[59] | 297 | } |
---|
| 298 | } |
---|
| 299 | else{ |
---|
[360] | 300 | if(PyDict_GetItem(res,name)==NULL){ |
---|
| 301 | PyObject* value=PyString_FromString(tmp->value); |
---|
| 302 | if(PyDict_SetItem(res,name,value)<0){ |
---|
| 303 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 304 | return NULL; |
---|
| 305 | } |
---|
[43] | 306 | } |
---|
[59] | 307 | } |
---|
| 308 | Py_DECREF(name); |
---|
[1] | 309 | tmp=tmp->next; |
---|
| 310 | } |
---|
| 311 | return (PyDictObject*) res; |
---|
| 312 | } |
---|
| 313 | |
---|
| 314 | maps* mapsFromPyDict(PyDictObject* t){ |
---|
| 315 | maps* res=NULL; |
---|
| 316 | maps* cursor=res; |
---|
| 317 | PyObject* list=PyDict_Keys((PyObject*)t); |
---|
| 318 | int nb=PyList_Size(list); |
---|
| 319 | int i; |
---|
| 320 | for(i=0;i<nb;i++){ |
---|
| 321 | #ifdef DEBUG |
---|
| 322 | fprintf(stderr,">> parsing maps %d\n",i); |
---|
| 323 | #endif |
---|
| 324 | PyObject* key=PyList_GetItem(list,i); |
---|
| 325 | PyObject* value=PyDict_GetItem((PyObject*)t,key); |
---|
| 326 | #ifdef DEBUG |
---|
| 327 | fprintf(stderr,">> DEBUG VALUES : %s => %s\n", |
---|
| 328 | PyString_AsString(key),PyString_AsString(value)); |
---|
| 329 | #endif |
---|
[9] | 330 | cursor=(maps*)malloc(MAPS_SIZE); |
---|
[1] | 331 | cursor->name=PyString_AsString(key); |
---|
[295] | 332 | cursor->content=mapFromPyDict((PyDictObject*)value); |
---|
[1] | 333 | #ifdef DEBUG |
---|
[295] | 334 | dumpMap(cursor->content); |
---|
[1] | 335 | #endif |
---|
| 336 | cursor->next=NULL; |
---|
| 337 | if(res==NULL) |
---|
[59] | 338 | res=dupMaps(&cursor); |
---|
[1] | 339 | else |
---|
| 340 | addMapsToMaps(&res,cursor); |
---|
[59] | 341 | freeMap(&cursor->content); |
---|
| 342 | free(cursor->content); |
---|
| 343 | free(cursor); |
---|
[1] | 344 | #ifdef DEBUG |
---|
| 345 | dumpMaps(res); |
---|
| 346 | fprintf(stderr,">> parsed maps %d\n",i); |
---|
| 347 | #endif |
---|
| 348 | } |
---|
| 349 | return res; |
---|
| 350 | } |
---|
| 351 | |
---|
| 352 | map* mapFromPyDict(PyDictObject* t){ |
---|
| 353 | map* res=NULL; |
---|
| 354 | PyObject* list=PyDict_Keys((PyObject*)t); |
---|
| 355 | int nb=PyList_Size(list); |
---|
| 356 | int i; |
---|
| 357 | for(i=0;i<nb;i++){ |
---|
| 358 | PyObject* key=PyList_GetItem(list,i); |
---|
| 359 | PyObject* value=PyDict_GetItem((PyObject*)t,key); |
---|
| 360 | #ifdef DEBUG |
---|
| 361 | fprintf(stderr,">> DEBUG VALUES : %s => %s\n", |
---|
| 362 | PyString_AsString(key),PyString_AsString(value)); |
---|
| 363 | #endif |
---|
[100] | 364 | if(strcmp(PyString_AsString(key),"value")==0){ |
---|
| 365 | char *buffer=NULL; |
---|
[67] | 366 | Py_ssize_t size; |
---|
| 367 | PyString_AsStringAndSize(value,&buffer,&size); |
---|
| 368 | if(res!=NULL){ |
---|
| 369 | addToMap(res,PyString_AsString(key),""); |
---|
| 370 | }else{ |
---|
| 371 | res=createMap(PyString_AsString(key),""); |
---|
| 372 | } |
---|
| 373 | map* tmpR=getMap(res,"value"); |
---|
| 374 | free(tmpR->value); |
---|
[100] | 375 | tmpR->value=(char*)malloc((size+1)*sizeof(char)); |
---|
| 376 | memmove(tmpR->value,buffer,size*sizeof(char)); |
---|
| 377 | tmpR->value[size]=0; |
---|
| 378 | char sin[1024]; |
---|
| 379 | sprintf(sin,"%d",size); |
---|
| 380 | addToMap(res,"size",sin); |
---|
[67] | 381 | }else{ |
---|
| 382 | if(res!=NULL) |
---|
| 383 | addToMap(res,PyString_AsString(key),PyString_AsString(value)); |
---|
| 384 | else |
---|
| 385 | res=createMap(PyString_AsString(key),PyString_AsString(value)); |
---|
| 386 | } |
---|
[1] | 387 | } |
---|
| 388 | return res; |
---|
| 389 | } |
---|