1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
// SPDX-FileCopyrightText: Copyright (c) Kitware Inc.
// SPDX-License-Identifier: BSD-3-Clause
extern "C"
{
void vtkPVInitializePythonModules();
}
#include "vtkCLIOptions.h"
#include "vtkInitializationHelper.h"
#include "vtkLogger.h"
#include "vtkMultiProcessController.h"
#include "vtkPVPluginTracker.h"
#include "vtkProcessModule.h"
#include "vtkProcessModuleConfiguration.h"
#include "vtkPythonInterpreter.h"
#include "vtkRemotingCoreConfiguration.h"
#include "vtkSMProxyManager.h"
#include "vtkSMSession.h"
#include "vtkSessionIterator.h"
#include <vector>
#include <vtksys/SystemTools.hxx>
#include "ParaView_paraview_plugins.h"
namespace ParaViewPython
{
//---------------------------------------------------------------------------
inline void ProcessArgsForPython(std::vector<char*>& pythonArgs,
const std::vector<std::string>& args, int vtkNotUsed(argc), char** argv)
{
pythonArgs.clear();
// push the executable name first.
pythonArgs.push_back(vtksys::SystemTools::DuplicateString(argv[0]));
// now push the unparsed arguments.
if (args.empty())
{
return;
}
// here we handle a special case when the filename specified is a zip
// archive.
if (vtksys::SystemTools::GetFilenameLastExtension(args[0]) == ".zip")
{
// add the archive to sys.path
vtkPythonInterpreter::PrependPythonPath(args[0].c_str());
pythonArgs.push_back(vtksys::SystemTools::DuplicateString("-m"));
std::string modulename = vtksys::SystemTools::GetFilenameWithoutLastExtension(
vtksys::SystemTools::GetFilenameName(args[0]));
pythonArgs.push_back(vtksys::SystemTools::DuplicateString(modulename.c_str()));
}
else
{
pythonArgs.push_back(vtksys::SystemTools::DuplicateString(args[0].c_str()));
}
for (size_t cc = 1, max = args.size(); cc < max; ++cc)
{
pythonArgs.push_back(vtksys::SystemTools::DuplicateString(args[cc].c_str()));
}
}
//---------------------------------------------------------------------------
inline int Run(int processType, int argc, char* argv[])
{
vtkInitializationHelper::SetApplicationName("ParaView");
// Setup options
auto options = vtk::TakeSmartPointer(vtkCLIOptions::New());
auto status = vtkInitializationHelper::InitializeOptions(argc, argv, processType, options);
if (!status)
{
return vtkInitializationHelper::GetExitCode();
}
// register callback to initialize modules statically. The callback is
// empty when BUILD_SHARED_LIBS is ON.
vtkPVInitializePythonModules();
vtkProcessModule* pm = vtkProcessModule::GetProcessModule();
// Setup python options
std::vector<char*> pythonArgs;
ProcessArgsForPython(pythonArgs, options->GetExtraArguments(), argc, argv);
pythonArgs.push_back(nullptr);
const char* programName = nullptr;
// Apple has a specific folder hierarchy that prevent to
// set the correct programName to pvpython
// https://gitlab.kitware.com/paraview/paraview/-/issues/20652
#if !defined(__APPLE__)
programName = pm->GetProgramPath().c_str();
#endif
vtkPythonInterpreter::InitializeWithArgs(
1, static_cast<int>(pythonArgs.size()) - 1, &pythonArgs.front(), programName);
// Do the rest of the initialization
status = vtkInitializationHelper::InitializeSettings(processType, /*defaultCoreConfig*/ true);
status &= vtkInitializationHelper::InitializeOthers();
if (!status)
{
return vtkInitializationHelper::GetExitCode();
}
if (processType == vtkProcessModule::PROCESS_BATCH && options->GetExtraArguments().empty())
{
vtkLogF(ERROR, "No script specified. Please specify a batch script or use 'pvpython'.");
return EXIT_FAILURE;
}
// register static plugins
ParaView_paraview_plugins_initialize();
vtkPVPluginTracker::GetInstance()->LoadPluginConfigurationXMLs("paraview");
int ret_val = 0;
if (pm->GetSymmetricMPIMode() == false && pm->GetPartitionId() > 0)
{
vtkIdType sid = vtkSMSession::ConnectToSelf();
pm->GetGlobalController()->ProcessRMIs();
pm->UnRegisterSession(sid);
}
else
{
// if user specified verbosity option on command line, then we make vtkPythonInterpreter post
// log information as INFO, otherwise we leave it at default which is TRACE.
auto pmConfig = vtkProcessModuleConfiguration::GetInstance();
vtkPythonInterpreter::SetLogVerbosity(
pmConfig->GetLogStdErrVerbosity() != vtkLogger::VERBOSITY_INVALID
? vtkLogger::VERBOSITY_INFO
: vtkLogger::VERBOSITY_TRACE);
ret_val =
vtkPythonInterpreter::PyMain(static_cast<int>(pythonArgs.size()) - 1, &pythonArgs.front());
// Make sure all RMI loop are aborted if paraview stack was not initialised
// https://gitlab.kitware.com/paraview/paraview/-/issues/21546
auto iter = vtk::TakeSmartPointer(pm->NewSessionIterator());
if (iter->IsDoneWithTraversal())
{
pm->GetGlobalController()->TriggerBreakRMIs();
}
}
// Free python args
for (auto& ptr : pythonArgs)
{
delete[] ptr;
}
// Exit application
vtkInitializationHelper::Finalize();
return ret_val;
}
}
|