Ignore:
Timestamp:
May 7, 2019, 2:17:08 PM (6 years ago)
Author:
djay
Message:

Merge prototype-v0 branch in trunk

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/zoo-project/zoo-kernel/service_internal_python.c

    r790 r917  
    5757 * from the ZOO-Kernel Python environment:
    5858 *  - "_" corresponding to the PythonTranslate function
    59  *  - "updte_status" corresponding to the PythonUpdateStatus function
     59 *  - "update_status" corresponding to the PythonUpdateStatus function
    6060 * @see PythonTranslate, PythonUpdateStatus
    6161 */
     
    146146 */
    147147int zoo_python_support(maps** main_conf,map* request,service* s,maps **real_inputs,maps **real_outputs){
     148
    148149  char *pythonpath;
    149150  char *python_path;
     151
     152#ifdef WIN32
     153  char* os_pathsep = ";";
     154#else
     155  char* os_pathsep = ":";
     156#endif
     157
    150158  maps* m=*main_conf;
    151159  maps* inputs=*real_inputs;
    152160  maps* outputs=*real_outputs;
    153161  map* tmp0=getMapFromMaps(*main_conf,"lenv","cwd");
    154   char *ntmp=tmp0->value;
     162  char *ntmp=tmp0->value; 
    155163  map* tmp=NULL;
    156164  int hasToClean=0;
    157   tmp=getMapFromMaps(*main_conf,"env","PYTHONPATH");
     165  tmp=getMapFromMaps(*main_conf,"env","PYTHONPATH");
     166
     167  map* kvp = NULL;
     168  char* libPath = NULL;
     169  if (hasvalue(*main_conf, "main", "libPath", &kvp)) {
     170          libPath = kvp->value;
     171  }
     172  else {
     173          libPath = "";
     174  }
     175
    158176#ifdef DEBUG
    159177  fprintf(stderr,"PYTHON SUPPORT \n");
     
    163181    fprintf(stderr,"PYTHON SUPPORT (%i)\n",strlen(tmp->value));
    164182#endif
    165     python_path=(char*)malloc((strlen(tmp->value))*sizeof(char));
     183    python_path=(char*)malloc((strlen(tmp->value)+1)*sizeof(char));
    166184    sprintf(python_path,"%s",tmp->value);
    167185    hasToClean=1;
     
    172190      python_path=cwdMap->value;
    173191    else
    174       python_path=(char*)".";
     192      python_path=(char*)".";   
    175193  }
    176194  tmp=NULL;
    177195  tmp=getMap(request,"metapath");
    178196  if(tmp!=NULL && strcmp(tmp->value,"")!=0){
    179     pythonpath=(char*)malloc((4+strlen(python_path)+strlen(ntmp)+strlen(tmp->value))*sizeof(char));
     197    //pythonpath=(char*)malloc((4+strlen(python_path)+strlen(ntmp)+strlen(tmp->value))*sizeof(char));
     198          pythonpath = (char*)malloc((5 + strlen(python_path) + strlen(ntmp) + strlen(tmp->value) + strlen(libPath)) * sizeof(char));
    180199#ifdef WIN32
    181   sprintf(pythonpath,"%s/%s/;%s",ntmp,tmp->value,python_path);
     200  //sprintf(pythonpath,"%s/%s/;%s",ntmp,tmp->value,python_path);
     201        sprintf(pythonpath, "%s/%s/;%s;%s", ntmp, tmp->value, python_path, libPath);
    182202#else
    183   sprintf(pythonpath,"%s/%s/:%s",ntmp,tmp->value,python_path);
     203  //sprintf(pythonpath,"%s/%s/:%s",ntmp,tmp->value,python_path);
     204  sprintf(pythonpath, "%s/%s/:%s:%s", ntmp, tmp->value, python_path, libPath); 
    184205#endif
    185206  }
    186207  else{
    187     pythonpath=(char*)malloc((2+strlen(python_path)+strlen(ntmp))*sizeof(char));
     208    //pythonpath=(char*)malloc((2+strlen(python_path)+strlen(ntmp))*sizeof(char));
     209          pythonpath = (char*)malloc((3 + strlen(python_path) + strlen(ntmp) + strlen(libPath)) * sizeof(char));
    188210#ifdef WIN32
    189     sprintf(pythonpath,"%s;%s",ntmp,python_path);
     211    //sprintf(pythonpath,"%s;%s",ntmp,python_path);
     212        sprintf(pythonpath, "%s;%s;%s", ntmp, python_path, libPath);
    190213#else
    191     sprintf(pythonpath,"%s:%s",ntmp,python_path);
    192 #endif
     214    //sprintf(pythonpath,"%s:%s",ntmp,python_path);
     215        sprintf(pythonpath, "%s:%s:%s", ntmp, python_path, libPath);
     216#endif 
    193217  }
    194218#ifdef DEBUG
    195219    fprintf(stderr,"PYTHONPATH=%s\n",pythonpath);
    196220#endif
    197 #ifndef WIN32
    198   setenv("PYTHONPATH",pythonpath,1);
    199 #else
     221        map* home = NULL;
     222// knut: also set PYTHONHOME environment variable so that Python can load standard modules
     223#ifndef WIN32   
     224  setenv("PYTHONPATH",pythonpath,1); 
     225  //= getMapFromMaps(*main_conf, "env", "PYTHONHOME");
     226  if (hasvalue(*main_conf, "env", "PYTHONHOME", &home)) {
     227          setenv("PYTHONHOME", home->value, 1); // overwrite
     228  }
     229#else   
    200230  SetEnvironmentVariable("PYTHONPATH",pythonpath);
    201231  char* toto=(char*)malloc((strlen(pythonpath)+12)*sizeof(char));
    202232  sprintf(toto,"PYTHONPATH=%s",pythonpath);
    203   putenv(toto);
     233  _putenv(toto);
    204234  free(toto);
    205 #endif
     235  if (hasvalue(*main_conf, "env", "PYTHONHOME", &home)) {
     236          SetEnvironmentVariable("PYTHONHOME", home->value);
     237  }
     238  char buffer[128];       
     239#endif 
    206240  if(hasToClean>0)
    207241    free(python_path);
     
    209243
    210244  PyThreadState *mainstate;
    211 #if PY_MAJOR_VERSION >= 3
    212   PyImport_AppendInittab("zoo", init_zoo);
     245#if PY_MAJOR_VERSION >= 3 
     246  PyImport_AppendInittab("zoo", init_zoo); 
    213247#else
    214248  PyEval_InitThreads();
    215 #endif
    216   Py_Initialize();
    217 #if PY_MAJOR_VERSION >= 3
     249#endif 
     250  Py_Initialize(); 
     251#if PY_MAJOR_VERSION >= 3 
    218252  PyEval_InitThreads();
    219   PyImport_ImportModule("zoo");
     253  PyImport_ImportModule("zoo"); 
    220254#else
    221255  init_zoo();
    222 #endif
     256#endif 
    223257  mainstate = PyThreadState_Swap(NULL);
    224258  PyEval_ReleaseLock();
     
    240274      char *mn=(char*)malloc((strlen(mps)+strlen(tmp->value)+2)*sizeof(char));
    241275      sprintf(mn,"%s.%s",mps,tmp->value);
    242       pName = PyString_FromString(mn);
     276      pName = PyString_FromString(mn);   
    243277      free(mn);
    244278      free(mps);
     
    251285    errorException (m, "Unable to parse serviceProvider please check your zcfg file.", "NoApplicableCode", NULL);
    252286    exit(-1);
    253   }
    254   pModule = PyImport_Import(pName);
     287  } 
     288  pModule = PyImport_Import(pName);   
    255289  int res=SERVICE_FAILED;
    256290  if (pModule != NULL) {
     
    267301      PyTuple_SetItem(pArgs, 1, (PyObject *)arg2);
    268302      PyTuple_SetItem(pArgs, 2, (PyObject *)arg3);
    269       pValue = PyObject_CallObject(pFunc, pArgs);
     303      pValue = PyObject_CallObject(pFunc, pArgs);         
    270304      if (pValue != NULL) {
    271305        res=PyInt_AsLong(pValue);
     
    275309        free(*main_conf);
    276310        *main_conf=mapsFromPyDict(arg1);
    277         *real_outputs=mapsFromPyDict(arg3);
     311        *real_outputs=mapsFromPyDict(arg3);     
    278312#ifdef DEBUG
    279313        fprintf(stderr,"Result of call: %i\n", PyInt_AsLong(pValue));
     
    281315        dumpMaps(*real_outputs);
    282316#endif
    283       }else{     
     317      }else{
    284318        PythonZooReport(m,tmp->value,0);
    285319        res=-1;
     
    301335#endif
    302336  PyThreadState_Swap(mainstate);
    303   Py_Finalize();
     337  Py_Finalize(); 
    304338  return res;
    305339}
     
    323357  char *pbt=NULL;
    324358  if(PyString_Check(trace)){
    325     pbt=(char*)malloc((7+strlen(PyString_AsString(trace))+1)*sizeof(char));
     359    pbt=(char*)malloc((8+strlen(PyString_AsString(trace)))*sizeof(char));
    326360    sprintf(pbt,"TRACE: %s",PyString_AsString(trace));
    327361  }
    328362  else
    329363    fprintf(stderr,"EMPTY TRACE ?");
    330  
     364
    331365  trace=NULL;
    332366 
     
    425459  map* isArray=getMap(tmp,"isArray");
    426460  map* size=getMap(tmp,"size");
     461  map* useFile=getMap(tmp,"use_file");
     462  map* cacheFile=getMap(tmp,"cache_file");
    427463  map* tmap=getMapType(tmp);
    428464  while(tmp!=NULL){
     
    435471        PyObject* mvalue=PyList_New(cnt);
    436472        PyObject* svalue=PyList_New(cnt);
     473        PyObject* cvalue=PyList_New(cnt);
    437474
    438475        for(int i=0;i<cnt;i++){
    439476         
    440           map* vMap=getMapArray(tmp,"value",i);     
    441           map* sMap=getMapArray(tmp,"size",i);
     477          map* vMap=getMapArray(t,"value",i);
     478          map* uMap=getMapArray(t,"use_file",i);
     479          map* sMap=getMapArray(t,"size",i);
     480          map* cMap=getMapArray(t,"cache_file",i);
    442481
    443482          if(vMap!=NULL){
     
    445484            PyObject* lvalue;
    446485            PyObject* lsvalue;
    447             if(sMap==NULL){
     486            PyObject* lcvalue;
     487            if(sMap==NULL || uMap!=NULL){
    448488              lvalue=PyString_FromString(vMap->value);
    449               lsvalue=Py_None;
    450489            }
    451490            else{   
    452491              lvalue=PyString_FromStringAndSize(vMap->value,atoi(sMap->value));
     492            }
     493            if(sMap!=NULL){
    453494              lsvalue=PyString_FromString(sMap->value);
    454495              hasSize=1;
    455496            }
     497            else
     498              lsvalue=Py_None;
     499            if(uMap!=NULL){
     500              lcvalue=PyString_FromString(cMap->value);;
     501            }else
     502              lcvalue=Py_None;
    456503
    457504            if(PyList_SetItem(value,i,lvalue)<0){
     
    463510              return NULL;
    464511            }
     512            if(PyList_SetItem(cvalue,i,lcvalue)<0){
     513              fprintf(stderr,"Unable to set key value pair...");
     514              return NULL;
     515            }
    465516          }
    466517         
     518          PyObject* lmvalue;
    467519          map* mMap=getMapArray(tmp,tmap->name,i);
    468           PyObject* lmvalue;
    469520          if(mMap!=NULL){
    470521            lmvalue=PyString_FromString(mMap->value);
     
    487538          return NULL;
    488539        }
     540        if(PyDict_SetItem(res,PyString_FromString("cache_file"),cvalue)<0){
     541          fprintf(stderr,"Unable to set key value pair...");
     542          return NULL;
     543        }
    489544        if(hasSize>0)
    490545          if(PyDict_SetItem(res,PyString_FromString("size"),svalue)<0){
     
    493548          }
    494549      }
    495       else if(size!=NULL){
     550      else if(size!=NULL && useFile==NULL){
    496551        PyObject* value=PyString_FromStringAndSize(tmp->value,atoi(size->value));
    497552        if(PyDict_SetItem(res,name,value)<0){
Note: See TracChangeset for help on using the changeset viewer.

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