| # Gaudi Framework, version v25r0 |

Home |  | Generated: Mon Feb 17 2014

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
PythonScriptingSvc.cpp
Go to the documentation of this file.
1 #include "Python.h"
2
3 // Include Files
4 #include "GaudiKernel/MsgStream.h"
5 #include "GaudiKernel/ISvcLocator.h"
6 #include "GaudiKernel/SmartIF.h"
7
8 #include "PythonScriptingSvc.h"
9
10 #include <fstream>
11 #include <sstream>
12
13 // Special for Unixes
14 #if defined(__linux)
15 #include "dlfcn.h"
16 #endif
17
18 // Instantiation of a static factory class used by clients to create
19 // instances of this service
20 DECLARE_COMPONENT(PythonScriptingSvc)
21
22 //----------------------------------------------------------------------------------
23 PythonScriptingSvc::PythonScriptingSvc( const std::string& name, ISvcLocator* svc )
24 //----------------------------------------------------------------------------------
25 : base_class(name, svc) {
26 // Declare the startup script Property
27 declareProperty( "StartupScript", m_startupScript = "" );
28 }
29
30 //----------------------------------------------------------------------------------
31 PythonScriptingSvc::~PythonScriptingSvc() { }
32 //----------------------------------------------------------------------------------
33
34 //----------------------------------------------------------------------------------
35 StatusCode PythonScriptingSvc::initialize()
36 //----------------------------------------------------------------------------------
37 {
38 // initialize the Service Base class
39 StatusCode sc = Service::initialize();
40 if ( sc.isFailure() ) return sc;
41
42 MsgStream log( msgSvc(), name() );
43
44 // Setup startup script. If none is explicitly specified, then
45 // use the ApplicationMgr JobOptionsPath property as long as
46 // the JobOptionsType property is set to "NONE".
47 if( m_startupScript == "" ) {
48 SmartIF<IProperty> prpMgr(serviceLocator());
49 if ( prpMgr.isValid() ) {
50 StringProperty tmp;
51 tmp.assign(prpMgr->getProperty("JobOptionsType"));
52 if ( tmp.value( ) == "NONE" ) {
53 tmp.assign(prpMgr->getProperty("JobOptionsPath"));
54 m_startupScript = tmp;
55 }
56 }
57 }
58
59 char* progName[] = { const_cast<char*>("GaudiPython") };
60
61 // Initialize the Python interpreter. Required.
62 Py_Initialize();
63 // Set argv for Tkinter (needs program name)
64 PySys_SetArgv( 1, progName );
65 // Get the Python version
66 std::string fullversion = Py_GetVersion();
67 std::string version( fullversion, 0, fullversion.find_first_of(' '));
68 std::string vers(version, 0, version.find_first_of('.',version.find_first_of('.')+1));
69 log << MSG::INFO << "Python version: [" << vers << "]" << endmsg;
70
71 #if defined(__linux)
72 // This is hack to make global the python symbols
73 // which are needed by the other python modules
74 // (eg. readline, math, etc,) libraries.
75 std::string libname = "libpython" + vers + ".so";
76 dlopen(libname.c_str(), RTLD_GLOBAL | RTLD_LAZY);
77 #endif
78
79
80 // Startup commands
81 PyRun_SimpleString( "from gaudimodule import *" );
82 PyRun_SimpleString( "g = AppMgr()" );
83 // backward compatibility with SIPython
84 PyRun_SimpleString( "theApp = g" );
85 PyRun_SimpleString( "def Service(n): return g.service(n)" );
86 PyRun_SimpleString( "def Algorithm(n): return g.algorithm(n)" );
87 PyRun_SimpleString( "def Property(n): return g.service(n)" );
88 // For command-line completion (unix only)
89 #if !defined( _WIN32 )
90 PyRun_SimpleString( "import rlcompleter");
91 PyRun_SimpleString( "rlcompleter.readline.parse_and_bind('tab: complete')");
92 #endif
93 return StatusCode::SUCCESS;
94 }
95
96 //----------------------------------------------------------------------------------
97 StatusCode PythonScriptingSvc::finalize()
98 //----------------------------------------------------------------------------------
99 {
100 // Finalize this specific service
101 StatusCode sc = Service::finalize();
102 if ( sc.isFailure() ) {
103 return sc;
104 }
105
106 // Shutdown the Python interpreter
107 Py_Finalize();
108 return StatusCode::SUCCESS;
109 }
110
111 //----------------------------------------------------------------------------------
112 StatusCode PythonScriptingSvc::run()
113 //----------------------------------------------------------------------------------
114 {
115 MsgStream log( msgSvc(), name() );
116 if ( m_startupScript != "" ) {
117 std::ifstream file(m_startupScript.c_str());
118 std::stringstream stream;
119 if( file ) {
120 char ch;
121 while( file.get(ch) ) stream.put(ch);
122 PyRun_SimpleString( const_cast<char*>(stream.str().c_str()) );
123 file.close();
124 }
125 else {
126 log << MSG::WARNING << "Python startup file " << m_startupScript << " not found" << endmsg;
127 }
128 }
129 PyRun_InteractiveLoop(stdin, "\0");
130 return StatusCode::SUCCESS;
131 }
132