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 
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 
87 
88  // Setup python options
89  std::vector<char*> pythonArgs;
90  ProcessArgsForPython(pythonArgs, options->GetExtraArguments(), argc, argv);
91  pythonArgs.push_back(nullptr);
92 
93  const char* programName = nullptr;
94 
95  // Apple has a specific folder hierarchy that prevent to
96  // set the correct programName to pvpython
97  // https://gitlab.kitware.com/paraview/paraview/-/issues/20652
98 #if !defined(__APPLE__)
99  programName = pm->GetProgramPath().c_str();
100 #endif
101 
102  vtkPythonInterpreter::InitializeWithArgs(
103  1, static_cast<int>(pythonArgs.size()) - 1, &pythonArgs.front(), programName);
104 
105  // Do the rest of the initialization
106  status = vtkInitializationHelper::InitializeSettings(processType, /*defaultCoreConfig*/ true);
108  if (!status)
109  {
111  }
112 
113  if (processType == vtkProcessModule::PROCESS_BATCH && options->GetExtraArguments().empty())
114  {
115  vtkLogF(ERROR, "No script specified. Please specify a batch script or use 'pvpython'.");
116  return EXIT_FAILURE;
117  }
118 
119  // register static plugins
121 
123 
124  int ret_val = 0;
125  if (pm->GetSymmetricMPIMode() == false && pm->GetPartitionId() > 0)
126  {
129  pm->UnRegisterSession(sid);
130  }
131  else
132  {
133  // if user specified verbosity option on command line, then we make vtkPythonInterpreter post
134  // log information as INFO, otherwise we leave it at default which is TRACE.
136  vtkPythonInterpreter::SetLogVerbosity(
137  pmConfig->GetLogStdErrVerbosity() != vtkLogger::VERBOSITY_INVALID
138  ? vtkLogger::VERBOSITY_INFO
139  : vtkLogger::VERBOSITY_TRACE);
140 
141  ret_val =
142  vtkPythonInterpreter::PyMain(static_cast<int>(pythonArgs.size()) - 1, &pythonArgs.front());
143 
144  // Make sure all RMI loop are aborted if paraview stack was not initialised
145  // https://gitlab.kitware.com/paraview/paraview/-/issues/21546
146  auto iter = vtk::TakeSmartPointer(pm->NewSessionIterator());
147  if (iter->IsDoneWithTraversal())
148  {
150  }
151  }
152 
153  // Free python args
154  for (auto& ptr : pythonArgs)
155  {
156  delete[] ptr;
157  }
158 
159  // Exit application
161  return ret_val;
162 }
163 }
static vtkProcessModuleConfiguration * GetInstance()
Provides access to the singleton.
void LoadPluginConfigurationXMLs(const char *appname)
Called to load application-specific configuration xml.
vtkSessionIterator * NewSessionIterator()
Returns a new session iterator that can be used to iterate over the registered sessions.
static int GetExitCode()
Returns the exit code after Initialize.
static vtkCLIOptions * New()
int GetPartitionId()
Returns the local process id.
int ProcessRMIs(int reportErrors, int dont_loop=0)
static bool GetSymmetricMPIMode()
static bool InitializeOthers()
Initialize everything that is not initialized by specific methods, see Initialize method for more inf...
static void Finalize()
Finalizes the server manager.
int vtkIdType
static vtkProcessModule * GetProcessModule()
Provides access to the global ProcessModule.
static bool InitializeSettings(int type, bool defaultCoreConfig)
Initialize the setting by reading the settings file, unless coreConfig DisableRegistry is set to true...
static vtkIdType ConnectToSelf()
These are static helper methods that help create standard ParaView sessions.
static void SetApplicationName(const std::string &appName)
Sets the name of the application.
void ProcessArgsForPython(std::vector< char *> &pythonArgs, const std::vector< std::string > &args, int vtkNotUsed(argc), char **argv)
Definition: pvpython.h:32
static vtkPVPluginTracker * GetInstance()
Provides access to the singleton.
vtkMultiProcessController * GetGlobalController()
Provides access to the global MPI controller, if any.
const std::string & GetProgramPath() const
The full path to the current executable that is running (or empty if unknown).
int Run(int processType, int argc, char *argv[])
Definition: pvpython.h:70
void vtkPVInitializePythonModules()
static void ParaView_paraview_plugins_initialize()
#define max(a, b)
static bool InitializeOptions(int argc, char **argv, int processType, vtkCLIOptions *options=nullptr, bool enableStandardArgs=true)
Initialize the process module and options of ParaView engine.
process initialization and management core for ParaView processes.
bool UnRegisterSession(vtkIdType sessionID)
Unregister a session given its ID.