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 
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 ) {
49  StringProperty tmp;
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 
79  // Startup commands
80  PyRun_SimpleString( "from gaudimodule import *" );
81  PyRun_SimpleString( "g = AppMgr()" );
82  // backward compatibility with SIPython
83  PyRun_SimpleString( "theApp = g" );
84  PyRun_SimpleString( "def Service(n): return g.service(n)" );
85  PyRun_SimpleString( "def Algorithm(n): return g.algorithm(n)" );
86  PyRun_SimpleString( "def Property(n): return g.service(n)" );
87  // For command-line completion (unix only)
88 #if !defined( _WIN32 )
89  PyRun_SimpleString( "import rlcompleter");
90  PyRun_SimpleString( "rlcompleter.readline.parse_and_bind('tab: complete')");
91 #endif
92  return StatusCode::SUCCESS;
93 }
94 
95 //----------------------------------------------------------------------------------
97 //----------------------------------------------------------------------------------
98 {
99  // Finalize this specific service
101  if ( sc.isFailure() ) return sc;
102 
103  // Shutdown the Python interpreter
104  Py_Finalize();
105  return StatusCode::SUCCESS;
106 }
107 
108 //----------------------------------------------------------------------------------
110 //----------------------------------------------------------------------------------
111 {
112  if ( !m_startupScript.empty() ) {
114  std::stringstream stream;
115  if( file ) {
116  std::string buffer;
117  file.seekg(0, std::ios::end);
118  buffer.reserve(file.tellg());
119  file.seekg(0, std::ios::beg);
122  file.close();
123  PyRun_SimpleString( buffer.c_str() );
124  } else {
125  warning() << "Python startup file " << m_startupScript << " not found" << endmsg;
126  }
127  }
128  PyRun_InteractiveLoop(stdin, "\0");
129  return StatusCode::SUCCESS;
130 }
StatusCode initialize() override
Definition: Service.cpp:68
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition: Service.cpp:324
T empty(T...args)
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
StatusCode finalize() override
Definition: Service.cpp:193
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
STL namespace.
virtual StatusCode finalize()
Finalize the service. [IService::finalize()].
SmartIF< IFace > as()
Definition: ISvcLocator.h:106
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:86
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:36
STL class.
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
std::string m_startupScript
Startup script.
T find_first_of(T...args)
auto end(reverse_wrapper< T > &w)
Definition: reverse.h:47
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:341
T assign(T...args)
bool assign(const Property &source) override
get the value from another property
Definition: Property.h:269
T c_str(T...args)
virtual StatusCode run()
Run the service by taking full control. [IRunable::run()].
The IProperty is the basic interface for all components which have properties that can be set or get...
Definition: IProperty.h:21
virtual StatusCode initialize()
Initialize the service. [IService::initialize()].
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
STL class.
T reserve(T...args)