Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules 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  // Setup startup script. If none is explicitly specified, then
43  // use the ApplicationMgr JobOptionsPath property as long as
44  // the JobOptionsType property is set to "NONE".
45  if ( m_startupScript.empty() ) {
46  auto prpMgr = serviceLocator()->as<IProperty>();
47  if ( prpMgr ) {
49  tmp.assign( prpMgr->getProperty( "JobOptionsType" ) );
50  if ( tmp.value() == "NONE" ) {
51  tmp.assign( prpMgr->getProperty( "JobOptionsPath" ) );
52  m_startupScript = tmp;
53  }
54  }
55  }
56 
57  char* progName[] = {const_cast<char*>( "GaudiPython" )};
58 
59  // Initialize the Python interpreter. Required.
60  Py_Initialize();
61  // Set argv for Tkinter (needs program name)
62  PySys_SetArgv( 1, progName );
63  // Get the Python version
64  std::string fullversion = Py_GetVersion();
65  std::string version( fullversion, 0, fullversion.find_first_of( ' ' ) );
66  std::string vers( version, 0, version.find_first_of( '.', version.find_first_of( '.' ) + 1 ) );
67  info() << "Python version: [" << vers << "]" << endmsg;
68 
69 #if defined( __linux )
70  // This is hack to make global the python symbols
71  // which are needed by the other python modules
72  // (eg. readline, math, etc,) libraries.
73  std::string libname = "libpython" + vers + ".so";
74  dlopen( libname.c_str(), RTLD_GLOBAL | RTLD_LAZY );
75 #endif
76 
77  // Startup commands
78  PyRun_SimpleString( "from gaudimodule import *" );
79  PyRun_SimpleString( "g = AppMgr()" );
80  // backward compatibility with SIPython
81  PyRun_SimpleString( "theApp = g" );
82  PyRun_SimpleString( "def Service(n): return g.service(n)" );
83  PyRun_SimpleString( "def Algorithm(n): return g.algorithm(n)" );
84  PyRun_SimpleString( "def Property(n): return g.service(n)" );
85 // For command-line completion (unix only)
86 #if !defined( _WIN32 )
87  PyRun_SimpleString( "import rlcompleter" );
88  PyRun_SimpleString( "rlcompleter.readline.parse_and_bind('tab: complete')" );
89 #endif
90  return StatusCode::SUCCESS;
91 }
92 
93 //----------------------------------------------------------------------------------
95 //----------------------------------------------------------------------------------
96 {
97  // Finalize this specific service
99  if ( sc.isFailure() ) return sc;
100 
101  // Shutdown the Python interpreter
102  Py_Finalize();
103  return StatusCode::SUCCESS;
104 }
105 
106 //----------------------------------------------------------------------------------
108 //----------------------------------------------------------------------------------
109 {
110  if ( !m_startupScript.empty() ) {
112  std::stringstream stream;
113  if ( file ) {
114  std::string buffer;
115  file.seekg( 0, std::ios::end );
116  buffer.reserve( file.tellg() );
117  file.seekg( 0, std::ios::beg );
119  file.close();
120  PyRun_SimpleString( buffer.c_str() );
121  } else {
122  warning() << "Python startup file " << m_startupScript << " not found" << endmsg;
123  }
124  }
125  PyRun_InteractiveLoop( stdin, "\0" );
126  return StatusCode::SUCCESS;
127 }
StatusCode initialize() override
Definition: Service.cpp:60
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 & info() const
shortcut for the method msgStream(MSG::INFO)
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
STL namespace.
SmartIF< IFace > as()
Definition: ISvcLocator.h:103
StatusCode finalize() override
Finalize the service. [IService::finalize()].
bool isFailure() const
Definition: StatusCode.h:130
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:50
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.
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:658
const ValueType & value() const
Backward compatibility (.
Definition: Property.h:525
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:277
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192
STL class.
T reserve(T...args)