From 5e69f189e46b9df44be3290184e6ee1a4372a806 Mon Sep 17 00:00:00 2001 From: Robert Smith Date: Tue, 20 Aug 2024 13:27:57 +1000 Subject: [PATCH] Python: Remove one of three filesystem structures For commands where the entry in python/mrtrix3/commands/ is not a standalone .py file but a directory, previously there were two possible filesystem contents. One was for that sub-directory to contain a .py file with the same name as the command, as determined by the name of the python/commands/ sub-directory, and that file is expected to contain functions called uage() and execute(). The second is for such a file to be absent, but for there to instead exist two files called usage.py and execute.py, which would provide those relevant functions. This commit removes support for the latter. --- cmake/MakePythonExecutable.cmake | 17 +++++--------- .../population_template.py | 23 +++++++++++++++++++ 2 files changed, 29 insertions(+), 11 deletions(-) create mode 100644 python/mrtrix3/commands/population_template/population_template.py diff --git a/cmake/MakePythonExecutable.cmake b/cmake/MakePythonExecutable.cmake index d23e3ea792..1c32f81a1c 100644 --- a/cmake/MakePythonExecutable.cmake +++ b/cmake/MakePythonExecutable.cmake @@ -17,21 +17,13 @@ set(BINPATH_CONTENTS "\n" ) -# Three possible interfaces: +# Two possible interfaces: # 1. Standalone file residing in commands/ # 2. File stored in location commands//.py, which will contain usage() and execute() functions -# 3. Two files stored at commands//usage.py and commands//execute.py, defining the two corresponding functions 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") - 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") + if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${CMDNAME}/${CMDNAME}.py") 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}") @@ -39,11 +31,14 @@ if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${CMDNAME}/__init__.py") elseif(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${CMDNAME}.py") string(APPEND BINPATH_CONTENTS "module = importlib.import_module('.${CMDNAME}', 'mrtrix3.commands')\n" - "_execute(module.usage, module.execute)\n" ) else() message(FATAL_ERROR "Malformed filesystem structure for Python command ${CMDNAME}") endif() +string(APPEND BINPATH_CONTENTS + "_execute(module.usage, module.execute)\n" +) + if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.19) file(WRITE ${OUTPUT_DIR}/${CMDNAME} ${BINPATH_CONTENTS}) diff --git a/python/mrtrix3/commands/population_template/population_template.py b/python/mrtrix3/commands/population_template/population_template.py new file mode 100644 index 0000000000..85d487efe8 --- /dev/null +++ b/python/mrtrix3/commands/population_template/population_template.py @@ -0,0 +1,23 @@ +# Copyright (c) 2008-2024 the MRtrix3 contributors. +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +# +# Covered Software is provided under this License on an "as is" +# basis, without warranty of any kind, either expressed, implied, or +# statutory, including, without limitation, warranties that the +# Covered Software is free of defects, merchantable, fit for a +# particular purpose or non-infringing. +# See the Mozilla Public License v. 2.0 for more details. +# +# For more details, see http://www.mrtrix.org/. + +from .usage import usage as my_usage +from .execute import execute as my_execute + +def usage(cmdline): #pylint: disable=unused-variable + return my_usage(cmdline) + +def execute(): #pylint: disable=unused-variable + return my_execute()