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 
16 extern "C"
17 {
19 }
20 
21 #include "vtkCLIOptions.h"
23 #include "vtkLogger.h"
25 #include "vtkPVPluginTracker.h"
26 #include "vtkProcessModule.h"
28 #include "vtkPythonInterpreter.h"
30 #include "vtkSMProxyManager.h"
31 #include "vtkSMSession.h"
32 
33 #include <vector>
34 #include <vtksys/SystemTools.hxx>
35 
37 
38 namespace ParaViewPython
39 {
40 
41 //---------------------------------------------------------------------------
42 
43 inline void ProcessArgsForPython(std::vector<char*>& pythonArgs,
44  const std::vector<std::string>& args, int vtkNotUsed(argc), char** argv)
45 {
46  pythonArgs.clear();
47 
48  // push the executable name first.
49  pythonArgs.push_back(vtksys::SystemTools::DuplicateString(argv[0]));
50 
51  // now push the unparsed arguments.
52  if (args.empty())
53  {
54  return;
55  }
56 
57  // here we handle a special case when the filename specified is a zip
58  // archive.
59  if (vtksys::SystemTools::GetFilenameLastExtension(args[0]) == ".zip")
60  {
61  // add the archive to sys.path
62  vtkPythonInterpreter::PrependPythonPath(args[0].c_str());
63  pythonArgs.push_back(vtksys::SystemTools::DuplicateString("-m"));
64 
65  std::string modulename = vtksys::SystemTools::GetFilenameWithoutLastExtension(
66  vtksys::SystemTools::GetFilenameName(args[0]));
67  pythonArgs.push_back(vtksys::SystemTools::DuplicateString(modulename.c_str()));
68  }
69  else
70  {
71  pythonArgs.push_back(vtksys::SystemTools::DuplicateString(args[0].c_str()));
72  }
73 
74  for (size_t cc = 1, max = args.size(); cc < max; ++cc)
75  {
76  pythonArgs.push_back(vtksys::SystemTools::DuplicateString(args[cc].c_str()));
77  }
78 }
79 
80 //---------------------------------------------------------------------------
81 inline int Run(int processType, int argc, char* argv[])
82 {
83  // Setup options
85 
86  auto options = vtk::TakeSmartPointer(vtkCLIOptions::New());
87  const auto status = vtkInitializationHelper::Initialize(argc, argv, processType, options);
88  if (!status)
89  {
91  }
92 
93  if (processType == vtkProcessModule::PROCESS_BATCH && options->GetExtraArguments().empty())
94  {
95  vtkLogF(ERROR, "No script specified. Please specify a batch script or use 'pvpython'.");
96  return EXIT_FAILURE;
97  }
98 
100 
101  // register callback to initialize modules statically. The callback is
102  // empty when BUILD_SHARED_LIBS is ON.
104 
105  // register static plugins
107 
109 
110  int ret_val = 0;
111  if (pm->GetSymmetricMPIMode() == false && pm->GetPartitionId() > 0)
112  {
115  pm->UnRegisterSession(sid);
116  }
117  else
118  {
119  // Process arguments
120  std::vector<char*> pythonArgs;
121  ProcessArgsForPython(pythonArgs, options->GetExtraArguments(), argc, argv);
122  pythonArgs.push_back(nullptr);
123 
124  // if user specified verbosity option on command line, then we make vtkPythonInterpreter post
125  // log information as INFO, otherwise we leave it at default which is TRACE.
127  vtkPythonInterpreter::SetLogVerbosity(
128  pmConfig->GetLogStdErrVerbosity() != vtkLogger::VERBOSITY_INVALID
129  ? vtkLogger::VERBOSITY_INFO
130  : vtkLogger::VERBOSITY_TRACE);
131 
132  ret_val =
133  vtkPythonInterpreter::PyMain(static_cast<int>(pythonArgs.size()) - 1, &pythonArgs.front());
134 
135  // Free python args
136  for (auto& ptr : pythonArgs)
137  {
138  delete[] ptr;
139  }
140  }
141  // Exit application
143  return ret_val;
144 }
145 }
static vtkProcessModuleConfiguration * GetInstance()
Provides access to the singleton.
void LoadPluginConfigurationXMLs(const char *appname)
Called to load application-specific configuration xml.
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:43
static vtkPVPluginTracker * GetInstance()
Provides access to the singleton.
vtkMultiProcessController * GetGlobalController()
Provides access to the global MPI controller, if any.
static bool Initialize(int argc, char **argv, int processType, vtkCLIOptions *options=nullptr, bool enableStandardArgs=true)
Initializes ParaView engine.
int Run(int processType, int argc, char *argv[])
Definition: pvpython.h:81
void vtkPVInitializePythonModules()
static void ParaView_paraview_plugins_initialize()
#define max(a, b)
process initialization and management core for ParaView processes.
bool UnRegisterSession(vtkIdType sessionID)
Unregister a session given its ID.