From a554b27aff2c19606688d32373805fef0409514b Mon Sep 17 00:00:00 2001 From: AntoinePrv Date: Tue, 7 Nov 2023 18:13:48 +0100 Subject: [PATCH] Read version from Python --- libmambapy/CMakeLists.txt | 1 - libmambapy/setup.py | 15 ++++++++++ libmambapy/src/libmambapy/__init__.py | 7 ++--- libmambapy/src/libmambapy/core/core.cpp | 1 - libmambapy/src/libmambapy/core/core.hpp | 5 ---- libmambapy/src/libmambapy/core/version.cpp | 28 ------------------- .../src/libmambapy/core/version.cpp.tmpl | 28 ------------------- libmambapy/src/libmambapy/version.py | 2 ++ libmambapy/src/libmambapy/version.py.tmpl | 2 ++ releaser.py | 2 +- 10 files changed, 22 insertions(+), 69 deletions(-) delete mode 100644 libmambapy/src/libmambapy/core/version.cpp delete mode 100644 libmambapy/src/libmambapy/core/version.cpp.tmpl create mode 100644 libmambapy/src/libmambapy/version.py create mode 100644 libmambapy/src/libmambapy/version.py.tmpl diff --git a/libmambapy/CMakeLists.txt b/libmambapy/CMakeLists.txt index 966471c0c1..b9740ab457 100644 --- a/libmambapy/CMakeLists.txt +++ b/libmambapy/CMakeLists.txt @@ -28,7 +28,6 @@ pybind11_add_module( src/libmambapy/core/longpath.manifest src/libmambapy/core/core.cpp src/libmambapy/core/main.cpp - src/libmambapy/core/version.cpp ) target_include_directories(core PRIVATE src/libmambapy/core) diff --git a/libmambapy/setup.py b/libmambapy/setup.py index 68456bc1b6..282dc95dc8 100644 --- a/libmambapy/setup.py +++ b/libmambapy/setup.py @@ -1,15 +1,30 @@ +import importlib.util import os +import pathlib import skbuild import skbuild.constants +__dir__ = pathlib.Path(__file__).parent.absolute() + def CMAKE_INSTALL_DIR(): """Where scikit-build configures CMAKE_INSTALL_PREFIX.""" return os.path.abspath(skbuild.constants.CMAKE_INSTALL_DIR()) +def libmambapy_version(): + """Get the version of libmambapy from its version module.""" + spec = importlib.util.spec_from_file_location( + "libmambapy_version", __dir__ / "src/libmambapy/version.py" + ) + ver = importlib.util.module_from_spec(spec) + spec.loader.exec_module(ver) + return ver.__version__ + + skbuild.setup( + version=libmambapy_version(), packages=["libmambapy", "libmambapy.core"], package_dir={"": "src"}, package_data={"libmambapy": ["py.typed", "__init__.pyi"]}, diff --git a/libmambapy/src/libmambapy/__init__.py b/libmambapy/src/libmambapy/__init__.py index 0f3ec68696..168009bfc3 100644 --- a/libmambapy/src/libmambapy/__init__.py +++ b/libmambapy/src/libmambapy/__init__.py @@ -1,8 +1,5 @@ -import libmambapy.core.version +import libmambapy.version from libmambapy.core.bindings import * # Legacy which used to combine everything -# Shim submodules as core submodules -version = libmambapy.core.version - # Define top-level attributes -__version__ = version.__version__ +__version__ = libmambapy.version.__version__ diff --git a/libmambapy/src/libmambapy/core/core.cpp b/libmambapy/src/libmambapy/core/core.cpp index 17978c6d2a..38bae7487c 100644 --- a/libmambapy/src/libmambapy/core/core.cpp +++ b/libmambapy/src/libmambapy/core/core.cpp @@ -8,6 +8,5 @@ PYBIND11_MODULE(core, m) { - mambapy::version::bind_submodule(m.def_submodule("version")); mambapy::bindings::bind_submodule(m.def_submodule("bindings")); } diff --git a/libmambapy/src/libmambapy/core/core.hpp b/libmambapy/src/libmambapy/core/core.hpp index d407050a9d..f4828b0b5e 100644 --- a/libmambapy/src/libmambapy/core/core.hpp +++ b/libmambapy/src/libmambapy/core/core.hpp @@ -11,11 +11,6 @@ namespace mambapy { - namespace version - { - void bind_submodule(pybind11::module_ m); - } - namespace bindings { void bind_submodule(pybind11::module_ m); diff --git a/libmambapy/src/libmambapy/core/version.cpp b/libmambapy/src/libmambapy/core/version.cpp deleted file mode 100644 index b2c30f6a73..0000000000 --- a/libmambapy/src/libmambapy/core/version.cpp +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright (c) 2019, QuantStack and Mamba Contributors -// -// Distributed under the terms of the BSD 3-Clause License. -// -// The full license is in the file LICENSE, distributed with this software. - -#include -#include - -#include - -#include "core.hpp" - -namespace mambapy::version -{ - void bind_submodule(pybind11::module_ m) - { - m.attr("__version__") = std::string_view{ - "1.5.1", - }; - - m.attr("version_info") = std::tuple{ - 1, - 5, - 1, - }; - } -} diff --git a/libmambapy/src/libmambapy/core/version.cpp.tmpl b/libmambapy/src/libmambapy/core/version.cpp.tmpl deleted file mode 100644 index 1beb74b42f..0000000000 --- a/libmambapy/src/libmambapy/core/version.cpp.tmpl +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright (c) 2019, QuantStack and Mamba Contributors -// -// Distributed under the terms of the BSD 3-Clause License. -// -// The full license is in the file LICENSE, distributed with this software. - -#include -#include - -#include - -#include "core.hpp" - -namespace mambapy::version -{ - void bind_submodule(pybind11::module_ m) - { - m.attr("__version__") = std::string_view( - "{{ version_major }}.{{ version_minor }}.{{ version_patch }}", - ); - - m.attr("version_info") = std::tuple{ - {{ version_major }}, - {{ version_minor }}, - {{ version_patch }}, - }; - } -} diff --git a/libmambapy/src/libmambapy/version.py b/libmambapy/src/libmambapy/version.py new file mode 100644 index 0000000000..a3a41b913b --- /dev/null +++ b/libmambapy/src/libmambapy/version.py @@ -0,0 +1,2 @@ +version_info = (1, 5, 1) +__version__ = ".".join(map(str, version_info)) diff --git a/libmambapy/src/libmambapy/version.py.tmpl b/libmambapy/src/libmambapy/version.py.tmpl new file mode 100644 index 0000000000..27bfa4bec8 --- /dev/null +++ b/libmambapy/src/libmambapy/version.py.tmpl @@ -0,0 +1,2 @@ +version_info = ({{ version_major }}, {{ version_minor }}, {{ version_patch }}) +__version__ = ".".join(map(str, version_info)) diff --git a/releaser.py b/releaser.py index 07b1900c80..874f41c1c8 100644 --- a/releaser.py +++ b/releaser.py @@ -8,7 +8,7 @@ templates = { "libmamba": "libmamba/include/mamba/version.hpp.tmpl", "micromamba": "micromamba/src/version.hpp.tmpl", - "libmambapy": "libmambapy/src/libmambapy/core/version.cpp.tmpl", + "libmambapy": "libmambapy/src/libmambapy/version.py.tmpl", }