Skip to content

Commit

Permalink
Python API: Store list of MRtrix3 commands using cmake
Browse files Browse the repository at this point in the history
  • Loading branch information
Lestropie committed Mar 8, 2024
1 parent 08db1d6 commit 02b0a7e
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 15 deletions.
3 changes: 1 addition & 2 deletions cmake/FindVersion.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ if(NOT MRTRIX_VERSION)
message(STATUS "Failed to determine version from Git, using default base version: ${MRTRIX_BASE_VERSION}")
endif()


configure_file(
${SRC}
${DST}
@ONLY
)
)
11 changes: 11 additions & 0 deletions python/mrtrix3/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
set(PYTHON_VERSION_FILE ${CMAKE_CURRENT_SOURCE_DIR}/_version.py)
set(PYTHON_CMDLIST_FILE ${CMAKE_CURRENT_SOURCE_DIR}/_commands.py)

find_package(Git QUIET)

Expand Down Expand Up @@ -35,6 +36,16 @@ execute_process(
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)

# TODO 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
execute_process(
COMMAND ${CMAKE_COMMAND}
-D DST=${PYTHON_CMDLIST_FILE}
-D SRC=${CMAKE_CURRENT_SOURCE_DIR}/_commands.py.in
-P ${PROJECT_SOURCE_DIR}/cmake/FindCommands.cmake
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
)

add_custom_target(LinkPythonFiles ALL)
add_custom_command(
TARGET LinkPythonFiles
Expand Down
5 changes: 0 additions & 5 deletions python/mrtrix3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ class MRtrixError(MRtrixBaseError): #pylint: disable=unused-variable
COMMAND_HISTORY_STRING += ' (version=' + __version__ + ')'


# Location of binaries that belong to the same MRtrix3 installation as the Python library being invoked
BIN_PATH = os.path.abspath(os.path.join(os.path.abspath(os.path.dirname(os.path.abspath(__file__))), os.pardir, os.pardir, 'bin'))
# Must remove the '.exe' from Windows binary executables
EXE_LIST = [ os.path.splitext(name)[0] for name in os.listdir(BIN_PATH) ] #pylint: disable=unused-variable


# 'CONFIG' is a dictionary containing those entries present in the MRtrix config files
# Can add default values here that would otherwise appear in multiple locations
Expand Down
3 changes: 3 additions & 0 deletions python/mrtrix3/_commands.py.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pylint: disable=unused-variable
COMMAND_PATH = os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), os.pardir, os.pardir, 'bin'))
COMMAND_LIST = [@MRTRIX_COMMAND_LIST@]
2 changes: 1 addition & 1 deletion python/mrtrix3/_version.py.in
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = "@MRTRIX_VERSION@" #pylint: disable=unused-variable
__tag__ = "@MRTRIX_GIT_TAG@" #pylint: disable=unused-variable
__tag__ = "@MRTRIX_GIT_TAG@" #pylint: disable=unused-variable
15 changes: 8 additions & 7 deletions python/mrtrix3/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
# For more details, see http://www.mrtrix.org/.

import collections, itertools, os, shlex, shutil, signal, string, subprocess, sys, tempfile, threading
from mrtrix3 import ANSI, BIN_PATH, COMMAND_HISTORY_STRING, EXE_LIST, MRtrixBaseError, MRtrixError
from mrtrix3 import ANSI, COMMAND_HISTORY_STRING, MRtrixBaseError, MRtrixError
from mrtrix3._commands import COMMAND_PATH, COMMAND_LIST

IOStream = collections.namedtuple('IOStream', 'handle filename')

Expand Down Expand Up @@ -338,7 +339,7 @@ def quote_nonpipe(item):
cmdstack[-1].extend([ '-append_property', 'command_history', COMMAND_HISTORY_STRING ])

for line in cmdstack:
is_mrtrix_exe = line[0] in EXE_LIST
is_mrtrix_exe = line[0] in COMMAND_LIST
if is_mrtrix_exe:
line[0] = version_match(line[0])
if shared.get_num_threads() is not None:
Expand Down Expand Up @@ -526,9 +527,9 @@ def exe_name(item):
path = item
elif item.endswith('.exe'):
path = item
elif os.path.isfile(os.path.join(BIN_PATH, item)):
elif os.path.isfile(os.path.join(COMMAND_PATH, item)):
path = item
elif os.path.isfile(os.path.join(BIN_PATH, item + '.exe')):
elif os.path.isfile(os.path.join(COMMAND_PATH, item + '.exe')):
path = item + '.exe'
elif shutil.which(item) is not None:
path = item
Expand All @@ -549,10 +550,10 @@ def exe_name(item):
# which checks system32\ before PATH)
def version_match(item):
from mrtrix3 import app #pylint: disable=import-outside-toplevel
if not item in EXE_LIST:
app.debug('Command ' + item + ' not found in MRtrix3 bin/ directory')
if not item in COMMAND_LIST:
app.debug('Command ' + item + ' not a part of MRtrix3')
return item
exe_path_manual = os.path.join(BIN_PATH, exe_name(item))
exe_path_manual = os.path.join(COMMAND_PATH, exe_name(item))
if os.path.isfile(exe_path_manual):
app.debug('Version-matched executable for ' + item + ': ' + exe_path_manual)
return exe_path_manual
Expand Down

0 comments on commit 02b0a7e

Please sign in to comment.