Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v38r1p1 (ae26267b)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
PythonScriptingSvc.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #include "Python.h"
12 
13 // Python 3 compatibility
14 #if PY_MAJOR_VERSION >= 3
15 
16 # define PySys_SetArgv_Char_t wchar_t
17 
18 #else
19 
20 # define PySys_SetArgv_Char_t char
21 
22 #endif
23 
24 // Include Files
26 #include "GaudiKernel/MsgStream.h"
27 #include "GaudiKernel/SmartIF.h"
28 
29 #include "PythonScriptingSvc.h"
30 
31 #include <fstream>
32 #include <sstream>
33 
34 // Special for Unixes
35 #if defined( __linux )
36 # include "dlfcn.h"
37 #endif
38 
39 // Instantiation of a static factory class used by clients to create
40 // instances of this service
42 
43 //----------------------------------------------------------------------------------
45  //----------------------------------------------------------------------------------
46  : base_class( name, svc ) {
47  // Declare the startup script Property
48  declareProperty( "StartupScript", m_startupScript = "" );
49 }
50 
51 //----------------------------------------------------------------------------------
53 //----------------------------------------------------------------------------------
54 
55 //----------------------------------------------------------------------------------
57 //----------------------------------------------------------------------------------
58 {
59  // initialize the Service Base class
61  if ( sc.isFailure() ) return sc;
62 
63  // Setup startup script. If none is explicitly specified, then
64  // use the ApplicationMgr JobOptionsPath property as long as
65  // the JobOptionsType property is set to "NONE".
66  if ( m_startupScript.empty() ) {
67  auto prpMgr = serviceLocator()->as<IProperty>();
68  if ( prpMgr ) {
70  tmp.assign( prpMgr->getProperty( "JobOptionsType" ) );
71  if ( tmp.value() == "NONE" ) {
72  tmp.assign( prpMgr->getProperty( "JobOptionsPath" ) );
73  m_startupScript = tmp;
74  }
75  }
76  }
77 
78  // Python 3 compatibility
79 #if PY_MAJOR_VERSION >= 3
80  wchar_t* progName[] = { const_cast<wchar_t*>( L"GaudiPython" ) };
81 #else
82  char* progName[] = { const_cast<char*>( "GaudiPython" ) };
83 #endif
84 
85  // Initialize the Python interpreter. Required.
86  Py_Initialize();
87  // Set argv for Tkinter (needs program name)
88  PySys_SetArgv( 1, progName );
89  // Get the Python version
90  std::string fullversion = Py_GetVersion();
91  std::string version( fullversion, 0, fullversion.find_first_of( ' ' ) );
92  std::string vers( version, 0, version.find_first_of( '.', version.find_first_of( '.' ) + 1 ) );
93  info() << "Python version: [" << vers << "]" << endmsg;
94 
95 #if defined( __linux )
96  // This is hack to make global the python symbols
97  // which are needed by the other python modules
98  // (eg. readline, math, etc,) libraries.
99  std::string libname = "libpython" + vers + ".so";
100  dlopen( libname.c_str(), RTLD_GLOBAL | RTLD_LAZY );
101 #endif
102 
103  // Startup commands
104  PyRun_SimpleString( "from gaudimodule import *" );
105  PyRun_SimpleString( "g = AppMgr()" );
106  // backward compatibility with SIPython
107  PyRun_SimpleString( "theApp = g" );
108  PyRun_SimpleString( "def Service(n): return g.service(n)" );
109  PyRun_SimpleString( "def Algorithm(n): return g.algorithm(n)" );
110  PyRun_SimpleString( "def Property(n): return g.service(n)" );
111 // For command-line completion (unix only)
112 #if !defined( _WIN32 )
113  PyRun_SimpleString( "import rlcompleter" );
114  PyRun_SimpleString( "rlcompleter.readline.parse_and_bind('tab: complete')" );
115 #endif
116  return StatusCode::SUCCESS;
117 }
118 
119 //----------------------------------------------------------------------------------
121 //----------------------------------------------------------------------------------
122 {
123  // Finalize this specific service
125  if ( sc.isFailure() ) return sc;
126 
127  // Shutdown the Python interpreter
128  Py_Finalize();
129  return StatusCode::SUCCESS;
130 }
131 
132 //----------------------------------------------------------------------------------
134 //----------------------------------------------------------------------------------
135 {
136  if ( !m_startupScript.empty() ) {
139  if ( file ) {
140  std::string buffer;
141  file.seekg( 0, std::ios::end );
142  buffer.reserve( file.tellg() );
143  file.seekg( 0, std::ios::beg );
145  file.close();
146  PyRun_SimpleString( buffer.c_str() );
147  } else {
148  warning() << "Python startup file " << m_startupScript << " not found" << endmsg;
149  }
150  }
151  PyRun_InteractiveLoop( stdin, "\0" );
152  return StatusCode::SUCCESS;
153 }
PythonScriptingSvc::m_startupScript
std::string m_startupScript
Startup script.
Definition: PythonScriptingSvc.h:45
PythonScriptingSvc.h
Write.stream
stream
Definition: Write.py:32
Service::initialize
StatusCode initialize() override
Definition: Service.cpp:118
std::string
STL class.
std::string::reserve
T reserve(T... args)
ISvcLocator
Definition: ISvcLocator.h:46
std::stringstream
STL class.
Gaudi::Property::assign
bool assign(const Details::PropertyBase &source) override
get the value from another property
Definition: Property.h:372
Service::finalize
StatusCode finalize() override
Definition: Service.cpp:222
IProperty
Definition: IProperty.h:33
SmartIF.h
StatusCode
Definition: StatusCode.h:65
conf.version
string version
Definition: conf.py:25
CommonMessaging
Definition: CommonMessaging.h:66
std::string::c_str
T c_str(T... args)
Gaudi::Property::value
const ValueType & value() const
Definition: Property.h:239
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:203
std::istreambuf_iterator
PythonScriptingSvc::~PythonScriptingSvc
~PythonScriptingSvc() override
Destructor.
Definition: PythonScriptingSvc.cpp:52
PythonScriptingSvc::run
StatusCode run() override
Run the service by taking full control. [IRunable::run()].
Definition: PythonScriptingSvc.cpp:133
SmartIF::as
SmartIF< IFace > as() const
return a new SmartIF instance to another interface
Definition: SmartIF.h:117
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:129
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
ConditionsStallTest.name
name
Definition: ConditionsStallTest.py:76
PythonScriptingSvc::initialize
StatusCode initialize() override
Initialize the service. [IService::initialize()].
Definition: PythonScriptingSvc.cpp:56
PythonScriptingSvc
Definition: PythonScriptingSvc.h:25
std
STL namespace.
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
std::string::empty
T empty(T... args)
std::string::assign
T assign(T... args)
PythonScriptingSvc::finalize
StatusCode finalize() override
Finalize the service. [IService::finalize()].
Definition: PythonScriptingSvc.cpp:120
std::string::find_first_of
T find_first_of(T... args)
IOTest.end
end
Definition: IOTest.py:123
Gaudi::Units::L
constexpr double L
Definition: SystemOfUnits.h:118
ISvcLocator.h
Gaudi::Property< std::string >
MsgStream.h
Service::serviceLocator
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator
Definition: Service.cpp:335
std::ifstream
STL class.