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
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
21 
22 //----------------------------------------------------------------------------------
24 //----------------------------------------------------------------------------------
25 : base_class(name, svc) {
26  // Declare the startup script Property
27  declareProperty( "StartupScript", m_startupScript = "" );
28 }
29 
30 //----------------------------------------------------------------------------------
32 //----------------------------------------------------------------------------------
33 
34 //----------------------------------------------------------------------------------
36 //----------------------------------------------------------------------------------
37 {
38  // initialize the Service Base class
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 == "" ) {
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 //----------------------------------------------------------------------------------
98 //----------------------------------------------------------------------------------
99 {
100  // Finalize this specific service
102  if ( sc.isFailure() ) {
103  return sc;
104  }
105 
106  // Shutdown the Python interpreter
107  Py_Finalize();
108  return StatusCode::SUCCESS;
109 }
110 
111 //----------------------------------------------------------------------------------
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 
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:26
SmartIF< IMessageSvc > & msgSvc() const
The standard message service.
virtual StatusCode finalize()
Finalize the service. [IService::finalize()].
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:85
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:36
bool isValid() const
Allow for check if smart pointer is valid.
Definition: SmartIF.h:51
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:30
std::string m_startupScript
Startup script.
This service handles scripting implemented using Python.
virtual ~PythonScriptingSvc()
Destructor.
list file
Definition: ana.py:160
const TYPE & value() const
explicit conversion
Definition: Property.h:355
virtual const std::string & name() const
Retrieve name of the service.
Definition: Service.cpp:331
virtual bool assign(const Property &source)
get the value from another property
Definition: Property.h:283
virtual StatusCode initialize()
Initialization (from CONFIGURED to INITIALIZED).
Definition: Service.cpp:74
virtual StatusCode run()
Run the service by taking full control. [IRunable::run()].
Templated class to add the standard messaging functionalities.
virtual StatusCode initialize()
Initialize the service. [IService::initialize()].
virtual StatusCode finalize()
Finalize (from INITIALIZED to CONFIGURED).
Definition: Service.cpp:199
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
SmartIF< ISvcLocator > & serviceLocator() const
Retrieve pointer to service locator.
Definition: Service.cpp:336