From e86c278c88a88253a9c92c6b0d15ee9166073eeb Mon Sep 17 00:00:00 2001
From: Jorrit Schaap <schaap@astron.nl>
Date: Fri, 20 Sep 2019 13:03:16 +0200
Subject: [PATCH] SW-818: handle user input for station groups

---
 LCS/PyCommon/lcu_utils.py            | 24 ++++++++++++++++++++++++
 MAC/TBB/lib/tbb_freeze.py            |  6 +++---
 MAC/TBB/lib/tbb_load_firmware.py     |  4 +++-
 MAC/TBB/lib/tbb_release_recording.py |  2 ++
 MAC/TBB/lib/tbb_restart_recording.py |  3 +++
 MAC/TBB/lib/tbb_set_storage.py       |  4 ++++
 MAC/TBB/lib/tbb_start_recording.py   |  3 +++
 MAC/TBB/lib/tbb_upload_to_cep.py     |  3 +++
 8 files changed, 45 insertions(+), 4 deletions(-)

diff --git a/LCS/PyCommon/lcu_utils.py b/LCS/PyCommon/lcu_utils.py
index e4e3abe0c73..97916ce208f 100644
--- a/LCS/PyCommon/lcu_utils.py
+++ b/LCS/PyCommon/lcu_utils.py
@@ -88,6 +88,30 @@ def execute_in_parallel_over_station_group(cmd, station_group='today', timeout=3
     stations = get_current_stations(station_group=station_group, as_host_names=True)
     return execute_in_parallel_over_stations(cmd=cmd, stations=stations, timeout=timeout, max_parallel=max_parallel)
 
+def translate_user_station_string_into_station_list(user_station_string: str):
+    '''
+    try to deal with user input like 'cs001,cs001' or 'today' or ... etc
+    No guarantees! just best effort!
+    :param user_station_string: a string like 'cs001,cs001' or 'today' or ... etc
+    :return: a list of station names
+    '''
+    if isinstance(user_station_string, bytes):
+        user_station_string = user_station_string.decode('utf-8')
+
+    if not isinstance(user_station_string, str):
+        raise ValueError("cannot parse user_station_string")
+
+    if ',' in user_station_string:
+        return user_station_string.split(',')
+
+    # maybe 'stations' is a group. Do lookup.
+    current_stations = get_current_stations(user_station_string, as_host_names=False)
+    if current_stations:
+        return current_stations
+
+    # just treat the stations string as list of stations and hope for the best
+    return [user_station_string]
+
 def get_current_stations(station_group='today', as_host_names=True):
     '''
     Wrapper function around the amazing lcurun and stations.txt operators system.
diff --git a/MAC/TBB/lib/tbb_freeze.py b/MAC/TBB/lib/tbb_freeze.py
index 815426bac1c..e9edd9be0bd 100755
--- a/MAC/TBB/lib/tbb_freeze.py
+++ b/MAC/TBB/lib/tbb_freeze.py
@@ -14,7 +14,7 @@ logger = logging.getLogger(__name__)
 
 from lofar.mac.tbb.tbb_config import lcurun_command, tbb_command
 from lofar.mac.tbb.tbb_util import split_stations_by_boardnumber
-from lofar.common.lcu_utils import execute_in_parallel_over_stations
+from lofar.common.lcu_utils import execute_in_parallel_over_stations, translate_user_station_string_into_station_list
 from lofar.common.subprocess_utils import wrap_composite_command
 
 def freeze_tbb(stations, dm, timesec, timensec):
@@ -26,8 +26,7 @@ def freeze_tbb(stations, dm, timesec, timensec):
     :return:
     """
 
-    if isinstance(stations, str):
-        stations = stations.split(',')
+    stations = translate_user_station_string_into_station_list(stations)
 
     logger.info('Freezing TBB boards for stations: %s', ', '.join(stations))
 
@@ -91,6 +90,7 @@ def parse_args():
 
     if args.dm is None:
         logger.error("No dm provided")
+        parser.print_help()
         exit(1)
 
     return args
diff --git a/MAC/TBB/lib/tbb_load_firmware.py b/MAC/TBB/lib/tbb_load_firmware.py
index 65e9e2ba355..29cdfe224e6 100755
--- a/MAC/TBB/lib/tbb_load_firmware.py
+++ b/MAC/TBB/lib/tbb_load_firmware.py
@@ -11,7 +11,7 @@ import time
 import subprocess
 import logging
 from lofar.mac.tbb.tbb_config import supported_modes, lcurun_command, tbb_command
-
+from lofar.common.lcu_utils import translate_user_station_string_into_station_list
 
 def load_tbb_firmware(stations, mode):
     logging.info('Loading TBB firmware for mode \"%s\"' % (mode))
@@ -26,6 +26,8 @@ def load_tbb_firmware(stations, mode):
     else:
         slot = 1
 
+    stations = translate_user_station_string_into_station_list(stations)
+
     logging.info("It is assumed that the firmware for mode \"%s\" is in slot %d!" % (mode, slot))
 
     relay = lcurun_command + [stations]
diff --git a/MAC/TBB/lib/tbb_release_recording.py b/MAC/TBB/lib/tbb_release_recording.py
index 7763247134e..8e7435259a1 100755
--- a/MAC/TBB/lib/tbb_release_recording.py
+++ b/MAC/TBB/lib/tbb_release_recording.py
@@ -11,10 +11,12 @@ import time
 import subprocess
 import logging
 from lofar.mac.tbb.tbb_config import lcurun_command, tbb_command
+from lofar.common.lcu_utils import translate_user_station_string_into_station_list
 
 def release_tbb(stations):
     logging.info('Releasing TBB recording')
 
+    stations = translate_user_station_string_into_station_list(stations)
     relay = lcurun_command + [stations]
 
     cmd = relay + [tbb_command, '--free']
diff --git a/MAC/TBB/lib/tbb_restart_recording.py b/MAC/TBB/lib/tbb_restart_recording.py
index 9f1b869b245..4dd5f2e2b8b 100755
--- a/MAC/TBB/lib/tbb_restart_recording.py
+++ b/MAC/TBB/lib/tbb_restart_recording.py
@@ -12,11 +12,14 @@ import time
 import subprocess
 import logging
 from lofar.mac.tbb.tbb_config import supported_modes, lcurun_command, tbb_command
+from lofar.common.lcu_utils import translate_user_station_string_into_station_list
 
 
 def restart_tbb_recording(stations):
     logging.info("Restarting TBB recording")
 
+    stations = translate_user_station_string_into_station_list(stations)
+
     relay = lcurun_command + [stations]
     cmd = relay + [tbb_command, "--record"]
     logging.info("Executing %s" % " ".join(cmd))
diff --git a/MAC/TBB/lib/tbb_set_storage.py b/MAC/TBB/lib/tbb_set_storage.py
index 85f90740b54..60f620661fe 100755
--- a/MAC/TBB/lib/tbb_set_storage.py
+++ b/MAC/TBB/lib/tbb_set_storage.py
@@ -11,11 +11,14 @@ import time
 import subprocess
 import logging
 from lofar.mac.tbb.tbb_config import lcurun_command, tbb_command
+from lofar.common.lcu_utils import translate_user_station_string_into_station_list
 
 def set_tbb_storage(map):
     logging.info('Setting TBB storage nodes')
 
     for stations, node in map.items():
+        stations = translate_user_station_string_into_station_list(stations)
+
         relay = lcurun_command + [stations]
 
         cmds = [
@@ -53,6 +56,7 @@ def create_mapping(stations, nodes):
     :param nodes: list of nodes
     :return: dict mapping stations to nodes, e.g. {station1: node1, station2: node2}
     """
+    stations = translate_user_station_string_into_station_list(stations)
 
     # zip truncates to shortest list, so make sure there are enough nodes, then map each station to a node
     logging.info("Mapping stations %s on %s nodes " % (stations, nodes))
diff --git a/MAC/TBB/lib/tbb_start_recording.py b/MAC/TBB/lib/tbb_start_recording.py
index 118edde5098..61877a912ff 100755
--- a/MAC/TBB/lib/tbb_start_recording.py
+++ b/MAC/TBB/lib/tbb_start_recording.py
@@ -11,6 +11,7 @@ import time
 import subprocess
 import logging
 from lofar.mac.tbb.tbb_config import supported_modes, lcurun_command, tbb_command, rsp_command
+from lofar.common.lcu_utils import translate_user_station_string_into_station_list
 
 def start_tbb(stations, mode, subbands):
 
@@ -31,6 +32,8 @@ def start_tbb(stations, mode, subbands):
         [tbb_command, '--record']
     ]
 
+    stations = translate_user_station_string_into_station_list(stations)
+
     relay = lcurun_command + [stations]
 
     for cmd in cmds:
diff --git a/MAC/TBB/lib/tbb_upload_to_cep.py b/MAC/TBB/lib/tbb_upload_to_cep.py
index dace5fd8360..dd8fb859e44 100755
--- a/MAC/TBB/lib/tbb_upload_to_cep.py
+++ b/MAC/TBB/lib/tbb_upload_to_cep.py
@@ -13,6 +13,7 @@ import logging
 from lofar.mac.tbb.tbb_config import *
 from lofar.mac.tbb.tbb_util import split_stations_by_boardnumber, expand_list, calculate_adjusted_start_time, wrap_remote_composite_command
 from lofar.common.lcu_utils import execute_in_parallel_over_stations
+from lofar.common.lcu_utils import translate_user_station_string_into_station_list
 
 def upload_tbb_data(stations, dm, start_time, duration, sub_bands, wait_time, boards):
     """
@@ -27,6 +28,8 @@ def upload_tbb_data(stations, dm, start_time, duration, sub_bands, wait_time, bo
     """
     logging.info("Uploadind TBB data...")
 
+    stations = translate_user_station_string_into_station_list(stations)
+
     # determine number of tbb boards per station:
     stationlists = split_stations_by_boardnumber(stations)
 
-- 
GitLab