Skip to content

Commit

Permalink
Change/extend inputs for MakePythonExecutable
Browse files Browse the repository at this point in the history
- Instead of specifying a BUILDDIR, the caller must specify an OUTPUT_DIR directory where the output file will be written.
- The script now additionally accepts a list of directories that will be prepended to the system path in the generated script. This shouldn't be necessary for most commands.
  • Loading branch information
daljit46 committed Jun 20, 2024
1 parent 70fea8d commit 6f2948a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
20 changes: 15 additions & 5 deletions cmake/MakePythonExecutable.cmake
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Creates within the bin/ sub-directory of the project build directory
# 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}")
# Creates a short Python executable that is used to run a Python command from the terminal.
# Inputs:
# - CMDNAME: Name of the command
# - OUTPUT_DIR: Directory in which to create the executable
# - EXTRA_PATH_DIRS: an optional list of directories to be prepended to the Python path (shouldn't be necessary for most commands).

set(BINPATH_CONTENTS
"#!/usr/bin/python3\n"
Expand All @@ -13,9 +14,18 @@ set(BINPATH_CONTENTS
"\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"
)

if(EXTRA_PATH_DIRS)
foreach(PATH_DIR ${EXTRA_PATH_DIRS})
string(APPEND BINPATH_CONTENTS
"sys.path.insert(0, '${PATH_DIR}')\n"
)
endforeach()
endif()

string(APPEND BINPATH_CONTENTS "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
Expand Down
3 changes: 1 addition & 2 deletions python/mrtrix3/commands/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,11 @@ foreach(CMDNAME ${PYTHON_COMMAND_LIST})
add_custom_command(
TARGET MakePythonExecutables
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMAND ${CMAKE_COMMAND} -DCMDNAME=${CMDNAME} -DBUILDDIR=${PROJECT_BINARY_DIR} -P ${PROJECT_SOURCE_DIR}/cmake/MakePythonExecutable.cmake
COMMAND ${CMAKE_COMMAND} -DCMDNAME=${CMDNAME} -DOUTPUT_DIR="${PROJECT_BINARY_DIR}/bin" -P ${PROJECT_SOURCE_DIR}/cmake/MakePythonExecutable.cmake
)
list(APPEND PYTHON_BIN_FILES ${PROJECT_BINARY_DIR}/bin/${CMDNAME})
endforeach()


# We need to generate a list of MRtrix3 commands:
# function run.command() does different things if it is executing an MRtrix3 command vs. an external command,
# but unlike prior software versions we cannot simply interrogate the contents of the bin/ directory at runtime
Expand Down

0 comments on commit 6f2948a

Please sign in to comment.