source: branches/branch-1.2/zoo-kernel/service_internal_python.c

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

Merge branch-1.2 r268:r296.

File size: 9.2 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  PyThreadState *mainstate;
77  PyEval_InitThreads();
78  Py_Initialize();
79  mainstate = PyThreadState_Swap(NULL);
80  PyEval_ReleaseLock();
81  PyGILState_STATE gstate;
82  gstate = PyGILState_Ensure();
83  PyObject *pName, *pModule, *pFunc;
84  tmp=getMap(s->content,"serviceProvider");
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  }
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)){
98      PyObject *pValue;
99      PyDictObject* arg1=PyDict_FromMaps(m);
100      PyDictObject* arg2=PyDict_FromMaps(inputs);
101      PyDictObject* arg3=PyDict_FromMaps(outputs);
102      PyObject *pArgs=PyTuple_New(3);
103      if (!pArgs)
104        return -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");
111      fflush(stderr);
112#endif
113      pValue = PyObject_CallObject(pFunc, pArgs);
114      if (pValue != NULL) {
115        res=PyInt_AsLong(pValue);
116        freeMaps(real_outputs);
117        free(*real_outputs);
118        freeMaps(main_conf);
119        free(*main_conf);
120        *main_conf=mapsFromPyDict(arg1);
121        *real_outputs=mapsFromPyDict(arg3);
122#ifdef DEBUG
123        fprintf(stderr,"Result of call: %i\n", PyInt_AsLong(pValue));
124        dumpMaps(inputs);
125        dumpMaps(outputs);
126#endif
127      }else{     
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);
138        if(PyString_Check(trace)){
139          char *tpbt=strdup(pbt);
140          sprintf(pbt,"%s\nTRACE : %s",tpbt,PyString_AsString(trace));
141          free(tpbt);
142        }
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))
154          sprintf(pbt,"%s\nUnable to run your python process properly. Please check the following messages : %s",pbt,PyString_AsString(trace));
155        else
156          sprintf(pbt,"%s \n Unable to run your python process properly. Unable to provide any futher informations.",pbt);
157        map* err=createMap("text",pbt);
158        addToMap(err,"code","NoApplicableCode");
159        printExceptionReportResponse(m,err);
160        res=-1;
161      }
162    }
163    else{
164      char tmpS[1024];
165      sprintf(tmpS, "Cannot find the %s function in the %s file.\n", s->name, tmp->value);
166      map* tmps=createMap("text",tmpS);
167      printExceptionReportResponse(m,tmps);
168      res=-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    PyErr_Clear();
178    res=-1;
179    //exit(-1);
180  } 
181  PyGILState_Release(gstate);
182  PyEval_AcquireLock();
183  PyThreadState_Swap(mainstate);
184  Py_Finalize();
185  return res;
186}
187
188PyDictObject* PyDict_FromMaps(maps* t){
189  PyObject* res=PyDict_New( );
190  maps* tmp=t;
191  while(tmp!=NULL){
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;
197    }
198    Py_DECREF(name);
199    tmp=tmp->next;
200  } 
201  return (PyDictObject*) res;
202}
203
204PyDictObject* PyDict_FromMap(map* t){
205  PyObject* res=PyDict_New( );
206  map* tmp=t;
207  map* size=getMap(tmp,"size");
208  while(tmp!=NULL){
209    PyObject* name=PyString_FromString(tmp->name);
210    if(strcasecmp(tmp->name,"value")==0){
211      if(size!=NULL){
212        PyObject* value=PyString_FromStringAndSize(tmp->value,atoi(size->value));
213        if(PyDict_SetItem(res,name,value)<0){
214          fprintf(stderr,"Unable to set key value pair...");
215          return NULL;
216        }
217      }
218      else{
219        PyObject* value=PyString_FromString(tmp->value);
220        if(PyDict_SetItem(res,name,value)<0){
221          fprintf(stderr,"Unable to set key value pair...");
222          return NULL;
223        }
224      }
225    }
226    else{
227      PyObject* value=PyString_FromString(tmp->value);
228      if(PyDict_SetItem(res,name,value)<0){
229        fprintf(stderr,"Unable to set key value pair...");
230        return NULL;
231      }
232    }
233    Py_DECREF(name);
234    tmp=tmp->next;
235  }
236  return (PyDictObject*) res;
237}
238
239maps* 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
255    cursor=(maps*)malloc(MAPS_SIZE);
256    cursor->name=PyString_AsString(key);
257    cursor->content=mapFromPyDict((PyDictObject*)value);
258#ifdef DEBUG
259    dumpMap(cursor->content);
260#endif
261    cursor->next=NULL;
262    if(res==NULL)
263      res=dupMaps(&cursor);
264    else
265      addMapsToMaps(&res,cursor);
266    freeMap(&cursor->content);
267    free(cursor->content);
268    free(cursor);
269#ifdef DEBUG
270    dumpMaps(res);
271    fprintf(stderr,">> parsed maps %d\n",i);
272#endif
273  }
274  return res;
275}
276
277map* 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
289    if(strcmp(PyString_AsString(key),"value")==0){
290      char *buffer=NULL;
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);
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);
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    }
312  }
313  return res;
314}
Note: See TracBrowser for help on using the repository browser.

Search

Context Navigation

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