[segyio] 30/376: Better handling of Python install prefix and module checking

Jørgen Kvalsvik jokva-guest at moszumanska.debian.org
Wed Sep 20 08:04:03 UTC 2017


This is an automated email from the git hooks/post-receive script.

jokva-guest pushed a commit to branch debian
in repository segyio.

commit 7e38d90819ca567bb8aeae73bbb8b70f6fd4e905
Author: Jean-Paul Balabanian <jepebe at users.noreply.github.com>
Date:   Tue Oct 11 11:29:51 2016 +0200

    Better handling of Python install prefix and module checking
---
 CMakeLists.txt                    |  3 ++-
 cmake/find_python_module.cmake    | 27 +++++++++++++++++++++++
 cmake/python.cmake                | 45 +++++++++++++++------------------------
 cmake/python_module_version.cmake | 40 ++++++++++++++++++++++++++++++++++
 cmake/segyio_testing.cmake        | 29 +++++++++++++++++++++++++
 python/CMakeLists.txt             | 15 ++++++++++---
 6 files changed, 127 insertions(+), 32 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index a6a53e7..0c09501 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -17,7 +17,7 @@ endif ()
 option(BUILD_MEX    "Build Matlab mex files"    OFF)
 option(BUILD_PYTHON "Build Python wrappers"     ON)
 
-include(cmake/python.cmake)
+include(cmake/segyio_testing.cmake)
 enable_testing()
 
 set(CMAKE_C_FLAGS "-std=c99 ${CMAKE_C_FLAGS}")
@@ -71,6 +71,7 @@ else (BUILD_MEX)
 endif ()
 
 if (BUILD_PYTHON)
+    include(cmake/python.cmake)
     add_subdirectory(python)
 endif ()
 
diff --git a/cmake/find_python_module.cmake b/cmake/find_python_module.cmake
new file mode 100644
index 0000000..aa48322
--- /dev/null
+++ b/cmake/find_python_module.cmake
@@ -0,0 +1,27 @@
+# Found from: github user ivansafrin
+#
+# Find if a Python module is installed
+# Found at http://www.cmake.org/pipermail/cmake/2011-January/041666.html
+# To use do: find_python_module(PyQt4 REQUIRED)
+function(find_python_module module)
+    string(TOUPPER ${module} module_upper)
+    if(NOT PY_${module_upper})
+        if(ARGC GREATER 1 AND ARGV1 STREQUAL "REQUIRED")
+            set(${module}_FIND_REQUIRED TRUE)
+        endif()
+        # A module's location is usually a directory, but for binary modules
+        # it's a .so file.
+        execute_process(COMMAND "${PYTHON_EXECUTABLE}" "-c"
+                "import re, ${module}; print(re.compile('/__init__.py.*').sub('',${module}.__file__))"
+                RESULT_VARIABLE _${module}_status
+                OUTPUT_VARIABLE _${module}_location
+                ERROR_QUIET
+                OUTPUT_STRIP_TRAILING_WHITESPACE)
+
+        if(NOT _${module}_status)
+            set(PY_${module_upper} ${_${module}_location} CACHE STRING
+                    "Location of Python module ${module}")
+        endif()
+    endif(NOT PY_${module_upper})
+    find_package_handle_standard_args(PY_${module} DEFAULT_MSG PY_${module_upper})
+endfunction(find_python_module)
\ No newline at end of file
diff --git a/cmake/python.cmake b/cmake/python.cmake
index 42dcf0f..ed9f01c 100644
--- a/cmake/python.cmake
+++ b/cmake/python.cmake
@@ -1,19 +1,17 @@
+include(cmake/find_python_module.cmake)
+include(cmake/python_module_version.cmake)
+
+find_package(PythonInterp)
+find_package(PythonLibs REQUIRED)
+
 configure_file(cmake/test_runner.py tests/test_runner.py COPYONLY)
 
-if(WINDOWS)
-    set(SEP "\\;")
-else() # e.g. Linux
-    set(SEP ":")
+if (EXISTS "/etc/debian_version")
+    set( PYTHON_PACKAGE_PATH "dist-packages")
+else()
+    set( PYTHON_PACKAGE_PATH "site-packages")
 endif()
