aboutsummaryrefslogtreecommitdiffstats
path: root/osmopy/osmo_interact/ctrl.py
diff options
context:
space:
mode:
authorNeels Hofmeyr <neels@hofmeyr.de>2017-12-19 14:12:16 +0100
committerNeels Hofmeyr <nhofmeyr@sysmocom.de>2017-12-19 15:11:31 +0000
commit56aa4785c028a72211913b6c1e11a0173abce062 (patch)
treed1e7125063a4f48c3e1e41f85b29e7c1e47ad824 /osmopy/osmo_interact/ctrl.py
parent1a5364696eaf66a382e6d451bc4255622d845a92 (diff)
fix osmo_interact_* and osmo_verify_transcript_* after dir split
After I30cdf0f85b2a60a235960911c9827f4129da40db, * the osmo_interact_{vty,ctrl}.py can no longer import osmo_interact_common, since it was moved to scripts/ in error. * the osmo_verify_{vty,ctrl} scripts can no longer import osmo_interact_{vty,ctrl}, since it is also in scripts/. Notably, the osmo_interact_{vty,ctrl}.py also served as scripts while being modules at the same time, which is not good. Fix these issues by adding a new osmopy/osmo_interact/ submodule with osmopy/osmo_interact/common.py, /vty.py and /ctrl.py as modules, and add in scripts thin wrappers that invoke the modules' main(). Change-Id: I40a37b212274cb70ebb1e1d9d1b3743eb2d64d05
Diffstat (limited to 'osmopy/osmo_interact/ctrl.py')
-rwxr-xr-xosmopy/osmo_interact/ctrl.py114
1 files changed, 114 insertions, 0 deletions
diff --git a/osmopy/osmo_interact/ctrl.py b/osmopy/osmo_interact/ctrl.py
new file mode 100755
index 0000000..b752351
--- /dev/null
+++ b/osmopy/osmo_interact/ctrl.py
@@ -0,0 +1,114 @@
+#!/usr/bin/env python3
+#
+# (C) 2017 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
+# All rights reserved.
+#
+# Author: Neels Hofmeyr <nhofmeyr@sysmocom.de>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 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 General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+'''
+Run CTRL commands or test transcripts against a given application. Commandline
+invocation exposes only direct command piping, the transcript verification code
+is exposed as commandline args by osmo_verify_transcript_ctrl.py.
+'''
+
+import re
+
+from .common import *
+from osmopy.osmo_ipa import Ctrl, IPA
+
+class InteractCtrl(Interact):
+ next_id = 1
+ keep_ids = True
+ re_command = re.compile('^(SET|GET) ([^ ]*) (.*)$')
+
+ class CtrlStep(Interact.StepBase):
+
+ @staticmethod
+ def is_next_step(line, interact_instance):
+ m = InteractCtrl.re_command.match(line)
+ if not m:
+ return None
+ next_step = InteractCtrl.CtrlStep()
+
+ set_get = m.group(1)
+ cmd_id = m.group(2)
+ var_val = m.group(3)
+ if not interact_instance.keep_ids:
+ cmd_id = interact_instance.next_id
+ interact_instance.next_id += 1
+ next_step.command = '%s %s %s' % (set_get, cmd_id, var_val)
+
+ return next_step
+
+ def __init__(self, port, host, verbose=False, update=False, keep_ids=True):
+ if not update:
+ keep_ids = True
+ self.keep_ids = keep_ids
+ super().__init__(InteractCtrl.CtrlStep, port=port, host=host, verbose=verbose, update=update)
+
+ def connect(self):
+ self.next_id = 1
+ super().connect()
+
+ def send(self, data):
+ data = Ctrl().add_header(data)
+ return self.socket.send(data) == len(data)
+
+ def receive(self):
+ responses = []
+ data = self.socket.recv(4096)
+ while (len(data)>0):
+ (response_with_header, data) = IPA().split_combined(data)
+ response = Ctrl().rem_header(response_with_header)
+ responses.append(response.decode('utf-8'))
+ return responses
+
+ def command(self, command):
+ assert self.send(command)
+ res = self.receive()
+ split_responses = []
+ for r in res:
+ split_responses.extend(r.splitlines())
+ sys.stdout.flush()
+ sys.stderr.flush()
+ return split_responses
+
+def main_interact_ctrl():
+ parser = common_parser()
+ parser_add_run_args(parser)
+ args = parser.parse_args()
+
+ interact = InteractCtrl(args.port, args.host, verbose=False, update=False,
+ keep_ids=True)
+
+ main_run_commands(args.run_app_str, args.output_path, args.cmd_str,
+ args.cmd_files, interact)
+
+
+def main_verify_transcript_ctrl():
+ parser = common_parser()
+ parser_add_verify_args(parser)
+ parser.add_argument('-i', '--keep-ids', dest='keep_ids', action='store_true',
+ help='With --update, default is to overwrite the command IDs'
+ ' so that they are consecutive numbers starting from 1.'
+ ' With --keep-ids, do not change these command IDs.')
+ args = parser.parse_args()
+
+ interact = InteractCtrl(args.port, args.host, args.verbose, args.update, args.keep_ids)
+
+ main_verify_transcripts(args.run_app_str, args.transcript_files, interact, args.verbose)
+
+# vim: tabstop=4 shiftwidth=4 expandtab nocin ai