From 36c9fa348dba5bb37874c2213d0a73e2ab1a0043 Mon Sep 17 00:00:00 2001
From: Ben Boeckel <ben.boeckel@kitware.com>
Date: Tue, 2 Jan 2018 17:16:43 -0500
Subject: [PATCH] Wrapping/Python: simplify installation of Python files

---
 CMakeLists.txt                              |   7 +-
 Wrapping/Python/CMakeLists.txt              | 150 ++++++++++----------
 Wrapping/Python/vtkmodules/all.py.in        |   2 +-
 Wrapping/Python/vtkmodules/kit_module.py.in |  15 --
 Wrapping/Python/vtkmodules/module.py.in     |   9 --
 5 files changed, 80 insertions(+), 103 deletions(-)
 delete mode 100644 Wrapping/Python/vtkmodules/kit_module.py.in
 delete mode 100644 Wrapping/Python/vtkmodules/module.py.in

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 5ee9b90ec69..5091ee5b5b3 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -208,6 +208,12 @@ if (vtk_required_modules OR vtk_unrecognized_modules)
     "${vtk_required_modules};${vtk_unrecognized_modules}.")
 endif ()
 
+list(FIND vtk_modules "VTK::Python" vtk_python_idx)
+if (NOT vtk_python_idx EQUAL "-1")
+  include(vtkModuleWrapPython)
+  vtk_module_python_default_destination(VTK_PYTHON_SITE_PACKAGES_SUFFIX)
+endif ()
+
 option(VTK_ENABLE_KITS "Enable kits compilation" OFF)
 mark_as_advanced(VTK_ENABLE_KITS)
 
