[1] | 1 | /** |
---|
| 2 | * Author : Gérald FENOY |
---|
| 3 | * |
---|
[69] | 4 | * Copyright (c) 2009-2011 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 | |
---|
[1] | 76 | Py_Initialize(); |
---|
| 77 | PyObject *pName, *pModule, *pFunc; |
---|
| 78 | tmp=getMap(s->content,"serviceProvider"); |
---|
[9] | 79 | if(tmp!=NULL) |
---|
| 80 | pName = PyString_FromString(tmp->value); |
---|
| 81 | else{ |
---|
| 82 | map* err=createMap("text","Unable to parse serviceProvider please check your zcfg file."); |
---|
| 83 | addToMap(err,"code","NoApplicableCode"); |
---|
| 84 | printExceptionReportResponse(m,err); |
---|
| 85 | exit(-1); |
---|
| 86 | } |
---|
[1] | 87 | pModule = PyImport_Import(pName); |
---|
| 88 | int i; |
---|
| 89 | int res=SERVICE_FAILED; |
---|
| 90 | int cpid=getpid(); |
---|
| 91 | if (pModule != NULL) { |
---|
| 92 | pFunc=PyObject_GetAttrString(pModule,s->name); |
---|
| 93 | if (pFunc && PyCallable_Check(pFunc)){ |
---|
| 94 | PyDictObject* arg1=PyDict_FromMaps(m); |
---|
| 95 | PyDictObject* arg2=PyDict_FromMaps(inputs); |
---|
| 96 | PyDictObject* arg3=PyDict_FromMaps(outputs); |
---|
| 97 | PyObject *pArgs=PyTuple_New(3); |
---|
| 98 | PyObject *pValue; |
---|
| 99 | PyTuple_SetItem(pArgs, 0, (PyObject *)arg1); |
---|
| 100 | PyTuple_SetItem(pArgs, 1, (PyObject *)arg2); |
---|
| 101 | PyTuple_SetItem(pArgs, 2, (PyObject *)arg3); |
---|
| 102 | tmp=getMap(request,"storeExecuteResponse"); |
---|
| 103 | #ifdef DEBUG |
---|
| 104 | fprintf(stderr,"RUN IN NORMAL MODE \n"); |
---|
[9] | 105 | fflush(stderr); |
---|
[1] | 106 | #endif |
---|
| 107 | pValue = PyObject_CallObject(pFunc, pArgs); |
---|
| 108 | if (pValue != NULL) { |
---|
| 109 | res=PyInt_AsLong(pValue); |
---|
[9] | 110 | freeMaps(real_outputs); |
---|
| 111 | free(*real_outputs); |
---|
[59] | 112 | freeMaps(main_conf); |
---|
| 113 | free(*main_conf); |
---|
[57] | 114 | *main_conf=mapsFromPyDict(arg1); |
---|
[9] | 115 | *real_outputs=mapsFromPyDict(arg3); |
---|
[1] | 116 | #ifdef DEBUG |
---|
| 117 | fprintf(stderr,"Result of call: %i\n", PyInt_AsLong(pValue)); |
---|
| 118 | dumpMaps(inputs); |
---|
| 119 | dumpMaps(outputs); |
---|
| 120 | #endif |
---|
[9] | 121 | Py_DECREF(arg1); |
---|
| 122 | Py_DECREF(arg2); |
---|
| 123 | Py_DECREF(arg3); |
---|
| 124 | Py_DECREF(pArgs); |
---|
[1] | 125 | Py_DECREF(pValue); |
---|
| 126 | Py_XDECREF(pFunc); |
---|
| 127 | Py_DECREF(pModule); |
---|
[9] | 128 | }else{ |
---|
[1] | 129 | PyObject *ptype,*pvalue, *ptraceback; |
---|
| 130 | PyErr_Fetch(&ptype, &pvalue, &ptraceback); |
---|
| 131 | PyObject *trace=PyObject_Str(pvalue); |
---|
| 132 | char tb[1024]; |
---|
| 133 | char pbt[10240]; |
---|
| 134 | if(PyString_Check(trace)) |
---|
| 135 | sprintf(pbt,"TRACE : %s",PyString_AsString(trace)); |
---|
| 136 | else |
---|
| 137 | fprintf(stderr,"EMPTY TRACE ?"); |
---|
| 138 | trace=NULL; |
---|
| 139 | trace=PyObject_Str(ptype); |
---|
[59] | 140 | if(PyString_Check(trace)){ |
---|
| 141 | char *tpbt=strdup(pbt); |
---|
| 142 | sprintf(pbt,"%s\nTRACE : %s",tpbt,PyString_AsString(trace)); |
---|
| 143 | free(tpbt); |
---|
| 144 | } |
---|
[1] | 145 | else |
---|
| 146 | fprintf(stderr,"EMPTY TRACE ?"); |
---|
| 147 | PyObject *t; |
---|
| 148 | pName = PyString_FromString("traceback"); |
---|
| 149 | pModule = PyImport_Import(pName); |
---|
| 150 | pArgs = PyTuple_New(1); |
---|
| 151 | PyTuple_SetItem(pArgs, 0, ptraceback); |
---|
| 152 | pFunc = PyObject_GetAttrString(pModule,"format_tb"); |
---|
| 153 | pValue = PyObject_CallObject(pFunc, pArgs); |
---|
| 154 | trace=NULL; |
---|
| 155 | trace=PyObject_Str(pValue); |
---|
| 156 | if(PyString_Check(trace)) |
---|
[59] | 157 | sprintf(pbt,"%s\nUnable to run your python process properly. Please check the following messages : %s",pbt,PyString_AsString(trace)); |
---|
[1] | 158 | else |
---|
[59] | 159 | sprintf(pbt,"%s \n Unable to run your python process properly. Unable to provide any futher informations.",pbt); |
---|
[1] | 160 | map* err=createMap("text",pbt); |
---|
| 161 | addToMap(err,"code","NoApplicableCode"); |
---|
| 162 | printExceptionReportResponse(m,err); |
---|
[59] | 163 | Py_DECREF(arg1); |
---|
| 164 | Py_DECREF(arg2); |
---|
| 165 | Py_DECREF(arg3); |
---|
[1] | 166 | Py_XDECREF(pFunc); |
---|
| 167 | Py_DECREF(pArgs); |
---|
| 168 | Py_DECREF(pModule); |
---|
[59] | 169 | Py_DECREF(ptraceback); |
---|
| 170 | Py_DECREF(ptype); |
---|
| 171 | Py_DECREF(pValue); |
---|
[75] | 172 | #if not(defined(macintosh)) && not(defined(__MACH__) && defined(__APPLE__)) |
---|
[59] | 173 | Py_Finalize(); |
---|
[75] | 174 | #endif |
---|
[1] | 175 | exit(-1); |
---|
| 176 | } |
---|
| 177 | } |
---|
| 178 | else{ |
---|
| 179 | char tmpS[1024]; |
---|
| 180 | sprintf(tmpS, "Cannot find the %s function int the %s file.\n", s->name, tmp->value); |
---|
| 181 | map* tmps=createMap("text",tmpS); |
---|
| 182 | printExceptionReportResponse(m,tmps); |
---|
| 183 | Py_XDECREF(pFunc); |
---|
| 184 | Py_DECREF(pModule); |
---|
| 185 | exit(-1); |
---|
| 186 | } |
---|
| 187 | } else{ |
---|
| 188 | char tmpS[1024]; |
---|
[9] | 189 | sprintf(tmpS, "Python module %s cannot be loaded.\n", tmp->value); |
---|
[1] | 190 | map* tmps=createMap("text",tmpS); |
---|
| 191 | printExceptionReportResponse(m,tmps); |
---|
| 192 | if (PyErr_Occurred()) |
---|
| 193 | PyErr_Print(); |
---|
| 194 | exit(-1); |
---|
| 195 | } |
---|
[75] | 196 | #if not(defined(macintosh)) && not(defined(__MACH__) && defined(__APPLE__)) |
---|
[1] | 197 | Py_Finalize(); |
---|
[75] | 198 | #endif |
---|
[1] | 199 | return res; |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | PyDictObject* PyDict_FromMaps(maps* t){ |
---|
| 203 | PyObject* res=PyDict_New( ); |
---|
| 204 | maps* tmp=t; |
---|
| 205 | while(tmp!=NULL){ |
---|
[61] | 206 | PyObject* subc=(PyObject*)PyDict_FromMap(tmp->content); |
---|
[59] | 207 | if(PyDict_SetItem(res,PyString_FromString(tmp->name),subc)<0){ |
---|
[1] | 208 | fprintf(stderr,"Unable to parse params..."); |
---|
| 209 | exit(1); |
---|
| 210 | } |
---|
[59] | 211 | Py_DECREF(subc); |
---|
[1] | 212 | tmp=tmp->next; |
---|
| 213 | } |
---|
| 214 | return (PyDictObject*) res; |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | PyDictObject* PyDict_FromMap(map* t){ |
---|
| 218 | PyObject* res=PyDict_New( ); |
---|
| 219 | map* tmp=t; |
---|
[58] | 220 | map* size=getMap(tmp,"size"); |
---|
[1] | 221 | while(tmp!=NULL){ |
---|
[59] | 222 | PyObject* name=PyString_FromString(tmp->name); |
---|
[58] | 223 | if(strcasecmp(tmp->name,"value")==0){ |
---|
| 224 | if(size!=NULL){ |
---|
[59] | 225 | PyObject* value=PyString_FromStringAndSize(tmp->value,atoi(size->value)); |
---|
| 226 | if(PyDict_SetItem(res,name,value)<0){ |
---|
[43] | 227 | fprintf(stderr,"Unable to parse params..."); |
---|
[67] | 228 | Py_DECREF(value); |
---|
[43] | 229 | exit(1); |
---|
| 230 | } |
---|
[59] | 231 | Py_DECREF(value); |
---|
[58] | 232 | } |
---|
[59] | 233 | else{ |
---|
| 234 | PyObject* value=PyString_FromString(tmp->value); |
---|
| 235 | if(PyDict_SetItem(res,name,value)<0){ |
---|
[43] | 236 | fprintf(stderr,"Unable to parse params..."); |
---|
[67] | 237 | Py_DECREF(value); |
---|
[43] | 238 | exit(1); |
---|
| 239 | } |
---|
[59] | 240 | Py_DECREF(value); |
---|
| 241 | } |
---|
| 242 | } |
---|
| 243 | else{ |
---|
| 244 | PyObject* value=PyString_FromString(tmp->value); |
---|
| 245 | if(PyDict_SetItem(res,name,value)<0){ |
---|
[43] | 246 | fprintf(stderr,"Unable to parse params..."); |
---|
[67] | 247 | Py_DECREF(value); |
---|
[43] | 248 | exit(1); |
---|
| 249 | } |
---|
[59] | 250 | Py_DECREF(value); |
---|
| 251 | } |
---|
| 252 | Py_DECREF(name); |
---|
[1] | 253 | tmp=tmp->next; |
---|
| 254 | } |
---|
| 255 | return (PyDictObject*) res; |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | maps* mapsFromPyDict(PyDictObject* t){ |
---|
| 259 | maps* res=NULL; |
---|
| 260 | maps* cursor=res; |
---|
| 261 | PyObject* list=PyDict_Keys((PyObject*)t); |
---|
| 262 | int nb=PyList_Size(list); |
---|
| 263 | int i; |
---|
| 264 | for(i=0;i<nb;i++){ |
---|
| 265 | #ifdef DEBUG |
---|
| 266 | fprintf(stderr,">> parsing maps %d\n",i); |
---|
| 267 | #endif |
---|
| 268 | PyObject* key=PyList_GetItem(list,i); |
---|
| 269 | PyObject* value=PyDict_GetItem((PyObject*)t,key); |
---|
| 270 | #ifdef DEBUG |
---|
| 271 | fprintf(stderr,">> DEBUG VALUES : %s => %s\n", |
---|
| 272 | PyString_AsString(key),PyString_AsString(value)); |
---|
| 273 | #endif |
---|
[9] | 274 | cursor=(maps*)malloc(MAPS_SIZE); |
---|
[1] | 275 | cursor->name=PyString_AsString(key); |
---|
| 276 | #ifdef DEBUG |
---|
| 277 | dumpMap(mapFromPyDict((PyDictObject*)value)); |
---|
| 278 | #endif |
---|
| 279 | cursor->content=mapFromPyDict((PyDictObject*)value); |
---|
| 280 | cursor->next=NULL; |
---|
| 281 | if(res==NULL) |
---|
[59] | 282 | res=dupMaps(&cursor); |
---|
[1] | 283 | else |
---|
| 284 | addMapsToMaps(&res,cursor); |
---|
[59] | 285 | freeMap(&cursor->content); |
---|
| 286 | free(cursor->content); |
---|
| 287 | free(cursor); |
---|
[67] | 288 | Py_DECREF(value); |
---|
| 289 | Py_DECREF(key); |
---|
[1] | 290 | #ifdef DEBUG |
---|
| 291 | dumpMaps(res); |
---|
| 292 | fprintf(stderr,">> parsed maps %d\n",i); |
---|
| 293 | #endif |
---|
| 294 | } |
---|
[59] | 295 | Py_DECREF(list); |
---|
[1] | 296 | return res; |
---|
| 297 | } |
---|
| 298 | |
---|
| 299 | map* mapFromPyDict(PyDictObject* t){ |
---|
| 300 | map* res=NULL; |
---|
| 301 | PyObject* list=PyDict_Keys((PyObject*)t); |
---|
| 302 | int nb=PyList_Size(list); |
---|
| 303 | int i; |
---|
[67] | 304 | int sizeValue=-1; |
---|
[1] | 305 | for(i=0;i<nb;i++){ |
---|
| 306 | PyObject* key=PyList_GetItem(list,i); |
---|
| 307 | PyObject* value=PyDict_GetItem((PyObject*)t,key); |
---|
| 308 | #ifdef DEBUG |
---|
| 309 | fprintf(stderr,">> DEBUG VALUES : %s => %s\n", |
---|
| 310 | PyString_AsString(key),PyString_AsString(value)); |
---|
| 311 | #endif |
---|
[100] | 312 | if(strcmp(PyString_AsString(key),"value")==0){ |
---|
| 313 | char *buffer=NULL; |
---|
[67] | 314 | Py_ssize_t size; |
---|
| 315 | PyString_AsStringAndSize(value,&buffer,&size); |
---|
[100] | 316 | fprintf(stderr,"SIZE %d\n",size); |
---|
[67] | 317 | if(res!=NULL){ |
---|
| 318 | addToMap(res,PyString_AsString(key),""); |
---|
| 319 | }else{ |
---|
| 320 | res=createMap(PyString_AsString(key),""); |
---|
| 321 | } |
---|
| 322 | map* tmpR=getMap(res,"value"); |
---|
| 323 | free(tmpR->value); |
---|
[100] | 324 | tmpR->value=(char*)malloc((size+1)*sizeof(char)); |
---|
| 325 | memmove(tmpR->value,buffer,size*sizeof(char)); |
---|
| 326 | tmpR->value[size]=0; |
---|
| 327 | char sin[1024]; |
---|
| 328 | sprintf(sin,"%d",size); |
---|
| 329 | addToMap(res,"size",sin); |
---|
[67] | 330 | }else{ |
---|
| 331 | if(res!=NULL) |
---|
| 332 | addToMap(res,PyString_AsString(key),PyString_AsString(value)); |
---|
| 333 | else |
---|
| 334 | res=createMap(PyString_AsString(key),PyString_AsString(value)); |
---|
| 335 | } |
---|
| 336 | Py_DECREF(key); |
---|
[1] | 337 | } |
---|
[67] | 338 | Py_DECREF(list); |
---|
[1] | 339 | return res; |
---|
| 340 | } |
---|