From c74cc9cd06f07b16bc5fd2418b68bc8b46e3ff72 Mon Sep 17 00:00:00 2001
From: lukken <lukken@astron.nl>
Date: Wed, 13 Oct 2021 11:14:21 +0000
Subject: [PATCH] L2SS-287: Do most things PBR does without PBR

This is required because we want a custom version string, PBR
has its own ideas about what a version string should look like
and does not allow any alternative
---
 tangostationcontrol/requirements.txt          |  4 ++-
 tangostationcontrol/setup.cfg                 | 26 ++++++++-----------
 tangostationcontrol/setup.py                  |  6 +++--
 .../tangostationcontrol/__init__.py           |  7 ++---
 .../common/lofar_logging.py                   |  2 +-
 .../common/{lofar_git.py => lofar_version.py} | 23 ++++++++++++----
 .../devices/docker_device.py                  |  2 +-
 .../devices/observation.py                    |  2 +-
 .../devices/observation_control.py            |  2 +-
 .../tangostationcontrol/devices/recv.py       |  2 +-
 .../tangostationcontrol/devices/sdp/sdp.py    |  2 +-
 .../devices/sdp/statistics.py                 |  2 +-
 .../tangostationcontrol/devices/unb2.py       |  2 +-
 .../test/common/test_lofar_git.py             | 24 ++++++++---------
 .../tangostationcontrol/version.py            |  6 -----
 15 files changed, 58 insertions(+), 54 deletions(-)
 rename tangostationcontrol/tangostationcontrol/common/{lofar_git.py => lofar_version.py} (82%)
 delete mode 100644 tangostationcontrol/tangostationcontrol/version.py

diff --git a/tangostationcontrol/requirements.txt b/tangostationcontrol/requirements.txt
index dbcf27551..fc06d01e7 100644
--- a/tangostationcontrol/requirements.txt
+++ b/tangostationcontrol/requirements.txt
@@ -2,6 +2,8 @@
 # order of appearance. Changing the order has an impact on the overall
 # integration process, which may cause wedges in the gate later.
 
-pbr >= 2
+opcua >= 0.98.9
+PyMySQL[rsa]
+sqlalchemy
 GitPython >= 3.1.24 # BSD
 snmp>=0.1.7 # GPL3
diff --git a/tangostationcontrol/setup.cfg b/tangostationcontrol/setup.cfg
index 49c884890..3fea7a30c 100644
--- a/tangostationcontrol/setup.cfg
+++ b/tangostationcontrol/setup.cfg
@@ -1,7 +1,6 @@
 [metadata]
 name = tangostationcontrol
-;version = attr: tangostationcontrol.__version__
-version = 0.0.1
+version = attr: tangostationcontrol.__version__
 summary = LOFAR 2.0 Station Control
 description_file =
     README.md
@@ -22,21 +21,18 @@ classifier =
     Programming Language :: Python :: 3.7
     Programming Language :: Python :: 3.8
     Programming Language :: Python :: 3.9
+    Programming Language :: Python :: 3.10
 
-[files]
-packages=tangostationcontrol
+[options]
+package_dir=
+    =./
+packages=find:
+python_requires = >=3.6
 
-;[options]
-;package_dir=
-;    =tangostationcontrol
-;packages=find:
-;
-;[options.packages.find]
-;where=tangostationcontrol
-;
-;[options.entry_points]
+[options.packages.find]
+where=./
 
-[entry_points]
+[options.entry_points]
 console_scripts =
     l2ss-docker-device = tangostationcontrol.devices.docker_device:main
     l2ss-observation = tangostationcontrol.devices.observation:main
@@ -54,4 +50,4 @@ console_scripts =
     l2ss-parse-statistics-packet = tangostationcontrol.devices.sdp.statistics_packet:main
     l2ss-random-data = tangostationcontrol.test.devices.random_data:main
     l2ss-snmp = tangostationcontrol.examples.snmp.snmp:main
-    l2ss-version = tangostationcontrol.common.lofar_git:main
+    l2ss-version = tangostationcontrol.common.lofar_version:main
diff --git a/tangostationcontrol/setup.py b/tangostationcontrol/setup.py
index d3ec3b44f..6356812fd 100644
--- a/tangostationcontrol/setup.py
+++ b/tangostationcontrol/setup.py
@@ -1,5 +1,7 @@
 import setuptools
 
+with open('requirements.txt') as f:
+    required = f.read().splitlines()
+
 # Requires: setup.cfg