-
-function(add_memcheck_test NAME BINARY)
-    # Valgrind on MacOS is experimental
-    if(LINUX AND (${CMAKE_BUILD_TYPE} MATCHES "DEBUG"))
-        set(memcheck_command "valgrind --trace-children=yes --leak-check=full --error-exitcode=31415")
-        separate_arguments(memcheck_command)
-        add_test(memcheck_${NAME} ${memcheck_command} ./${BINARY})
-    endif()
-endfunction(add_memcheck_test)
+set(PYTHON_INSTALL_PREFIX "lib/python${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}/${PYTHON_PACKAGE_PATH}" CACHE STRING "Subdirectory to install Python modules in")
 
 function(add_python_package PACKAGE_NAME PACKAGE_PATH PYTHON_FILES)
     add_custom_target(package_${PACKAGE_NAME} ALL)
@@ -24,7 +22,7 @@ function(add_python_package PACKAGE_NAME PACKAGE_PATH PYTHON_FILES)
                 COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/${file} ${CMAKE_BINARY_DIR}/python/${PACKAGE_PATH}
                 )
     endforeach ()
-    install(FILES ${PYTHON_FILES} DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/python2.7/site-packages/${PACKAGE_PATH})
+    install(FILES ${PYTHON_FILES} DESTINATION ${CMAKE_INSTALL_PREFIX}/${PYTHON_INSTALL_PREFIX}/${PACKAGE_PATH})
 endfunction()
 
 function(add_python_test TESTNAME PYTHON_TEST_FILE)
@@ -34,9 +32,9 @@ function(add_python_test TESTNAME PYTHON_TEST_FILE)
             WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/tests
             COMMAND python test_runner.py ${PYTHON_TEST_FILE}
             )
-    set_tests_properties(${TESTNAME} PROPERTIES ENVIRONMENT
-                        "PYTHONPATH=${CMAKE_BINARY_DIR}/python${SEP}$ENV{PYTHONPATH}"
-                        )
+
+    to_path_list(pythonpath "${CMAKE_BINARY_DIR}/python" "$ENV{PYTHONPATH}")
+    set_tests_properties(${TESTNAME} PROPERTIES ENVIRONMENT "PYTHONPATH=${pythonpath}")
 endfunction()
 
 function(add_python_example TESTNAME PYTHON_TEST_FILE)
@@ -46,15 +44,6 @@ function(add_python_example TESTNAME PYTHON_TEST_FILE)
             WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/examples
             COMMAND python ${PYTHON_TEST_FILE} ${ARGN}
             )
-    set_tests_properties(${TESTNAME} PROPERTIES ENVIRONMENT
-                        "PYTHONPATH=${CMAKE_BINARY_DIR}/python${SEP}$ENV{PYTHONPATH}"
-                        )
-endfunction()
-
-function(add_segyio_test TESTNAME TEST_SOURCES)
-    add_executable(test_${TESTNAME} unittest.h "${TEST_SOURCES}")
-    target_link_libraries(test_${TESTNAME} segyio-static m)
-    add_dependencies(test_${TESTNAME} segyio-static)
-    add_test(NAME ${TESTNAME} COMMAND ${EXECUTABLE_OUTPUT_PATH}/test_${TESTNAME})
-    add_memcheck_test(${TESTNAME} ${EXECUTABLE_OUTPUT_PATH}/test_${TESTNAME})
+    to_path_list(pythonpath "${CMAKE_BINARY_DIR}/python" "$ENV{PYTHONPATH}")
+    set_tests_properties(${TESTNAME} PROPERTIES ENVIRONMENT "PYTHONPATH=${pythonpath}")
 endfunction()
