source: trunk/zoo-project/zoo-kernel/service_internal_python.c @ 364

Last change on this file since 364 was 364, checked in by djay, 10 years ago

Update to make ZOO-Kernel able to compile and run from Windows Platforms. A special thanks to Espen Messel, Knut Landmark and Benrd Härtwig for providing many patches that I can successfully apply on the SVN source tree and to Farkas for continuing requesting for ZOO-Kernel to run on Windows platforms privately and through the ZOO-Discuss mailing list.

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 11.1 KB
Line 
1/**
2 * Author : Gérald FENOY
3 *
4 * Copyright (c) 2009-2012 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
27int 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");
35  char *python_path;
36#ifdef DEBUG
37  fprintf(stderr,"PYTHON SUPPORT \n");
38#endif
39  fflush(stderr);
40  if(tmp!=NULL){
41#ifdef DEBUG
42    fprintf(stderr,"PYTHON SUPPORT (%i)\n",strlen(tmp->value));
43#endif
44    python_path=(char*)malloc((strlen(tmp->value))*sizeof(char));
45    sprintf(python_path,"%s",tmp->value);
46  }
47  else{
48    python_path=strdup(".");
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
55    sprintf(pythonpath,"%s/%s/;%s",ntmp,tmp->value,python_path);
56#else
57  sprintf(pythonpath,"%s/%s/:%s",ntmp,tmp->value,python_path);
58#endif
59  else
60#ifdef WIN32
61    sprintf(pythonpath,"%s;%s",ntmp,python_path);
62#else
63  sprintf(pythonpath,"%s:%s",ntmp,python_path);
64#endif
65#ifdef DEBUG
66    fprintf(stderr,"PYTHONPATH=%s\n",pythonpath);
67#endif
68#ifndef WIN32
69  setenv("PYTHONPATH",pythonpath,1);
70#else
71  SetEnvironmentVariable("PYTHONPATH",pythonpath);
72  char* toto=(char*)malloc((strlen(pythonpath)+12)*sizeof(char));
73  sprintf(toto,"PYTHONPATH=%s",pythonpath);
74  putenv(toto);
75#endif
76  free(python_path);
77  free(pythonpath);
78
79  PyThreadState *mainstate;
80  PyEval_InitThreads();
81  Py_Initialize();
82  mainstate = PyThreadState_Swap(NULL);
83  PyEval_ReleaseLock();
84  PyGILState_STATE gstate;
85  gstate = PyGILState_Ensure();
86  PyObject *pName, *pModule, *pFunc;
87  tmp=getMap(s->content,"serviceProvider");
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  }
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)){
101      PyObject *pValue;
102      PyDictObject* arg1=PyDict_FromMaps(m);
103      PyDictObject* arg2=PyDict_FromMaps(inputs);
104      PyDictObject* arg3=PyDict_FromMaps(outputs);
105      PyObject *pArgs=PyTuple_New(3);
106      if (!pArgs)
107        return -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");
114      fflush(stderr);
115#endif
116      pValue = PyObject_CallObject(pFunc, pArgs);
117      if (pValue != NULL) {
118        res=PyInt_AsLong(pValue);
119        freeMaps(real_outputs);
120        free(*real_outputs);
121        freeMaps(main_conf);
122        free(*main_conf);
123        *main_conf=mapsFromPyDict(arg1);
124        *real_outputs=mapsFromPyDict(arg3);
125#ifdef DEBUG
126        fprintf(stderr,"Result of call: %i\n", PyInt_AsLong(pValue));
127        dumpMaps(inputs);
128        dumpMaps(*real_outputs);
129#endif
130      }else{     
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);
141        if(PyString_Check(trace)){
142          char *tpbt=strdup(pbt);
143          sprintf(pbt,"%s\n%s\0",tpbt,PyString_AsString(trace));
144          free(tpbt);
145        }
146        else
147          fprintf(stderr,"EMPTY TRACE ?");
148       
149        char *tpbt=strdup(pbt);
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))
159          sprintf(pbt,"%s\nUnable to run your python process properly. Please check the following messages : %s",tpbt,PyString_AsString(trace));
160        else
161          sprintf(pbt,"%s \n Unable to run your python process properly. Unable to provide any futher informations. %s",tpbt);
162        free(tpbt);
163        map* err=createMap("text",pbt);
164        addToMap(err,"code","NoApplicableCode");
165        printExceptionReportResponse(m,err);
166        res=-1;
167      }
168    }
169    else{
170      char tmpS[1024];
171      sprintf(tmpS, "Cannot find the %s function in the %s file.\n", s->name, tmp->value);
172      map* tmps=createMap("text",tmpS);
173      printExceptionReportResponse(m,tmps);
174      res=-1;
175    }
176  } else{
177    char tmpS[1024];
178    sprintf(tmpS, "Python module %s cannot be loaded.\n", tmp->value);
179    map* tmps=createMap("text",tmpS);
180    printExceptionReportResponse(m,tmps);
181    if (PyErr_Occurred())
182      PyErr_Print();
183    PyErr_Clear();
184    res=-1;
185    //exit(-1);
186  } 
187  PyGILState_Release(gstate);
188  PyEval_AcquireLock();
189  PyThreadState_Swap(mainstate);
190  Py_Finalize();
191  return res;
192}
193
194PyDictObject* PyDict_FromMaps(maps* t){
195  PyObject* res=PyDict_New( );
196  maps* tmp=t;
197  while(tmp!=NULL){
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;
203    }
204    Py_DECREF(name);
205    tmp=tmp->next;
206  } 
207  return (PyDictObject*) res;
208}
209
210PyDictObject* PyDict_FromMap(map* t){
211  PyObject* res=PyDict_New( );
212  map* tmp=t;
213  int hasSize=0;
214  map* isArray=getMap(tmp,"isArray");
215  map* size=getMap(tmp,"size");
216  map* tmap=getMapType(tmp);
217  while(tmp!=NULL){
218    PyObject* name=PyString_FromString(tmp->name);
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){
285        PyObject* value=PyString_FromStringAndSize(tmp->value,atoi(size->value));
286        if(PyDict_SetItem(res,name,value)<0){
287          fprintf(stderr,"Unable to set key value pair...");
288          return NULL;
289        }
290      }
291      else{
292        PyObject* value=PyString_FromString(tmp->value);
293        if(PyDict_SetItem(res,name,value)<0){
294          fprintf(stderr,"Unable to set key value pair...");
295          return NULL;
296        }
297      }
298    }
299    else{
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        }
306      }
307    }
308    Py_DECREF(name);
309    tmp=tmp->next;
310  }
311  return (PyDictObject*) res;
312}
313
314maps* 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
330    cursor=(maps*)malloc(MAPS_SIZE);
331    cursor->name=PyString_AsString(key);
332    cursor->content=mapFromPyDict((PyDictObject*)value);
333#ifdef DEBUG
334    dumpMap(cursor->content);
335#endif
336    cursor->next=NULL;
337    if(res==NULL)
338      res=dupMaps(&cursor);
339    else
340      addMapsToMaps(&res,cursor);
341    freeMap(&cursor->content);
342    free(cursor->content);
343    free(cursor);
344#ifdef DEBUG
345    dumpMaps(res);
346    fprintf(stderr,">> parsed maps %d\n",i);
347#endif
348  }
349  return res;
350}
351
352map* 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
364    if(strcmp(PyString_AsString(key),"value")==0){
365      char *buffer=NULL;
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);
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);
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    }
387  }
388  return res;
389}
Note: See TracBrowser for help on using the repository browser.

Search

ZOO Sponsors

http://www.zoo-project.org/trac/chrome/site/img/geolabs-logo.pnghttp://www.zoo-project.org/trac/chrome/site/img/neogeo-logo.png http://www.zoo-project.org/trac/chrome/site/img/apptech-logo.png http://www.zoo-project.org/trac/chrome/site/img/3liz-logo.png http://www.zoo-project.org/trac/chrome/site/img/gateway-logo.png

Become a sponsor !

Knowledge partners

http://www.zoo-project.org/trac/chrome/site/img/ocu-logo.png http://www.zoo-project.org/trac/chrome/site/img/gucas-logo.png http://www.zoo-project.org/trac/chrome/site/img/polimi-logo.png http://www.zoo-project.org/trac/chrome/site/img/fem-logo.png http://www.zoo-project.org/trac/chrome/site/img/supsi-logo.png http://www.zoo-project.org/trac/chrome/site/img/cumtb-logo.png

Become a knowledge partner

Related links

http://zoo-project.org/img/ogclogo.png http://zoo-project.org/img/osgeologo.png