pvpython.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3 Program: ParaView
4 Module: pvpython.cxx
5 
6 Copyright (c) Kitware, Inc.
7 All rights reserved.
8 See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.
9 
10 This software is distributed WITHOUT ANY WARRANTY; without even
11 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
15 #include "vtkPVConfig.h" // Required to get build options for paraview
16 
17 extern "C" {
19 }
20 
22 #include "vtkLogger.h"
24 #include "vtkPVPluginTracker.h"
25 #include "vtkPVPythonOptions.h"
26 #include "vtkProcessModule.h"
27 #include "vtkPythonInterpreter.h"
28 #include "vtkSMSession.h"
29 
30 #include <vector>
31 #include <vtksys/SystemTools.hxx>
32 
34 
35 namespace ParaViewPython
36 {
37 
38 //---------------------------------------------------------------------------
39 
41  std::vector<char*>& pythonArgs, const char* first_unknown_arg, int argc, char* argv[])
42 {
43  pythonArgs.clear();
44  pythonArgs.push_back(vtksys::SystemTools::DuplicateString(argv[0]));
45  if (first_unknown_arg && strcmp(first_unknown_arg, "--") == 0)
46  {
47  // `--` causes ParaView to not process the arguments anymore and simply
48  // pass them to the Python interpreter.
49  }
50  else if (first_unknown_arg && strlen(first_unknown_arg) > 0)
51  {
52  // here we handle a special case when the filename specified is a zip
53  // archive.
54  if (vtksys::SystemTools::GetFilenameLastExtension(first_unknown_arg) == ".zip")
55  {
56  // add the archive to sys.path
57  vtkPythonInterpreter::PrependPythonPath(first_unknown_arg);
58  pythonArgs.push_back(vtksys::SystemTools::DuplicateString("-m"));
59 
60  std::string modulename = vtksys::SystemTools::GetFilenameWithoutLastExtension(
61  vtksys::SystemTools::GetFilenameName(first_unknown_arg));
62  pythonArgs.push_back(vtksys::SystemTools::DuplicateString(modulename.c_str()));
63  }
64  else
65  {
66  pythonArgs.push_back(vtksys::SystemTools::DuplicateString(first_unknown_arg));
67  }
68  }
69 
70  for (int cc = 1; cc < argc; cc++)
71  {
72  pythonArgs.push_back(vtksys::SystemTools::DuplicateString(argv[cc]));
73  }
74 }
75 
76 //---------------------------------------------------------------------------
77 int Run(int processType, int argc, char* argv[])
78 {
79  // Setup options
80  // Marking this static avoids the false leak messages from vtkDebugLeaks when
81  // using mpich. It appears that the root process which spawns all the
82  // main processes waits in MPI_Init() and calls exit() when
83  // the others are done, causing apparent memory leaks for any non-static objects
84  // created before MPI_Init().
87  vtkInitializationHelper::Initialize(argc, argv, processType, options);
88  if (options->GetTellVersion() || options->GetHelpSelected() || options->GetPrintMonitors())
89  {
91  return EXIT_SUCCESS;
92  }
93 
94  if (processType == vtkProcessModule::PROCESS_BATCH && options->GetUnknownArgument() == nullptr)
95  {
96  vtkGenericWarningMacro("No script specified. "
97  "Please specify a batch script or use 'pvpython'.");
99  return EXIT_FAILURE;
100  }
101 
103 
104  // register callback to initialize modules statically. The callback is
105  // empty when BUILD_SHARED_LIBS is ON.
107 
108  // register static plugins
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  int remaining_argc;
123  char** remaining_argv;
124  options->GetRemainingArguments(&remaining_argc, &remaining_argv);
125 
126  // Process arguments
127  std::vector<char*> pythonArgs;
128  ProcessArgsForPython(pythonArgs, options->GetUnknownArgument(), remaining_argc, remaining_argv);
129  pythonArgs.push_back(nullptr);
130 
131  // if user specified verbosity option on command line, then we make vtkPythonInterpreter post
132  // log information as INFO, otherwise we leave it at default which is TRACE.
133  vtkPythonInterpreter::SetLogVerbosity(
134  options->GetLogStdErrVerbosity() != vtkLogger::VERBOSITY_INVALID
135  ? vtkLogger::VERBOSITY_INFO
136  : vtkLogger::VERBOSITY_TRACE);
137 
138  // Start interpretor
139  vtkPythonInterpreter::Initialize();
140 
141  ret_val =
142  vtkPythonInterpreter::PyMain(static_cast<int>(pythonArgs.size()) - 1, &*pythonArgs.begin());
143 
144  // Free python args
145  std::vector<char*>::iterator it = pythonArgs.begin();
146  while (it != pythonArgs.end())
147  {
148  delete[] * it;
149  ++it;
150  }
151  }
152  // Exit application
154  return ret_val;
155 }
156 }
void LoadPluginConfigurationXMLs(const char *appname)
Called to load application-specific configuration xml.
virtual char * GetUnknownArgument()
In case of unknown argument, set this variable with the unknown argument.
void ProcessArgsForPython(std::vector< char *> &pythonArgs, const char *first_unknown_arg, int argc, char *argv[])
Definition: pvpython.h:40
static vtkIdType ConnectToSelf(int timeout=60)
These are static helper methods that help create standard ParaView sessions.
int GetPartitionId()
Returns the local process id.
int ProcessRMIs(int reportErrors, int dont_loop=0)
static void Finalize()
Finalizes the server manager.
int vtkIdType
static vtkSmartPointer< T > New()
static vtkProcessModule * GetProcessModule()
Provides access to the global ProcessModule.
void ParaView_paraview_plugins_initialize()
void GetRemainingArguments(int *argc, char **argv[])
virtual int GetHelpSelected()
Was help selected?
static void SetApplicationName(const std::string &appName)
Sets the name of the application.
virtual bool GetSymmetricMPIMode()
Returns true if ParaView is to be run in symmetric mode.
static vtkPVPluginTracker * GetInstance()
Provides access to the singleton.
virtual int GetPrintMonitors()
Should this process just print monitor information and exit?
vtkMultiProcessController * GetGlobalController()
Provides access to the global MPI controller, if any.
static void Initialize(const char *executable, int type)
Initializes the server manager.
virtual int GetLogStdErrVerbosity()
Returns the verbosity level for stderr output chosen.
int Run(int processType, int argc, char *argv[])
Definition: pvpython.h:77
virtual int GetTellVersion()
Should this run print the version numbers and exit.
void vtkPVInitializePythonModules()
process initialization and management core for ParaView processes.
bool UnRegisterSession(vtkIdType sessionID)
Unregister a session given its ID.