@@ -242,7 +248,6 @@ vtk_module_graphviz(
 ExternalData_Add_Target(VTKData)
 
 if (VTK_WRAP_PYTHON)
-  include(vtkModuleWrapPython)
   vtk_module_wrap_python(
     MODULES         ${vtk_modules}
     INSTALL_EXPORT  VTK
diff --git a/Wrapping/Python/CMakeLists.txt b/Wrapping/Python/CMakeLists.txt
index 1dbe18f5d7c..dd38f68eb88 100644
--- a/Wrapping/Python/CMakeLists.txt
+++ b/Wrapping/Python/CMakeLists.txt
@@ -43,83 +43,79 @@ if (TARGET VTK::ParallelMPI)
   endif ()
 endif ()
 
-if (FALSE)
-# "Configure" files that need to be configured, including generation of the *.py
-# files for each of the enabled VTK modules.
-# We don't directly configure to `VTK_BUILD_PYTHON_MODULES_DIR` since of VS generator, the
-# `VTK_BUILD_PYTHON_MODULES_DIR` depends on build configuration not known at configure time.
-# So we configure into a temporary location and then copy those files over to
-# `VTK_BUILD_PYTHON_MODULES_DIR` using a custom_command.
+set(_vtk_python_imports)
+foreach (_vtk_python_module IN LISTS vtk_python_wrapped_modules)
+  get_property(_vtk_python_library_name
+    TARGET    "${_vtk_python_module}"
+    PROPERTY  "INTERFACE_vtk_module_library_name")
+  string(APPEND _vtk_python_imports
+    "from .${_vtk_python_library_name} import *\n")
+endforeach ()
 
-# Wrapping/Python/vtk/*.py
-unset(VTK_PYTHON_IMPORT_ALL)
-unset(configured_py_files)
-foreach(module IN LISTS VTK_PYTHON_MODULES_AND_KITS)
-  set(VTK_PYTHON_IMPORT_ALL
-    "${VTK_PYTHON_IMPORT_ALL}from .${module} import *\n")
-  configure_file(vtkmodules/module.py.in
-    "${CMAKE_CURRENT_BINARY_DIR}/vtkmodules/${module}.py" @ONLY)
-  list(APPEND configured_py_files "${CMAKE_CURRENT_BINARY_DIR}/vtkmodules/${module}.py")
-endforeach()
-configure_file(vtkmodules/all.py.in
-  "${CMAKE_CURRENT_BINARY_DIR}/vtkmodules/all.py" @ONLY)
-list(APPEND configured_py_files "${CMAKE_CURRENT_BINARY_DIR}/vtkmodules/all.py")
+configure_file(
+  "${CMAKE_CURRENT_SOURCE_DIR}/vtkmodules/all.py.in"
+  "${CMAKE_BINARY_DIR}/${VTK_PYTHON_SITE_PACKAGES_SUFFIX}/vtkmodules/all.py"
+  @ONLY)
+install(
+  FILES       "${CMAKE_BINARY_DIR}/${VTK_PYTHON_SITE_PACKAGES_SUFFIX}/vtkmodules/all.py"
+  DESTINATION "${VTK_PYTHON_SITE_PACKAGES_SUFFIX}/vtkmodules")
 
-# Kit module adapters
-foreach(kit IN LISTS vtk_kits)
-  set(_module_kit ${kit}${VTK_KIT_SUFFIX})
-  foreach(dep IN LISTS _${kit}_modules)
-    configure_file(vtkmodules/kit_module.py.in
-      "${CMAKE_CURRENT_BINARY_DIR}/vtkmodules/${dep}.py" @ONLY)
-    list(APPEND configured_py_files "${CMAKE_CURRENT_BINARY_DIR}/vtkmodules/${dep}.py")
-  endforeach()
-  unset(_module_kit)
-endforeach()
+set(python_files
+  vtk.py
+  vtkmodules/__init__.py
+  vtkmodules/gtk/GtkGLExtVTKRenderWindow.py
+  vtkmodules/gtk/GtkGLExtVTKRenderWindowInteractor.py
+  vtkmodules/gtk/GtkVTKRenderWindow.py
+  vtkmodules/gtk/GtkVTKRenderWindowInteractor.py
+  vtkmodules/gtk/__init__.py
+  vtkmodules/numpy_interface/__init__.py
+  vtkmodules/numpy_interface/algorithms.py
+  vtkmodules/numpy_interface/dataset_adapter.py
+  vtkmodules/numpy_interface/internal_algorithms.py
+  vtkmodules/qt/QVTKRenderWindowInteractor.py
+  vtkmodules/qt/__init__.py
+  vtkmodules/test/BlackBox.py
+  vtkmodules/test/Testing.py
+  vtkmodules/test/__init__.py
+  vtkmodules/tk/__init__.py
+  vtkmodules/tk/vtkLoadPythonTkWidgets.py
+  vtkmodules/tk/vtkTkImageViewerWidget.py
+  vtkmodules/tk/vtkTkPhotoImage.py
+  vtkmodules/tk/vtkTkRenderWidget.py
+  vtkmodules/tk/vtkTkRenderWindowInteractor.py
+  vtkmodules/util/__init__.py
+  vtkmodules/util/colors.py
+  vtkmodules/util/keys.py
+  vtkmodules/util/misc.py
+  vtkmodules/util/numpy_support.py
+  vtkmodules/util/vtkAlgorithm.py
+  vtkmodules/util/vtkConstants.py
+  vtkmodules/util/vtkImageExportToArray.py
+  vtkmodules/util/vtkImageImportFromArray.py
+  vtkmodules/util/vtkMethodParser.py
+  vtkmodules/util/vtkVariant.py
+  vtkmodules/wx/wxVTKRenderWindow.py
+  vtkmodules/wx/wxVTKRenderWindowInteractor.py)
 
-# Now copy configured files to build-cfg specific dir.
-add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/copy-complete"
-  COMMAND ${CMAKE_COMMAND}
-          -E copy_directory "${CMAKE_CURRENT_BINARY_DIR}/vtkmodules" "${VTK_BUILD_PYTHON_MODULES_DIR}/vtkmodules"
-  COMMAND ${CMAKE_COMMAND}
-          -E touch "${CMAKE_CURRENT_BINARY_DIR}/copy-complete"
-  DEPENDS ${configured_py_files})
-
-
-# Build all py files that form the `vtk` package.
-vtk_python_package(vtkpython_pyc vtkmodules DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/copy-complete")
-vtk_python_module(vtk_python_module vtk.py)
-
-# Let's add a target that generates the `vtk` python package
-# including all the platform dependent and independent modules.
-set(module_Python_libs)
-foreach(module IN LISTS VTK_PYTHON_MODULES_AND_KITS)
-  list(APPEND module_Python_libs ${module}Python)
-endforeach()
-
-add_custom_target(vtk_python_package)
-add_dependencies(vtk_python_package vtkpython_pyc ${module_Python_libs})
-unset(module_Python_libs)
-
-if(TARGET vtkpython)
-  add_dependencies(vtkpython vtk_python_package)
-endif()
-if(TARGET pvtkpython)
-  add_dependencies(pvtkpython vtk_python_package)
-endif()
-
-# If no runtime is to be installed then do not install python modules.
-if(NOT VTK_INSTALL_NO_RUNTIME)
-  # Install the conveniently configured python interpretters
-  if(NOT VTK_INSTALL_NO_PYTHON_EXES AND VTK_ENABLE_VTKPYTHON)
-    # Install the vtkpython executable
-    install(TARGETS vtkpython
-      DESTINATION ${VTK_INSTALL_RUNTIME_DIR})
-
-    if(PVTKPYTHON_EXECUTABLE)
-      # Install the mpi enabled vtkpython executable
-      install(TARGETS pvtkpython
-        DESTINATION ${VTK_INSTALL_RUNTIME_DIR})
-    endif()
-  endif()
-endif()
-endif ()
+set(python_copied_modules)
+foreach (python_file IN LISTS python_files)
+  set(output_python_file
+    "${CMAKE_BINARY_DIR}/${VTK_PYTHON_SITE_PACKAGES_SUFFIX}/${python_file}")
+  add_custom_command(
+    OUTPUT  "${output_python_file}"
+    DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/${python_file}"
+    COMMAND "${CMAKE_COMMAND}" -E copy_if_different
+            "${CMAKE_CURRENT_SOURCE_DIR}/${python_file}"
+            "${output_python_file}"
+    COMMENT "Copying ${python_file} to the binary directory")
+  get_filename_component(python_file_directory "${python_file}" DIRECTORY)
+  install(
+    FILES       "${python_file}"
+    DESTINATION "${VTK_PYTHON_SITE_PACKAGES_SUFFIX}/${python_file_directory}"
+    COMPONENT   "python")
+  list(APPEND python_copied_modules
+    "${output_python_file}")
+endforeach ()
+add_custom_target(vtk_python_copy ALL
+  DEPENDS
+    ${python_copied_modules})
diff --git a/Wrapping/Python/vtkmodules/all.py.in b/Wrapping/Python/vtkmodules/all.py.in
index 3498bef03b5..fafd728233d 100644
--- a/Wrapping/Python/vtkmodules/all.py.in
+++ b/Wrapping/Python/vtkmodules/all.py.in
@@ -4,7 +4,7 @@ also allows one to use specific packages inside the vtk directory.."""
 from __future__ import absolute_import
 
 # --------------------------------------
-@VTK_PYTHON_IMPORT_ALL@# --------------------------------------
+@_vtk_python_imports@
 
 # useful macro for getting type names
 from .util.vtkConstants import vtkImageScalarTypeNameMacro
diff --git a/Wrapping/Python/vtkmodules/kit_module.py.in b/Wrapping/Python/vtkmodules/kit_module.py.in
deleted file mode 100644
index 1ae93aba41f..00000000000
--- a/Wrapping/Python/vtkmodules/kit_module.py.in
+++ /dev/null
@@ -1,15 +0,0 @@
-"""
-This module is an adapter for scripts that explicitly import from named
-submodules as opposed to from the top-level vtk module. This is necessary
-because the specific submodules might not exist when VTK_ENABLE_KITS is enabled.
-"""
-
-from __future__ import absolute_import
-
-try:
-    # use relative import for installed modules
-    from .@_module_kit@Python import *
-except ImportError:
-    # during build and testing, the modules will be elsewhere,
-    # e.g. in lib directory or Release/Debug config directories
-    from @_module_kit@Python import *
diff --git a/Wrapping/Python/vtkmodules/module.py.in b/Wrapping/Python/vtkmodules/module.py.in
deleted file mode 100644
index 140087ccf55..00000000000
--- a/Wrapping/Python/vtkmodules/module.py.in
+++ /dev/null
@@ -1,9 +0,0 @@
-from __future__ import absolute_import
-
-try:
-    # use relative import for installed modules
-    from .@module@Python import *
-except ImportError:
-    # during build and testing, the modules will be elsewhere,
-    # e.g. in lib directory or Release/Debug config directories
-    from @module@Python import *
-- 
GitLab