Advertisement
Guest User

ZOO patch to update status from python

a guest
Sep 5th, 2012
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.61 KB | None | 0 0
  1. Index: zoo-project/zoo-kernel/service_internal_python.c
  2. ===================================================================
  3. --- zoo-project/zoo-kernel/service_internal_python.c    (revision 362)
  4. +++ zoo-project/zoo-kernel/service_internal_python.c    (working copy)
  5. @@ -24,6 +24,26 @@
  6.  
  7.  #include "service_internal_python.h"
  8.  
  9. +PyObject* PythonUpdateStatus(PyObject*, PyObject*);
  10. +
  11. +PyMethodDef zooMethods[] = {
  12. +   {"update_status", PythonUpdateStatus, METH_VARARGS, "Update status percentage of a running process."},
  13. +   {NULL, NULL, 0, NULL} /* tempt not the blade, all fear the sentinel */
  14. +};
  15. +
  16. +static PyObject* ZooError;
  17. +
  18. +PyMODINIT_FUNC init_zoo()
  19. +{
  20. +   PyObject* module = Py_InitModule("zoo", zooMethods);
  21. +   if (module == NULL)
  22. +      return;
  23. +
  24. +   ZooError = PyErr_NewException("zoo.error", NULL, NULL);
  25. +   Py_INCREF(ZooError);
  26. +   PyModule_AddObject(module, "error", ZooError);
  27. +}
  28. +
  29.  int zoo_python_support(maps** main_conf,map* request,service* s,maps **real_inputs,maps **real_outputs){
  30.    maps* m=*main_conf;
  31.    maps* inputs=*real_inputs;
  32. @@ -76,6 +96,7 @@
  33.    PyThreadState *mainstate;
  34.    PyEval_InitThreads();
  35.    Py_Initialize();
  36. +  init_zoo();
  37.    mainstate = PyThreadState_Swap(NULL);
  38.    PyEval_ReleaseLock();
  39.    PyGILState_STATE gstate;
  40. @@ -384,3 +405,51 @@
  41.    }
  42.    return res;
  43.  }
  44. +
  45. +PyObject*
  46. +PythonUpdateStatus(PyObject* self, PyObject* args)
  47. +{
  48. +  maps* conf;
  49. +  PyObject* confdict;
  50. +  int istatus;
  51. +  char* status;
  52. +  if (!PyArg_ParseTuple(args, "O!i", &PyDict_Type, &confdict, &istatus)){
  53. +#ifdef DEBUG
  54. +    fprintf(stderr,"Incorrect arguments to update status function");
  55. +#endif
  56. +    return NULL;
  57. +  }
  58. +  if (istatus < 0 || istatus > 100){
  59. +     PyErr_SetString(ZooError, "Status must be a percentage.");
  60. +     return NULL;
  61. +  }else{
  62. +     char tmpStatus[4];
  63. +     snprintf(tmpStatus, 4, "%i", istatus);
  64. +     status = strdup(tmpStatus);
  65. +  }
  66. +  /* now update the map */
  67. +  {
  68. +    PyObject* lenv = PyMapping_GetItemString(confdict, "lenv");
  69. +    if (lenv && PyMapping_Check(lenv)){
  70. +      PyObject* valobj = PyString_FromString(status);
  71. +      PyMapping_SetItemString(lenv, "status", valobj);
  72. +      Py_DECREF(valobj);
  73. +    }
  74. +    Py_DECREF(lenv);
  75. +  }
  76. +  conf = mapsFromPyDict((PyDictObject*)confdict);
  77. +  if (getMapFromMaps(conf,"lenv","status") != NULL){
  78. +    fprintf(stderr,"STATUS RETURNED : %s\n",status);
  79. +    if(status!=NULL){
  80. +      setMapInMaps(conf,"lenv","status",status);
  81. +      free(status);
  82. +    }
  83. +    else
  84. +      setMapInMaps(conf,"lenv","status","15");
  85. +    updateStatus(conf);
  86. +  }
  87. +  freeMaps(&conf);
  88. +  free(conf);
  89. +  Py_RETURN_NONE;
  90. +}
  91. +
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement