From 7ec82bdd71d4609d3b9c1c8e27080bbfa4257ead Mon Sep 17 00:00:00 2001
From: Marcel Loose <loose@astron.nl>
Date: Wed, 23 Dec 2009 10:43:07 +0000
Subject: [PATCH] Bug 1310: Fixed Python issues -- i.e. MWImager Python test
 scripts that couldn't find the right Python modules -- with quite some
 duck-taping. This solution doesn't win the "Beautiful Code Contest", but is
 solves the problems, at least temporarily. Need to think of a more permanent
 solution, though.

---
 CEP/Imager/MWImager/CMakeLists.txt      |  2 +-
 CEP/Imager/MWImager/src/mwimager        |  6 ------
 CEP/Imager/MWImager/src/mwimager-part   |  4 ++--
 CEP/Imager/MWImager/test/CMakeLists.txt | 21 ++++++++++++++++++++-
 CMake/FindPython.cmake                  | 10 ++++++----
 CMake/LofarMacros.cmake                 |  4 +++-
 CMake/PythonInstall.cmake               | 20 ++++++++++++++++----
 LCS/pyparameterset/src/CMakeLists.txt   | 11 +++++++----
 autoconf_share/runctest.sh.in           |  6 ++++++
 9 files changed, 61 insertions(+), 23 deletions(-)

diff --git a/CEP/Imager/MWImager/CMakeLists.txt b/CEP/Imager/MWImager/CMakeLists.txt
index 474b0e7e656..61a4bbfb01e 100644
--- a/CEP/Imager/MWImager/CMakeLists.txt
+++ b/CEP/Imager/MWImager/CMakeLists.txt
@@ -1,6 +1,6 @@
 # $Id$
 
-lofar_package(MWImager 0.1 DEPENDS Common MWCommon)
+lofar_package(MWImager 0.1 DEPENDS Common MWCommon pyparameterset)
 
 lofar_find_package(Casacore COMPONENTS images REQUIRED)
 
diff --git a/CEP/Imager/MWImager/src/mwimager b/CEP/Imager/MWImager/src/mwimager
index 7c5c22a6eec..9673aebd15f 100755
--- a/CEP/Imager/MWImager/src/mwimager
+++ b/CEP/Imager/MWImager/src/mwimager
@@ -29,12 +29,6 @@
 pgmpath=`dirname $0`
 pgmpath=`cd $pgmpath > /dev/null 2>&1  &&  pwd`
 
-# Check if LOFARROOT is set.
-if test "$LOFARROOT" = ""; then
-  echo "LOFARROOT is undefined; source lofarinit.(c)sh first"
-  exit 1
-fi
-
 # Handle possible options.
 imager=cimager
 casaopt=none
diff --git a/CEP/Imager/MWImager/src/mwimager-part b/CEP/Imager/MWImager/src/mwimager-part
index 5d67b757465..91d2c33a823 100755
--- a/CEP/Imager/MWImager/src/mwimager-part
+++ b/CEP/Imager/MWImager/src/mwimager-part
@@ -72,9 +72,9 @@ if test -d "$vds"  -a  -e "$vds/table.dat"; then
 fi
 
 if test "$img" = "casa"; then
-  $pgmpath/mwimager-casa $psn $seqnr $msn $vds $dry
+  $pgmpath/mwimager-casa $psn $seqnr $msn $vds $dry 2>/dev/null || exit 1
 else
-  $pgmpath/mwimager-askap $psn $seqnr $msn $vds $dry
+  $pgmpath/mwimager-askap $psn $seqnr $msn $vds $dry 2>/dev/null || exit 1
 fi
 
 # Convert to FITS.
diff --git a/CEP/Imager/MWImager/test/CMakeLists.txt b/CEP/Imager/MWImager/test/CMakeLists.txt
index 1603186c1fb..e0f70ad71b4 100644
--- a/CEP/Imager/MWImager/test/CMakeLists.txt
+++ b/CEP/Imager/MWImager/test/CMakeLists.txt
@@ -2,6 +2,25 @@
 
 include(LofarCTest)
 
