Exemple #1
0
/**
* Should be called before executing a script
*/
void XBPython::Initialize()
{
  CLog::Log(LOGINFO, "initializing python engine. ");
  EnterCriticalSection(&m_critSection);
  m_iDllScriptCounter++;
  if (!m_bInitialized)
  {
    if (dThreadId == GetCurrentThreadId())
    {
#ifndef _LINUX
      //DllLoader* pDll = g_sectionLoader.LoadDLL(PYTHON_DLL);
      m_hModule = dllLoadLibraryA(PYTHON_DLL);
      m_pDll = DllLoaderContainer::GetModule(m_hModule);
#else
      m_pDll = DllLoaderContainer::LoadModule(PYTHON_DLL, NULL, true);
#endif

      if (!m_pDll || !python_load_dll(*m_pDll))
      {
        CLog::Log(LOGFATAL, "Python: error loading python24.dll");
        Finalize();
        LeaveCriticalSection(&m_critSection);
        return;
      }

      // first we check if all necessary files are installed
#ifndef _LINUX      
      if (!FileExist("Q:\\system\\python\\python24.zlib") ||
        !FileExist("Q:\\system\\python\\DLLs\\_socket.pyd") ||
        !FileExist("Q:\\system\\python\\DLLs\\_ssl.pyd") ||
        !FileExist("Q:\\system\\python\\DLLs\\bz2.pyd") ||
        !FileExist("Q:\\system\\python\\DLLs\\pyexpat.pyd") ||
        !FileExist("Q:\\system\\python\\DLLs\\select.pyd") ||
        !FileExist("Q:\\system\\python\\DLLs\\unicodedata.pyd") ||
        !FileExist("Q:\\system\\python\\DLLs\\zlib.pyd"))
      {
        CLog::Log(LOGERROR, "Python: Missing files, unable to execute script");
        Finalize();
        LeaveCriticalSection(&m_critSection);
        return;
      }
#endif        

#ifdef _LINUX
      // Required for python to find optimized code (pyo) files
      setenv("PYTHONOPTIMIZE", "1", 1);
      setenv("PYTHONHOME", _P("Q:/system/python"), 1);
      setenv("PYTHONPATH", _P("Q:/system/python/python24.zip"), 1);
      //setenv("PYTHONDEBUG", "1", 1);
      //setenv("PYTHONINSPECT", "1", 1);
      //setenv("PYTHONVERBOSE", "1", 1);
      setenv("PYTHONCASEOK", "1", 1);
#endif
      
#ifdef __APPLE__
      setenv("PYTHONHOME", _P("Q:\\system\\python"), 1);
#endif

      Py_Initialize();
      PyEval_InitThreads();

      char* python_argv[1] = { (char*)"" } ;
      PySys_SetArgv(1, python_argv);

      InitXBMCTypes();
      InitGUITypes();
      InitPluginTypes();

      mainThreadState = PyThreadState_Get();

      // release the lock
      PyEval_ReleaseLock();

      m_bInitialized = true;
      PulseEvent(m_hEvent);
    }
    else
    {
      // only the main thread should initialize python.
      m_iDllScriptCounter--;

      LeaveCriticalSection(&m_critSection);
      WaitForSingleObject(m_hEvent, INFINITE);
      EnterCriticalSection(&m_critSection);
    }
  }
  LeaveCriticalSection(&m_critSection);
}
Exemple #2
0
/**
* Should be called before executing a script
*/
void XBPython::Initialize()
{
  CLog::Log(LOGINFO, "initializing python engine. ");
  CSingleLock lock(m_critSection);
  m_iDllScriptCounter++;
  if (!m_bInitialized)
  {
    if (dThreadId == GetCurrentThreadId())
    {
      m_hModule = dllLoadLibraryA(PYTHON_DLL);
      LibraryLoader* pDll = DllLoaderContainer::GetModule(m_hModule);
      if (!pDll || !python_load_dll(*pDll))
      {
        CLog::Log(LOGFATAL, "Python: error loading python27.dll");
        Finalize();
        return;
      }

      // first we check if all necessary files are installed
      if (!FileExist("Q:\\system\\python\\python27.zlib") ||
        !FileExist("Q:\\system\\python\\DLLs\\_socket.pyd") ||
        !FileExist("Q:\\system\\python\\DLLs\\_sqlite3.pyd") ||
        !FileExist("Q:\\system\\python\\DLLs\\_ssl.pyd") ||
        !FileExist("Q:\\system\\python\\DLLs\\bz2.pyd") ||
        !FileExist("Q:\\system\\python\\DLLs\\pyexpat.pyd") ||
        !FileExist("Q:\\system\\python\\DLLs\\select.pyd") ||
        !FileExist("Q:\\system\\python\\DLLs\\unicodedata.pyd"))
      {
        CLog::Log(LOGERROR, "Python: Missing files, unable to execute script");
        Finalize();
        return;
      }

      Py_Initialize();
      PyEval_InitThreads();

      if (g_advancedSettings.m_bPythonVerbose)
      {
        CLog::Log(LOGDEBUG, "%s - Running Python in verbose(--verbose) mode", __FUNCTION__);
        char* python_argv[1] = { (char*)"--verbose" };
        PySys_SetArgv(1, python_argv);
      }
      else
      {
        char* python_argv[1] = { (char*)"" };
        PySys_SetArgv(1, python_argv);
      }

      initxbmc(); // init xbmc modules
      initxbmcplugin(); // init plugin modules
      initxbmcgui(); // init xbmcgui modules
      // redirecting default output to debug console
      if (PyRun_SimpleString(""
        "import xbmc\n"
        "class xbmcout:\n"
        "\tdef write(self, data):\n"
        "\t\txbmc.log(data)\n"
        "\tdef close(self):\n"
        "\t\txbmc.log('.')\n"
        "\tdef flush(self):\n"
        "\t\txbmc.log('.')\n"
        "\n"
        "import sys\n"
        "sys.stdout = xbmcout()\n"
        "sys.stderr = xbmcout()\n"
        "print '-->Python Initialized<--'\n"
        "") == -1)
      {
        CLog::Log(LOGFATAL, "Python Initialize Error");
      }

      if (!(mainThreadState = PyThreadState_Get()))
        CLog::Log(LOGERROR, "Python threadstate is NULL.");
      PyEval_ReleaseLock();

      m_bInitialized = true;
      bThreadInitialize = false;
      PulseEvent(m_hEvent);
    }
    else
    {
      // only the main thread should initialize python.
      m_iDllScriptCounter--;
      bThreadInitialize = true;

      lock.Leave();
      WaitForSingleObject(m_hEvent, INFINITE);
      lock.Enter();
    }
  }
}