[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 | |
---|
[301] | 76 | PyThreadState *mainstate; |
---|
| 77 | PyEval_InitThreads(); |
---|
[1] | 78 | Py_Initialize(); |
---|
[301] | 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)){ |
---|
[217] | 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); |
---|
[217] | 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); |
---|
| 140 | sprintf(pbt,"%s\nTRACE : %s",tpbt,PyString_AsString(trace)); |
---|
| 141 | free(tpbt); |
---|
| 142 | } |
---|
[1] | 143 | else |
---|
| 144 | fprintf(stderr,"EMPTY TRACE ?"); |
---|
| 145 | pName = PyString_FromString("traceback"); |
---|
| 146 | pModule = PyImport_Import(pName); |
---|
| 147 | pArgs = PyTuple_New(1); |
---|
| 148 | PyTuple_SetItem(pArgs, 0, ptraceback); |
---|
| 149 | pFunc = PyObject_GetAttrString(pModule,"format_tb"); |
---|
| 150 | pValue = PyObject_CallObject(pFunc, pArgs); |
---|
| 151 | trace=NULL; |
---|
| 152 | trace=PyObject_Str(pValue); |
---|
| 153 | if(PyString_Check(trace)) |
---|
[59] | 154 | sprintf(pbt,"%s\nUnable to run your python process properly. Please check the following messages : %s",pbt,PyString_AsString(trace)); |
---|
[1] | 155 | else |
---|
[59] | 156 | sprintf(pbt,"%s \n Unable to run your python process properly. Unable to provide any futher informations.",pbt); |
---|
[1] | 157 | map* err=createMap("text",pbt); |
---|
| 158 | addToMap(err,"code","NoApplicableCode"); |
---|
| 159 | printExceptionReportResponse(m,err); |
---|
[301] | 160 | res=-1; |
---|
[1] | 161 | } |
---|
| 162 | } |
---|
| 163 | else{ |
---|
| 164 | char tmpS[1024]; |
---|
[301] | 165 | sprintf(tmpS, "Cannot find the %s function in the %s file.\n", s->name, tmp->value); |
---|
[1] | 166 | map* tmps=createMap("text",tmpS); |
---|
| 167 | printExceptionReportResponse(m,tmps); |
---|
[301] | 168 | res=-1; |
---|
[1] | 169 | } |
---|
| 170 | } else{ |
---|
| 171 | char tmpS[1024]; |
---|
[9] | 172 | sprintf(tmpS, "Python module %s cannot be loaded.\n", tmp->value); |
---|
[1] | 173 | map* tmps=createMap("text",tmpS); |
---|
| 174 | printExceptionReportResponse(m,tmps); |
---|
| 175 | if (PyErr_Occurred()) |
---|
| 176 | PyErr_Print(); |
---|
[301] | 177 | PyErr_Clear(); |
---|
| 178 | res=-1; |
---|
| 179 | //exit(-1); |
---|
[1] | 180 | } |
---|
[301] | 181 | PyGILState_Release(gstate); |
---|
| 182 | PyEval_AcquireLock(); |
---|
| 183 | PyThreadState_Swap(mainstate); |
---|
[1] | 184 | Py_Finalize(); |
---|
| 185 | return res; |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | PyDictObject* PyDict_FromMaps(maps* t){ |
---|
| 189 | PyObject* res=PyDict_New( ); |
---|
| 190 | maps* tmp=t; |
---|
| 191 | while(tmp!=NULL){ |
---|
[301] | 192 | PyObject* value=(PyObject*)PyDict_FromMap(tmp->content); |
---|
| 193 | PyObject* name=PyString_FromString(tmp->name); |
---|
| 194 | if(PyDict_SetItem(res,name,value)<0){ |
---|
| 195 | fprintf(stderr,"Unable to set map value ..."); |
---|
| 196 | return NULL; |
---|
[1] | 197 | } |
---|
[301] | 198 | Py_DECREF(name); |
---|
[1] | 199 | tmp=tmp->next; |
---|
| 200 | } |
---|
| 201 | return (PyDictObject*) res; |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | PyDictObject* PyDict_FromMap(map* t){ |
---|
| 205 | PyObject* res=PyDict_New( ); |
---|
| 206 | map* tmp=t; |
---|
[58] | 207 | map* size=getMap(tmp,"size"); |
---|
[1] | 208 | while(tmp!=NULL){ |
---|
[59] | 209 | PyObject* name=PyString_FromString(tmp->name); |
---|
[58] | 210 | if(strcasecmp(tmp->name,"value")==0){ |
---|
| 211 | if(size!=NULL){ |
---|
[59] | 212 | PyObject* value=PyString_FromStringAndSize(tmp->value,atoi(size->value)); |
---|
| 213 | if(PyDict_SetItem(res,name,value)<0){ |
---|
[301] | 214 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 215 | return NULL; |
---|
[43] | 216 | } |
---|
[58] | 217 | } |
---|
[59] | 218 | else{ |
---|
| 219 | PyObject* value=PyString_FromString(tmp->value); |
---|
| 220 | if(PyDict_SetItem(res,name,value)<0){ |
---|
[301] | 221 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 222 | return NULL; |
---|
[43] | 223 | } |
---|
[59] | 224 | } |
---|
| 225 | } |
---|
| 226 | else{ |
---|
| 227 | PyObject* value=PyString_FromString(tmp->value); |
---|
| 228 | if(PyDict_SetItem(res,name,value)<0){ |
---|
[301] | 229 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 230 | return NULL; |
---|
[43] | 231 | } |
---|
[59] | 232 | } |
---|
| 233 | Py_DECREF(name); |
---|
[1] | 234 | tmp=tmp->next; |
---|
| 235 | } |
---|
| 236 | return (PyDictObject*) res; |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | maps* mapsFromPyDict(PyDictObject* t){ |
---|
| 240 | maps* res=NULL; |
---|
| 241 | maps* cursor=res; |
---|
| 242 | PyObject* list=PyDict_Keys((PyObject*)t); |
---|
| 243 | int nb=PyList_Size(list); |
---|
| 244 | int i; |
---|
| 245 | for(i=0;i<nb;i++){ |
---|
| 246 | #ifdef DEBUG |
---|
| 247 | fprintf(stderr,">> parsing maps %d\n",i); |
---|
| 248 | #endif |
---|
| 249 | PyObject* key=PyList_GetItem(list,i); |
---|
| 250 | PyObject* value=PyDict_GetItem((PyObject*)t,key); |
---|
| 251 | #ifdef DEBUG |
---|
| 252 | fprintf(stderr,">> DEBUG VALUES : %s => %s\n", |
---|
| 253 | PyString_AsString(key),PyString_AsString(value)); |
---|
| 254 | #endif |
---|
[9] | 255 | cursor=(maps*)malloc(MAPS_SIZE); |
---|
[1] | 256 | cursor->name=PyString_AsString(key); |
---|
[301] | 257 | cursor->content=mapFromPyDict((PyDictObject*)value); |
---|
[1] | 258 | #ifdef DEBUG |
---|
[301] | 259 | dumpMap(cursor->content); |
---|
[1] | 260 | #endif |
---|
| 261 | cursor->next=NULL; |
---|
| 262 | if(res==NULL) |
---|
[59] | 263 | res=dupMaps(&cursor); |
---|
[1] | 264 | else |
---|
| 265 | addMapsToMaps(&res,cursor); |
---|
[59] | 266 | freeMap(&cursor->content); |
---|
| 267 | free(cursor->content); |
---|
| 268 | free(cursor); |
---|
[1] | 269 | #ifdef DEBUG |
---|
| 270 | dumpMaps(res); |
---|
| 271 | fprintf(stderr,">> parsed maps %d\n",i); |
---|
| 272 | #endif |
---|
| 273 | } |
---|
| 274 | return res; |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | map* mapFromPyDict(PyDictObject* t){ |
---|
| 278 | map* res=NULL; |
---|
| 279 | PyObject* list=PyDict_Keys((PyObject*)t); |
---|
| 280 | int nb=PyList_Size(list); |
---|
| 281 | int i; |
---|
| 282 | for(i=0;i<nb;i++){ |
---|
| 283 | PyObject* key=PyList_GetItem(list,i); |
---|
| 284 | PyObject* value=PyDict_GetItem((PyObject*)t,key); |
---|
| 285 | #ifdef DEBUG |
---|
| 286 | fprintf(stderr,">> DEBUG VALUES : %s => %s\n", |
---|
| 287 | PyString_AsString(key),PyString_AsString(value)); |
---|
| 288 | #endif |
---|
[100] | 289 | if(strcmp(PyString_AsString(key),"value")==0){ |
---|
| 290 | char *buffer=NULL; |
---|
[67] | 291 | Py_ssize_t size; |
---|
| 292 | PyString_AsStringAndSize(value,&buffer,&size); |
---|
| 293 | if(res!=NULL){ |
---|
| 294 | addToMap(res,PyString_AsString(key),""); |
---|
| 295 | }else{ |
---|
| 296 | res=createMap(PyString_AsString(key),""); |
---|
| 297 | } |
---|
| 298 | map* tmpR=getMap(res,"value"); |
---|
| 299 | free(tmpR->value); |
---|
[100] | 300 | tmpR->value=(char*)malloc((size+1)*sizeof(char)); |
---|
| 301 | memmove(tmpR->value,buffer,size*sizeof(char)); |
---|
| 302 | tmpR->value[size]=0; |
---|
| 303 | char sin[1024]; |
---|
| 304 | sprintf(sin,"%d",size); |
---|
| 305 | addToMap(res,"size",sin); |
---|
[67] | 306 | }else{ |
---|
| 307 | if(res!=NULL) |
---|
| 308 | addToMap(res,PyString_AsString(key),PyString_AsString(value)); |
---|
| 309 | else |
---|
| 310 | res=createMap(PyString_AsString(key),PyString_AsString(value)); |
---|
| 311 | } |
---|
[1] | 312 | } |
---|
| 313 | return res; |
---|
| 314 | } |
---|