From 24de3774ad592e83d1c9e93eb7037d057d343754 Mon Sep 17 00:00:00 2001
From: lukken <lukken@astron.nl>
Date: Tue, 5 Oct 2021 15:46:34 +0000
Subject: [PATCH] L2SS-287: implement python packaging

---
 devices/common/lofar_git.py                   |  2 +-
 devices/devices/docker_device.py              | 10 ----------
 devices/devices/hardware_device.py            |  1 -
 devices/devices/observation.py                | 11 -----------
 devices/devices/observation_control.py        | 11 -----------
 devices/devices/recv.py                       | 10 ----------
 devices/devices/sdp/sdp.py                    | 11 -----------
 devices/devices/sdp/sst.py                    | 11 -----------
 devices/devices/sdp/statistics.py             |  7 -------
 devices/devices/sdp/statistics_packet.py      |  2 +-
 devices/devices/sdp/xst.py                    | 11 -----------
 devices/devices/unb2.py                       | 11 -----------
 devices/examples/HW_device_template.py        | 10 ----------
 devices/examples/load_from_disk/ini_device.py | 12 ------------
 devices/examples/snmp/snmp.py                 | 11 -----------
 devices/setup.cfg                             | 19 +++++++++++++++++--
 devices/test/devices/random_data.py           | 10 ----------
 devices/toolkit/lts_cold_start.py             | 10 ++--------
 18 files changed, 21 insertions(+), 149 deletions(-)

diff --git a/devices/common/lofar_git.py b/devices/common/lofar_git.py
index f4f621728..edc4dd569 100644
--- a/devices/common/lofar_git.py
+++ b/devices/common/lofar_git.py
@@ -80,5 +80,5 @@ except:
     pass
 
 
-if __name__ == "__main__":
+def main(args=None, **kwargs):
     print(get_version())
diff --git a/devices/devices/docker_device.py b/devices/devices/docker_device.py
index 5ff0ec366..a85f7b4fa 100644
--- a/devices/devices/docker_device.py
+++ b/devices/devices/docker_device.py
@@ -11,12 +11,6 @@
 
 """
 
-# TODO(Corne): Remove sys.path.append hack once packaging is in place!
-import os, sys
-currentdir = os.path.dirname(os.path.realpath(__file__))
-parentdir = os.path.dirname(currentdir)
-sys.path.append(parentdir)
-
 # PyTango imports
 from tango import DebugIt
 from tango.server import run, command
@@ -161,7 +155,3 @@ def main(args=None, **kwargs):
     configure_logger()
 
     return run((Docker,), args=args, **kwargs)
-
-
-if __name__ == '__main__':
-    main()
diff --git a/devices/devices/hardware_device.py b/devices/devices/hardware_device.py
index f8f6ca50d..809401298 100644
--- a/devices/devices/hardware_device.py
+++ b/devices/devices/hardware_device.py
@@ -223,4 +223,3 @@ class hardware_device(Device, metaclass=AbstractDeviceMetas):
                 except Exception as e:
                     # log which attribute we're addressing
                     raise Exception(f"Cannot assign default to attribute {name}") from e
-
diff --git a/devices/devices/observation.py b/devices/devices/observation.py
index 0ac9cbc18..2297c5304 100644
--- a/devices/devices/observation.py
+++ b/devices/devices/observation.py
@@ -5,13 +5,6 @@
 # Distributed under the terms of the APACHE license.
 # See LICENSE.txt for more info.
 
-
-# TODO(Corne): Remove sys.path.append hack once packaging is in place!
-import os, sys
-currentdir = os.path.dirname(os.path.realpath(__file__))
-parentdir = os.path.dirname(currentdir)
-sys.path.append(parentdir)
-
 # PyTango imports
 from tango import server, Except, DevState, AttrWriteType, DevString, DebugIt
 from tango.server import Device, run, command, attribute
@@ -120,7 +113,3 @@ class Observation(Device):
 def main(args = None, **kwargs):
     """Main function of the ObservationControl module."""
     return run((Observation,), args = args, **kwargs)
-
-
-if __name__ == '__main__':
-    main()
diff --git a/devices/devices/observation_control.py b/devices/devices/observation_control.py
index 9b60f86bb..0cba0af6a 100644
--- a/devices/devices/observation_control.py
+++ b/devices/devices/observation_control.py
@@ -5,13 +5,6 @@
 # Distributed under the terms of the APACHE license.
 # See LICENSE.txt for more info.
 
-
-# TODO(Corne): Remove sys.path.append hack once packaging is in place!
-import os, sys
-currentdir = os.path.dirname(os.path.realpath(__file__))
-parentdir = os.path.dirname(currentdir)
-sys.path.append(parentdir)
-
 # PyTango imports
 from tango import Except, DevFailed, DevState, AttrWriteType, DebugIt, DeviceProxy, Util, DevBoolean, DevString
 from tango.server import Device, run, command, device_property, attribute
@@ -448,7 +441,3 @@ class ObservationControl(Device):
 def main(args = None, **kwargs):
     """Main function of the ObservationControl module."""
     return run((ObservationControl, Observation), verbose = True, args = args, **kwargs)
-
-
-if __name__ == '__main__':
-    main()
diff --git a/devices/devices/recv.py b/devices/devices/recv.py
index 6f1de6aed..18dcac5ea 100644
--- a/devices/devices/recv.py
+++ b/devices/devices/recv.py
@@ -11,12 +11,6 @@
 
 """
 
