source: trunk/zoo-kernel/service_internal_python.c @ 56

Last change on this file since 56 was 45, checked in by djay, 14 years ago

Solving the issue about ZOO Python Support. I will have to review after coming back at home. Now it works, I've tested from the official zoo-project server and the Boundary got the same issue as reported by Niels, now fixed. I have to go to my gate in 5 minutes and to pack all my stuff now..

File size: 8.4 KB
Line 
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
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  fprintf(stderr,"PYTHON SUPPORT \n");
37  fflush(stderr);
38  if(tmp!=NULL){
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);
42  }
43  else{
44    python_path=strdup(".");
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
51    sprintf(pythonpath,"%s/%s/;%s",ntmp,tmp->value,python_path);
52#else
53  sprintf(pythonpath,"%s/%s/:%s",ntmp,tmp->value,python_path);
54#endif
55  else
56#ifdef WIN32
57    sprintf(pythonpath,"%s;%s",ntmp,python_path);
58#else
59  sprintf(pythonpath,"%s:%s",ntmp,python_path);
60#endif
61  //#ifdef DEBUG
62    fprintf(stderr,"PYTHONPATH=%s\n",pythonpath);
63  //#endif
64#ifndef WIN32
65  setenv("PYTHONPATH",pythonpath,1);
66#else
67  SetEnvironmentVariable("PYTHONPATH",pythonpath);
68#endif
69  free(python_path);
70  free(pythonpath);
71
72  Py_Initialize();
73  PyObject *pName, *pModule, *pFunc;
74  tmp=getMap(s->content,"serviceProvider");
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  }
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");
101      fflush(stderr);
102#endif
103      pValue = PyObject_CallObject(pFunc, pArgs);
104      if (pValue != NULL) {
105        res=PyInt_AsLong(pValue);
106        freeMaps(real_outputs);
107        free(*real_outputs);
108        //*real_inputs=mapsFromPyDict(arg2);
109        //createMapsFromPyDict(real_outputs,arg3);
110        *real_outputs=mapsFromPyDict(arg3);
111#ifdef DEBUG
112        fprintf(stderr,"Result of call: %i\n", PyInt_AsLong(pValue));
113        dumpMaps(inputs);
114        dumpMaps(outputs);
115#endif
116        Py_DECREF(arg1);
117        Py_DECREF(arg2);
118        Py_DECREF(arg3);
119        Py_DECREF(pArgs);
120        Py_DECREF(pValue);
121        Py_XDECREF(pFunc);
122        Py_DECREF(pModule);
123      }else{     
124        PyObject *ptype,*pvalue, *ptraceback;
125        PyErr_Fetch(&ptype, &pvalue, &ptraceback);
126        PyObject *trace=PyObject_Str(pvalue);
127        char tb[1024];
128        char pbt[10240];
129        if(PyString_Check(trace))
130          sprintf(pbt,"TRACE : %s",PyString_AsString(trace));
131        else
132          fprintf(stderr,"EMPTY TRACE ?");
133        trace=NULL;
134        trace=PyObject_Str(ptype);
135        if(PyString_Check(trace))
136          sprintf(pbt,"%s\nTRACE : %s",strdup(pbt),PyString_AsString(trace));
137        else
138          fprintf(stderr,"EMPTY TRACE ?");
139        PyObject *t;
140        pName = PyString_FromString("traceback");
141        pModule = PyImport_Import(pName);
142        pArgs = PyTuple_New(1);
143        PyTuple_SetItem(pArgs, 0, ptraceback);
144        pFunc = PyObject_GetAttrString(pModule,"format_tb");
145        pValue = PyObject_CallObject(pFunc, pArgs);
146        trace=NULL;
147        trace=PyObject_Str(pValue);
148        if(PyString_Check(trace))
149          sprintf(pbt,"%s\nUnable to run your python process properly. Please check the following messages : %s",strdup(pbt),PyString_AsString(trace));
150        else
151          sprintf(pbt,"%s \n Unable to run your python process properly. Unable to provide any futher informations.",strdup(pbt));
152        map* err=createMap("text",pbt);
153        addToMap(err,"code","NoApplicableCode");
154        printExceptionReportResponse(m,err);
155        Py_XDECREF(pFunc);
156        Py_DECREF(pArgs);
157        Py_DECREF(pModule);
158        exit(-1);
159      }
160    }
161    else{
162      char tmpS[1024];
163      sprintf(tmpS, "Cannot find the %s function int the %s file.\n", s->name, tmp->value);
164      map* tmps=createMap("text",tmpS);
165      printExceptionReportResponse(m,tmps);
166      Py_XDECREF(pFunc);
167      Py_DECREF(pModule);
168      exit(-1);
169    }
170  } else{
171    char tmpS[1024];
172    sprintf(tmpS, "Python module %s cannot be loaded.\n", tmp->value);
173    map* tmps=createMap("text",tmpS);
174    printExceptionReportResponse(m,tmps);
175    if (PyErr_Occurred())
176      PyErr_Print();
177    exit(-1);
178  } 
179#ifndef DEBUG
180  // Failed when DEBUG is defined
181  Py_Finalize();
182#endif
183  return res;
184}
185
186PyDictObject* PyDict_FromMaps(maps* t){
187  PyObject* res=PyDict_New( );
188  maps* tmp=t;
189  while(tmp!=NULL){
190    if(PyDict_SetItem(res,PyString_FromString(tmp->name),(PyObject*)PyDict_FromMap(tmp->content))<0){
191      fprintf(stderr,"Unable to parse params...");
192      exit(1);
193    }
194    tmp=tmp->next;
195  } 
196  return (PyDictObject*) res;
197}
198
199PyDictObject* PyDict_FromMap(map* t){
200  PyObject* res=PyDict_New( );
201  map* tmp=t;
202  while(tmp!=NULL){
203    /*if(strcasecmp(tmp->name,"value")==0){
204      map* ttmp=getMap(t,"size");
205      if(ttmp!=NULL)
206        if(PyDict_SetItem(res,PyString_FromString(tmp->name),PyString_FromStringAndSize(tmp->value,(Py_ssize_t) atoi(ttmp->value)))<0){
207          fprintf(stderr,"Unable to parse params...");
208          exit(1);
209        }
210      else
211        if(PyDict_SetItem(res,PyString_FromString(tmp->name),PyString_FromString(tmp->value))<0){
212          fprintf(stderr,"Unable to parse params...");
213          exit(1);
214        }
215    }
216    else*/
217      if(PyDict_SetItem(res,PyString_FromString(tmp->name),PyString_FromString(tmp->value))<0){
218        fprintf(stderr,"Unable to parse params...");
219        exit(1);
220      }
221    tmp=tmp->next;
222  }
223  return (PyDictObject*) res;
224}
225
226maps* mapsFromPyDict(PyDictObject* t){
227  maps* res=NULL;
228  maps* cursor=res;
229  PyObject* list=PyDict_Keys((PyObject*)t);
230  int nb=PyList_Size(list);
231  int i;
232  for(i=0;i<nb;i++){
233#ifdef DEBUG
234    fprintf(stderr,">> parsing maps %d\n",i);
235#endif
236    PyObject* key=PyList_GetItem(list,i);
237    PyObject* value=PyDict_GetItem((PyObject*)t,key);
238#ifdef DEBUG
239    fprintf(stderr,">> DEBUG VALUES : %s => %s\n",
240            PyString_AsString(key),PyString_AsString(value));
241#endif
242    while(cursor!=NULL){
243      cursor=cursor->next;
244    }
245    cursor=(maps*)malloc(MAPS_SIZE);
246    cursor->name=PyString_AsString(key);
247#ifdef DEBUG
248    dumpMap(mapFromPyDict((PyDictObject*)value));
249#endif
250    cursor->content=mapFromPyDict((PyDictObject*)value);
251    cursor->next=NULL;
252    if(res==NULL)
253      res=cursor;
254    else
255      addMapsToMaps(&res,cursor);
256#ifdef DEBUG
257    dumpMaps(res);
258    fprintf(stderr,">> parsed maps %d\n",i);
259#endif
260  }
261  return res;
262}
263
264map* mapFromPyDict(PyDictObject* t){
265  map* res=NULL;
266  PyObject* list=PyDict_Keys((PyObject*)t);
267  int nb=PyList_Size(list);
268  int i;
269  for(i=0;i<nb;i++){
270    PyObject* key=PyList_GetItem(list,i);
271    PyObject* value=PyDict_GetItem((PyObject*)t,key);
272#ifdef DEBUG
273    fprintf(stderr,">> DEBUG VALUES : %s => %s\n",
274            PyString_AsString(key),PyString_AsString(value));
275#endif
276    if(res!=NULL)
277      addToMap(res,PyString_AsString(key),PyString_AsString(value));
278    else
279      res=createMap(PyString_AsString(key),PyString_AsString(value));
280  }
281  return res;
282}
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