From 2029a627cb9b154f0bf06b613a77f237846a4d15 Mon Sep 17 00:00:00 2001 From: Axel Garcia Date: Fri, 27 Mar 2026 12:54:59 +0100 Subject: [PATCH] ENH: Use __version__ instead of version() in python --- CMakeLists.txt | 20 ++++++++++---------- applications/pctargumentparser.py | 4 ++-- wrapping/CMakeLists.txt | 7 +++---- wrapping/__init_pct__.py | 5 ++++- wrapping/pctConfig.py.in | 1 + wrapping/pctExtras.py | 2 +- 6 files changed, 21 insertions(+), 18 deletions(-) create mode 100644 wrapping/pctConfig.py.in diff --git a/CMakeLists.txt b/CMakeLists.txt index c501954..55103c5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,6 +12,16 @@ set(PCT_LIBRARIES PCT) if(NOT ITK_SOURCE_DIR) find_package(ITK REQUIRED) list(APPEND CMAKE_MODULE_PATH ${ITK_CMAKE_DIR}) +endif() +include(GetGitRevisionDescription) +get_git_head_revision(GIT_REFSPEC GIT_SHA1) +if(GIT_SHA1 MATCHES ".*NOTFOUND") + set(PCT_VERSION_HASH "") +else() + set(PCT_VERSION_HASH ", git hash ${GIT_SHA1}") +endif() + +if(NOT ITK_SOURCE_DIR) include(ITKModuleExternal) else() set(ITK_DIR ${CMAKE_BINARY_DIR}) @@ -41,16 +51,6 @@ if(PCT_BUILD_APPLICATIONS) add_subdirectory(applications) endif() -#----------------------------------------------------------------------------- -# Common revision info between applications -include(GetGitRevisionDescription) -get_git_head_revision(GIT_REFSPEC GIT_SHA1) -if(GIT_SHA1 MATCHES ".*NOTFOUND") - set(PCT_VERSION_HASH "") -else() - set(PCT_VERSION_HASH ", git hash ${GIT_SHA1}") -endif() - #========================================================= # Build doc option(PCT_BUILD_SPHINX "Build Sphinx Documentation" OFF) diff --git a/applications/pctargumentparser.py b/applications/pctargumentparser.py index 966863c..0fb099e 100644 --- a/applications/pctargumentparser.py +++ b/applications/pctargumentparser.py @@ -12,7 +12,7 @@ class PCTHelpFormatter(argparse.ArgumentDefaultsHelpFormatter): def _format_usage(self, usage, actions, groups, prefix=None): if prefix is None: - prefix = pct.version() + "\n\nusage: " + prefix = pct.__version__ + "\n\nusage: " return super()._format_usage(usage, actions, groups, prefix) @@ -22,7 +22,7 @@ def __init__(self, description=None, **kwargs): self.formatter_class = PCTHelpFormatter # allow negative numeric tokens to be treated as values, not options. This mirrors CPython behavior in python 3.14 self._negative_number_matcher = re.compile(r"-\.?\d") - self.add_argument("-V", "--version", action="version", version=pct.version()) + self.add_argument("-V", "--version", action="version", version=pct.__version__) def build_signature(self) -> inspect.Signature: """Build a compact Python signature: only required kwargs + **kwargs.""" diff --git a/wrapping/CMakeLists.txt b/wrapping/CMakeLists.txt index 4e8115f..303fda6 100644 --- a/wrapping/CMakeLists.txt +++ b/wrapping/CMakeLists.txt @@ -2,10 +2,9 @@ itk_wrap_module(PCT) itk_auto_load_submodules() itk_end_wrap_module() -set(PCT_VERSION_SCRIPT "${PCT_BINARY_DIR}/applications/pctversion.py") configure_file( - ${PCT_SOURCE_DIR}/applications/pctversion.py.in - ${PCT_VERSION_SCRIPT} + ${PCT_SOURCE_DIR}/wrapping/pctConfig.py.in + ${PCT_BINARY_DIR}/Wrapping/Generators/Python/pctConfig.py ) file( @@ -17,9 +16,9 @@ file( wrap_itk_python_bindings_install(/itk "PCT" __init_pct__.py ${PCT_PYTHON_APP} - ${PCT_VERSION_SCRIPT} ${PCT_SOURCE_DIR}/wrapping/pctExtras.py ${PCT_SOURCE_DIR}/applications/pctargumentparser.py + ${PCT_BINARY_DIR}/Wrapping/Generators/Python/pctConfig.py ) # Copy python applications to the ITK wrapping directory to ensure they can be imported in tests. diff --git a/wrapping/__init_pct__.py b/wrapping/__init_pct__.py index f695de2..57e5d85 100644 --- a/wrapping/__init_pct__.py +++ b/wrapping/__init_pct__.py @@ -4,9 +4,12 @@ itk_module = sys.modules["itk"] pct_module = getattr(itk_module, "PCT") +# Load the CMake-generated version and assign it to `itk.PCT.__version__`. +pct_version = importlib.import_module("itk.pctConfig").PCT_GLOBAL_VERSION_STRING +setattr(pct_module, "__version__", pct_version) + # Import PCT submodules pct_submodules = [ - "itk.pctversion", "itk.pctargumentparser", "itk.pctExtras", ] diff --git a/wrapping/pctConfig.py.in b/wrapping/pctConfig.py.in new file mode 100644 index 0000000..2689069 --- /dev/null +++ b/wrapping/pctConfig.py.in @@ -0,0 +1 @@ +PCT_GLOBAL_VERSION_STRING = "@PCT_VERSION_MAJOR@.@PCT_VERSION_MINOR@.@PCT_VERSION_PATCH@@PCT_VERSION_HASH@" diff --git a/wrapping/pctExtras.py b/wrapping/pctExtras.py index e4d7438..8d4767f 100644 --- a/wrapping/pctExtras.py +++ b/wrapping/pctExtras.py @@ -42,7 +42,7 @@ def app_func(*args, **kwargs): options = _parser.format_help() idx = options.lower().find("options:") opt_text = options[idx:].strip() - parts = [pct.version(), description, examples, opt_text] + parts = [pct.__version__, description, examples, opt_text] app_func.__doc__ = "\n\n".join(parts) return app_func