pvpython.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Kitware Inc.
2 // SPDX-License-Identifier: BSD-3-Clause
3 
4 extern "C"
5 {
7 }
8 
9 #include "vtkCLIOptions.h"
11 #include "vtkLogger.h"
13 #include "vtkPVPluginTracker.h"
14 #include "vtkProcessModule.h"
16 #include "vtkPythonInterpreter.h"
18 #include "vtkSMProxyManager.h"
19 #include "vtkSMSession.h"
20 #include "vtkSessionIterator.h"
21 
22 #include <vector>
23 #include <vtksys/SystemTools.hxx>
24 
25 #include "ParaView_paraview_plugins.h"
26 
27 namespace ParaViewPython
28 {
29 
30 //---------------------------------------------------------------------------
31 
32 inline void ProcessArgsForPython(std::vector<char*>& pythonArgs,
33  const std::vector<std::string>& args, int vtkNotUsed(argc), char** argv)
34 {
35  pythonArgs.clear();
36 
37  // push the executable name first.
38  pythonArgs.push_back(vtksys::SystemTools::DuplicateString(argv[0]));
39 
40  // now push the unparsed arguments.
41  if (args.empty())
42  {
43  return;
44  }
45 
46  // here we handle a special case when the filename specified is a zip
47  // archive.
48  if (vtksys::SystemTools::GetFilenameLastExtension(args[0]) == ".zip")
49  {
50  // add the archive to sys.path
51  vtkPythonInterpreter::PrependPythonPath(args[0].c_str());
52  pythonArgs.push_back(vtksys::SystemTools::DuplicateString("-m"));
53 
54  std::string modulename = vtksys::SystemTools::GetFilenameWithoutLastExtension(
55  vtksys::SystemTools::GetFilenameName(args[0]));
56  pythonArgs.push_back(vtksys::SystemTools::DuplicateString(modulename.c_str()));
57  }
58  else
59  {
60  pythonArgs.push_back(vtksys::SystemTools::DuplicateString(args[0].c_str()));
61  }
62 
63  for (size_t cc = 1, max = args.size(); cc < max; ++cc)
64  {
65  pythonArgs.push_back(vtksys::SystemTools::DuplicateString(args[cc].c_str()));
66  }
67 }
68 
69 //---------------------------------------------------------------------------
70 inline int Run(int processType, int argc, char* argv[])
71 {
73 
74  // Setup options
75  auto options = vtk::TakeSmartPointer(vtkCLIOptions::New());
76  auto status = vtkInitializationHelper::InitializeOptions(argc, argv, processType, options);
77  if (!status)
78  {
80  }
81 
82  // register callback to initialize modules statically. The callback is
83  // empty when BUILD_SHARED_LIBS is ON.
85 
86  // Setup python options
87  std::vector<char*> pythonArgs;
88  ProcessArgsForPython(pythonArgs, options->GetExtraArguments(), argc, argv);
89  pythonArgs.push_back(nullptr);
90  vtkPythonInterpreter::InitializeWithArgs(
91  1, static_cast<int>(pythonArgs.size()) - 1, &pythonArgs.front());
92 
93  // Do the rest of the initialization
95  if (!status)
96  {
98  }
99 
100  if (processType == vtkProcessModule::PROCESS_BATCH && options->GetExtraArguments().empty())
101  {
102  vtkLogF(ERROR, "No script specified. Please specify a batch script or use 'pvpython'.");
103  return EXIT_FAILURE;
104  }
105 
107 
108  // register static plugins
109  ParaView_paraview_plugins_initialize();
110 
112 
113  int ret_val = 0;
114  if (pm->GetSymmetricMPIMode() == false && pm->GetPartitionId() > 0)
115  {
118  pm->UnRegisterSession(sid);
119  }
120  else
121  {
122  // if user specified verbosity option on command line, then we make vtkPythonInterpreter post
123  // log information as INFO, otherwise we leave it at default which is TRACE.
125  vtkPythonInterpreter::SetLogVerbosity(
126  pmConfig->GetLogStdErrVerbosity() != vtkLogger::VERBOSITY_INVALID
127  ? vtkLogger::VERBOSITY_INFO
128  : vtkLogger::VERBOSITY_TRACE);
129 
130  ret_val =
131  vtkPythonInterpreter::PyMain(static_cast<int>(pythonArgs.size()) - 1, &pythonArgs.front());
132 
133  // Make sure all RMI loop are aborted if paraview stack was not initialised
134  // https://gitlab.kitware.com/paraview/paraview/-/issues/21546
135  auto iter = vtk::TakeSmartPointer(pm->NewSessionIterator());
136  if (iter->IsDoneWithTraversal())
137  {
139  }
140  }
141 
142  // Free python args
143  for (auto& ptr : pythonArgs)
144  {
145  delete[] ptr;
146  }
147 
148  // Exit application
150  return ret_val;
151 }
152 }
vtkInitializationHelper::InitializeOptions
static bool InitializeOptions(int argc, char **argv, int processType, vtkCLIOptions *options=nullptr, bool enableStandardArgs=true)
Initialize only the options of ParaView engine.
vtkProcessModule::GetPartitionId
int GetPartitionId()
Returns the local process id.
vtkPVPluginTracker::GetInstance
static vtkPVPluginTracker * GetInstance()
Provides access to the singleton.
vtkSMProxyManager.h
vtkProcessModule.h
vtkProcessModule
process initialization and management core for ParaView processes. vtkProcessModule is the process in...
Definition: vtkProcessModule.h:29
vtkInitializationHelper::Finalize
static void Finalize()
Finalizes the server manager.
vtkIdType
int vtkIdType
vtkProcessModule::GetProcessModule
static vtkProcessModule * GetProcessModule()
Provides access to the global ProcessModule.
vtkProcessModule::NewSessionIterator
vtkSessionIterator * NewSessionIterator()
Returns a new session iterator that can be used to iterate over the registered sessions.
vtkMultiProcessController.h
vtkInitializationHelper::GetExitCode
static int GetExitCode()
Returns the exit code after Initialize.
Definition: vtkInitializationHelper.h:91
vtkPVInitializePythonModules
void vtkPVInitializePythonModules()
vtkCLIOptions::New
static vtkCLIOptions * New()
vtkRemotingCoreConfiguration.h
max
#define max(a, b)
vtkInitializationHelper::SetApplicationName
static void SetApplicationName(const std::string &appName)
Sets the name of the application.
ParaViewPython::Run
int Run(int processType, int argc, char *argv[])
Definition: pvpython.h:70
vtkProcessModuleConfiguration::GetInstance
static vtkProcessModuleConfiguration * GetInstance()
Provides access to the singleton.
vtkMultiProcessController::TriggerBreakRMIs
void TriggerBreakRMIs()
vtkCLIOptions.h
ParaViewPython::ProcessArgsForPython
void ProcessArgsForPython(std::vector< char * > &pythonArgs, const std::vector< std::string > &args, int vtkNotUsed(argc), char **argv)
Definition: pvpython.h:32
vtkProcessModule::PROCESS_BATCH
@ PROCESS_BATCH
Definition: vtkProcessModule.h:43
vtkMultiProcessController::ProcessRMIs
int ProcessRMIs(int reportErrors, int dont_loop=0)
vtkInitializationHelper.h
vtkProcessModuleConfiguration.h
vtkSMSession::ConnectToSelf
static vtkIdType ConnectToSelf()
These are static helper methods that help create standard ParaView sessions.
vtkProcessModule::UnRegisterSession
bool UnRegisterSession(vtkIdType sessionID)
Unregister a session given its ID.
vtkInitializationHelper::InitializeMiscellaneous
static bool InitializeMiscellaneous(int type)
Initialize everything that needs to be initialized in the paraview engine after the options.
vtkSMSession.h
vtkProcessModule::GetSymmetricMPIMode
static bool GetSymmetricMPIMode()
ParaViewPython
Definition: pvpython.h:27
vtkProcessModule::GetGlobalController
vtkMultiProcessController * GetGlobalController()
Provides access to the global MPI controller, if any.
vtkPVPluginTracker.h
vtkPVPluginTracker::LoadPluginConfigurationXMLs
void LoadPluginConfigurationXMLs(const char *appname)
Called to load application-specific configuration xml.
vtkSessionIterator.h