Go to the documentation of this file.00001
00002
00003 #include "Python.h"
00004
00005
00006 #include "GaudiKernel/MsgStream.h"
00007 #include "GaudiKernel/ISvcLocator.h"
00008 #include "GaudiKernel/SvcFactory.h"
00009 #include "GaudiKernel/SmartIF.h"
00010
00011 #include "PythonScriptingSvc.h"
00012
00013 #include <fstream>
00014 #include <sstream>
00015
00016
00017 #if defined(linux)
00018 #include "dlfcn.h"
00019 #endif
00020
00021
00022
00023 DECLARE_SERVICE_FACTORY(PythonScriptingSvc)
00024
00025
00026 PythonScriptingSvc::PythonScriptingSvc( const std::string& name, ISvcLocator* svc )
00027
00028 : base_class(name, svc) {
00029
00030 declareProperty( "StartupScript", m_startupScript = "" );
00031 }
00032
00033
00034 PythonScriptingSvc::~PythonScriptingSvc() { }
00035
00036
00037
00038 StatusCode PythonScriptingSvc::initialize()
00039
00040 {
00041
00042 StatusCode sc = Service::initialize();
00043 if ( sc.isFailure() ) return sc;
00044
00045 MsgStream log( msgSvc(), name() );
00046
00047
00048
00049
00050 if( m_startupScript == "" ) {
00051 SmartIF<IProperty> prpMgr(serviceLocator());
00052 if ( prpMgr.isValid() ) {
00053 StringProperty tmp;
00054 tmp.assign(prpMgr->getProperty("JobOptionsType"));
00055 if ( tmp.value( ) == "NONE" ) {
00056 tmp.assign(prpMgr->getProperty("JobOptionsPath"));
00057 m_startupScript = tmp;
00058 }
00059 }
00060 }
00061
00062 char* progName[] = { const_cast<char*>("GaudiPython") };
00063
00064
00065 Py_Initialize();
00066
00067 PySys_SetArgv( 1, progName );
00068
00069 std::string fullversion = Py_GetVersion();
00070 std::string version( fullversion, 0, fullversion.find_first_of(' '));
00071 std::string vers(version, 0, version.find_first_of('.',version.find_first_of('.')+1));
00072 log << MSG::INFO << "Python version: [" << vers << "]" << endmsg;
00073
00074 #if defined(linux)
00075
00076
00077
00078 std::string libname = "libpython" + vers + ".so";
00079 dlopen(libname.c_str(), RTLD_GLOBAL | RTLD_LAZY);
00080 #endif
00081
00082
00083
00084 PyRun_SimpleString( "from gaudimodule import *" );
00085 PyRun_SimpleString( "g = AppMgr()" );
00086
00087 PyRun_SimpleString( "theApp = g" );
00088 PyRun_SimpleString( "def Service(n): return g.service(n)" );
00089 PyRun_SimpleString( "def Algorithm(n): return g.algorithm(n)" );
00090 PyRun_SimpleString( "def Property(n): return g.service(n)" );
00091
00092 #if !defined( _WIN32 )
00093 PyRun_SimpleString( "import rlcompleter");
00094 PyRun_SimpleString( "rlcompleter.readline.parse_and_bind('tab: complete')");
00095 #endif
00096 return StatusCode::SUCCESS;
00097 }
00098
00099
00100 StatusCode PythonScriptingSvc::finalize()
00101
00102 {
00103
00104 StatusCode sc = Service::finalize();
00105 if ( sc.isFailure() ) {
00106 return sc;
00107 }
00108
00109
00110 Py_Finalize();
00111 return StatusCode::SUCCESS;
00112 }
00113
00114
00115 StatusCode PythonScriptingSvc::run()
00116
00117 {
00118 MsgStream log( msgSvc(), name() );
00119 if ( m_startupScript != "" ) {
00120 std::ifstream file(m_startupScript.c_str());
00121 std::stringstream stream;
00122 if( file ) {
00123 char ch;
00124 while( file.get(ch) ) stream.put(ch);
00125 PyRun_SimpleString( const_cast<char*>(stream.str().c_str()) );
00126 file.close();
00127 }
00128 else {
00129 log << MSG::WARNING << "Python startup file " << m_startupScript << " not found" << endmsg;
00130 }
00131 }
00132 PyRun_InteractiveLoop(stdin, "\0");
00133 return StatusCode::SUCCESS;
00134 }
00135