Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions bindings/Sofa/package/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ def unloadModules():
for name in toremove:
del(sys.modules[name]) # unload it

def GetVersion():
"""Returns the version of SOFA as a string in the format 'vMM.mm', where MM is the major version and mm is the
minor version.
"""
return Sofa.Helper.GetVersion()

def formatStackForSofa(o):
""" format the stack trace provided as a parameter into a string like that:
Expand Down
57 changes: 57 additions & 0 deletions bindings/Sofa/src/SofaPython3/Sofa/Helper/Binding_Version.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/******************************************************************************
* SofaPython3 plugin *
* (c) 2021 CNRS, University of Lille, INRIA *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Contact information: contact@sofa-framework.org *
******************************************************************************/

#include <pybind11/pybind11.h>

#include <SofaPython3/PythonFactory.h>
#include <SofaPython3/Sofa/Helper/Binding_Version.h>

#include <iomanip>
#include <sofa/version.h>


/// Makes an alias for the pybind11 namespace to increase readability.
namespace py { using namespace pybind11; }

namespace sofapython3
{

void moduleAddVersion(py::module &m)
{
m.def("GetVersion",
[]()
{
static const std::string sofaVersion = []() {
std::stringstream version;
constexpr auto major = SOFA_VERSION / 10000;
constexpr auto minor = SOFA_VERSION / 100 % 100;
version << 'v'
<< std::setfill('0') << std::setw(2) << major
<< "."
<< std::setfill('0') << std::setw(2) << minor;
return version.str();
}();
return sofaVersion;
},
"Returns the version of SOFA as a string in the format 'vMM.mm', where MM is the major version and mm is the minor version.");
}


}
30 changes: 30 additions & 0 deletions bindings/Sofa/src/SofaPython3/Sofa/Helper/Binding_Version.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/******************************************************************************
* SofaPython3 plugin *
* (c) 2021 CNRS, University of Lille, INRIA *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Contact information: contact@sofa-framework.org *
******************************************************************************/

#pragma once

#include <pybind11/pybind11.h>

namespace sofapython3
{

void moduleAddVersion(pybind11::module &m);

}
2 changes: 2 additions & 0 deletions bindings/Sofa/src/SofaPython3/Sofa/Helper/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ project(Bindings.Sofa.Helper)
set(HEADER_FILES
${CMAKE_CURRENT_SOURCE_DIR}/Binding_Utils.h
${CMAKE_CURRENT_SOURCE_DIR}/Binding_Vector.h
${CMAKE_CURRENT_SOURCE_DIR}/Binding_Version.h
${CMAKE_CURRENT_SOURCE_DIR}/System/Submodule_System.h
${CMAKE_CURRENT_SOURCE_DIR}/Binding_MessageHandler.h
${CMAKE_CURRENT_SOURCE_DIR}/System/Binding_FileRepository.h
Expand All @@ -13,6 +14,7 @@ set(SOURCE_FILES
${CMAKE_CURRENT_SOURCE_DIR}/Binding_MessageHandler.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Binding_Utils.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Binding_Vector.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Binding_Version.cpp
${CMAKE_CURRENT_SOURCE_DIR}/System/Submodule_System.cpp
${CMAKE_CURRENT_SOURCE_DIR}/System/Binding_FileRepository.cpp
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <SofaPython3/Sofa/Helper/Binding_MessageHandler.h>
#include <SofaPython3/Sofa/Helper/Binding_Vector.h>
#include <SofaPython3/Sofa/Helper/Binding_Utils.h>
#include <SofaPython3/Sofa/Helper/Binding_Version.h>

/// Makes an alias for the pybind11 namespace to increase readability.
namespace py { using namespace pybind11; }
Expand Down Expand Up @@ -153,6 +154,7 @@ PYBIND11_MODULE(Helper, helper)
moduleAddVector(helper);
moduleAddSystem(helper);
moduleAddUtils(helper);
moduleAddVersion(helper);

auto atexit = py::module_::import("atexit");
atexit.attr("register")(py::cpp_function([]() {
Expand Down
1 change: 1 addition & 0 deletions bindings/Sofa/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ set(PYTHON_FILES
${CMAKE_CURRENT_SOURCE_DIR}/Core/Mass.py
${CMAKE_CURRENT_SOURCE_DIR}/Core/MyRestShapeForceField.py
${CMAKE_CURRENT_SOURCE_DIR}/Helper/Message.py
${CMAKE_CURRENT_SOURCE_DIR}/Helper/Version.py
${CMAKE_CURRENT_SOURCE_DIR}/Simulation/Node.py
${CMAKE_CURRENT_SOURCE_DIR}/Simulation/Simulation.py
${CMAKE_CURRENT_SOURCE_DIR}/Types/BoundingBox.py
Expand Down
26 changes: 26 additions & 0 deletions bindings/Sofa/tests/Helper/Version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import Sofa
import Sofa.Helper
import unittest

class GetVersionTest(unittest.TestCase):
"""
Contains unit tests for verifying the existence and format of the `GetVersion` method
in the `Sofa.Helper.Utils` module.

Includes tests to assert that the version is correctly formatted and meets the expected
structure.
"""
def test_exist(self):
self.assertTrue(hasattr(Sofa.Helper, "GetVersion"))

def test_version_format(self):
version = Sofa.Helper.GetVersion()
print(f"SOFA Version: {version}")
self.assertTrue(version.startswith('v'))
version = version.replace('v', '')
self.assertTrue(version.count('.') == 1)
major, minor = version.split('.')
self.assertTrue(major.isdigit())
self.assertTrue(minor.isdigit())
self.assertEqual(len(major), 2)
self.assertEqual(len(minor), 2)
Loading