The Gaudi Framework  master (37c0b60a)
PythonScriptingSvc.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 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 #include <cpython/initconfig.h>
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  wchar_t* progName[] = { const_cast<wchar_t*>( L"GaudiPython" ) };
69 
70  PyStatus status;
71 
72  PyConfig config;
73  PyConfig_InitPythonConfig( &config );
74 
75  status = PyConfig_SetString( &config, &config.program_name, progName[0] );
76  // Set argv for Tkinter (needs program name)
77  status = PyConfig_SetArgv( &config, 1, progName );
78  status = Py_InitializeFromConfig( &config );
79  PyConfig_Clear( &config );
80 
81  // Get the Python version
82  std::string fullversion = Py_GetVersion();
83  std::string version( fullversion, 0, fullversion.find_first_of( ' ' ) );
84  std::string vers( version, 0, version.find_first_of( '.', version.find_first_of( '.' ) + 1 ) );
85  info() << "Python version: [" << vers << "]" << endmsg;
86 
87 #if defined( __linux )
88  // This is hack to make global the python symbols
89  // which are needed by the other python modules
90  // (eg. readline, math, etc,) libraries.
91  std::string libname = "libpython" + vers + ".so";
92  dlopen( libname.c_str(), RTLD_GLOBAL | RTLD_LAZY );
93 #endif
94 
95  // Startup commands
96  PyRun_SimpleString( "from gaudimodule import *" );
97  PyRun_SimpleString( "g = AppMgr()" );
98  // backward compatibility with SIPython
99  PyRun_SimpleString( "theApp = g" );
100  PyRun_SimpleString( "def Service(n): return g.service(n)" );
101  PyRun_SimpleString( "def Algorithm(n): return g.algorithm(n)" );
102  PyRun_SimpleString( "def Property(n): return g.service(n)" );
103 // For command-line completion (unix only)
104 #if !defined( _WIN32 )
105  PyRun_SimpleString( "import rlcompleter" );
106  PyRun_SimpleString( "rlcompleter.readline.parse_and_bind('tab: complete')" );
107 #endif
108  return StatusCode::SUCCESS;
109 }
110 
111 //----------------------------------------------------------------------------------
113 //----------------------------------------------------------------------------------
114 {
115  // Finalize this specific service
117  if ( sc.isFailure() ) return sc;
118 
119  // Shutdown the Python interpreter
120  Py_Finalize();
121  return StatusCode::SUCCESS;
122 }
123 
124 //----------------------------------------------------------------------------------
126 //----------------------------------------------------------------------------------
127 {
128  if ( !m_startupScript.empty() ) {
131  if ( file ) {
132  std::string buffer;
133  file.seekg( 0, std::ios::end );
134  buffer.reserve( file.tellg() );
135  file.seekg( 0, std::ios::beg );
137  file.close();
138  PyRun_SimpleString( buffer.c_str() );
139  } else {
140  warning() << "Python startup file " << m_startupScript << " not found" << endmsg;
141  }
142  }
143  PyRun_InteractiveLoop( stdin, "\0" );
144  return StatusCode::SUCCESS;
145 }
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:370
Service::finalize
StatusCode finalize() override
Definition: Service.cpp:222
prepareBenchmark.config
config
Definition: prepareBenchmark.py:43
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:237
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
std::istreambuf_iterator
PythonScriptingSvc::~PythonScriptingSvc
~PythonScriptingSvc() override
Destructor.
Definition: PythonScriptingSvc.cpp:42
PythonScriptingSvc::run
StatusCode run() override
Run the service by taking full control. [IRunable::run()].
Definition: PythonScriptingSvc.cpp:125
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:77
PythonScriptingSvc::initialize
StatusCode initialize() override
Initialize the service. [IService::initialize()].
Definition: PythonScriptingSvc.cpp:46
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:112
std::string::find_first_of
T find_first_of(T... args)
IOTest.end
end
Definition: IOTest.py:125
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.