ParaView
vtkProcessModuleInitializePython.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ParaView
4  Module: $RCSfile$
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 #ifndef vtkProcessModulePythonInitializePython_h
16 #define vtkProcessModulePythonInitializePython_h
17 
18 #include "vtkPSystemTools.h"
19 #include "vtkPVConfig.h" // needed for PARAVIEW_FREEZE_PYTHON
20 #include "vtkPythonInterpreter.h"
21 #include <string>
22 #include <vtksys/SystemTools.hxx>
23 
24 /* The maximum length of a file name. */
25 #if defined(PATH_MAX)
26 #define VTK_PYTHON_MAXPATH PATH_MAX
27 #elif defined(MAXPATHLEN)
28 #define VTK_PYTHON_MAXPATH MAXPATHLEN
29 #else
30 #define VTK_PYTHON_MAXPATH 16384
31 #endif
32 namespace
33 {
34 // ParaView should setup Python Path to find the modules in the following
35 // locations for various platforms. There are always two cases to handle: when
36 // running from build-location and when running from installed-location.
37 
38 //----------------------------------------------------------------------------
39 void vtkPythonAppInitPrependPythonPath(const std::string& dir)
40 {
41  if (dir != "")
42  {
43  std::string collapsed_dir = vtkPSystemTools::CollapseFullPath(dir.c_str());
44  if (vtkPSystemTools::FileIsDirectory(collapsed_dir.c_str()))
45  {
46  vtkPythonInterpreter::PrependPythonPath(collapsed_dir.c_str());
47  }
48  }
49 }
50 
51 #ifndef PARAVIEW_FREEZE_PYTHON
52 #if defined(_WIN32)
53 void vtkPythonAppInitPrependPathWindows(const std::string& SELF_DIR);
54 #elif defined(__APPLE__)
55 void vtkPythonAppInitPrependPathOsX(const std::string& SELF_DIR);
56 #else
57 void vtkPythonAppInitPrependPathLinux(const std::string& SELF_DIR);
58 #endif
59 #endif // ifndef PARAVIEW_FREEZE_PYTHON
60 
61 //----------------------------------------------------------------------------
62 void vtkPythonAppInitPrependPath(const std::string& SELF_DIR)
63 {
64 // We don't initialize Python paths when Frozen Python is being used. This
65 // avoid unnecessary file-system accesses..
66 #ifndef PARAVIEW_FREEZE_PYTHON
67 #if defined(_WIN32)
68  vtkPythonAppInitPrependPathWindows(SELF_DIR);
69 #elif defined(__APPLE__)
70  vtkPythonAppInitPrependPathOsX(SELF_DIR);
71 #else
72  vtkPythonAppInitPrependPathLinux(SELF_DIR);
73 #endif
74 #endif // ifndef PARAVIEW_FREEZE_PYTHON
75 
76 // *** The following maybe obsolete. Need to verify and remove. ***
77 
78 // This executable does not actually link to the python wrapper
79 // libraries, though it probably should now that the stub-modules
80 // are separated from them. Since it does not we have to make
81 // sure the wrapper libraries can be found by the dynamic loader
82 // when the stub-modules are loaded. On UNIX this executable must
83 // be running in an environment where the main VTK libraries (to
84 // which this executable does link) have been found, so the
85 // wrapper libraries will also be found. On Windows this
86 // executable may have simply found its .dll files next to itself
87 // so the wrapper libraries may not be found when the wrapper
88 // modules are loaded. Solve this problem by adding this
89 // executable's location to the system PATH variable. Note that
90 // this need only be done for an installed VTK because in the
91 // build tree the wrapper modules are in the same directory as the
92 // wrapper libraries.
93 #if defined(_WIN32)
94  static char system_path[(VTK_PYTHON_MAXPATH + 1) * 10] = "PATH=";
95  strcat(system_path, SELF_DIR.c_str());
96  if (char* oldpath = getenv("PATH"))
97  {
98  strcat(system_path, ";");
99  strcat(system_path, oldpath);
100  }
101  putenv(system_path);
102 #endif // if defined(_WIN32)
103 }
104 
105 #ifndef PARAVIEW_FREEZE_PYTHON
106 #if defined(_WIN32)
107 //===========================================================================
108 // Windows
109 // Key:
110 // - SELF_DIR: directory containing the pvserver/pvpython/paraview
111 // executables.
112 //---------------------------------------------------------------------------
113 // + BUILD_LOCATION
114 // + ParaView C/C++ library location
115 // - SELF_DIR
116 // + ParaView Python modules
117 // - SELF_DIR/../lib/site-packages (when CMAKE_INTDIR is not defined).
118 // OR
119 // - SELF_DIR/../../lib/site-packages (when CMAKE_INTDIR is defined).
120 // + INSTALL_LOCATION
121 // + ParaView C/C++ library location
122 // - SELF_DIR
123 // - SELF_DIR/../lib/paraview-<major>.<minor>/
124 // + ParaView Python modules
125 // - SELF_DIR/Lib
126 // - SELF_DIR/Lib/site-packages
127 // + VTK Python Module libraries
128 // - SELF_DIR/../lib/paraview-<major>.<minor>/site-packages/vtk
129 //===========================================================================
130 void vtkPythonAppInitPrependPathWindows(const std::string& SELF_DIR)
131 {
132  // For use in MS VS IDE builds we need to account for selection of
133  // build configuration in the IDE. CMAKE_INTDIR is how we know which
134  // configuration user has selected. It will be one of Debug, Release,
135  // ... etc. With in VS builds SELF_DIR will be set like
136  // "builddir/bin/CMAKE_INTDIR". PYHTONPATH should have SELF_DIR and
137  // "builddir/lib/CMAKE_INTDIR"
138  std::string build_dir_site_packages;
139 #if defined(CMAKE_INTDIR)
140  build_dir_site_packages = SELF_DIR + "/../../lib/site-packages";
141 #else
142  build_dir_site_packages = SELF_DIR + "/../lib/site-packages";
143 #endif
144  bool is_build_dir = vtkPSystemTools::FileExists(build_dir_site_packages.c_str());
145  if (is_build_dir)
146  {
147  vtkPythonAppInitPrependPythonPath(SELF_DIR);
148 #if defined(CMAKE_INTDIR)
149  vtkPythonAppInitPrependPythonPath(SELF_DIR + "/../../lib/" + std::string(CMAKE_INTDIR));
150 #else
151  vtkPythonAppInitPrependPythonPath(SELF_DIR + "/../lib");
152 #endif
153  vtkPythonAppInitPrependPythonPath(build_dir_site_packages);
154  }
155  else
156  {
157  vtkPythonAppInitPrependPythonPath(SELF_DIR);
158  vtkPythonAppInitPrependPythonPath(SELF_DIR + "Lib");
159  vtkPythonAppInitPrependPythonPath(SELF_DIR + "Lib/site-packages");
160  vtkPythonAppInitPrependPythonPath(SELF_DIR + "/../lib/paraview-" PARAVIEW_VERSION);
161  vtkPythonAppInitPrependPythonPath(
162  SELF_DIR + "/../lib/paraview-" PARAVIEW_VERSION "/site-packages");
163  // BUG #14263 happened with windows installed versions too. This addresses
164  // that problem.
165  vtkPythonAppInitPrependPythonPath(
166  SELF_DIR + "/../lib/paraview-" PARAVIEW_VERSION "/site-packages/vtk");
167  }
168 }
169 #elif defined(__APPLE__)
170 //===========================================================================
171 // OsX
172 // Key:
173 // - SELF_DIR: directory containing the pvserver/pvpython/paraview
174 // executables. This is different for app and non-app executables.
175 //---------------------------------------------------------------------------
176 // + BUILD_LOCATION
177 // - LIB_DIR
178 // - SELF_DIR/../lib (when executable is not APP, e.g pvpython)
179 // OR
180 // - SELF_DIR/../../../../lib (when executable is APP, e.g. paraview)
181 // + ParaView C/C++ library location
182 // - LIB_DIR
183 // + ParaView Python modules
184 // - LIB_DIR/site-packages
185 // + INSTALL_LOCATION (APP)
186 // - APP_ROOT
187 // - SELF_DIR/../.. (this is same for paraview and pvpython)
188 // - ParaView C/C++ library location
189 // - APP_ROOT/Contents/Libraries/
190 // - ParaView Python modules
191 // - APP_ROOT/Contents/Python
192 // + INSTALL_LOCATION (UNIX STYLE)
193 // + SELF_DIR is "bin"
194 // + ParaView C/C++ library location
195 // - SELF_DIR/../lib/paraview-<major>.<minor>
196 // + ParaView Python modules
197 // - SELF_DIR/../lib/paraview-<major>.<minor>/site-packages
198 // + VTK Python Module libraries
199 // - SELF_DIR/../lib/paraview-<major>.<minor>/site-packages/vtk
200 //===========================================================================
201 void vtkPythonAppInitPrependPathOsX(const std::string& SELF_DIR)
202 {
203  bool is_app = false;
204  {
205  // if SELF_DIR/../ is named "Contents", we are in an App.
206  std::string contents_dir = vtkPSystemTools::CollapseFullPath((SELF_DIR + "/..").c_str());
207  is_app = (vtksys::SystemTools::GetFilenameName(contents_dir) == "Contents");
208  }
209 
210  std::string lib_dir =
211  (is_app == false) ? (SELF_DIR + "/../lib") : (SELF_DIR + "/../../../../lib");
212  lib_dir = vtkPSystemTools::CollapseFullPath(lib_dir.c_str());
213 
214  std::string cmakeconfig = (is_app == false) ? (SELF_DIR + "/../ParaViewConfig.cmake")
215  : (SELF_DIR + "/../../../../ParaViewConfig.cmake");
216  cmakeconfig = vtkPSystemTools::CollapseFullPath(cmakeconfig.c_str());
217 
218  bool is_build_dir = vtkPSystemTools::FileExists(cmakeconfig.c_str());
219 
220  // when we install on OsX using unix-style the test for is_build_dir is
221  // valid for install dir too. So we do an extra check.
222  bool is_unix_style_install =
223  vtkPSystemTools::FileExists((lib_dir + "/paraview-" PARAVIEW_VERSION).c_str());
224  if (is_build_dir)
225  {
226  if (is_unix_style_install)
227  {
228  lib_dir = lib_dir + "/paraview-" PARAVIEW_VERSION;
229  vtkPythonAppInitPrependPythonPath(lib_dir);
230  vtkPythonAppInitPrependPythonPath(lib_dir + "/site-packages");
231  // site-packages/vtk needs to be added so the Python wrapped VTK modules
232  // can be loaded from paraview e.g. import vtkCommonCorePython can work
233  // (BUG #14263).
234  vtkPythonAppInitPrependPythonPath(lib_dir + "/site-packages/vtk");
235  }
236  else // App bundle in build dir
237  {
238  vtkPythonAppInitPrependPythonPath(lib_dir);
239  vtkPythonAppInitPrependPythonPath(lib_dir + "/site-packages");
240  }
241  }
242  else
243  {
244  if (is_app)
245  {
246  std::string app_root = SELF_DIR + "/../..";
247  app_root = vtkPSystemTools::CollapseFullPath(app_root.c_str());
248  vtkPythonAppInitPrependPythonPath(app_root + "/Contents/Libraries");
249  vtkPythonAppInitPrependPythonPath(app_root + "/Contents/Python");
250  }
251  else
252  {
253  vtkGenericWarningMacro("Non-app bundle in install directory not supported");
254  }
255  }
256 }
257 #else
258 void vtkPythonAppInitPrependPathLinux(const std::string& SELF_DIR);
259 //===========================================================================
260 // Linux/UNIX (not OsX)
261 // Key:
262 // - SELF_DIR: directory containing the pvserver/pvpython/paraview
263 // executables. For installed locations, this corresponds to the "real"
264 // executable, not the shared-forwarded executable (if applicable).
265 //---------------------------------------------------------------------------
266 // + BUILD_LOCATION
267 // + ParaView C/C++ library location
268 // - SELF_DIR/../lib
269 // + ParaView Python modules
270 // - SELF_DIR/../lib/site-packages
271 // + INSTALL_LOCATION (shared builds with shared forwarding)
272 // + ParaView C/C++ library location
273 // - SELF_DIR
274 // + ParaView Python modules
275 // - SELF_DIR/site-packages
276 // + VTK Python Module libraries
277 // - SELF_DIR/site-packages/vtk
278 // + INSTALL_LOCATION (static builds)
279 // + ParaView C/C++ library location
280 // - (not applicable)
281 // + ParaView Python modules
282 // - SELF_DIR/../lib/paraview-<version>/site-packages
283 // + VTK Python Module libraries
284 // - SELF_DIR/../lib/paraview-<version>/site-packages/vtk
285 void vtkPythonAppInitPrependPathLinux(const std::string& SELF_DIR)
286 {
287  // Determine if running from build or install dir.
288  // If SELF_DIR/../ParaViewConfig.cmake, it must be running from the build
289  // directory.
290  bool is_build_dir = vtkPSystemTools::FileExists((SELF_DIR + "/../ParaViewConfig.cmake").c_str());
291  if (is_build_dir)
292  {
293  vtkPythonAppInitPrependPythonPath(SELF_DIR + "/../lib");
294  vtkPythonAppInitPrependPythonPath(SELF_DIR + "/../lib/site-packages");
295  return;
296  }
297 
298  // We're running from installed directory. We could be either a shared build
299  // or a static build.
300  bool using_shared_libs = false;
301 #ifdef BUILD_SHARED_LIBS
302  using_shared_libs = true;
303 #endif
304  if (using_shared_libs)
305  {
306  vtkPythonAppInitPrependPythonPath(SELF_DIR);
307  vtkPythonAppInitPrependPythonPath(SELF_DIR + "/site-packages");
308  // site-packages/vtk needs to be added so the Python wrapped VTK modules
309  // can be loaded from paraview e.g. import vtkCommonCorePython can work
310  // (BUG #14263).
311  vtkPythonAppInitPrependPythonPath(SELF_DIR + "/site-packages/vtk");
312  }
313  else
314  {
315  vtkPythonAppInitPrependPythonPath(
316  SELF_DIR + "/../lib/paraview-" PARAVIEW_VERSION "/site-packages");
317  vtkPythonAppInitPrependPythonPath(
318  SELF_DIR + "/../lib/paraview-" PARAVIEW_VERSION "/site-packages/vtk");
319  }
320 }
321 //===========================================================================
322 #endif
323 #endif // ifndef PARAVIEW_FREEZE_PYTHON
324 }
325 
326 #endif
327 // VTK-HeaderTest-Exclude: vtkProcessModuleInitializePython.h
#define VTK_PYTHON_MAXPATH