diff --git a/cmake/python_module_version.cmake b/cmake/python_module_version.cmake
new file mode 100644
index 0000000..5d67d8f
--- /dev/null
+++ b/cmake/python_module_version.cmake
@@ -0,0 +1,40 @@
+# try import python module, if success, check its version, store as PY_module.
+# the module is imported as-is, hence the case (e.g. PyQt4) must be correct.
+function(python_module_version module)
+    set(PY_VERSION_ACCESSOR "__version__")
+    set(PY_module_name ${module})
+
+    if(${module} MATCHES "PyQt4")
+        set(PY_module_name "PyQt4.Qt")
+        set(PY_VERSION_ACCESSOR "PYQT_VERSION_STR")
+    endif()
+
+    execute_process(COMMAND "${PYTHON_EXECUTABLE}" "-c" "import ${PY_module_name} as py_m; print(py_m.${PY_VERSION_ACCESSOR})"
+            RESULT_VARIABLE _${module}_fail#    error code 0 if success
+            OUTPUT_VARIABLE _${module}_version# major.minor.patch
+            ERROR_VARIABLE stderr_output
+            OUTPUT_STRIP_TRAILING_WHITESPACE)
+
+    if(NOT _${module}_fail)
+        set(PY_${module} ${_${module}_version})# local scope, for message
+        set(PY_${module} ${_${module}_version} PARENT_SCOPE)
+    endif()
+endfunction()
+
+
+# If we find the correct module and new enough version, set PY_package, where
+# "package" is the given argument to the version we found else, display warning
+# and do not set any variables.
+function(python_module package version)
+    python_module_version(${package})
+
+    if(NOT DEFINED PY_${package})
+        message("Could not find Python module " ${package})
+    elseif(${PY_${package}} VERSION_LESS ${version})
+        message(WARNING "Python module ${package} too old.  "
+                "Wanted ${version}, found ${PY_${package}}")
+    else()
+        message(STATUS "Found ${package}.  ${PY_${package}} >= ${version}")
+        set(PY_${package} ${version} PARENT_SCOPE)
+    endif()
+endfunction()
\ No newline at end of file
diff --git a/cmake/segyio_testing.cmake b/cmake/segyio_testing.cmake
new file mode 100644
index 0000000..5b6f42c
--- /dev/null
+++ b/cmake/segyio_testing.cmake
@@ -0,0 +1,29 @@
+function(to_path_list var path1)
+    if("${CMAKE_HOST_SYSTEM}" MATCHES ".*Windows.*")
+        set(sep "\\;")
+    else()
+        set(sep ":")
+    endif()
+    set(result "${path1}") # First element doesn't require separator at all...
+    foreach(path ${ARGN})
+        set(result "${result}${sep}${path}") # .. but other elements do.
+    endforeach()
+    set(${var} "${result}" PARENT_SCOPE)
+endfunction()
+
+function(add_memcheck_test NAME BINARY)
+    # Valgrind on MacOS is experimental
+    if(LINUX AND (${CMAKE_BUILD_TYPE} MATCHES "DEBUG"))
+        set(memcheck_command "valgrind --trace-children=yes --leak-check=full --error-exitcode=31415")
+        separate_arguments(memcheck_command)
+        add_test(memcheck_${NAME} ${memcheck_command} ./${BINARY})
+    endif()
+endfunction(add_memcheck_test)
+
+function(add_segyio_test TESTNAME TEST_SOURCES)
+    add_executable(test_${TESTNAME} unittest.h "${TEST_SOURCES}")
+    target_link_libraries(test_${TESTNAME} segyio-static m)
+    add_dependencies(test_${TESTNAME} segyio-static)
+    add_test(NAME ${TESTNAME} COMMAND ${EXECUTABLE_OUTPUT_PATH}/test_${TESTNAME})
+    add_memcheck_test(${TESTNAME} ${EXECUTABLE_OUTPUT_PATH}/test_${TESTNAME})
+endfunction()
diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt
index fa50f0a..ff713d1 100644
--- a/python/CMakeLists.txt
+++ b/python/CMakeLists.txt
@@ -1,5 +1,14 @@
-find_package(PythonInterp)
-find_package(PythonLibs REQUIRED)
+if (NOT DEFINED PYTHON_EXECUTABLE)
+    message("Python interpreter not found - Python wrappers not enabled")
+    return()
+endif()
+
+
+python_module(numpy 1.6)
+if (NOT DEFINED PY_numpy)
+    message("numpy module not found - Python wrappers not enabled")
+    return()
+endif()
 
 if (PYTHONLIBS_FOUND)
     include_directories(${PYTHON_INCLUDE_DIRS})
@@ -12,7 +21,7 @@ if (PYTHONLIBS_FOUND)
     endif ()
 
     set_target_properties(_segyio PROPERTIES PREFIX "" SUFFIX ".so")
-    install(TARGETS _segyio DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/python2.7/site-packages/segyio)
+    install(TARGETS _segyio DESTINATION ${CMAKE_INSTALL_PREFIX}/${PYTHON_INSTALL_PREFIX}/segyio)
 
     add_custom_command(TARGET _segyio POST_BUILD
             COMMAND ${CMAKE_COMMAND} -E copy

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/segyio.git



More information about the debian-science-commits mailing list