From f76438144d99e1afb729c8c055e9d259cfd8d25f Mon Sep 17 00:00:00 2001 From: Martyn Gigg <martyn.gigg@stfc.ac.uk> Date: Thu, 25 Oct 2012 15:31:53 +0100 Subject: [PATCH] Add module to set NEXUSLIB when starting mantid... Refs #6007 so that Mantid users can "import nxs" if without hassle. The module is autogenerated as CMake "knows" the locations of the NeXus library. --- Code/Mantid/Build/CMake/MantidUtils.cmake | 1 + .../mantid/kernel/CMakeLists.txt | 41 ++++++++++++++++--- .../PythonInterface/mantid/kernel/__init__.py | 11 ++++- .../PythonInterface/mantid/kernel/dlopen.py | 22 +++++----- .../mantid/kernel/nexuslib.py.in | 23 +++++++++++ 5 files changed, 82 insertions(+), 16 deletions(-) create mode 100644 Code/Mantid/Framework/PythonInterface/mantid/kernel/nexuslib.py.in diff --git a/Code/Mantid/Build/CMake/MantidUtils.cmake b/Code/Mantid/Build/CMake/MantidUtils.cmake index 666b5ba2ad5..16bb99ff8c9 100644 --- a/Code/Mantid/Build/CMake/MantidUtils.cmake +++ b/Code/Mantid/Build/CMake/MantidUtils.cmake @@ -72,6 +72,7 @@ endfunction( ADD_COMPILE_PY_TARGET ) # - INSTALLED_FILES :: An output variable containing the list of copied # files including their full paths function( COPY_PYTHON_FILES_TO_DIR PY_FILES SRC_DIR DEST_DIR INSTALLED_FILES ) + set ( COPIED_FILES ${${INSTALLED_FILES}} ) foreach ( PYFILE ${PY_FILES} ) get_filename_component( _basefilename ${PYFILE} NAME_WE ) set( _py_src ${SRC_DIR}/${PYFILE} ) diff --git a/Code/Mantid/Framework/PythonInterface/mantid/kernel/CMakeLists.txt b/Code/Mantid/Framework/PythonInterface/mantid/kernel/CMakeLists.txt index 445d99575a9..58cce7ae488 100644 --- a/Code/Mantid/Framework/PythonInterface/mantid/kernel/CMakeLists.txt +++ b/Code/Mantid/Framework/PythonInterface/mantid/kernel/CMakeLists.txt @@ -94,6 +94,7 @@ set ( PY_FILES __init__.py _aliases.py dlopen.py + environment.py funcreturns.py plugins.py ) @@ -105,11 +106,9 @@ create_module ( ${MODULE_TEMPLATE} ${CMAKE_CURRENT_BINARY_DIR}/kernel.cpp EXPORT SRC_FILES ) ############################################################################################# -# Copy over the pure Python files for the module +# Add rule to copy over the pure Python files for the module ############################################################################################# -# Set the destination directory set ( OUTPUT_DIR ${PYTHON_PKG_ROOT}/kernel ) - copy_python_files_to_dir ( "${PY_FILES}" ${CMAKE_CURRENT_SOURCE_DIR} ${OUTPUT_DIR} PYTHON_INSTALL_FILES ) @@ -137,12 +136,44 @@ endif() add_dependencies( PythonKernelModule _dlopen ) endif() +############################################################################################# +# Generate the nexuslib module for the build +############################################################################################# +set ( NEXUSLIB_PY nexuslib ) +set ( NEXUSLIB ${NEXUS_C_LIBRARIES} ) +if ( WIN32 ) #.lib -> .dll + string ( REPLACE ".lib" ".dll" NEXUSLIB ${NEXUSLIB} ) +endif() + +# Build version +configure_file ( ${CMAKE_CURRENT_SOURCE_DIR}/${NEXUSLIB_PY}.py.in ${CMAKE_CURRENT_BINARY_DIR}/${NEXUSLIB_PY}.py ) +if ( WIN32 OR APPLE ) + # NeXus library is in the same place relative to the Python library + get_filename_component ( NEXUSLIB_FILE ${NEXUSLIB} NAME ) + set ( NEXUSLIB ../../${NEXUSLIB_FILE} ) +endif() +# Install version +configure_file ( ${CMAKE_CURRENT_SOURCE_DIR}/${NEXUSLIB_PY}.py.in ${CMAKE_CURRENT_BINARY_DIR}/${NEXUSLIB_PY}.install.py ) + +# Copy over in post-build so that it is not automatically included for installation. We need +# to use a different file for install, which is handled below +add_custom_command ( TARGET PythonKernelModule POST_BUILD + COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different + ${CMAKE_CURRENT_BINARY_DIR}/${NEXUSLIB_PY}.py ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}/mantid/kernel + COMMAND ${PYTHON_EXECUTABLE} ARGS -m compileall ${CMAKE_CURRENT_BINARY_DIR}/${NEXUSLIB_PY}.install.py ) ########################################################################### # Installation settings ########################################################################### -install ( TARGETS PythonKernelModule ${SYSTEM_PACKAGE_TARGET} DESTINATION ${BIN_DIR}/mantid/kernel ) +install ( TARGETS PythonKernelModule ${SYSTEM_PACKAGE_TARGET} DESTINATION + ${BIN_DIR}/mantid/kernel ) if ( UNIX ) -install ( TARGETS _dlopen DESTINATION ${BIN_DIR}/mantid/kernel ) + install ( TARGETS _dlopen DESTINATION ${BIN_DIR}/mantid/kernel ) endif() +# nexuslib.py & nexuslib.pyc +install ( FILES ${CMAKE_CURRENT_BINARY_DIR}/${NEXUSLIB_PY}.install.py DESTINATION + ${BIN_DIR}/mantid/kernel RENAME ${NEXUSLIB_PY}.py ) +# .pyc file +install ( FILES ${CMAKE_CURRENT_BINARY_DIR}/${NEXUSLIB_PY}.install.pyc DESTINATION + ${BIN_DIR}/mantid/kernel RENAME ${NEXUSLIB_PY}.pyc ) \ No newline at end of file diff --git a/Code/Mantid/Framework/PythonInterface/mantid/kernel/__init__.py b/Code/Mantid/Framework/PythonInterface/mantid/kernel/__init__.py index 023e9d4a7b7..c2e9724e4f1 100644 --- a/Code/Mantid/Framework/PythonInterface/mantid/kernel/__init__.py +++ b/Code/Mantid/Framework/PythonInterface/mantid/kernel/__init__.py @@ -14,10 +14,19 @@ import os as _os clib = _os.path.join(_os.path.dirname(__file__), '_kernel.so') flags = _dlopen.setup_dlopen(clib, ['libMantidKernel']) from _kernel import * -dlopen.restore_flags(flags) +_dlopen.restore_flags(flags) ############################################################################### +############################################################################### +# Set the path to the NeXus C library. It is required by the nxs package so +# make it a little bit easier for our users +############################################################################### +import nexuslib as _nexuslib +_nexuslib.set_NEXUSLIB_var() + ############################################################################### # Make modules available in this namespace ############################################################################### +import environment +import funcreturns from _aliases import * diff --git a/Code/Mantid/Framework/PythonInterface/mantid/kernel/dlopen.py b/Code/Mantid/Framework/PythonInterface/mantid/kernel/dlopen.py index 0a238526e45..56ade6d59a2 100644 --- a/Code/Mantid/Framework/PythonInterface/mantid/kernel/dlopen.py +++ b/Code/Mantid/Framework/PythonInterface/mantid/kernel/dlopen.py @@ -7,20 +7,20 @@ For Mantid this ensures the singleton symbols are wired up correctly """ import sys import os -import platform +import environment ####################################################################### # Ensure the correct Mantid shared libaries are found when loading the # python shared libraries ####################################################################### -if sys.platform.startswith('win32'): +if environment.is_windows(): _var = 'PATH' _sep = ';' else: _sep = ':' - if sys.platform.startswith('linux'): + if environment.is_linux(): _var = 'LD_LIBRARY_PATH' - elif sys.platform.startswith('darwin'): + elif environment.is_mac(): _var = 'DYLD_LIBRARY_PATH' else: # Give it a go how it is _var = '' @@ -54,7 +54,8 @@ def setup_dlopen(library, depends=[]): Returns the original flags """ - if os.name == 'nt': return None + if environment.is_windows(): + return None old_flags = sys.getdlopenflags() import _dlopen @@ -64,7 +65,7 @@ def setup_dlopen(library, depends=[]): _bin = os.path.join(os.path.abspath(os.path.dirname(__file__)), '../../') def get_libpath(mainlib, dependency): - if platform.system() == 'Linux': + if environment.is_linux(): cmd = 'ldd %s | grep %s' % (mainlib, dependency) subp = subprocess.Popen(cmd,stdout=subprocess.PIPE, stderr=subprocess.STDOUT,shell=True) @@ -77,14 +78,14 @@ def setup_dlopen(library, depends=[]): return libpath library_var = "LD_LIBRARY_PATH" - if platform.system() == 'Darwin': + if environment.is_mac(): library_var = 'DY' + library_var ldpath = os.environ.get(library_var, "") ldpath += ":" + _bin os.environ[library_var] = ldpath pythonlib = library - if platform.system() == "Linux": + if environment.is_linux(): # stdc++ has to be loaded first or exceptions don't get translated # properly across bounadries # NeXus has to be loaded as well as there seems to be an issue with @@ -97,7 +98,7 @@ def setup_dlopen(library, depends=[]): dlloader(get_libpath(pythonlib, dep)) oldflags = sys.getdlopenflags() - if platform.system() == "Darwin": + if environment.is_mac(): try: import dl RTLD_LOCAL = dl.RTLD_LOCAL @@ -113,5 +114,6 @@ def restore_flags(flags): """Restores the dlopen flags to those provided, usually with the results from a call to setup_dlopen """ - if flags is None: return + if flags is None: + return sys.setdlopenflags(flags) diff --git a/Code/Mantid/Framework/PythonInterface/mantid/kernel/nexuslib.py.in b/Code/Mantid/Framework/PythonInterface/mantid/kernel/nexuslib.py.in new file mode 100644 index 00000000000..98499f3b195 --- /dev/null +++ b/Code/Mantid/Framework/PythonInterface/mantid/kernel/nexuslib.py.in @@ -0,0 +1,23 @@ +""" + ===== AUTO-GENERATED MODULE ==== + + Defines a function that sets the NEXUSLIB environment variable required + by the nxs package. The variable value is set by CMake +""" + +def set_NEXUSLIB_var(): + """ + Sets the NEXUSLIB environment variable to point to the NeXus shared library + """ + import os as _os + _nexuslib = r"@NEXUSLIB@" + if _nexuslib == "": + return + + if not _os.path.isabs(_nexuslib): + # Assume relative to this file + thisdir = _os.path.dirname(__file__) + _nexuslib = _os.path.normpath(_os.path.join(thisdir, _nexuslib)) + + # Add to current environment + _os.environ['NEXUSLIB'] = _nexuslib -- GitLab