-# TODO(Corne): Remove sys.path.append hack once packaging is in place!
-import os, sys
-currentdir = os.path.dirname(os.path.realpath(__file__))
-parentdir = os.path.dirname(currentdir)
-sys.path.append(parentdir)
-
 # PyTango imports
 from tango import DebugIt
 from tango.server import run, command
@@ -253,7 +247,3 @@ def main(args=None, **kwargs):
     configure_logger()
 
     return run((RECV,), args=args, **kwargs)
-
-
-if __name__ == '__main__':
-    main()
diff --git a/devices/devices/sdp/sdp.py b/devices/devices/sdp/sdp.py
index 16f6d8973..a60b76046 100644
--- a/devices/devices/sdp/sdp.py
+++ b/devices/devices/sdp/sdp.py
@@ -11,13 +11,6 @@
 
 """
 
-# TODO(Corne): Remove sys.path.append hack once packaging is in place!
-import os, sys
-currentdir = os.path.dirname(os.path.realpath(__file__))
-parentdir = os.path.dirname(currentdir)
-parentdir = os.path.dirname(parentdir)
-sys.path.append(parentdir)
-
 # PyTango imports
 from tango.server import run
 from tango.server import device_property, attribute
@@ -215,7 +208,3 @@ def main(args=None, **kwargs):
     configure_logger()
 
     return run((SDP,), args=args, **kwargs)
-
-
-if __name__ == '__main__':
-    main()
diff --git a/devices/devices/sdp/sst.py b/devices/devices/sdp/sst.py
index 3b2f36236..acd3e7c66 100644
--- a/devices/devices/sdp/sst.py
+++ b/devices/devices/sdp/sst.py
@@ -11,13 +11,6 @@
 
 """
 
-# TODO(Corne): Remove sys.path.append hack once packaging is in place!
-import os, sys
-currentdir = os.path.dirname(os.path.realpath(__file__))
-parentdir = os.path.dirname(currentdir)
-parentdir = os.path.dirname(parentdir)
-sys.path.append(parentdir)
-
 # PyTango imports
 from tango.server import run
 from tango.server import device_property, attribute
@@ -110,7 +103,3 @@ def main(args=None, **kwargs):
     configure_logger()
 
     return run((SST,), args=args, **kwargs)
