The Gaudi Framework  v30r3 (a5ef0a68)
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 {
27  // Declare the startup script Property
28  declareProperty( "StartupScript", m_startupScript = "" );
29 }
30 
31 //----------------------------------------------------------------------------------
33 //----------------------------------------------------------------------------------
34 
35 //----------------------------------------------------------------------------------
37 //----------------------------------------------------------------------------------
38 {
39  // initialize the Service Base class
41  if ( sc.isFailure() ) return sc;
42 
43  // Setup startup script. If none is explicitly specified, then
44  // use the ApplicationMgr JobOptionsPath property as long as
45  // the JobOptionsType property is set to "NONE".
46  if ( m_startupScript.empty() ) {
47  auto prpMgr = serviceLocator()->as<IProperty>();
48  if ( prpMgr ) {
50  tmp.assign( prpMgr->getProperty( "JobOptionsType" ) );
51  if ( tmp.value() == "NONE" ) {
52  tmp.assign( prpMgr->getProperty( "JobOptionsPath" ) );
53  m_startupScript = tmp;
54  }
55  }
56  }
57 
58  char* progName[] = {const_cast<char*>( "GaudiPython" )};
59 
60  // Initialize the Python interpreter. Required.
61  Py_Initialize();
62  // Set argv for Tkinter (needs program name)
63  PySys_SetArgv( 1, progName );
64  // Get the Python version
65  std::string fullversion = Py_GetVersion();
66  std::string version( fullversion, 0, fullversion.find_first_of( ' ' ) );
67  std::string vers( version, 0, version.find_first_of( '.', version.find_first_of( '.' ) + 1 ) );
68  info() << "Python version: [" << vers << "]" << endmsg;
69 
70 #if defined( __linux )
71  // This is hack to make global the python symbols
72  // which are needed by the other python modules
73  // (eg. readline, math, etc,) libraries.
74  std::string libname = "libpython" + vers + ".so";
75  dlopen( libname.c_str(), RTLD_GLOBAL | RTLD_LAZY );
76 #endif
77 
78  // Startup commands
79  PyRun_SimpleString( "from gaudimodule import *" );
80  PyRun_SimpleString( "g = AppMgr()" );
81  // backward compatibility with SIPython
82  PyRun_SimpleString( "theApp = g" );
83  PyRun_SimpleString( "def Service(n): return g.service(n)" );
84  PyRun_SimpleString( "def Algorithm(n): return g.algorithm(n)" );
85  PyRun_SimpleString( "def Property(n): return g.service(n)" );
86 // For command-line completion (unix only)
87 #if !defined( _WIN32 )
88  PyRun_SimpleString( "import rlcompleter" );
89  PyRun_SimpleString( "rlcompleter.readline.parse_and_bind('tab: complete')" );
90 #endif
91  return StatusCode::SUCCESS;
92 }
93 
94 //----------------------------------------------------------------------------------
96 //----------------------------------------------------------------------------------
97 {
98  // Finalize this specific service
100  if ( sc.isFailure() ) return sc;
101 
102  // Shutdown the Python interpreter
103  Py_Finalize();
104  return StatusCode::SUCCESS;
105 }
106 
107 //----------------------------------------------------------------------------------
109 //----------------------------------------------------------------------------------
110 {
111  if ( !m_startupScript.empty() ) {
113  std::stringstream stream;
114  if ( file ) {
115  std::string buffer;
116  file.seekg( 0, std::ios::end );
117  buffer.reserve( file.tellg() );
118  file.seekg( 0, std::ios::beg );
120  file.close();
121  PyRun_SimpleString( buffer.c_str() );
122  } else {
123  warning() << "Python startup file " << m_startupScript << " not found" << endmsg;
124  }
125  }
126  PyRun_InteractiveLoop( stdin, "\0" );
127  return StatusCode::SUCCESS;
128 }
StatusCode initialize() override
Definition: Service.cpp:63
T empty(T...args)
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
~PythonScriptingSvc() override
Destructor.
StatusCode finalize() override
Definition: Service.cpp:173
Implementation of property with value of concrete type.
Definition: Property.h:381
StatusCode run() override
Run the service by taking full control. [IRunable::run()].
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
STL namespace.
SmartIF< IFace > as()
Definition: ISvcLocator.h:109
StatusCode finalize() override
Finalize the service. [IService::finalize()].
bool isFailure() const
Definition: StatusCode.h:139
STL class.
#define DECLARE_COMPONENT(type)
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:51
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, TYPE &value, const std::string &doc="none")
Declare a property (templated)
std::string m_startupScript
Startup script.
T find_first_of(T...args)
This service handles scripting implemented using Python.
constexpr static const auto SUCCESS
Definition: StatusCode.h:87
T assign(T...args)
T c_str(T...args)
StatusCode initialize() override
Initialize the service. [IService::initialize()].
bool assign(const Details::PropertyBase &source) override
get the value from another property
Definition: Property.h:701
const ValueType & value() const
Backward compatibility (.
Definition: Property.h:540
The IProperty is the basic interface for all components which have properties that can be set or get...
Definition: IProperty.h:20
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition: Service.cpp:291
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:209
STL class.
T reserve(T...args)