+# Create symbolic links to scripts used by the tmakems test program.
+get_target_property(_loc getparsetvalue LOCATION)
+execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink
+  ${_loc}
+  ${CMAKE_CURRENT_BINARY_DIR}/getparsetvalue)
+get_target_property(_loc finddproc LOCATION)
+execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink
+  ${_loc}
+  ${CMAKE_CURRENT_BINARY_DIR}/finddproc)
+execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink
+  ${MWCommon_SOURCE_DIR}/src/startdistproc
+  ${CMAKE_CURRENT_BINARY_DIR}/startdistproc)
+execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink
+  ${MWCommon_SOURCE_DIR}/src/socketrun
+  ${CMAKE_CURRENT_BINARY_DIR}/socketrun)
+execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink
+  ${CMAKE_CURRENT_BINARY_DIR}/../src/convertimagerparset
+  ${CMAKE_CURRENT_BINARY_DIR}/convertimagerparset)
+
 lofar_add_test(tconvertimagerparset DEPENDS convertimagerparset)
 lofar_add_test(tmwimager DEPENDS convertimagerparset)
-lofar_add_test(tmwimager-dd DEPENDS convertimagerparset)
+#lofar_add_test(tmwimager-dd DEPENDS convertimagerparset <AND MORE>)
diff --git a/CMake/FindPython.cmake b/CMake/FindPython.cmake
index 63bed4219d0..283ae5b2ba1 100644
--- a/CMake/FindPython.cmake
+++ b/CMake/FindPython.cmake
@@ -7,8 +7,8 @@
 #  PYTHON_FOUND        - system has Python interpreter, Python headers
 #                        files and libraries
 #  PYTHON_INCLUDE_DIRS - path to the Python header files
-#  PYTHON_INSTALL_DIR  - installation directory for Python packages
-#                        (cached)
+#  PYTHON_BUILD_DIR    - build directory for Python packages (cached)
+#  PYTHON_INSTALL_DIR  - installation directory for Python packages (cached)
 
 # Copyright (C) 2009
 # ASTRON (Netherlands Institute for Radio Astronomy)
@@ -47,7 +47,7 @@ if(NOT DEFINED PYTHON_INSTALL_DIR)
   if(PYTHON_EXECUTABLE)
     set(_cmd
       "from distutils.sysconfig import *"
-      "print get_python_lib(prefix='${CMAKE_INSTALL_PREFIX}')")
+      "print get_python_lib(prefix='')")
     execute_process(
       COMMAND "${PYTHON_EXECUTABLE}" "-c" "${_cmd}"
       OUTPUT_VARIABLE _pydir
@@ -56,7 +56,9 @@ if(NOT DEFINED PYTHON_INSTALL_DIR)
     if(_err)
       message(FATAL_ERROR "Python command failed:\n${_err}")
     endif(_err)
-    set(PYTHON_INSTALL_DIR "${_pydir}" CACHE PATH 
+    set(PYTHON_BUILD_DIR "${CMAKE_BINARY_DIR}/${_pydir}" CACHE PATH 
+      "Python site-packages build directory")
+    set(PYTHON_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/${_pydir}" CACHE PATH 
       "Python site-packages installation directory")
   endif(PYTHON_EXECUTABLE)
 endif(NOT DEFINED PYTHON_INSTALL_DIR)
diff --git a/CMake/LofarMacros.cmake b/CMake/LofarMacros.cmake
index a45cbddda38..54ce9d0f8fe 100644
--- a/CMake/LofarMacros.cmake
+++ b/CMake/LofarMacros.cmake
@@ -146,7 +146,9 @@ if(NOT DEFINED LOFAR_MACROS_INCLUDED)
   # --------------------------------------------------------------------------
   macro(lofar_add_library _name)
     add_library(${_name} ${ARGN})
-    set_property(GLOBAL APPEND PROPERTY ${PACKAGE_NAME}_LIBRARIES ${_name})
+    if(NOT "${ARGN}" MATCHES "^MODULE")
+      set_property(GLOBAL APPEND PROPERTY ${PACKAGE_NAME}_LIBRARIES ${_name})
+    endif(NOT "${ARGN}" MATCHES "^MODULE")
     set(_link_libs)
     foreach(_dep ${${PACKAGE_NAME}_DEPENDENCIES})
       get_property(_dep_libs GLOBAL PROPERTY ${_dep}_LIBRARIES)
diff --git a/CMake/PythonInstall.cmake b/CMake/PythonInstall.cmake
index 222e9d9a0d6..a9b67415cd0 100644
--- a/CMake/PythonInstall.cmake
+++ b/CMake/PythonInstall.cmake
@@ -50,16 +50,28 @@ macro(python_install)
   endif(_dest_dir MATCHES "^$" OR _dest_dir MATCHES ";")
 
   # Set python package install directory.
-  set(_dest_dir "${PYTHON_INSTALL_DIR}/${_dest_dir}")
+  set(_inst_dir "${PYTHON_INSTALL_DIR}/${_dest_dir}")
+  set(_build_dir "${PYTHON_BUILD_DIR}/${_dest_dir}")
+
+  # Make sure that there's a __init__.py file in each directory
+  string(REGEX REPLACE "/" ";" _dir_list ${_dest_dir})
+  set(_init_dir "${PYTHON_BUILD_DIR}")
+  foreach(_dir ${_dir_list})
+    set(_init_dir "${_init_dir}/${_dir}")
+    execute_process(COMMAND 
+      ${CMAKE_COMMAND} -E touch "${_init_dir}/__init__.py")
+  endforeach(_dir ${_dir_list})
 
   # Install and byte-compile each Python file.
   foreach(_py ${_py_files})
-    install(FILES ${_py} DESTINATION ${_dest_dir})
+    get_filename_component(_src_dir ${_py} ABSOLUTE)
+    configure_file(${_src_dir} ${_build_dir}/${_py} COPYONLY)
+    install(FILES ${_py} DESTINATION ${_inst_dir})
     get_filename_component(_py ${_py} NAME)
     set(_py_code
       "import py_compile"
-      "print '-- Byte-compiling: ${_dest_dir}/${_py}'"
-      "py_compile.compile('${_dest_dir}/${_py}')")
+      "print '-- Byte-compiling: ${_inst_dir}/${_py}'"
+      "py_compile.compile('${_inst_dir}/${_py}')")
     install(CODE 
       "execute_process(COMMAND ${PYTHON_EXECUTABLE} -c \"${_py_code}\")")
   endforeach(_py ${_py_files})
diff --git a/LCS/pyparameterset/src/CMakeLists.txt b/LCS/pyparameterset/src/CMakeLists.txt
index a93fd2f9c0f..60592a42e65 100644
--- a/LCS/pyparameterset/src/CMakeLists.txt
+++ b/LCS/pyparameterset/src/CMakeLists.txt
@@ -7,8 +7,10 @@ execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink
   ${CMAKE_CURRENT_SOURCE_DIR}
   ${CMAKE_BINARY_DIR}/include/${PACKAGE_NAME})
 
