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

Last change on this file since 114 was 114, checked in by djay, 13 years ago

Code cleanup to avoid most of the warning messages at compilation time.

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