The Gaudi Framework  v32r2 (46d42edc)
PythonScriptingSvc.cpp
Go to the documentation of this file.
1 #include "Python.h"
2 
3 // Python 3 compatibility
4 #if PY_MAJOR_VERSION >= 3
5 
6 # define PySys_SetArgv_Char_t wchar_t
7 
8 #else
9 
10 # define PySys_SetArgv_Char_t char
11 
12 #endif
13 
14 // Include Files
16 #include "GaudiKernel/MsgStream.h"
17 #include "GaudiKernel/SmartIF.h"
18 
19 #include "PythonScriptingSvc.h"
20 
21 #include <fstream>
22 #include <sstream>
23 
24 // Special for Unixes
25 #if defined( __linux )
26 # include "dlfcn.h"
27 #endif
28 
29 // Instantiation of a static factory class used by clients to create
30 // instances of this service
32 
33 //----------------------------------------------------------------------------------
35  //----------------------------------------------------------------------------------
36  : base_class( name, svc ) {
37  // Declare the startup script Property
38  declareProperty( "StartupScript", m_startupScript = "" );
39 }
40 
41 //----------------------------------------------------------------------------------
43 //----------------------------------------------------------------------------------
44 
45 //----------------------------------------------------------------------------------
47 //----------------------------------------------------------------------------------
48 {
49  // initialize the Service Base class
51  if ( sc.isFailure() ) return sc;
52 
53  // Setup startup script. If none is explicitly specified, then
54  // use the ApplicationMgr JobOptionsPath property as long as
55  // the JobOptionsType property is set to "NONE".
56  if ( m_startupScript.empty() ) {
57  auto prpMgr = serviceLocator()->as<IProperty>();
58  if ( prpMgr ) {
60  tmp.assign( prpMgr->getProperty( "JobOptionsType" ) );
61  if ( tmp.value() == "NONE" ) {
62  tmp.assign( prpMgr->getProperty( "JobOptionsPath" ) );
63  m_startupScript = tmp;
64  }
65  }
66  }
67 
68  // Python 3 compatibility
69 #if PY_MAJOR_VERSION >= 3
70  wchar_t* progName[] = {const_cast<wchar_t*>( L"GaudiPython" )};
71 #else
72  char* progName[] = {const_cast<char*>( "GaudiPython" )};
73 #endif
74 
75  // Initialize the Python interpreter. Required.
76  Py_Initialize();
77  // Set argv for Tkinter (needs program name)
78  PySys_SetArgv( 1, progName );
79  // Get the Python version
80  std::string fullversion = Py_GetVersion();
81  std::string version( fullversion, 0, fullversion.find_first_of( ' ' ) );
82  std::string vers( version, 0, version.find_first_of( '.', version.find_first_of( '.' ) + 1 ) );
83  info() << "Python version: [" << vers << "]" << endmsg;
84 
85 #if defined( __linux )
86  // This is hack to make global the python symbols
87  // which are needed by the other python modules
88  // (eg. readline, math, etc,) libraries.
89  std::string libname = "libpython" + vers + ".so";
90  dlopen( libname.c_str(), RTLD_GLOBAL | RTLD_LAZY );
91 #endif
92 
93  // Startup commands
94  PyRun_SimpleString( "from gaudimodule import *" );
95  PyRun_SimpleString( "g = AppMgr()" );
96  // backward compatibility with SIPython
97  PyRun_SimpleString( "theApp = g" );
98  PyRun_SimpleString( "def Service(n): return g.service(n)" );
99  PyRun_SimpleString( "def Algorithm(n): return g.algorithm(n)" );
100  PyRun_SimpleString( "def Property(n): return g.service(n)" );
101 // For command-line completion (unix only)
102 #if !defined( _WIN32 )
103  PyRun_SimpleString( "import rlcompleter" );
104  PyRun_SimpleString( "rlcompleter.readline.parse_and_bind('tab: complete')" );
105 #endif
106  return StatusCode::SUCCESS;
107 }
108 
109 //----------------------------------------------------------------------------------
111 //----------------------------------------------------------------------------------
112 {
113  // Finalize this specific service
115  if ( sc.isFailure() ) return sc;
116 
117  // Shutdown the Python interpreter
118  Py_Finalize();
119  return StatusCode::SUCCESS;
120 }
121 
122 //----------------------------------------------------------------------------------
124 //----------------------------------------------------------------------------------
125 {
126  if ( !m_startupScript.empty() ) {
128  std::stringstream stream;
129  if ( file ) {
130  std::string buffer;
131  file.seekg( 0, std::ios::end );
132  buffer.reserve( file.tellg() );
133  file.seekg( 0, std::ios::beg );
135  file.close();
136  PyRun_SimpleString( buffer.c_str() );
137  } else {
138  warning() << "Python startup file " << m_startupScript << " not found" << endmsg;
139  }
140  }
141  PyRun_InteractiveLoop( stdin, "\0" );
142  return StatusCode::SUCCESS;
143 }
StatusCode initialize() override
Definition: Service.cpp:60
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition: Service.cpp:277
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:164
Implementation of property with value of concrete type.
Definition: Property.h:352
StatusCode run() override
Run the service by taking full control. [IRunable::run()].
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
STL namespace.
SmartIF< IFace > as()
Definition: ISvcLocator.h:103
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
StatusCode finalize() override
Finalize the service. [IService::finalize()].
STL class.
#define DECLARE_COMPONENT(type)
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
const ValueType & value() const
Backward compatibility (.
Definition: Property.h:526
std::string m_startupScript
Startup script.
T find_first_of(T... args)
This service handles scripting implemented using Python.
def end
Definition: IOTest.py:113
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:659
bool isFailure() const
Definition: StatusCode.h:130
The IProperty is the basic interface for all components which have properties that can be set or get.
Definition: IProperty.h:20
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192
STL class.
T reserve(T... args)