source/bases/Win32Service.c
//-----------------------------------------------------------------------------
// Service_SetupPython()
//   Setup Python usage for the service.
//-----------------------------------------------------------------------------
Service_SetupPython()
Setup Python usage for the service.
Service_SetupPython () Setup Python usage for the service .
static int Service_SetupPython(udt_ServiceInfo *info)
{
    PyObject *module, *serviceModule, *temp;
    PyThreadState *threadState;
    if (Service_StartLogging() < 0)
        return -1;
#if PY_VERSION_HEX < 0x03070000
    PyEval_InitThreads();
#endif
    threadState = PyThreadState_Swap(NULL);
    if (!threadState) {
        LogMessage(LOG_LEVEL_ERROR, "cannot set up interpreter state");
        Service_SetStatus(info, SERVICE_STOPPED);
        return -1;
    }
    gInterpreterState = threadState->interp;
    PyThreadState_Swap(threadState);
    LogMessage(LOG_LEVEL_DEBUG, "running base Python script");
    if (ExecuteScript() < 0)
        return -1;
    module = PyImport_ImportModule("__main__");
    if (!module)
        return LogPythonException("unable to import __main__");
    info->nameFormat = PyObject_GetAttrString(module, CX_SERVICE_NAME);
    if (!info->nameFormat)
        return LogPythonException("cannot locate service name");
    info->displayNameFormat = PyObject_GetAttrString(module,
            CX_SERVICE_DISPLAY_NAME);
    if (!info->displayNameFormat)
        return LogPythonException("cannot locate service display name");
    info->description = PyObject_GetAttrString(module, CX_SERVICE_DESCRIPTION);
    if (!info->description)
        PyErr_Clear();
    info->startType = SERVICE_DEMAND_START;
    temp = PyObject_GetAttrString(module, CX_SERVICE_AUTO_START);
    if (!temp)
        PyErr_Clear();
    else if (temp == Py_True)
        info->startType = SERVICE_AUTO_START;
    info->sessionChanges = 0;
    temp = PyObject_GetAttrString(module, CX_SERVICE_SESSION_CHANGES);
    if (!temp)
        PyErr_Clear();
    else if (temp == Py_True)
        info->sessionChanges = 1;
    temp = PyObject_GetAttrString(module, CX_SERVICE_MODULE_NAME);
    if (!temp)
        return LogPythonException("cannot locate service module name");
    serviceModule = PyImport_Import(temp);
    Py_DECREF(temp);
    if (!serviceModule)
        return LogPythonException("cannot import service module");
    temp = PyObject_GetAttrString(module, CX_SERVICE_CLASS_NAME);
    if (!temp)
        return LogPythonException("cannot locate service class name");
    info->cls = PyObject_GetAttr(serviceModule, temp);
    Py_DECREF(temp);
    if (!info->cls)
        return LogPythonException("cannot get class from service module");
    return 0;
}
static int Service_SetupPython ( udt_ServiceInfo * info ) { PyObject * module , * serviceModule , * temp ; PyThreadState * threadState ; if ( Service_StartLogging ( ) < 0 ) return -1 ; #if PY_VERSION_HEX < 0x03070000 PyEval_InitThreads ( ) ; #endif threadState = PyThreadState_Swap ( NULL ) ; if ( ! threadState ) { LogMessage ( LOG_LEVEL_ERROR , " " ) ; Service_SetStatus ( info , SERVICE_STOPPED ) ; return -1 ; } gInterpreterState = threadState -> interp ; PyThreadState_Swap ( threadState ) ; LogMessage ( LOG_LEVEL_DEBUG , " " ) ; if ( ExecuteScript ( ) < 0 ) return -1 ; module = PyImport_ImportModule ( " " ) ; if ( ! module ) return LogPythonException ( " " ) ; info -> nameFormat = PyObject_GetAttrString ( module , CX_SERVICE_NAME ) ; if ( ! info -> nameFormat ) return LogPythonException ( " " ) ; info -> displayNameFormat = PyObject_GetAttrString ( module , CX_SERVICE_DISPLAY_NAME ) ; if ( ! info -> displayNameFormat ) return LogPythonException ( " " ) ; info -> description = PyObject_GetAttrString ( module , CX_SERVICE_DESCRIPTION ) ; if ( ! info -> description ) PyErr_Clear ( ) ; info -> startType = SERVICE_DEMAND_START ; temp = PyObject_GetAttrString ( module , CX_SERVICE_AUTO_START ) ; if ( ! temp ) PyErr_Clear ( ) ; else if ( temp == Py_True ) info -> startType = SERVICE_AUTO_START ; info -> sessionChanges = 0 ; temp = PyObject_GetAttrString ( module , CX_SERVICE_SESSION_CHANGES ) ; if ( ! temp ) PyErr_Clear ( ) ; else if ( temp == Py_True ) info -> sessionChanges = 1 ; temp = PyObject_GetAttrString ( module , CX_SERVICE_MODULE_NAME ) ; if ( ! temp ) return LogPythonException ( " " ) ; serviceModule = PyImport_Import ( temp ) ; Py_DECREF ( temp ) ; if ( ! serviceModule ) return LogPythonException ( " " ) ; temp = PyObject_GetAttrString ( module , CX_SERVICE_CLASS_NAME ) ; if ( ! temp ) return LogPythonException ( " " ) ; info -> cls = PyObject_GetAttr ( serviceModule , temp ) ; Py_DECREF ( temp ) ; if ( ! info -> cls ) return LogPythonException ( " " ) ; return 0 ; }
Service_SetupPython()
Setup Python usage for the service.
Service_SetupPython () Setup Python usage for the service .
// initialize logging // ensure threading is initialized and interpreter state saved // running base script // acquire the __main__ module // determine name to use for the service // determine display name to use for the service // determine description to use for the service (optional) // determine if service should be automatically started (optional) // determine if service should monitor session changes (optional) // import the module which implements the service // create an instance of the class which implements the service