Skip to content

Commit

Permalink
Cleanup cmake scripts for Python commands
Browse files Browse the repository at this point in the history
  • Loading branch information
daljit46 committed Jun 13, 2024
1 parent 9ff374b commit 648468f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 29 deletions.
13 changes: 3 additions & 10 deletions cmake/GenPythonCommandsLists.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,17 @@ file(
set(MRTRIX_CPP_COMMAND_LIST "")
foreach(CPP_COMMAND_FILE ${CPP_COMMAND_FILES})
get_filename_component(CPP_COMMAND_NAME ${CPP_COMMAND_FILE} NAME_WE)
if(MRTRIX_CPP_COMMAND_LIST STREQUAL "")
set(MRTRIX_CPP_COMMAND_LIST "\"${CPP_COMMAND_NAME}\"")
else()
set(MRTRIX_CPP_COMMAND_LIST "${MRTRIX_CPP_COMMAND_LIST},\n \"${CPP_COMMAND_NAME}\"")
endif()
list(APPEND MRTRIX_CPP_COMMAND_LIST ${CPP_COMMAND_NAME})
endforeach()

set(MRTRIX_PYTHON_COMMAND_LIST "")
foreach(PYTHON_ROOT_ENTRY ${PYTHON_ROOT_ENTRIES})
get_filename_component(PYTHON_COMMAND_NAME ${PYTHON_ROOT_ENTRY} NAME_WE)
if(NOT ${PYTHON_COMMAND_NAME} STREQUAL "CMakeLists" AND NOT ${PYTHON_COMMAND_NAME} STREQUAL "__init__")
if(MRTRIX_PYTHON_COMMAND_LIST STREQUAL "")
set(MRTRIX_PYTHON_COMMAND_LIST "\"${PYTHON_COMMAND_NAME}\"")
else()
set(MRTRIX_PYTHON_COMMAND_LIST "${MRTRIX_PYTHON_COMMAND_LIST},\n \"${PYTHON_COMMAND_NAME}\"")
endif()
list(APPEND MRTRIX_PYTHON_COMMAND_LIST ${PYTHON_COMMAND_NAME})
endif()
endforeach()

message(VERBOSE "Completed GenPythonCommandsList() function")
message(VERBOSE "Formatted list of MRtrix3 C++ commands: ${MRTRIX_CPP_COMMAND_LIST}")
message(VERBOSE "Formatted list of MRtrix3 Python commands: ${MRTRIX_PYTHON_COMMAND_LIST}")
Expand Down
48 changes: 29 additions & 19 deletions cmake/MakePythonExecutable.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,49 @@
# a short Python executable that is used to run a Python command from the terminal
# Receives name of the command as ${CMDNAME}; output build directory as ${BUILDDIR}
set(BINPATH "${BUILDDIR}/temporary/python/${CMDNAME}")
file(WRITE ${BINPATH} "#!/usr/bin/python3\n")
file(APPEND ${BINPATH} "# -*- coding: utf-8 -*-\n")
file(APPEND ${BINPATH} "\n")
file(APPEND ${BINPATH} "import importlib\n")
file(APPEND ${BINPATH} "import os\n")
file(APPEND ${BINPATH} "import sys\n")
file(APPEND ${BINPATH} "\n")
file(APPEND ${BINPATH} "mrtrix_lib_path = os.path.normpath(os.path.join(os.path.dirname(os.path.realpath(__file__)), os.pardir, 'lib'))\n")
file(APPEND ${BINPATH} "sys.path.insert(0, mrtrix_lib_path)\n")
file(APPEND ${BINPATH} "from mrtrix3.app import _execute\n")

set(BINPATH_CONTENTS
"#!/usr/bin/python3\n"
"# -*- coding: utf-8 -*-\n"
"\n"
"import importlib\n"
"import os\n"
"import sys\n"
"\n"
"mrtrix_lib_path = os.path.normpath(os.path.join(os.path.dirname(os.path.realpath(__file__)), os.pardir, 'lib'))\n"
"sys.path.insert(0, mrtrix_lib_path)\n"
"from mrtrix3.app import _execute\n"
)

# Three possible interfaces:
# 1. Standalone file residing in commands/
# 2. File stored in location commands/<cmdname>/<cmdname>.py, which will contain usage() and execute() functions
# 3. Two files stored at commands/<cmdname>/usage.py and commands/<cmdname>/execute.py, defining the two corresponding functions
# TODO Port population_template to type 3; both for readability and to ensure that it works
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${CMDNAME}/__init__.py")
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${CMDNAME}/usage.py" AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${CMDNAME}/execute.py")
file(APPEND ${BINPATH} "module_usage = importlib.import_module('.usage', 'mrtrix3.commands.${CMDNAME}')\n")
file(APPEND ${BINPATH} "module_execute = importlib.import_module('.execute', 'mrtrix3.commands.${CMDNAME}')\n")
file(APPEND ${BINPATH} "_execute(module_usage.usage, module_execute.execute)\n")
string(APPEND BINPATH_CONTENTS
"module_usage = importlib.import_module('.usage', 'mrtrix3.commands.${CMDNAME}')\n"
"module_execute = importlib.import_module('.execute', 'mrtrix3.commands.${CMDNAME}')\n"
"_execute(module_usage.usage, module_execute.execute)\n"
)
elseif(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${CMDNAME}/${CMDNAME}.py")
file(APPEND ${BINPATH} "module = importlib.import_module('.${CMDNAME}', 'mrtrix3.commands.${CMDNAME}')\n")
file(APPEND ${BINPATH} "_execute(module.usage, module.execute)\n")
string(APPEND BINPATH_CONTENTS
"module = importlib.import_module('.${CMDNAME}', 'mrtrix3.commands.${CMDNAME}')\n"
"_execute(module.usage, module.execute)\n"
)
else()
message(FATAL_ERROR "Malformed filesystem structure for Python command ${CMDNAME}")
endif()
elseif(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${CMDNAME}.py")
file(APPEND ${BINPATH} "module = importlib.import_module('.${CMDNAME}', 'mrtrix3.commands')\n")
file(APPEND ${BINPATH} "_execute(module.usage, module.execute)\n")
string(APPEND BINPATH_CONTENTS
"module = importlib.import_module('${CMDNAME}')\n"
"_execute(module.usage, module.execute)\n"
)
else()
message(FATAL_ERROR "Malformed filesystem structure for Python command ${CMDNAME}")
endif()
file(COPY ${BINPATH} DESTINATION ${BUILDDIR}/bin

file(WRITE ${BUILDDIR}/bin/${CMDNAME} ${BINPATH_CONTENTS}
FILE_PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ GROUP_EXECUTE GROUP_READ WORLD_EXECUTE WORLD_READ
)

0 comments on commit 648468f

Please sign in to comment.