-
-
-if __name__ == '__main__':
-    main()
diff --git a/devices/devices/sdp/statistics.py b/devices/devices/sdp/statistics.py
index 7d0b970b0..05ed29837 100644
--- a/devices/devices/sdp/statistics.py
+++ b/devices/devices/sdp/statistics.py
@@ -11,13 +11,6 @@
 
 """
 
-# TODO(Corne): Remove sys.path.append hack once packaging is in place!
-import os, sys
-currentdir = os.path.dirname(os.path.realpath(__file__))
-parentdir = os.path.dirname(currentdir)
-parentdir = os.path.dirname(parentdir)
-sys.path.append(parentdir)
-
 from abc import ABCMeta, abstractmethod
 
 # PyTango imports
diff --git a/devices/devices/sdp/statistics_packet.py b/devices/devices/sdp/statistics_packet.py
index 9bac22707..b68525039 100644
--- a/devices/devices/sdp/statistics_packet.py
+++ b/devices/devices/sdp/statistics_packet.py
@@ -331,7 +331,7 @@ class BSTPacket(StatisticsPacket):
         return header
 
 
-if __name__ == "__main__":
+def main(args=None, **kwargs):
     # parse one packet from stdin
     import sys
     import pprint
diff --git a/devices/devices/sdp/xst.py b/devices/devices/sdp/xst.py
index caeeb5d34..2ae5519a1 100644
--- a/devices/devices/sdp/xst.py
+++ b/devices/devices/sdp/xst.py
@@ -11,13 +11,6 @@
 
 """
 
-# TODO(Corne): Remove sys.path.append hack once packaging is in place!
-import os, sys
-currentdir = os.path.dirname(os.path.realpath(__file__))
-parentdir = os.path.dirname(currentdir)
-parentdir = os.path.dirname(parentdir)
-sys.path.append(parentdir)
-
 # PyTango imports
 from tango.server import run
 from tango.server import device_property, attribute
@@ -144,7 +137,3 @@ def main(args=None, **kwargs):
     configure_logger()
 
     return run((XST,), args=args, **kwargs)
-
-
-if __name__ == '__main__':
-    main()
diff --git a/devices/devices/unb2.py b/devices/devices/unb2.py
index 7c2575991..71c92f49d 100644
--- a/devices/devices/unb2.py
+++ b/devices/devices/unb2.py
@@ -11,12 +11,6 @@
 
 """
 
-# TODO(Corne): Remove sys.path.append hack once packaging is in place!
-import os, sys
-currentdir = os.path.dirname(os.path.realpath(__file__))
-parentdir = os.path.dirname(currentdir)
-sys.path.append(parentdir)
-
 # PyTango imports
 from tango.server import run
 from tango.server import device_property, attribute
@@ -225,8 +219,3 @@ def main(args=None, **kwargs):
     configure_logger()
 
     return run((UNB2,), args=args, **kwargs)
-
-
-if __name__ == '__main__':
-    main()
-
diff --git a/devices/examples/HW_device_template.py b/devices/examples/HW_device_template.py
index 605973302..c51538c9d 100644
--- a/devices/examples/HW_device_template.py
+++ b/devices/examples/HW_device_template.py
@@ -9,12 +9,6 @@
 
 """
 
-# TODO(Corne): Remove sys.path.append hack once packaging is in place!
-import os, sys
-currentdir = os.path.dirname(os.path.realpath(__file__))
-parentdir = os.path.dirname(currentdir)
-sys.path.append(parentdir)
-
 # PyTango imports
 from tango.server import run
 from tango import AttrWriteType
@@ -90,7 +84,3 @@ class HW_dev(hardware_device):
 def main(args=None, **kwargs):
     """Main function of the hardware device module."""
     return run((HW_dev,), args=args, **kwargs)
-
-
-if __name__ == '__main__':
-    main()
diff --git a/devices/examples/load_from_disk/ini_device.py b/devices/examples/load_from_disk/ini_device.py
index 07b2f419a..8391d2823 100644
--- a/devices/examples/load_from_disk/ini_device.py
+++ b/devices/examples/load_from_disk/ini_device.py
@@ -9,13 +9,6 @@
 
 """
 
-# TODO(Corne): Remove sys.path.append hack once packaging is in place!
-import os, sys
-currentdir = os.path.dirname(os.path.realpath(__file__))
-parentdir = os.path.dirname(currentdir)
-parentdir = os.path.dirname(parentdir)
-sys.path.append(parentdir)
-
 # PyTango imports
 from tango.server import run
 from tango import AttrWriteType
@@ -130,10 +123,5 @@ class ini_device(hardware_device):
 def main(args=None, **kwargs):
     write_ini_file("example.ini")
 
-
     """Main function of the hardware device module."""
     return run((ini_device,), args=args, **kwargs)
-
-
-if __name__ == '__main__':
-    main()
diff --git a/devices/examples/snmp/snmp.py b/devices/examples/snmp/snmp.py
index 2a912ce14..9ef61ada5 100644
--- a/devices/examples/snmp/snmp.py
+++ b/devices/examples/snmp/snmp.py
@@ -11,13 +11,6 @@
 
 """
 
