Skip to content

Commit

Permalink
Perform a check on the Python version on startup. Refs #1592
Browse files Browse the repository at this point in the history
Now only Python versions with matching major/minor numbers will be able
to import mantid.
  • Loading branch information
martyngigg committed May 20, 2013
1 parent 332e58f commit 7f52556
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Code/Mantid/Framework/PythonInterface/mantid/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ set ( PY_FILES

copy_python_files_to_dir ( "${PY_FILES}" ${CMAKE_CURRENT_SOURCE_DIR} ${OUTPUT_DIR}
PYTHON_PKGROOT_INSTALL_FILES )
# Generate the version module
set ( VERSION_PY pyversion )
# Build version
configure_file ( ${CMAKE_CURRENT_SOURCE_DIR}/${VERSION_PY}.py.in ${CMAKE_CURRENT_BINARY_DIR}/${VERSION_PY}.py )
# Copy py to build directory, taking care of multi-config (MSVC) builds
copy_python_files_to_dir ( ${VERSION_PY}.py ${CMAKE_CURRENT_BINARY_DIR} ${OUTPUT_DIR}
PYTHON_PKGROOT_INSTALL_FILES )
# Package version
configure_file ( ${CMAKE_CURRENT_SOURCE_DIR}/${VERSION_PY}.py.in ${CMAKE_CURRENT_BINARY_DIR}/${VERSION_PY}.install.py )

if ( MSVC )
# On windows copy over the python wrapper to the build directory (hang it off kernel build)
Expand Down Expand Up @@ -102,3 +111,7 @@ set_property ( TARGET PythonInterface PROPERTY FOLDER "MantidFramework/Python" )

# Pure Python files
install ( FILES ${PY_FILES} DESTINATION ${BIN_DIR}/mantid )
# version.py that will overwrite the ones from the built target
install ( FILES ${CMAKE_CURRENT_BINARY_DIR}/${VERSION_PY}.install.py DESTINATION
${BIN_DIR}/mantid RENAME ${VERSION_PY}.py )

4 changes: 4 additions & 0 deletions Code/Mantid/Framework/PythonInterface/mantid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
Implementing Algorithms, Virtual Instrument Geometry.
"""
###############################################################################
# Check the current Python version is correct
###############################################################################
import pyversion

###############################################################################
# Define the api version
Expand Down
19 changes: 19 additions & 0 deletions Code/Mantid/Framework/PythonInterface/mantid/pyversion.py.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""
Checks that the running version of Python is compatible with the version of
Python that Mantid was built with.
"""
import sys

# Define the target major.minor version (i.e. the one mantid was built against)
TARGET_VERSION="@PYTHON_VERSION_MAJOR@.@PYTHON_VERSION_MINOR@"

_vers_info = sys.version_info
_running_vers = "%d.%d" % (_vers_info[0],_vers_info[1])
if _running_vers != TARGET_VERSION:
message = \
"""Python version mismatch, cannot continue.
Mantid was built against version '%s' but you are running version '%s'. These versions
must match.
"""
raise ImportError(message % (TARGET_VERSION, _running_vers))

0 comments on commit 7f52556

Please sign in to comment.