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
107  if (!status)
108  {
110  }
111 
112  if (processType == vtkProcessModule::PROCESS_BATCH && options->GetExtraArguments().empty())
113  {
114  vtkLogF(ERROR, "No script specified. Please specify a batch script or use 'pvpython'.");
115  return EXIT_FAILURE;
116  }
117 
118  // register static plugins
120 
122 
123  int ret_val = 0;
124  if (pm->GetSymmetricMPIMode() == false && pm->GetPartitionId() > 0)
125  {
128  pm->UnRegisterSession(sid);
129  }
130  else
131  {
132  // if user specified verbosity option on command line, then we make vtkPythonInterpreter post
133  // log information as INFO, otherwise we leave it at default which is TRACE.
135  vtkPythonInterpreter::SetLogVerbosity(
136  pmConfig->GetLogStdErrVerbosity() != vtkLogger::VERBOSITY_INVALID
137  ? vtkLogger::VERBOSITY_INFO
138  : vtkLogger::VERBOSITY_TRACE);
139 
140  ret_val =
141  vtkPythonInterpreter::PyMain(static_cast<int>(pythonArgs.size()) - 1, &pythonArgs.front());
142 
143  // Make sure all RMI loop are aborted if paraview stack was not initialised
144  // https://gitlab.kitware.com/paraview/paraview/-/issues/21546
145  auto iter = vtk::TakeSmartPointer(pm->NewSessionIterator());
146  if (iter->IsDoneWithTraversal())
147  {
149  }
150  }
151 
152  // Free python args
153  for (auto& ptr : pythonArgs)
154  {
155  delete[] ptr;
156  }
157 
158  // Exit application
160  return ret_val;
161 }
162 }
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 void Finalize()
Finalizes the server manager.
int vtkIdType
static vtkProcessModule * GetProcessModule()
Provides access to the global ProcessModule.
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()
static bool InitializeMiscellaneous(int type)
Initialize everything that needs to be initialized in the paraview engine after the options...
#define max(a, b)
static bool InitializeOptions(int argc, char **argv, int processType, vtkCLIOptions *options=nullptr, bool enableStandardArgs=true)
Initialize only the options of ParaView engine.
process initialization and management core for ParaView processes.
bool UnRegisterSession(vtkIdType sessionID)
Unregister a session given its ID.