[1] | 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 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; |
---|
| 36 | fprintf(stderr,"PYTHON SUPPORT \n"); |
---|
| 37 | fflush(stderr); |
---|
[1] | 38 | if(tmp!=NULL){ |
---|
[9] | 39 | fprintf(stderr,"PYTHON SUPPORT (%i)\n",strlen(tmp->value)); |
---|
| 40 | python_path=(char*)malloc((strlen(tmp->value))*sizeof(char)); |
---|
| 41 | sprintf(python_path,"%s",tmp->value); |
---|
[1] | 42 | } |
---|
| 43 | else{ |
---|
[9] | 44 | python_path=strdup("."); |
---|
[1] | 45 | } |
---|
| 46 | tmp=NULL; |
---|
| 47 | tmp=getMap(request,"metapath"); |
---|
| 48 | char *pythonpath=(char*)malloc((1+strlen(python_path)+2048)*sizeof(char)); |
---|
| 49 | if(tmp!=NULL && strcmp(tmp->value,"")!=0) |
---|
| 50 | #ifdef WIN32 |
---|
[9] | 51 | sprintf(pythonpath,"%s/%s/;%s",ntmp,tmp->value,python_path); |
---|
[1] | 52 | #else |
---|
[9] | 53 | sprintf(pythonpath,"%s/%s/:%s",ntmp,tmp->value,python_path); |
---|
[1] | 54 | #endif |
---|
| 55 | else |
---|
| 56 | #ifdef WIN32 |
---|
| 57 | sprintf(pythonpath,"%s;%s",ntmp,python_path); |
---|
| 58 | #else |
---|
[9] | 59 | sprintf(pythonpath,"%s:%s",ntmp,python_path); |
---|
[1] | 60 | #endif |
---|
[9] | 61 | //#ifdef DEBUG |
---|
| 62 | fprintf(stderr,"PYTHONPATH=%s\n",pythonpath); |
---|
| 63 | //#endif |
---|
[1] | 64 | #ifndef WIN32 |
---|
| 65 | setenv("PYTHONPATH",pythonpath,1); |
---|
| 66 | #else |
---|
| 67 | SetEnvironmentVariable("PYTHONPATH",pythonpath); |
---|
| 68 | #endif |
---|
| 69 | free(python_path); |
---|
| 70 | free(pythonpath); |
---|
[9] | 71 | |
---|
[1] | 72 | Py_Initialize(); |
---|
| 73 | PyObject *pName, *pModule, *pFunc; |
---|
| 74 | tmp=getMap(s->content,"serviceProvider"); |
---|
[9] | 75 | if(tmp!=NULL) |
---|
| 76 | pName = PyString_FromString(tmp->value); |
---|
| 77 | else{ |
---|
| 78 | map* err=createMap("text","Unable to parse serviceProvider please check your zcfg file."); |
---|
| 79 | addToMap(err,"code","NoApplicableCode"); |
---|
| 80 | printExceptionReportResponse(m,err); |
---|
| 81 | exit(-1); |
---|
| 82 | } |
---|
[1] | 83 | pModule = PyImport_Import(pName); |
---|
| 84 | int i; |
---|
| 85 | int res=SERVICE_FAILED; |
---|
| 86 | int cpid=getpid(); |
---|
| 87 | if (pModule != NULL) { |
---|
| 88 | pFunc=PyObject_GetAttrString(pModule,s->name); |
---|
| 89 | if (pFunc && PyCallable_Check(pFunc)){ |
---|
| 90 | PyDictObject* arg1=PyDict_FromMaps(m); |
---|
| 91 | PyDictObject* arg2=PyDict_FromMaps(inputs); |
---|
| 92 | PyDictObject* arg3=PyDict_FromMaps(outputs); |
---|
| 93 | PyObject *pArgs=PyTuple_New(3); |
---|
| 94 | PyObject *pValue; |
---|
| 95 | PyTuple_SetItem(pArgs, 0, (PyObject *)arg1); |
---|
| 96 | PyTuple_SetItem(pArgs, 1, (PyObject *)arg2); |
---|
| 97 | PyTuple_SetItem(pArgs, 2, (PyObject *)arg3); |
---|
| 98 | tmp=getMap(request,"storeExecuteResponse"); |
---|
| 99 | #ifdef DEBUG |
---|
| 100 | fprintf(stderr,"RUN IN NORMAL MODE \n"); |
---|
[9] | 101 | fflush(stderr); |
---|
[1] | 102 | #endif |
---|
| 103 | pValue = PyObject_CallObject(pFunc, pArgs); |
---|
| 104 | if (pValue != NULL) { |
---|
| 105 | res=PyInt_AsLong(pValue); |
---|
[9] | 106 | freeMaps(real_outputs); |
---|
| 107 | free(*real_outputs); |
---|
[59] | 108 | freeMaps(main_conf); |
---|
| 109 | free(*main_conf); |
---|
[57] | 110 | *main_conf=mapsFromPyDict(arg1); |
---|
[9] | 111 | *real_outputs=mapsFromPyDict(arg3); |
---|
[1] | 112 | #ifdef DEBUG |
---|
| 113 | fprintf(stderr,"Result of call: %i\n", PyInt_AsLong(pValue)); |
---|
| 114 | dumpMaps(inputs); |
---|
| 115 | dumpMaps(outputs); |
---|
| 116 | #endif |
---|
[9] | 117 | Py_DECREF(arg1); |
---|
| 118 | Py_DECREF(arg2); |
---|
| 119 | Py_DECREF(arg3); |
---|
| 120 | Py_DECREF(pArgs); |
---|
[1] | 121 | Py_DECREF(pValue); |
---|
| 122 | Py_XDECREF(pFunc); |
---|
| 123 | Py_DECREF(pModule); |
---|
[9] | 124 | }else{ |
---|
[1] | 125 | PyObject *ptype,*pvalue, *ptraceback; |
---|
| 126 | PyErr_Fetch(&ptype, &pvalue, &ptraceback); |
---|
| 127 | PyObject *trace=PyObject_Str(pvalue); |
---|
| 128 | char tb[1024]; |
---|
| 129 | char pbt[10240]; |
---|
| 130 | if(PyString_Check(trace)) |
---|
| 131 | sprintf(pbt,"TRACE : %s",PyString_AsString(trace)); |
---|
| 132 | else |
---|
| 133 | fprintf(stderr,"EMPTY TRACE ?"); |
---|
| 134 | trace=NULL; |
---|
| 135 | trace=PyObject_Str(ptype); |
---|
[59] | 136 | if(PyString_Check(trace)){ |
---|
| 137 | char *tpbt=strdup(pbt); |
---|
| 138 | sprintf(pbt,"%s\nTRACE : %s",tpbt,PyString_AsString(trace)); |
---|
| 139 | free(tpbt); |
---|
| 140 | } |
---|
[1] | 141 | else |
---|
| 142 | fprintf(stderr,"EMPTY TRACE ?"); |
---|
| 143 | PyObject *t; |
---|
| 144 | pName = PyString_FromString("traceback"); |
---|
| 145 | pModule = PyImport_Import(pName); |
---|
| 146 | pArgs = PyTuple_New(1); |
---|
| 147 | PyTuple_SetItem(pArgs, 0, ptraceback); |
---|
| 148 | pFunc = PyObject_GetAttrString(pModule,"format_tb"); |
---|
| 149 | pValue = PyObject_CallObject(pFunc, pArgs); |
---|
| 150 | trace=NULL; |
---|
| 151 | trace=PyObject_Str(pValue); |
---|
| 152 | if(PyString_Check(trace)) |
---|
[59] | 153 | sprintf(pbt,"%s\nUnable to run your python process properly. Please check the following messages : %s",pbt,PyString_AsString(trace)); |
---|
[1] | 154 | else |
---|
[59] | 155 | sprintf(pbt,"%s \n Unable to run your python process properly. Unable to provide any futher informations.",pbt); |
---|
[1] | 156 | map* err=createMap("text",pbt); |
---|
| 157 | addToMap(err,"code","NoApplicableCode"); |
---|
| 158 | printExceptionReportResponse(m,err); |
---|
[59] | 159 | Py_DECREF(arg1); |
---|
| 160 | Py_DECREF(arg2); |
---|
| 161 | Py_DECREF(arg3); |
---|
[1] | 162 | Py_XDECREF(pFunc); |
---|
| 163 | Py_DECREF(pArgs); |
---|
| 164 | Py_DECREF(pModule); |
---|
[59] | 165 | Py_DECREF(ptraceback); |
---|
| 166 | Py_DECREF(ptype); |
---|
| 167 | Py_DECREF(pValue); |
---|
| 168 | Py_Finalize(); |
---|
[1] | 169 | exit(-1); |
---|
| 170 | } |
---|
| 171 | } |
---|
| 172 | else{ |
---|
| 173 | char tmpS[1024]; |
---|
| 174 | sprintf(tmpS, "Cannot find the %s function int the %s file.\n", s->name, tmp->value); |
---|
| 175 | map* tmps=createMap("text",tmpS); |
---|
| 176 | printExceptionReportResponse(m,tmps); |
---|
| 177 | Py_XDECREF(pFunc); |
---|
| 178 | Py_DECREF(pModule); |
---|
| 179 | exit(-1); |
---|
| 180 | } |
---|
| 181 | } else{ |
---|
| 182 | char tmpS[1024]; |
---|
[9] | 183 | sprintf(tmpS, "Python module %s cannot be loaded.\n", tmp->value); |
---|
[1] | 184 | map* tmps=createMap("text",tmpS); |
---|
| 185 | printExceptionReportResponse(m,tmps); |
---|
| 186 | if (PyErr_Occurred()) |
---|
| 187 | PyErr_Print(); |
---|
| 188 | exit(-1); |
---|
| 189 | } |
---|
| 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){ |
---|
[59] | 198 | PyObject* subc=PyDict_FromMap(tmp->content); |
---|
| 199 | if(PyDict_SetItem(res,PyString_FromString(tmp->name),subc)<0){ |
---|
[1] | 200 | fprintf(stderr,"Unable to parse params..."); |
---|
| 201 | exit(1); |
---|
| 202 | } |
---|
[59] | 203 | Py_DECREF(subc); |
---|
[1] | 204 | tmp=tmp->next; |
---|
| 205 | } |
---|
| 206 | return (PyDictObject*) res; |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | PyDictObject* PyDict_FromMap(map* t){ |
---|
| 210 | PyObject* res=PyDict_New( ); |
---|
| 211 | map* tmp=t; |
---|
[58] | 212 | map* size=getMap(tmp,"size"); |
---|
[1] | 213 | while(tmp!=NULL){ |
---|
[59] | 214 | PyObject* name=PyString_FromString(tmp->name); |
---|
[58] | 215 | if(strcasecmp(tmp->name,"value")==0){ |
---|
| 216 | if(size!=NULL){ |
---|
[59] | 217 | PyObject* value=PyString_FromStringAndSize(tmp->value,atoi(size->value)); |
---|
| 218 | if(PyDict_SetItem(res,name,value)<0){ |
---|
[43] | 219 | fprintf(stderr,"Unable to parse params..."); |
---|
| 220 | exit(1); |
---|
| 221 | } |
---|
[59] | 222 | Py_DECREF(value); |
---|
[58] | 223 | } |
---|
[59] | 224 | else{ |
---|
| 225 | PyObject* value=PyString_FromString(tmp->value); |
---|
| 226 | if(PyDict_SetItem(res,name,value)<0){ |
---|
[43] | 227 | fprintf(stderr,"Unable to parse params..."); |
---|
| 228 | exit(1); |
---|
| 229 | } |
---|
[59] | 230 | Py_DECREF(value); |
---|
| 231 | } |
---|
| 232 | } |
---|
| 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..."); |
---|
| 237 | exit(1); |
---|
| 238 | } |
---|
[59] | 239 | Py_DECREF(value); |
---|
| 240 | } |
---|
| 241 | Py_DECREF(name); |
---|
[1] | 242 | tmp=tmp->next; |
---|
| 243 | } |
---|
| 244 | return (PyDictObject*) res; |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | maps* mapsFromPyDict(PyDictObject* t){ |
---|
| 248 | maps* res=NULL; |
---|
| 249 | maps* cursor=res; |
---|
| 250 | PyObject* list=PyDict_Keys((PyObject*)t); |
---|
| 251 | int nb=PyList_Size(list); |
---|
| 252 | int i; |
---|
| 253 | for(i=0;i<nb;i++){ |
---|
| 254 | #ifdef DEBUG |
---|
| 255 | fprintf(stderr,">> parsing maps %d\n",i); |
---|
| 256 | #endif |
---|
| 257 | PyObject* key=PyList_GetItem(list,i); |
---|
| 258 | PyObject* value=PyDict_GetItem((PyObject*)t,key); |
---|
| 259 | #ifdef DEBUG |
---|
| 260 | fprintf(stderr,">> DEBUG VALUES : %s => %s\n", |
---|
| 261 | PyString_AsString(key),PyString_AsString(value)); |
---|
| 262 | #endif |
---|
[9] | 263 | cursor=(maps*)malloc(MAPS_SIZE); |
---|
[1] | 264 | cursor->name=PyString_AsString(key); |
---|
| 265 | #ifdef DEBUG |
---|
| 266 | dumpMap(mapFromPyDict((PyDictObject*)value)); |
---|
| 267 | #endif |
---|
| 268 | cursor->content=mapFromPyDict((PyDictObject*)value); |
---|
| 269 | cursor->next=NULL; |
---|
| 270 | if(res==NULL) |
---|
[59] | 271 | res=dupMaps(&cursor); |
---|
[1] | 272 | else |
---|
| 273 | addMapsToMaps(&res,cursor); |
---|
[59] | 274 | freeMap(&cursor->content); |
---|
| 275 | free(cursor->content); |
---|
| 276 | free(cursor); |
---|
[1] | 277 | #ifdef DEBUG |
---|
| 278 | dumpMaps(res); |
---|
| 279 | fprintf(stderr,">> parsed maps %d\n",i); |
---|
| 280 | #endif |
---|
| 281 | } |
---|
[59] | 282 | Py_DECREF(list); |
---|
[1] | 283 | return res; |
---|
| 284 | } |
---|
| 285 | |
---|
| 286 | map* mapFromPyDict(PyDictObject* t){ |
---|
| 287 | map* res=NULL; |
---|
| 288 | PyObject* list=PyDict_Keys((PyObject*)t); |
---|
| 289 | int nb=PyList_Size(list); |
---|
| 290 | int i; |
---|
| 291 | for(i=0;i<nb;i++){ |
---|
| 292 | PyObject* key=PyList_GetItem(list,i); |
---|
| 293 | PyObject* value=PyDict_GetItem((PyObject*)t,key); |
---|
| 294 | #ifdef DEBUG |
---|
| 295 | fprintf(stderr,">> DEBUG VALUES : %s => %s\n", |
---|
| 296 | PyString_AsString(key),PyString_AsString(value)); |
---|
| 297 | #endif |
---|
| 298 | if(res!=NULL) |
---|
| 299 | addToMap(res,PyString_AsString(key),PyString_AsString(value)); |
---|
| 300 | else |
---|
| 301 | res=createMap(PyString_AsString(key),PyString_AsString(value)); |
---|
| 302 | } |
---|
| 303 | return res; |
---|
| 304 | } |
---|