File: pvpython.h

package info (click to toggle)
paraview 4.1.0%2Bdfsg%2B1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 278,136 kB
  • ctags: 340,527
  • sloc: cpp: 2,314,053; ansic: 817,139; python: 245,770; xml: 68,996; tcl: 48,285; fortran: 39,116; yacc: 5,713; java: 3,827; perl: 3,108; sh: 2,045; lex: 1,809; makefile: 935; asm: 471; pascal: 228
file content (125 lines) | stat: -rw-r--r-- 4,037 bytes parent folder | download
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
/*=========================================================================

Program:   ParaView
Module:    pvpython.cxx

Copyright (c) Kitware, Inc.
All rights reserved.
See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.

This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
#include "vtkPVConfig.h" // Required to get build options for paraview

extern "C" {
  void vtkPVInitializePythonModules();  
}

#include "vtkInitializationHelper.h"
#include "vtkMultiProcessController.h"
#include "vtkProcessModule.h"
#include "vtkPVPythonOptions.h"
#include "vtkPythonInterpreter.h"
#include "vtkSMSession.h"

#include <vtksys/SystemTools.hxx>
#include <vector>

namespace ParaViewPython {

  //---------------------------------------------------------------------------

  void ProcessArgsForPython( std::vector<char*> & pythonArgs,
                             const char *script,
                             int argc, char* argv[] )
    {
    pythonArgs.clear();
    pythonArgs.push_back(vtksys::SystemTools::DuplicateString(argv[0]));
    if(script)
      {
      pythonArgs.push_back(vtksys::SystemTools::DuplicateString(script));
      }
    else if (argc > 1)
      {
      pythonArgs.push_back(vtksys::SystemTools::DuplicateString("-"));
      }
    for (int cc=1; cc < argc; cc++)
      {
      pythonArgs.push_back(vtksys::SystemTools::DuplicateString(argv[cc]));
      }
    }

  //---------------------------------------------------------------------------
  int Run(int processType, int argc, char* argv[])
    {
    // Setup options
    // Marking this static avoids the false leak messages from vtkDebugLeaks when
    // using mpich. It appears that the root process which spawns all the
    // main processes waits in MPI_Init() and calls exit() when
    // the others are done, causing apparent memory leaks for any non-static objects
    // created before MPI_Init().
    static vtkSmartPointer<vtkPVPythonOptions> options =
      vtkSmartPointer<vtkPVPythonOptions>::New();
    vtkInitializationHelper::Initialize( argc, argv, processType, options );
    if (options->GetTellVersion() || options->GetHelpSelected() ||
        options->GetPrintMonitors())
      {
      vtkInitializationHelper::Finalize();
      return 1;
      }

    if (processType == vtkProcessModule::PROCESS_BATCH &&
      options->GetPythonScriptName() == 0)
      {
      vtkGenericWarningMacro("No script specified. "
        "Please specify a batch script or use 'pvpython'.");
      vtkInitializationHelper::Finalize();
      return 1;
      }

    vtkProcessModule* pm = vtkProcessModule::GetProcessModule();

    int ret_val = 0;
    if (pm->GetSymmetricMPIMode() == false &&
      pm->GetPartitionId() > 0)
      {
      vtkIdType sid = vtkSMSession::ConnectToSelf();
      pm->GetGlobalController()->ProcessRMIs();
      pm->UnRegisterSession(sid);
      }
    else
      {
      int remaining_argc;
      char** remaining_argv;
      options->GetRemainingArguments(&remaining_argc, &remaining_argv);

      // Process arguments
      std::vector<char*> pythonArgs;
      ProcessArgsForPython(pythonArgs, options->GetPythonScriptName(),
        remaining_argc, remaining_argv);

      // register callback to initialize modules statically. The callback is
      // empty when BUILD_SHARED_LIBS is ON.
      vtkPVInitializePythonModules();

      // Start interpretor
      vtkPythonInterpreter::Initialize();

      ret_val = vtkPythonInterpreter::PyMain(static_cast<int>(pythonArgs.size()), &*pythonArgs.begin());

      // Free python args
      std::vector<char*>::iterator it = pythonArgs.begin();
      while(it != pythonArgs.end())
        {
        delete [] *it;
        ++it;
        }
      }
    // Exit application
    vtkInitializationHelper::Finalize();
    return ret_val;
    }
}