From 3b66e1dd3b9c5b1895f7302c36c6bfc476492a56 Mon Sep 17 00:00:00 2001 From: Yaroslav Brustinov Date: Sun, 13 Dec 2015 18:00:30 +0200 Subject: python API: determine TRex version in more robust way --- scripts/automation/trex_control_plane/client/trex_client.py | 6 ++++-- scripts/automation/trex_control_plane/server/trex_server.py | 11 +++++++---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/scripts/automation/trex_control_plane/client/trex_client.py b/scripts/automation/trex_control_plane/client/trex_client.py index 77b11c37..e1130a97 100755 --- a/scripts/automation/trex_control_plane/client/trex_client.py +++ b/scripts/automation/trex_control_plane/client/trex_client.py @@ -546,7 +546,7 @@ class CTRexClient(object): Get TRex version details. :return: - Trex details (Version, User, Date, Uuid) as ordered dictionary + Trex details (Version, User, Date, Uuid, Git SHA) as ordered dictionary :raises: + :exc:`trex_exceptions.TRexRequestDenied`, in case TRex version could not be determined. @@ -558,9 +558,11 @@ class CTRexClient(object): version_dict = OrderedDict() result_lines = binascii.a2b_base64(self.server.get_trex_version()).split('\n') for line in result_lines: + if not line: + continue key, value = line.strip().split(':', 1) version_dict[key.strip()] = value.strip() - for key in ('Version', 'User', 'Date', 'Uuid'): + for key in ('Version', 'User', 'Date', 'Uuid', 'Git SHA'): if key not in version_dict: raise Exception('get_trex_version: got server response without key: {0}'.format(key)) return version_dict diff --git a/scripts/automation/trex_control_plane/server/trex_server.py b/scripts/automation/trex_control_plane/server/trex_server.py index b1ce54e1..9868d6c8 100755 --- a/scripts/automation/trex_control_plane/server/trex_server.py +++ b/scripts/automation/trex_control_plane/server/trex_server.py @@ -26,6 +26,7 @@ from trex_launch_thread import AsynchronousTRexSession from zmq_monitor_thread import ZmqMonitorSession from argparse import ArgumentParser, RawTextHelpFormatter from json import JSONEncoder +import re # setup the logger @@ -167,15 +168,17 @@ class CTRexServer(object): logger.info("Processing get_trex_daemon_log() command.") return self._pull_file('/var/log/trex/trex_daemon_server.log') - # get Trex version from ./t-rex-64 --help (last 4 lines) + # get Trex version from ./t-rex-64 --help (last lines starting with "Version : ...") def get_trex_version (self, base64 = True): try: logger.info("Processing get_trex_version() command.") if not self.trex_version: help_print = subprocess.Popen(['./t-rex-64', '--help'], cwd = self.TREX_PATH, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - help_print.wait() - help_print_stdout = help_print.stdout.read() - self.trex_version = binascii.b2a_base64('\n'.join(help_print_stdout.split('\n')[-5:-1])) + (stdout, stderr) = help_print.communicate() + search_result = re.search('\n\s*(Version\s*:.+)', stdout, re.DOTALL) + if not search_result: + raise Exception('Could not determine version from ./t-rex-64 --help') + self.trex_version = binascii.b2a_base64(search_result.group(1)) if base64: return self.trex_version else: -- cgit 1.2.3-korg