diff mbox series

[libgpiod,09/11] bindings: python: decouple the version of the bindings from libgpiod API version

Message ID 20221130124231.1054001-10-brgl@bgdev.pl
State New
Headers show
Series treewide: an assortment of tweaks and improvements | expand

Commit Message

Bartosz Golaszewski Nov. 30, 2022, 12:42 p.m. UTC
From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

Python bindings now have their own setup.py script and can be built
separately from the rest of the code-base. Let's decouple the python
package version from libgpiod API (but let's keep a module attribute
containing the API version) by introducing a version.py submodule that
can be executed by the setup.py script. This way we have a single
canonical place defining the version number.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
---
 bindings/python/gpiod/__init__.py     |  3 ++-
 bindings/python/gpiod/ext/module.c    |  2 +-
 bindings/python/gpiod/version.py      |  5 +++++
 bindings/python/setup.py              | 10 +++-------
 bindings/python/tests/tests_module.py | 15 ++++++++-------
 5 files changed, 19 insertions(+), 16 deletions(-)
 create mode 100644 bindings/python/gpiod/version.py
diff mbox series

Patch

diff --git a/bindings/python/gpiod/__init__.py b/bindings/python/gpiod/__init__.py
index 7854cfd..9cbb8df 100644
--- a/bindings/python/gpiod/__init__.py
+++ b/bindings/python/gpiod/__init__.py
@@ -16,8 +16,9 @@  from .exception import ChipClosedError, RequestReleasedError
 from .info_event import InfoEvent
 from .line_request import LineRequest
 from .line_settings import LineSettings
+from .version import __version__
 
-__version__ = _ext.__version__
+api_version = _ext.api_version
 
 
 def is_gpiochip_device(path: str) -> bool:
diff --git a/bindings/python/gpiod/ext/module.c b/bindings/python/gpiod/ext/module.c
index 12fb92c..8b5a032 100644
--- a/bindings/python/gpiod/ext/module.c
+++ b/bindings/python/gpiod/ext/module.c
@@ -165,7 +165,7 @@  PyMODINIT_FUNC PyInit__ext(void)
 	if (!module)
 		return NULL;
 
-	ret = PyModule_AddStringConstant(module, "__version__",
+	ret = PyModule_AddStringConstant(module, "api_version",
 					 gpiod_version_string());
 	if (ret) {
 		Py_DECREF(module);
diff --git a/bindings/python/gpiod/version.py b/bindings/python/gpiod/version.py
new file mode 100644
index 0000000..c650969
--- /dev/null
+++ b/bindings/python/gpiod/version.py
@@ -0,0 +1,5 @@ 
+# SPDX-License-Identifier: LGPL-2.1-or-later
+# SPDX-FileCopyrightText: 2022 Linaro Ltd.
+# SPDX-FileCopyrightTest: 2022 Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
+
+__version__ = "2.0.0"
diff --git a/bindings/python/setup.py b/bindings/python/setup.py
index ec8f99d..2a8481c 100644
--- a/bindings/python/setup.py
+++ b/bindings/python/setup.py
@@ -32,16 +32,12 @@  with_tests = bool(environ["GPIOD_WITH_TESTS"])
 if with_tests:
     extensions.append(gpiosim_ext)
 
-# FIXME Find a better way to get the version
-version = None
-try:
-    version = environ["GPIOD_VERSION_STR"]
-except KeyError:
-    pass
+with open("gpiod/version.py", "r") as fd:
+    exec(fd.read())
 
 setup(
     name="gpiod",
     packages=find_packages(include=["gpiod"]),
     ext_modules=extensions,
-    version=version,
+    version=__version__,
 )
diff --git a/bindings/python/tests/tests_module.py b/bindings/python/tests/tests_module.py
index 4eeae76..de56356 100644
--- a/bindings/python/tests/tests_module.py
+++ b/bindings/python/tests/tests_module.py
@@ -3,7 +3,6 @@ 
 
 import gpiod
 import os
-import re
 import unittest
 
 from . import gpiosim
@@ -51,9 +50,11 @@  class IsGPIOChip(TestCase):
 
 
 class VersionString(TestCase):
-    def test_version_string(self):
-        self.assertTrue(
-            re.match(
-                "^[0-9][1-9]?\\.[0-9][1-9]?([\\.0-9]?|\\-devel)$", gpiod.__version__
-            )
-        )
+
+    VERSION_PATTERN = "^[0-9][1-9]?\\.[0-9][1-9]?(\\.[0-9]?|\\-devel)$"
+
+    def test_api_version_string(self):
+        self.assertRegex(gpiod.api_version, VersionString.VERSION_PATTERN)
+
+    def test_module_version(self):
+        self.assertRegex(gpiod.__version__, VersionString.VERSION_PATTERN)