-# TODO(Corne): Remove sys.path.append hack once packaging is in place!
-import os, sys
-currentdir = os.path.dirname(os.path.realpath(__file__))
-parentdir = os.path.dirname(currentdir)
-parentdir = os.path.dirname(parentdir)
-sys.path.append(parentdir)
-
 # PyTango imports
 from tango.server import run
 from tango.server import device_property
@@ -123,7 +116,3 @@ def main(args=None, **kwargs):
     configure_logger(logging.getLogger())
 
     return run((SNMP,), args=args, **kwargs)
-
-
-if __name__ == '__main__':
-    main()
diff --git a/devices/setup.cfg b/devices/setup.cfg
index 55b29032e..26fb9e4fb 100644
--- a/devices/setup.cfg
+++ b/devices/setup.cfg
@@ -26,5 +26,20 @@ package_dir=./
 
 [entry_points]
 console_scripts =
-    SDP = SDP:main
-   RECV = RECV:main
+    l2ss-docker-device = devices.docker_device:main
+    l2ss-observation = devices.observation:main
+    l2ss-observation-control = devices.observation_control:main
+    l2ss-receiver = devices.recv:main
+    l2ss-sdp = devices.sdp.sdp:main
+    l2ss-sst = devices.sdp.sst:main
+    l2ss-unb2 = devices.unb2:main
+    l2ss-xst = devices.sdp.xst:main
+
+# The following entry points should eventually be removed / replaced
+    l2ss-cold-start = toolkit.lts_cold_start:main
+    l2ss-hardware-device-template = examples.HW_device_template:main
+    l2ss-ini-device = examples.load_from_disc.ini_device:main
+    l2ss-parse-statistics-packet = devices.sdp.statistics_packet:main
+    l2ss-random-data = test.devices.random_data:main
+    l2ss-snmp = examples.snmp.snmp:main
+    l2ss-version = common.lofar_git:main
diff --git a/devices/test/devices/random_data.py b/devices/test/devices/random_data.py
index 43e1a0376..51d7b269f 100644
--- a/devices/test/devices/random_data.py
+++ b/devices/test/devices/random_data.py
@@ -7,13 +7,6 @@
 # Distributed under the terms of the APACHE license.
 # See LICENSE.txt for more info.
 
-# TODO(Corne): Remove sys.path.append hack once packaging is in place!
-import os, sys
-currentdir = os.path.dirname(os.path.realpath(__file__))
-parentdir = os.path.dirname(currentdir)
-parentdir = os.path.dirname(parentdir)
-sys.path.append(parentdir)
-
 # PyTango imports
 from tango import DevState
 from tango.server import run, Device, attribute, command
@@ -497,6 +490,3 @@ def main(args = None, **kwargs):
     Main function of the RandomData module.
     """
     return run((Random_Data,), args = args, **kwargs)
-
-if __name__ == '__main__':
-    main()
diff --git a/devices/toolkit/lts_cold_start.py b/devices/toolkit/lts_cold_start.py
index 47d3243e2..1158422d8 100644
--- a/devices/toolkit/lts_cold_start.py
+++ b/devices/toolkit/lts_cold_start.py
@@ -2,12 +2,6 @@
 import logging
 from time import sleep
 
-# TODO(Corne): Remove sys.path.append hack once packaging is in place!
-import os, sys
-currentdir = os.path.dirname(os.path.realpath(__file__))
-parentdir = os.path.dirname(currentdir)
-sys.path.append(parentdir)
-
 from toolkit.startup import startup
 from toolkit.lofar2_config import configure_logging
 
@@ -223,6 +217,6 @@ def lts_cold_start():
 
     logging.info("LTS has been successfully started and configured.")
 
+def main(args=None, **kwargs):
 
-if __name__ == '__main__':
-    lts_cold_start()
+    return lts_cold_start()
\ No newline at end of file
-- 
GitLab