-lofar_add_library(_pyparameterset SHARED pyparameterset.cc)
-set_target_properties(_pyparameterset PROPERTIES PREFIX "")
+lofar_add_library(_pyparameterset MODULE pyparameterset.cc)
+set_target_properties(_pyparameterset PROPERTIES 
+  PREFIX ""
+  LIBRARY_OUTPUT_DIRECTORY ${PYTHON_BUILD_DIR}/lofar/parameterset)
 
 # This is a quick-and-dirty fix to install the Python binding module in the
 # right place. It will now be installed twice, because lofar_add_library()
@@ -16,9 +18,10 @@ set_target_properties(_pyparameterset PROPERTIES PREFIX "")
 install(TARGETS _pyparameterset 
   DESTINATION ${PYTHON_INSTALL_DIR}/lofar/parameterset)
 
+lofar_add_library(lofar_pyparameterset Package__Version.cc)
+
 lofar_add_bin_program(versionpyparameterset 
-  versionpyparameterset.cc
-  Package__Version.cc)
+  versionpyparameterset.cc)
 
 # Install Python modules
 include(PythonInstall)
diff --git a/autoconf_share/runctest.sh.in b/autoconf_share/runctest.sh.in
index 3e05316ce98..95bce74b17e 100755
--- a/autoconf_share/runctest.sh.in
+++ b/autoconf_share/runctest.sh.in
@@ -27,6 +27,12 @@ lofar_sharedir=@lofar_sharedir@; export lofar_sharedir
 prefix=@prefix@; export prefix
 srcdir=@srcdir@; export srcdir
 
+# When using CMake, add the Python build directory to PYTHONPATH.
+if ! echo "@PYTHON_BUILD_DIR@" | grep "[@]PYTHON_BUILD_DIR[@]" > /dev/null
+then
+  PYTHONPATH=@PYTHON_BUILD_DIR@; export PYTHONPATH
+fi
+
 # Start autoconf_share/runtest.sh
 $lofar_sharedir/runtest.sh "$@"
 
-- 
GitLab