Neels Hofmeyr | 6562c08 | 2017-10-18 03:20:04 +0200 | [diff] [blame^] | 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # (C) 2017 by sysmocom s.f.m.c. GmbH <info@sysmocom.de> |
| 4 | # All rights reserved. |
| 5 | # |
| 6 | # Author: Neels Hofmeyr <nhofmeyr@sysmocom.de> |
| 7 | # |
| 8 | # This program is free software: you can redistribute it and/or modify |
| 9 | # it under the terms of the GNU General Public License as published by |
| 10 | # the Free Software Foundation, either version 3 of the License, or |
| 11 | # (at your option) any later version. |
| 12 | # |
| 13 | # This program is distributed in the hope that it will be useful, |
| 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | # GNU General Public License for more details. |
| 17 | # |
| 18 | # You should have received a copy of the GNU General Public License |
| 19 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | |
| 21 | ''' |
| 22 | Run CTRL commands or test transcripts against a given application. Commandline |
| 23 | invocation exposes only direct command piping, the transcript verification code |
| 24 | is exposed as commandline args by osmo_verify_transcript_ctrl.py. |
| 25 | ''' |
| 26 | |
| 27 | import re |
| 28 | |
| 29 | from osmopy.osmo_interact_common import * |
| 30 | from osmopy.osmo_ipa import Ctrl, IPA |
| 31 | |
| 32 | class InteractCtrl(Interact): |
| 33 | next_id = 1 |
| 34 | keep_ids = True |
| 35 | re_command = re.compile('^(SET|GET) ([^ ]*) (.*)$') |
| 36 | |
| 37 | class CtrlStep(Interact.StepBase): |
| 38 | |
| 39 | @staticmethod |
| 40 | def is_next_step(line, interact_instance): |
| 41 | m = InteractCtrl.re_command.match(line) |
| 42 | if not m: |
| 43 | return None |
| 44 | next_step = InteractCtrl.CtrlStep() |
| 45 | |
| 46 | set_get = m.group(1) |
| 47 | cmd_id = m.group(2) |
| 48 | var_val = m.group(3) |
| 49 | if not interact_instance.keep_ids: |
| 50 | cmd_id = interact_instance.next_id |
| 51 | interact_instance.next_id += 1 |
| 52 | next_step.command = '%s %s %s' % (set_get, cmd_id, var_val) |
| 53 | |
| 54 | return next_step |
| 55 | |
| 56 | def __init__(self, port, host, verbose=False, update=False, keep_ids=True): |
| 57 | if not update: |
| 58 | keep_ids = True |
| 59 | self.keep_ids = keep_ids |
| 60 | super().__init__(InteractCtrl.CtrlStep, port=port, host=host, verbose=verbose, update=update) |
| 61 | |
| 62 | def connect(self): |
| 63 | self.next_id = 1 |
| 64 | super().connect() |
| 65 | |
| 66 | def send(self, data): |
| 67 | data = Ctrl().add_header(data) |
| 68 | return self.socket.send(data) == len(data) |
| 69 | |
| 70 | def receive(self): |
| 71 | responses = [] |
| 72 | data = self.socket.recv(4096) |
| 73 | while (len(data)>0): |
| 74 | (response_with_header, data) = IPA().split_combined(data) |
| 75 | response = Ctrl().rem_header(response_with_header) |
| 76 | responses.append(response.decode('utf-8')) |
| 77 | return responses |
| 78 | |
| 79 | def command(self, command): |
| 80 | assert self.send(command) |
| 81 | res = self.receive() |
| 82 | split_responses = [] |
| 83 | for r in res: |
| 84 | split_responses.extend(r.splitlines()) |
| 85 | sys.stdout.flush() |
| 86 | sys.stderr.flush() |
| 87 | return split_responses |
| 88 | |
| 89 | if __name__ == '__main__': |
| 90 | parser = common_parser() |
| 91 | parser_add_run_args(parser) |
| 92 | args = parser.parse_args() |
| 93 | |
| 94 | interact = InteractCtrl(args.port, args.host, verbose=False, update=False, |
| 95 | keep_ids=True) |
| 96 | |
| 97 | main_run_commands(args.run_app_str, args.output_path, args.cmd_str, |
| 98 | args.cmd_files, interact) |
| 99 | |
| 100 | # vim: tabstop=4 shiftwidth=4 expandtab nocin ai |