-setuptools.setup(setup_requires=['pbr>=2.0.0'], pbr=True)
-# setuptools.setup()
+setuptools.setup(install_requires=required)
diff --git a/tangostationcontrol/tangostationcontrol/__init__.py b/tangostationcontrol/tangostationcontrol/__init__.py
index 11f6b43c1..c6e48f3e8 100644
--- a/tangostationcontrol/tangostationcontrol/__init__.py
+++ b/tangostationcontrol/tangostationcontrol/__init__.py
@@ -1,6 +1,3 @@
-# from tangostationcontrol.common.lofar_git import get_version
-#
-# __version__ = get_version()
+from tangostationcontrol.common.lofar_version import get_version
 
-import pbr.version
-__version__ = pbr.version.VersionInfo('tangostationcontrol').version_string()
+__version__ = get_version()
diff --git a/tangostationcontrol/tangostationcontrol/common/lofar_logging.py b/tangostationcontrol/tangostationcontrol/common/lofar_logging.py
index 18e07423d..be71e4149 100644
--- a/tangostationcontrol/tangostationcontrol/common/lofar_logging.py
+++ b/tangostationcontrol/tangostationcontrol/common/lofar_logging.py
@@ -5,7 +5,7 @@ import traceback
 import socket
 import time
 
-from .lofar_git import get_version
+from .lofar_version import get_version
 
 class TangoLoggingHandler(logging.Handler):
     level_to_device_stream = {
diff --git a/tangostationcontrol/tangostationcontrol/common/lofar_git.py b/tangostationcontrol/tangostationcontrol/common/lofar_version.py
similarity index 82%
rename from tangostationcontrol/tangostationcontrol/common/lofar_git.py
rename to tangostationcontrol/tangostationcontrol/common/lofar_version.py
index f35d990ee..92fe0f5fa 100644
--- a/tangostationcontrol/tangostationcontrol/common/lofar_git.py
+++ b/tangostationcontrol/tangostationcontrol/common/lofar_version.py
@@ -1,9 +1,10 @@
 import git
 import os
 import functools
+import pkg_resources
 import re
 
-def get_repo(starting_directory: str = os.path.dirname(os.path.abspath(__file__))) -> git.Repo:
+def get_repo(starting_directory: str = os.path.dirname(os.path.abspath(__file__)), limit = 10) -> git.Repo:
     """ Try finding the repository by traversing up the tree.
 
         By default, the repository containing this module is returned.
@@ -16,9 +17,11 @@ def get_repo(starting_directory: str = os.path.dirname(os.path.abspath(__file__)
     except git.InvalidGitRepositoryError:
         pass
 
-    # We now have to traverse up the tree
-    while directory != "/" and os.path.exists(directory):
-        # Go to parent
+    # We now have to traverse up the tree up until limit diretories
+    for i in range(limit):
+        if directory == "/" or not os.path.exists(directory):
+            break
+
         directory = os.path.abspath(directory + os.path.sep + "..")
 
         try:
@@ -26,7 +29,9 @@ def get_repo(starting_directory: str = os.path.dirname(os.path.abspath(__file__)
         except git.InvalidGitRepositoryError:
             pass
 
-    raise git.InvalidGitRepositoryError("Could not find git repository root in {}".format(starting_directory))
+    # Could not find a repo within the limit so return None
+    return None
+
 
 @functools.lru_cache(maxsize=None)
 def get_version(repo: git.Repo = None) -> str:
@@ -50,6 +55,14 @@ def get_version(repo: git.Repo = None) -> str:
     if repo is None:
         repo = get_repo()
 
+    # When we can't find a git repo anymore, we must be packaged. Extract the
+    # package version directly
+    if repo is None:
+        try:
+            return pkg_resources.require("tangostationcontrol")[0].version
+        except Exception:
+            pass
+
     # Filter all tags so that they must match vMAJOR.MINOR.PATCH or
     # vMAJOR.MINOR.PATCH.BRANCHCOMMIT
     reg = re.compile(r'^v[0-9](\.[0-9]){2}(\.[a-z]*[0-9]*)?')
diff --git a/tangostationcontrol/tangostationcontrol/devices/docker_device.py b/tangostationcontrol/tangostationcontrol/devices/docker_device.py
index 1f1fd245e..7d4337b13 100644
--- a/tangostationcontrol/tangostationcontrol/devices/docker_device.py
+++ b/tangostationcontrol/tangostationcontrol/devices/docker_device.py
@@ -26,7 +26,7 @@ from tangostationcontrol.clients.docker_client import DockerClient
 from tangostationcontrol.clients.attribute_wrapper import attribute_wrapper
 from tangostationcontrol.devices.hardware_device import hardware_device
 from tangostationcontrol.common.lofar_logging import device_logging_to_python, log_exceptions
-from tangostationcontrol.common.lofar_git import get_version
+from tangostationcontrol.common.lofar_version import get_version
 
 __all__ = ["Docker", "main"]
 
diff --git a/tangostationcontrol/tangostationcontrol/devices/observation.py b/tangostationcontrol/tangostationcontrol/devices/observation.py
index 0dbec128d..86a730320 100644
--- a/tangostationcontrol/tangostationcontrol/devices/observation.py
+++ b/tangostationcontrol/tangostationcontrol/devices/observation.py
@@ -13,7 +13,7 @@ from time import time
 
 from tangostationcontrol.devices.device_decorators import *
 from tangostationcontrol.common.lofar_logging import device_logging_to_python, log_exceptions
-from tangostationcontrol.common.lofar_git import get_version
+from tangostationcontrol.common.lofar_version import get_version
 
 from json import loads
 
diff --git a/tangostationcontrol/tangostationcontrol/devices/observation_control.py b/tangostationcontrol/tangostationcontrol/devices/observation_control.py
index 20bd9f546..5676a7a18 100644
--- a/tangostationcontrol/tangostationcontrol/devices/observation_control.py
+++ b/tangostationcontrol/tangostationcontrol/devices/observation_control.py
@@ -15,7 +15,7 @@ import time
 from json import loads
 
 from tangostationcontrol.common.lofar_logging import device_logging_to_python, log_exceptions
-from tangostationcontrol.common.lofar_git import get_version
+from tangostationcontrol.common.lofar_version import get_version
 from tangostationcontrol.devices.device_decorators import *
 from tangostationcontrol.devices.observation import Observation
 
diff --git a/tangostationcontrol/tangostationcontrol/devices/recv.py b/tangostationcontrol/tangostationcontrol/devices/recv.py
index aa0d098cf..78479ac0e 100644
--- a/tangostationcontrol/tangostationcontrol/devices/recv.py
+++ b/tangostationcontrol/tangostationcontrol/devices/recv.py
@@ -22,7 +22,7 @@ import numpy
 from tangostationcontrol.clients.opcua_client import OPCUAConnection
 from tangostationcontrol.clients.attribute_wrapper import attribute_wrapper
 from tangostationcontrol.common.lofar_logging import device_logging_to_python, log_exceptions
-from tangostationcontrol.common.lofar_git import get_version
+from tangostationcontrol.common.lofar_version import get_version
 from tangostationcontrol.devices.device_decorators import *
 from tangostationcontrol.devices.hardware_device import hardware_device
 
diff --git a/tangostationcontrol/tangostationcontrol/devices/sdp/sdp.py b/tangostationcontrol/tangostationcontrol/devices/sdp/sdp.py
index dd755853d..71611166b 100644
--- a/tangostationcontrol/tangostationcontrol/devices/sdp/sdp.py
+++ b/tangostationcontrol/tangostationcontrol/devices/sdp/sdp.py
@@ -22,7 +22,7 @@ from tangostationcontrol.clients.attribute_wrapper import attribute_wrapper
 from tangostationcontrol.devices.hardware_device import hardware_device
 
 from tangostationcontrol.common.lofar_logging import device_logging_to_python, log_exceptions
-from tangostationcontrol.common.lofar_git import get_version
+from tangostationcontrol.common.lofar_version import get_version
 
 import numpy
 
diff --git a/tangostationcontrol/tangostationcontrol/devices/sdp/statistics.py b/tangostationcontrol/tangostationcontrol/devices/sdp/statistics.py
index abb9bddc9..6da1263ff 100644
--- a/tangostationcontrol/tangostationcontrol/devices/sdp/statistics.py
+++ b/tangostationcontrol/tangostationcontrol/devices/sdp/statistics.py
@@ -24,7 +24,7 @@ from tangostationcontrol.clients.statistics_client import StatisticsClient
 from tangostationcontrol.clients.opcua_client import OPCUAConnection
 from tangostationcontrol.clients.attribute_wrapper import attribute_wrapper
 from tangostationcontrol.devices.hardware_device import hardware_device
-from tangostationcontrol.common.lofar_git import get_version
+from tangostationcontrol.common.lofar_version import get_version
 from tangostationcontrol.common.lofar_logging import device_logging_to_python, log_exceptions
 
 import logging
diff --git a/tangostationcontrol/tangostationcontrol/devices/unb2.py b/tangostationcontrol/tangostationcontrol/devices/unb2.py
index 4216374e9..6824d7b26 100644
--- a/tangostationcontrol/tangostationcontrol/devices/unb2.py
+++ b/tangostationcontrol/tangostationcontrol/devices/unb2.py
@@ -22,7 +22,7 @@ from tangostationcontrol.clients.attribute_wrapper import attribute_wrapper
 from tangostationcontrol.devices.hardware_device import hardware_device
 
 from tangostationcontrol.common.lofar_logging import device_logging_to_python, log_exceptions
-from tangostationcontrol.common.lofar_git import get_version
+from tangostationcontrol.common.lofar_version import get_version
 
 import numpy
 
diff --git a/tangostationcontrol/tangostationcontrol/test/common/test_lofar_git.py b/tangostationcontrol/tangostationcontrol/test/common/test_lofar_git.py
index 02700b8d9..ff51bafbf 100644
--- a/tangostationcontrol/tangostationcontrol/test/common/test_lofar_git.py
+++ b/tangostationcontrol/tangostationcontrol/test/common/test_lofar_git.py
@@ -10,7 +10,7 @@
 import git
 from unittest import mock
 
-from tangostationcontrol.common import lofar_git
+from tangostationcontrol.common import lofar_version
 
 from tangostationcontrol.test import base
 
@@ -20,15 +20,15 @@ class TestLofarGit(base.TestCase):
     def setUp(self):
         super(TestLofarGit, self).setUp()
 
-        # Clear the cache as this function of lofar_git uses LRU decorator
+        # Clear the cache as this function of lofar_version uses LRU decorator
         # This is a good demonstration of how unit tests in Python can have
         # permanent effects, typically fixtures are needed to restore these.
-        lofar_git.get_version.cache_clear()
+        lofar_version.get_version.cache_clear()
 
     def test_get_version(self):
         """Test if attributes of get_repo are correctly used by get_version"""
 
-        with mock.patch.object(lofar_git, 'get_repo') as m_get_repo:
+        with mock.patch.object(lofar_version, 'get_repo') as m_get_repo:
             m_commit = mock.Mock()
             m_commit.return_value.__str__ = mock.Mock(return_value="123456")
             m_commit.return_value.iter_items.return_value = []
@@ -43,12 +43,12 @@ class TestLofarGit(base.TestCase):
                 is_dirty=m_is_dirty, head=m_head)
 
             # No need for special string equal in Python
-            self.assertEqual("0.0.0.main123456", lofar_git.get_version())
+            self.assertEqual("0.0.0.main123456", lofar_version.get_version())
 
     def test_get_version_tag(self):
         """Test if get_version determines production_ready for tagged commit"""
 
-        with mock.patch.object(lofar_git, 'get_repo') as m_get_repo:
+        with mock.patch.object(lofar_version, 'get_repo') as m_get_repo:
             m_commit = mock.Mock()
             m_commit.return_value.__str__ = mock.Mock(return_value="123456")
             m_commit.return_value.iter_items.return_value = []
@@ -72,9 +72,9 @@ class TestLofarGit(base.TestCase):
                 active_branch="main", commit=m_commit,
                 tags=[m_tag], is_dirty=m_is_dirty, head=m_head)
 
-            self.assertEqual("0.0.3", lofar_git.get_version())
+            self.assertEqual("0.0.3", lofar_version.get_version())
 
-    @mock.patch.object(lofar_git, 'get_repo')
+    @mock.patch.object(lofar_version, 'get_repo')
     def test_get_version_tag_dirty(self, m_get_repo):
 
         """Test if get_version determines dirty tagged commit"""
@@ -92,16 +92,16 @@ class TestLofarGit(base.TestCase):
             is_dirty=m_is_dirty, head=m_head)
 
         # No need for special string equal in Python
-        self.assertEqual("0.0.0.main123456.dirty", lofar_git.get_version())
+        self.assertEqual("0.0.0.main123456.dirty", lofar_version.get_version())
 
     def test_catch_repo_error(self):
         """Test if invalid git directories will raise error"""
 
-        with mock.patch.object(lofar_git, 'get_repo') as m_get_repo:
+        with mock.patch.object(lofar_version, 'get_repo') as m_get_repo:
 
-            # Configure lofar_git.get_repo to raise InvalidGitRepositoryError
+            # Configure lofar_version.get_repo to raise InvalidGitRepositoryError
             m_get_repo.side_effect = git.InvalidGitRepositoryError
 
             # Test that error is raised by get_version
             self.assertRaises(
-                git.InvalidGitRepositoryError, lofar_git.get_version)
+                git.InvalidGitRepositoryError, lofar_version.get_version)
diff --git a/tangostationcontrol/tangostationcontrol/version.py b/tangostationcontrol/tangostationcontrol/version.py
deleted file mode 100644
index c320dcf06..000000000
--- a/tangostationcontrol/tangostationcontrol/version.py
+++ /dev/null
@@ -1,6 +0,0 @@
-# version = "v0.0.1"
-
-import pbr.version
-
-version_info = pbr.version.VersionInfo('python-watcher')
-version_string = version_info.version_string()
-- 
GitLab