Changeset 9 for trunk/zoo-kernel/service_internal_python.c
- Timestamp:
- Sep 14, 2010, 2:04:55 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/zoo-kernel/service_internal_python.c
r1 r9 25 25 #include "service_internal_python.h" 26 26 27 int python_support(maps* m,service* s,int argc,char** argv,map *inputs,map *outputs){28 Py_Initialize();29 PyObject *pName, *pModule, *pFunc;30 pName = PyString_FromString(argv[2]);31 pModule = PyImport_Import(pName);32 int i;33 if (pModule != NULL) {34 pFunc=PyObject_GetAttrString(pModule,argv[4]);35 if (pFunc && PyCallable_Check(pFunc)){36 PyDictObject* arg1=PyDict_FromMap(inputs);37 PyDictObject* arg2=PyDict_FromMap(outputs);38 PyObject *pArgs=PyTuple_New(2);39 PyObject *pValue;40 PyTuple_SetItem(pArgs, 0, (PyObject *)arg1);41 PyTuple_SetItem(pArgs, 1, (PyObject *)arg2);42 pValue = PyString_FromString(argv[argc-1]);43 #ifdef DEBUG44 fprintf(stderr,"Function successfully loaded\n");45 #endif46 /**47 * Need to check if we need to fork to load a status enabled48 */49 if(strcmp(PyString_AsString(pValue),"bg")!=0){50 #ifdef DEBUG51 fprintf(stderr,"RUN IN NORMAL MODE \n");52 #endif53 pValue = PyObject_CallObject(pFunc, pArgs);54 if (pValue != NULL) {55 inputs=mapFromPyDict(arg1);56 outputs=mapFromPyDict(arg2);57 #ifdef DEBUG58 fprintf(stderr,"Result of call: %i\n", PyInt_AsLong(pValue));59 dumpMap(inputs);60 dumpMap(outputs);61 #ifndef WIN3262 fprintf(stderr,"printProcessResponse(%i,\"%s\",%i,inputs,outputs);",getpid(),argv[1],PyInt_AsLong(pValue));63 #endif64 #endif65 printProcessResponse(m,getpid(),s,argv[2],PyInt_AsLong(pValue),inputs,outputs);66 }67 }68 else{69 pid_t pid;70 #ifdef WIN3271 pid = 0;72 #else73 pid = fork ();74 #endif75 if (pid > 0) {76 /* dady */77 printProcessResponse(m,pid,s,argv[2],SERVICE_STARTED,inputs,outputs);78 }else if (pid == 0) {79 /* son */80 #ifdef DEBUG81 fprintf(stderr,"RUN IN BACKGROUND MODE \n");82 #endif83 char tmp1[256];84 sprintf(tmp1,"service/temp/%s_%d.xml",argv[2],getpid());85 #ifndef WIN3286 stdout =87 #endif88 freopen(tmp1 , "w+" , stdout);89 printProcessResponse(m,getpid(),s,argv[2],SERVICE_STARTED,inputs,outputs);90 fflush(stdout);91 pValue = PyObject_CallObject(pFunc, pArgs);92 if (pValue != NULL) {93 rewind(stdout);94 inputs=mapFromPyDict(arg1);95 outputs=mapFromPyDict(arg2);96 #ifdef DEBUG97 fprintf(stderr,"Result of call: %ld\n", PyInt_AsLong(pValue));98 dumpMap(inputs);99 dumpMap(outputs);100 fprintf(stderr,"printProcessResponse(%i,%s,%i,inputs,outputs);",s,getpid(),argv[2],PyInt_AsLong(pValue));101 #endif102 // sleep(6);103 printProcessResponse(m,getpid(),s,argv[2],PyInt_AsLong(pValue),inputs,outputs);104 }105 exit(1);106 } else {107 /* error */108 }109 }110 Py_DECREF(pArgs);111 Py_DECREF(pValue);112 #ifdef DEBUG113 fprintf(stderr,"Function ran successfully\n");114 #endif115 }116 else{117 char tmpS[1024];118 sprintf(tmpS, "Cannot find function %s \n", argv[4]);119 map* tmps=createMap("text",tmpS);120 printExceptionReportResponse(m,tmps);121 //fprintf(stderr, "Cannot find function %s \n", argv[2]);122 exit(-1);123 }124 }else{125 char tmpS[1024];126 sprintf(tmpS, "Cannot find function %s \n", argv[4]);127 map* tmps=createMap("text",tmpS);128 printExceptionReportResponse(m,tmps);129 //fprintf(stderr, "Cannot find function %s \n", argv[4]);130 if (PyErr_Occurred())131 PyErr_Print();132 133 }134 Py_Finalize();135 return 2;136 }137 138 27 int zoo_python_support(maps** main_conf,map* request,service* s,maps **real_inputs,maps **real_outputs){ 139 28 maps* m=*main_conf; … … 144 33 map* tmp=NULL; 145 34 tmp=getMapFromMaps(*main_conf,"env","PYTHONPATH"); 146 char *python_path=NULL; 35 char *python_path; 36 fprintf(stderr,"PYTHON SUPPORT \n"); 37 fflush(stderr); 147 38 if(tmp!=NULL){ 148 python_path=(char*)malloc((1+strlen(tmp->value))*sizeof(char)); 149 sprintf(python_path,"%s",tmp->value); 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); 150 42 } 151 43 else{ 152 python_path=(char*)malloc(2*sizeof(char)); 153 python_path="."; 44 python_path=strdup("."); 154 45 } 155 46 tmp=NULL; … … 158 49 if(tmp!=NULL && strcmp(tmp->value,"")!=0) 159 50 #ifdef WIN32 160 51 sprintf(pythonpath,"%s/%s/;%s",ntmp,tmp->value,python_path); 161 52 #else 162 53 sprintf(pythonpath,"%s/%s/:%s",ntmp,tmp->value,python_path); 163 54 #endif 164 55 else … … 166 57 sprintf(pythonpath,"%s;%s",ntmp,python_path); 167 58 #else 168 169 #endif 170 #ifdef DEBUG171 fprintf(stderr,"PYTHONPATH=%s\n",pythonpath);172 #endif59 sprintf(pythonpath,"%s:%s",ntmp,python_path); 60 #endif 61 //#ifdef DEBUG 62 fprintf(stderr,"PYTHONPATH=%s\n",pythonpath); 63 //#endif 173 64 #ifndef WIN32 174 65 setenv("PYTHONPATH",pythonpath,1); … … 178 69 free(python_path); 179 70 free(pythonpath); 180 // 71 181 72 Py_Initialize(); 182 73 PyObject *pName, *pModule, *pFunc; 183 74 tmp=getMap(s->content,"serviceProvider"); 184 //if(tmp!=NULL) 185 pName = PyString_FromString(tmp->value); 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 } 186 83 pModule = PyImport_Import(pName); 187 84 int i; … … 202 99 #ifdef DEBUG 203 100 fprintf(stderr,"RUN IN NORMAL MODE \n"); 204 //fflush(stderr);101 fflush(stderr); 205 102 #endif 206 103 pValue = PyObject_CallObject(pFunc, pArgs); 207 104 if (pValue != NULL) { 208 105 res=PyInt_AsLong(pValue); 209 /*inputs=mapsFromPyDict(arg2);210 dumpMaps(inputs);*/211 outputs=mapsFromPyDict(arg3);212 *real_outputs=outputs;213 106 freeMaps(real_outputs); 107 free(*real_outputs); 108 //*real_inputs=mapsFromPyDict(arg2); 109 //createMapsFromPyDict(real_outputs,arg3); 110 *real_outputs=mapsFromPyDict(arg3); 214 111 #ifdef DEBUG 215 112 fprintf(stderr,"Result of call: %i\n", PyInt_AsLong(pValue)); 216 113 dumpMaps(inputs); 217 114 dumpMaps(outputs); 218 fprintf(stderr,"printProcessResponse(%i,\"%s\",%i,inputs,outputs);", 219 getpid(),tmp->value,PyInt_AsLong(pValue)); 220 dumpMaps(outputs); 221 #endif 115 #endif 116 Py_DECREF(arg1); 117 Py_DECREF(arg2); 118 Py_DECREF(arg3); 119 Py_DECREF(pArgs); 222 120 Py_DECREF(pValue); 223 121 Py_XDECREF(pFunc); 224 Py_DECREF(pArgs);225 122 Py_DECREF(pModule); 226 }else{ 227 123 }else{ 228 124 PyObject *ptype,*pvalue, *ptraceback; 229 125 PyErr_Fetch(&ptype, &pvalue, &ptraceback); … … 268 164 map* tmps=createMap("text",tmpS); 269 165 printExceptionReportResponse(m,tmps); 270 //fprintf(stderr, "Cannot find function %s \n", argv[2]);271 166 Py_XDECREF(pFunc); 272 167 Py_DECREF(pModule); … … 275 170 } else{ 276 171 char tmpS[1024]; 277 sprintf(tmpS, " Cannot find function %s\n", tmp->value);172 sprintf(tmpS, "Python module %s cannot be loaded.\n", tmp->value); 278 173 map* tmps=createMap("text",tmpS); 279 174 printExceptionReportResponse(m,tmps); 280 //fprintf(stderr, "Cannot find function %s \n", argv[4]);281 175 if (PyErr_Occurred()) 282 176 PyErr_Print(); 283 177 exit(-1); 284 178 } 179 #ifndef DEBUG 180 // Failed when DEBUG is defined 285 181 Py_Finalize(); 182 #endif 286 183 return res; 287 184 } … … 329 226 PyString_AsString(key),PyString_AsString(value)); 330 227 #endif 331 if(cursor!=NULL){228 while(cursor!=NULL){ 332 229 cursor=cursor->next; 333 230 } 334 cursor=(maps*)malloc( sizeof(maps*));231 cursor=(maps*)malloc(MAPS_SIZE); 335 232 cursor->name=PyString_AsString(key); 336 233 #ifdef DEBUG
Note: See TracChangeset
for help on using the changeset viewer.