diff options
author | imarom <imarom@cisco.com> | 2016-03-21 14:48:00 +0200 |
---|---|---|
committer | imarom <imarom@cisco.com> | 2016-03-21 14:49:07 +0200 |
commit | f72c6df9d2e9998ae1f3529d729ab7930b35785a (patch) | |
tree | d4301fa01faf1bf5dedd6da66af44295ad77a2db /scripts/automation/trex_control_plane | |
parent | b9c241cb1445701dc3bf77527cecf6e4db201d0b (diff) |
python 3 support for package - phase 1
Diffstat (limited to 'scripts/automation/trex_control_plane')
15 files changed, 145 insertions, 150 deletions
diff --git a/scripts/automation/trex_control_plane/stl/trex_stl_lib/__init__.py b/scripts/automation/trex_control_plane/stl/trex_stl_lib/__init__.py index 672036b4..ba9459c1 100644 --- a/scripts/automation/trex_control_plane/stl/trex_stl_lib/__init__.py +++ b/scripts/automation/trex_control_plane/stl/trex_stl_lib/__init__.py @@ -4,8 +4,4 @@ if sys.version_info < (2, 7): print("\n**** TRex STL pacakge requires Python version >= 2.7 ***\n") exit(-1) -if sys.version_info >= (3, 0): - print("\n**** TRex STL pacakge does not support Python 3 (yet) ***\n") - #exit(-1) - -import trex_stl_ext +from . import trex_stl_ext diff --git a/scripts/automation/trex_control_plane/stl/trex_stl_lib/api.py b/scripts/automation/trex_control_plane/stl/trex_stl_lib/api.py index 9b8f9f79..082cd1a4 100644 --- a/scripts/automation/trex_control_plane/stl/trex_stl_lib/api.py +++ b/scripts/automation/trex_control_plane/stl/trex_stl_lib/api.py @@ -1,18 +1,18 @@ # client and exceptions -from trex_stl_exceptions import * -from trex_stl_client import STLClient, LoggerApi +from .trex_stl_exceptions import * +from .trex_stl_client import STLClient, LoggerApi # streams -from trex_stl_streams import * +from .trex_stl_streams import * # packet builder -from trex_stl_packet_builder_scapy import * -from scapy.all import * +from .trex_stl_packet_builder_scapy import * +#from scapy.all import * # simulator -from trex_stl_sim import STLSim +from .trex_stl_sim import STLSim # std lib (various lib functions) -from trex_stl_std import * +from .trex_stl_std import * diff --git a/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_async_client.py b/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_async_client.py index ae6cb497..0f0fe83e 100644 --- a/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_async_client.py +++ b/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_async_client.py @@ -8,11 +8,11 @@ import zmq import re import random -from trex_stl_jsonrpc_client import JsonRpcClient, BatchMessage +from .trex_stl_jsonrpc_client import JsonRpcClient, BatchMessage -from utils.text_opts import * -from trex_stl_stats import * -from trex_stl_types import * +from .utils.text_opts import * +from .trex_stl_stats import * +from .trex_stl_types import * # basic async stats class class CTRexAsyncStats(object): @@ -102,7 +102,7 @@ class CTRexAsyncStatsManager(): port_stats = {} # filter the values per port and general - for key, value in snapshot.iteritems(): + for key, value in snapshot.items(): # match a pattern of ports m = re.search('(.*)\-([0-8])', key) @@ -124,7 +124,7 @@ class CTRexAsyncStatsManager(): self.general_stats.update(general_stats) # update all ports - for port_id, data in port_stats.iteritems(): + for port_id, data in port_stats.items(): if not port_id in self.port_stats: self.port_stats[port_id] = CTRexAsyncStatsPort() @@ -209,7 +209,7 @@ class CTRexAsyncClient(): def _run (self): # socket must be created on the same thread - self.socket.setsockopt(zmq.SUBSCRIBE, '') + self.socket.setsockopt(zmq.SUBSCRIBE, b'') self.socket.setsockopt(zmq.RCVTIMEO, 5000) self.socket.connect(self.tr) @@ -316,7 +316,7 @@ class CTRexAsyncClient(): return rc # fast loop - for i in xrange(0, 100): + for i in range(0, 100): if self.async_barrier['ack']: break time.sleep(0.001) diff --git a/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_client.py b/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_client.py index 33a2cd21..d307a383 100644 --- a/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_client.py +++ b/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_client.py @@ -1,18 +1,18 @@ #!/router/bin/python # for API usage the path name must be full -from trex_stl_lib.trex_stl_exceptions import * -from trex_stl_lib.trex_stl_streams import * +from .trex_stl_exceptions import * +from .trex_stl_streams import * -from trex_stl_jsonrpc_client import JsonRpcClient, BatchMessage -import trex_stl_stats +from .trex_stl_jsonrpc_client import JsonRpcClient, BatchMessage +from . import trex_stl_stats -from trex_stl_port import Port -from trex_stl_types import * -from trex_stl_async_client import CTRexAsyncClient +from .trex_stl_port import Port +from .trex_stl_types import * +from .trex_stl_async_client import CTRexAsyncClient -from utils import parsing_opts, text_tables, common -from utils.text_opts import * +from .utils import parsing_opts, text_tables, common +from .utils.text_opts import * from functools import wraps from collections import namedtuple @@ -47,7 +47,7 @@ class LoggerApi(object): raise Exception("implement this") def set_verbose (self, level): - if not level in xrange(self.VERBOSE_QUIET, self.VERBOSE_HIGH + 1): + if not level in range(self.VERBOSE_QUIET, self.VERBOSE_HIGH + 1): raise ValueError("bad value provided for logger") self.level = level @@ -113,9 +113,9 @@ class DefaultLogger(LoggerApi): def write (self, msg, newline = True): if newline: - print msg + print(msg) else: - print msg, + print (msg), def flush (self): sys.stdout.flush() @@ -165,13 +165,13 @@ class AsyncEventHandler(object): port_stats = {} # filter the values per port and general - for key, value in dump_data.iteritems(): + for key, value in dump_data.items(): # match a pattern of ports m = re.search('(.*)\-(\d+)', key) if m: port_id = int(m.group(2)) field_name = m.group(1) - if self.client.ports.has_key(port_id): + if port_id in self.client.ports: if not port_id in port_stats: port_stats[port_id] = {} port_stats[port_id][field_name] = value @@ -185,7 +185,7 @@ class AsyncEventHandler(object): self.client.global_stats.update(global_stats, baseline) # update all ports - for port_id, data in port_stats.iteritems(): + for port_id, data in port_stats.items(): self.client.ports[port_id].port_stats.update(data, baseline) @@ -351,7 +351,7 @@ class CCommLink(object): if self.virtual: self._prompt_virtual_tx_msg() _, msg = self.rpc_link.create_jsonrpc_v2(method_name, params) - print msg + print(msg) return else: return self.rpc_link.invoke_rpc_method(method_name, params) @@ -359,9 +359,9 @@ class CCommLink(object): def transmit_batch(self, batch_list): if self.virtual: self._prompt_virtual_tx_msg() - print [msg + print([msg for _, msg in [self.rpc_link.create_jsonrpc_v2(command.method, command.params) - for command in batch_list]] + for command in batch_list]]) else: batch = self.rpc_link.create_batch() for command in batch_list: @@ -370,8 +370,8 @@ class CCommLink(object): return batch.invoke() def _prompt_virtual_tx_msg(self): - print "Transmitting virtually over tcp://{server}:{port}".format(server=self.server, - port=self.port) + print("Transmitting virtually over tcp://{server}:{port}".format(server=self.server, + port=self.port)) @@ -710,7 +710,7 @@ class STLClient(object): self.supported_cmds = rc.data() # create ports - for port_id in xrange(self.system_info["port_count"]): + for port_id in range(self.system_info["port_count"]): info = self.system_info['ports'][port_id] self.ports[port_id] = Port(port_id, @@ -781,7 +781,7 @@ class STLClient(object): port_stats = self.ports[port_id].get_stats() stats[port_id] = port_stats - for k, v in port_stats.iteritems(): + for k, v in port_stats.items(): if not k in total: total[k] = v else: @@ -859,7 +859,7 @@ class STLClient(object): @staticmethod def __get_mask_keys(ok_values={True}, **kwargs): masked_keys = set() - for key, val in kwargs.iteritems(): + for key, val in kwargs.items(): if val in ok_values: masked_keys.add(key) return masked_keys @@ -1074,30 +1074,30 @@ class STLClient(object): """ - return self.ports.keys() + return list(self.ports) # get all acquired ports def get_acquired_ports(self): return [port_id - for port_id, port_obj in self.ports.iteritems() + for port_id, port_obj in self.ports.items() if port_obj.is_acquired()] # get all active ports (TX or pause) def get_active_ports(self): return [port_id - for port_id, port_obj in self.ports.iteritems() + for port_id, port_obj in self.ports.items() if port_obj.is_active()] # get paused ports def get_paused_ports (self): return [port_id - for port_id, port_obj in self.ports.iteritems() + for port_id, port_obj in self.ports.items() if port_obj.is_paused()] # get all TX ports def get_transmitting_ports (self): return [port_id - for port_id, port_obj in self.ports.iteritems() + for port_id, port_obj in self.ports.items() if port_obj.is_transmitting()] @@ -2154,7 +2154,7 @@ class STLClient(object): # print stats to screen - for stat_type, stat_data in stats.iteritems(): + for stat_type, stat_data in stats.items(): text_tables.print_table_with_header(stat_data.text_table, stat_type) @@ -2179,7 +2179,7 @@ class STLClient(object): else: # print stats to screen - for stream_hdr, port_streams_data in streams.iteritems(): + for stream_hdr, port_streams_data in streams.items(): text_tables.print_table_with_header(port_streams_data.text_table, header= stream_hdr.split(":")[0] + ":", untouched_header= stream_hdr.split(":")[1]) @@ -2304,7 +2304,7 @@ class STLClient(object): if profile_type == 'python': self.logger.log('Type: {:^12}'.format('Python Module')) - self.logger.log('Tunables: {:^12}'.format(['{0} = {1}'.format(k ,v) for k, v in info['tunables'].iteritems()])) + self.logger.log('Tunables: {:^12}'.format(['{0} = {1}'.format(k ,v) for k, v in info['tunables'].items()])) elif profile_type == 'yaml': self.logger.log('Type: {:^12}'.format('YAML')) diff --git a/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_exceptions.py b/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_exceptions.py index e84b032b..585af231 100644 --- a/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_exceptions.py +++ b/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_exceptions.py @@ -1,7 +1,7 @@ import os import sys -from utils.text_opts import * +from .utils.text_opts import * # basic error for API class STLError(Exception): diff --git a/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_ext.py b/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_ext.py index f91ddfab..34402993 100644 --- a/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_ext.py +++ b/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_ext.py @@ -52,9 +52,9 @@ def import_module_list(modules_list): full_path = generate_module_path(p, is_python3, is_64bit, is_cel) if not os.path.exists(full_path): - print "Unable to find required module library: '{0}'".format(p) - print "Please provide the correct path using TREX_STL_EXT_PATH variable" - print "current path used: '{0}'".format(full_path) + print("Unable to find required module library: '{0}'".format(p)) + print("Please provide the correct path using TREX_STL_EXT_PATH variable") + print("current path used: '{0}'".format(full_path)) exit(0) sys.path.insert(1, full_path) diff --git a/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_jsonrpc_client.py b/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_jsonrpc_client.py index 7b284cb5..1c783046 100644 --- a/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_jsonrpc_client.py +++ b/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_jsonrpc_client.py @@ -4,11 +4,11 @@ import zmq import json import re from collections import namedtuple -from trex_stl_types import * -from utils.common import random_id_gen import zlib import struct +from .trex_stl_types import * +from .utils.common import random_id_gen class bcolors: BLUE = '\033[94m' @@ -100,7 +100,7 @@ class JsonRpcClient(object): msg["params"] = params - msg["id"] = self.id_gen.next() + msg["id"] = next(self.id_gen) if encode: return id, json.dumps(msg) @@ -177,7 +177,7 @@ class JsonRpcClient(object): tries = 0 while True: try: - self.socket.send(msg) + self.socket.send_string(msg) break except zmq.Again: tries += 1 @@ -189,7 +189,7 @@ class JsonRpcClient(object): tries = 0 while True: try: - response = self.socket.recv() + response = self.socket.recv_string() break except zmq.Again: tries += 1 diff --git a/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_port.py b/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_port.py index 0558360d..47124114 100644 --- a/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_port.py +++ b/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_port.py @@ -1,11 +1,12 @@ from collections import namedtuple, OrderedDict -from trex_stl_packet_builder_scapy import STLPktBuilder -from trex_stl_streams import STLStream +from .trex_stl_packet_builder_scapy import STLPktBuilder +from .trex_stl_streams import STLStream +from .trex_stl_types import * +from . import trex_stl_stats + import base64 -import trex_stl_stats -from trex_stl_types import * import time import copy @@ -59,7 +60,7 @@ class Port(object): self.port_stats = trex_stl_stats.CPortStats(self) - self.next_available_id = long(1) + self.next_available_id = 1 def err(self, msg): @@ -138,7 +139,7 @@ class Port(object): raise Exception("port {0}: bad state received from server '{1}'".format(self.port_id, port_state)) - self.next_available_id = long(rc.data()['max_stream_id']) + 1 + self.next_available_id = int(rc.data()['max_stream_id']) + 1 # attributes self.attr = rc.data()['attr'] @@ -151,7 +152,7 @@ class Port(object): if rc.bad(): return self.err(rc.err()) - for k, v in rc.data()['streams'].iteritems(): + for k, v in rc.data()['streams'].items(): self.streams[k] = {'next_id': v['next_stream_id'], 'pkt' : base64.b64decode(v['packet']['binary']), 'mode' : v['mode']['type'], @@ -488,21 +489,21 @@ class Port(object): rate = self.get_profile()['rate'] graph = self.get_profile()['graph'] - print format_text("Profile Map Per Port\n", 'underline', 'bold') + print(format_text("Profile Map Per Port\n", 'underline', 'bold')) factor = mult_to_factor(mult, rate['max_bps_l2'], rate['max_pps'], rate['max_line_util']) - print "Profile max BPS L2 (base / req): {:^12} / {:^12}".format(format_num(rate['max_bps_l2'], suffix = "bps"), - format_num(rate['max_bps_l2'] * factor, suffix = "bps")) + print("Profile max BPS L2 (base / req): {:^12} / {:^12}".format(format_num(rate['max_bps_l2'], suffix = "bps"), + format_num(rate['max_bps_l2'] * factor, suffix = "bps"))) - print "Profile max BPS L1 (base / req): {:^12} / {:^12}".format(format_num(rate['max_bps_l1'], suffix = "bps"), - format_num(rate['max_bps_l1'] * factor, suffix = "bps")) + print("Profile max BPS L1 (base / req): {:^12} / {:^12}".format(format_num(rate['max_bps_l1'], suffix = "bps"), + format_num(rate['max_bps_l1'] * factor, suffix = "bps"))) - print "Profile max PPS (base / req): {:^12} / {:^12}".format(format_num(rate['max_pps'], suffix = "pps"), - format_num(rate['max_pps'] * factor, suffix = "pps"),) + print("Profile max PPS (base / req): {:^12} / {:^12}".format(format_num(rate['max_pps'], suffix = "pps"), + format_num(rate['max_pps'] * factor, suffix = "pps"),)) - print "Profile line util. (base / req): {:^12} / {:^12}".format(format_percentage(rate['max_line_util']), - format_percentage(rate['max_line_util'] * factor)) + print("Profile line util. (base / req): {:^12} / {:^12}".format(format_percentage(rate['max_line_util']), + format_percentage(rate['max_line_util'] * factor))) # duration @@ -517,9 +518,9 @@ class Port(object): exp_time_factor_sec = duration - print "Duration (base / req): {:^12} / {:^12}".format(format_time(exp_time_base_sec), - format_time(exp_time_factor_sec)) - print "\n" + print("Duration (base / req): {:^12} / {:^12}".format(format_time(exp_time_base_sec), + format_time(exp_time_factor_sec))) + print("\n") # generate port info def get_info (self): @@ -576,7 +577,7 @@ class Port(object): return {} data = {} - for id, obj in self.streams.iteritems(): + for id, obj in self.streams.items(): # lazy build scapy repr. if not 'pkt_type' in obj: diff --git a/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_sim.py b/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_sim.py index 00fa6a93..eb1ffff7 100644 --- a/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_sim.py +++ b/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_sim.py @@ -17,23 +17,21 @@ See the License for the specific language governing permissions and limitations under the License. """ # simulator can be run as a standalone -import trex_stl_ext +from . import trex_stl_ext +from .trex_stl_exceptions import * +from .trex_stl_streams import * +from .utils import parsing_opts +from .trex_stl_client import STLClient -from trex_stl_exceptions import * from yaml import YAMLError -from trex_stl_streams import * -from utils import parsing_opts -from trex_stl_client import STLClient import re import json - - import argparse import tempfile import subprocess import os -from dpkt import pcap +#from dpkt import pcap from operator import itemgetter class BpSimException(Exception): @@ -43,7 +41,7 @@ def merge_cap_files (pcap_file_list, out_filename, delete_src = False): out_pkts = [] if not all([os.path.exists(f) for f in pcap_file_list]): - print "failed to merge cap file list...\nnot all files exist\n" + print("failed to merge cap file list...\nnot all files exist\n") return # read all packets to a list @@ -214,16 +212,16 @@ class STLSim(object): duration = duration)) if mode == 'json': - print json.dumps(cmds_json, indent = 4, separators=(',', ': '), sort_keys = True) + print(json.dumps(cmds_json, indent = 4, separators=(',', ': '), sort_keys = True)) return elif mode == 'yaml': - print STLProfile(stream_list).dump_to_yaml() + print(STLProfile(stream_list).dump_to_yaml()) return elif mode == 'pkt': - print STLProfile(stream_list).dump_as_pkt(); + print(STLProfile(stream_list).dump_as_pkt()) return elif mode == 'native': - print STLProfile(stream_list).dump_to_code() + print(STLProfile(stream_list).dump_to_code()) return @@ -296,7 +294,7 @@ class STLSim(object): elif self.mode == 'gdb': cmd = ['/usr/bin/gdb', '--args'] + cmd - print "executing command: '{0}'".format(" ".join(cmd)) + print("executing command: '{0}'".format(" ".join(cmd))) if self.silent: FNULL = open(os.devnull, 'w') @@ -321,7 +319,7 @@ class STLSim(object): return - print "Mering cores output to a single pcap file...\n" + print("Mering cores output to a single pcap file...\n") inputs = ["{0}-{1}".format(self.outfile, index) for index in xrange(0, self.dp_core_count)] merge_cap_files(inputs, self.outfile, delete_src = True) @@ -504,11 +502,11 @@ def main (args = None): tunables = options.tunables) except KeyboardInterrupt as e: - print "\n\n*** Caught Ctrl + C... Exiting...\n\n" + print("\n\n*** Caught Ctrl + C... Exiting...\n\n") return (-1) except STLError as e: - print e + print(e) return (-1) return (0) diff --git a/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_stats.py b/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_stats.py index 12bf881a..19479163 100644 --- a/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_stats.py +++ b/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_stats.py @@ -1,9 +1,9 @@ #!/router/bin/python -from utils import text_tables -from utils.text_opts import format_text, format_threshold, format_num +from .utils import text_tables +from .utils.text_opts import format_text, format_threshold, format_num -from trex_stl_async_client import CTRexAsyncStats +from .trex_stl_async_client import CTRexAsyncStats from collections import namedtuple, OrderedDict, deque import copy @@ -28,7 +28,7 @@ ExportableStats = namedtuple('ExportableStats', ['raw_data', 'text_table']) # deep mrege of dicts dst = src + dst def deep_merge_dicts (dst, src): - for k, v in src.iteritems(): + for k, v in src.items(): # if not exists - deep copy it if not k in dst: dst[k] = copy.deepcopy(v) @@ -57,10 +57,10 @@ def is_intable (value): def calculate_diff (samples): total = 0.0 - weight_step = 1.0 / sum(xrange(0, len(samples))) + weight_step = 1.0 / sum(range(0, len(samples))) weight = weight_step - for i in xrange(0, len(samples) - 1): + for i in range(0, len(samples) - 1): current = samples[i] if samples[i] > 0 else 1 next = samples[i + 1] if samples[i + 1] > 0 else 1 @@ -77,10 +77,10 @@ def calculate_diff (samples): def calculate_diff_raw (samples): total = 0.0 - weight_step = 1.0 / sum(xrange(0, len(samples))) + weight_step = 1.0 / sum(range(0, len(samples))) weight = weight_step - for i in xrange(0, len(samples) - 1): + for i in range(0, len(samples) - 1): current = samples[i] next = samples[i + 1] @@ -140,7 +140,7 @@ class CTRexInfoGenerator(object): stats_table.set_cols_align(["l", "l"]) stats_table.add_rows([[k.replace("_", " ").title(), v] - for k, v in stats_data.iteritems()], + for k, v in stats_data.items()], header=False) return {"global_statistics": ExportableStats(stats_data, stats_table)} @@ -156,7 +156,7 @@ class CTRexInfoGenerator(object): stats_table.set_cols_dtype(['t'] + ['t'] * stream_count) stats_table.add_rows([[k] + v - for k, v in sstats_data.iteritems()], + for k, v in sstats_data.items()], header=False) header = ["PG ID"] + [key for key in streams_keys] @@ -221,7 +221,7 @@ class CTRexInfoGenerator(object): stats_table.set_cols_dtype(['t'] + ['t'] * total_cols) stats_table.add_rows([[k] + v - for k, v in per_field_stats.iteritems()], + for k, v in per_field_stats.items()], header=False) stats_table.header(header) @@ -288,7 +288,7 @@ class CTRexInfoGenerator(object): stats_table.set_cols_dtype(['t'] + ['t'] * total_cols) stats_table.add_rows([[k] + v - for k, v in per_field_stats.iteritems()], + for k, v in per_field_stats.items()], header=False) stats_table.header(header) @@ -328,7 +328,7 @@ class CTRexInfoGenerator(object): stats_table.set_cols_width([15] + [20] * len(relevant_ports)) stats_table.add_rows([[k] + v - for k, v in per_field_status.iteritems()], + for k, v in per_field_status.items()], header=False) stats_table.header(["port"] + [port.port_id for port in relevant_ports]) @@ -350,7 +350,7 @@ class CTRexInfoGenerator(object): p_type_field_len = 0 - for stream_id, stream_id_sum in return_streams_data['streams'].iteritems(): + for stream_id, stream_id_sum in return_streams_data['streams'].items(): stream_id_sum['packet_type'] = self._trim_packet_headers(stream_id_sum['packet_type'], 30) p_type_field_len = max(p_type_field_len, len(stream_id_sum['packet_type'])) @@ -360,7 +360,7 @@ class CTRexInfoGenerator(object): info_table.set_cols_dtype(["t"] + ["t"] + ["t"] + ["t"] + ["t"] + ["t"]) info_table.add_rows([v.values() - for k, v in return_streams_data['streams'].iteritems()], + for k, v in return_streams_data['streams'].items()], header=False) info_table.header(["ID", "packet type", "length", "mode", "rate", "next stream"]) @@ -370,17 +370,17 @@ class CTRexInfoGenerator(object): def __get_relevant_ports(self, port_id_list): # fetch owned ports ports = [port_obj - for _, port_obj in self._ports_dict.iteritems() + for _, port_obj in self._ports_dict.items() if port_obj.port_id in port_id_list] # display only the first FOUR options, by design if len(ports) > 4: - print format_text("[WARNING]: ", 'magenta', 'bold'), format_text("displaying up to 4 ports", 'magenta') + self.logger.log(format_text("[WARNING]: ", 'magenta', 'bold'), format_text("displaying up to 4 ports", 'magenta')) ports = ports[:4] return ports def __update_per_field_dict(self, dict_src_data, dict_dest_ref): - for key, val in dict_src_data.iteritems(): + for key, val in dict_src_data.items(): if key in dict_dest_ref: dict_dest_ref[key].append(val) @@ -634,7 +634,7 @@ class CPortStats(CTRexStats): @staticmethod def __merge_dicts (target, src): - for k, v in src.iteritems(): + for k, v in src.items(): if k in target: target[k] += v else: @@ -799,7 +799,7 @@ class CRxStats(CTRexStats): # does the current snapshot has this field ? if field in current_pg: - for port, pv in current_pg[field].iteritems(): + for port, pv in current_pg[field].items(): if not is_intable(port): continue @@ -807,7 +807,7 @@ class CRxStats(CTRexStats): # sum up total = None - for port, pv in output[field].iteritems(): + for port, pv in output[field].items(): if not is_intable(port): continue if total is None: @@ -953,7 +953,7 @@ class CRxStats(CTRexStats): def get_stats (self): stats = {} - for pg_id, value in self.latest_stats.iteritems(): + for pg_id, value in self.latest_stats.items(): # skip non ints if not is_intable(pg_id): continue @@ -962,7 +962,7 @@ class CRxStats(CTRexStats): for field in ['tx_pkts', 'tx_bytes', 'rx_pkts']: stats[int(pg_id)][field] = {'total': self.get_rel([pg_id, field, 'total'])} - for port, pv in value[field].iteritems(): + for port, pv in value[field].items(): try: int(port) except ValueError: diff --git a/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_std.py b/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_std.py index 20600791..30fdb2dd 100644 --- a/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_std.py +++ b/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_std.py @@ -1,5 +1,5 @@ -from trex_stl_streams import * -from trex_stl_packet_builder_scapy import * +from .trex_stl_streams import * +from .trex_stl_packet_builder_scapy import * # map ports # will destroy all streams/data on the ports diff --git a/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_streams.py b/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_streams.py index b14353f4..4d0444b2 100644 --- a/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_streams.py +++ b/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_streams.py @@ -1,19 +1,19 @@ #!/router/bin/python -from trex_stl_exceptions import * -from trex_stl_types import verify_exclusive_arg, validate_type -from trex_stl_packet_builder_interface import CTrexPktBuilderInterface -from trex_stl_packet_builder_scapy import STLPktBuilder, Ether, IP, UDP, TCP, RawPcapReader +from .trex_stl_exceptions import * +from .trex_stl_types import verify_exclusive_arg, validate_type +from .trex_stl_packet_builder_interface import CTrexPktBuilderInterface +from .trex_stl_packet_builder_scapy import STLPktBuilder, Ether, IP, UDP, TCP, RawPcapReader from collections import OrderedDict, namedtuple -from scapy.utils import ltoa +#from scapy.utils import ltoa import random import yaml import base64 import string import traceback -from types import NoneType import copy +import imp # base class for TX mode class STLTXMode(object): @@ -318,11 +318,11 @@ class STLStream(object): # type checking validate_type('mode', mode, STLTXMode) - validate_type('packet', packet, (NoneType, CTrexPktBuilderInterface)) + validate_type('packet', packet, (type(None), CTrexPktBuilderInterface)) validate_type('enabled', enabled, bool) validate_type('self_start', self_start, bool) validate_type('isg', isg, (int, float)) - validate_type('stream_id', stream_id, (NoneType, int)) + validate_type('stream_id', stream_id, (type(None), int)) validate_type('random_seed',random_seed,int); if (type(mode) == STLTXCont) and (next != None): @@ -474,12 +474,12 @@ class STLStream(object): def to_pkt_dump (self): """ print packet description from scapy """ if self.name: - print "Stream Name: ",self.name + print("Stream Name: ",self.name) scapy_b = self.scapy_pkt_builder; if scapy_b and isinstance(scapy_b,STLPktBuilder): scapy_b.to_pkt_dump() else: - print "Nothing to dump" + print("Nothing to dump") @@ -844,7 +844,7 @@ class STLProfile(object): tunables = func.__code__.co_varnames[1:argc] # fetch defaults - defaults = func.func_defaults + defaults = func.__defaults__ if len(defaults) != (argc - 1): raise STLError("Module should provide default values for all arguments on get_streams()") @@ -868,8 +868,8 @@ class STLProfile(object): try: file = os.path.basename(python_file).split('.')[0] - module = __import__(file, globals(), locals(), [], -1) - reload(module) # reload the update + module = __import__(file, globals(), locals(), [], 0) + imp.reload(module) # reload the update t = STLProfile.get_module_tunables(module) for arg in kwargs: @@ -1018,9 +1018,9 @@ class STLProfile(object): """ dump the profile as scapy packet. in case it is raw convert to scapy and dump it""" cnt=0; for stream in self.streams: - print "=======================" - print "Stream %d" % cnt - print "=======================" + print("=======================") + print("Stream %d" % cnt) + print("=======================") cnt = cnt +1 stream.to_pkt_dump() diff --git a/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_types.py b/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_types.py index 4b599f16..3af20f17 100644 --- a/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_types.py +++ b/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_types.py @@ -1,7 +1,7 @@ from collections import namedtuple -from utils.text_opts import * -from trex_stl_exceptions import * +from .utils.text_opts import * +from .trex_stl_exceptions import * import types RpcCmdData = namedtuple('RpcCmdData', ['method', 'params']) @@ -60,9 +60,9 @@ class RC(): def prn_func (self, msg, newline = True): if newline: - print msg + print(msg) else: - print msg, + print(msg), def annotate (self, log_func = None, desc = None, show_status = True): @@ -76,12 +76,12 @@ class RC(): if self.bad(): # print all the errors - print "" + print("") for x in self.rc_list: if not x.rc: log_func(format_text("\n{0}".format(x.data), 'bold')) - print "" + print("") if show_status: log_func(format_text("[FAILED]\n", 'red', 'bold')) @@ -135,6 +135,6 @@ def validate_type(arg_name, arg, valid_types): # throws STLError if not exactly one argument is present def verify_exclusive_arg (args_list): - if not (len(filter(lambda x: x is not None, args_list)) == 1): + if not (len(list(filter(lambda x: x is not None, args_list))) == 1): raise STLError('exactly one parameter from {0} should be provided'.format(args_list)) diff --git a/scripts/automation/trex_control_plane/stl/trex_stl_lib/utils/text_opts.py b/scripts/automation/trex_control_plane/stl/trex_stl_lib/utils/text_opts.py index 78a0ab1f..bc2d44f4 100644 --- a/scripts/automation/trex_control_plane/stl/trex_stl_lib/utils/text_opts.py +++ b/scripts/automation/trex_control_plane/stl/trex_stl_lib/utils/text_opts.py @@ -19,8 +19,8 @@ TEXT_CODES = {'bold': {'start': '\x1b[1m', 'end': '\x1b[24m'}} class TextCodesStripper: - keys = [re.escape(v['start']) for k,v in TEXT_CODES.iteritems()] - keys += [re.escape(v['end']) for k,v in TEXT_CODES.iteritems()] + keys = [re.escape(v['start']) for k,v in TEXT_CODES.items()] + keys += [re.escape(v['end']) for k,v in TEXT_CODES.items()] pattern = re.compile("|".join(keys)) @staticmethod diff --git a/scripts/automation/trex_control_plane/stl/trex_stl_lib/utils/text_tables.py b/scripts/automation/trex_control_plane/stl/trex_stl_lib/utils/text_tables.py index 07753fda..29b6ba58 100644 --- a/scripts/automation/trex_control_plane/stl/trex_stl_lib/utils/text_tables.py +++ b/scripts/automation/trex_control_plane/stl/trex_stl_lib/utils/text_tables.py @@ -1,5 +1,5 @@ from texttable import Texttable -from text_opts import format_text +from .text_opts import format_text class TRexTextTable(Texttable): @@ -22,9 +22,9 @@ def generate_trex_stats_table(): def print_table_with_header(texttable_obj, header="", untouched_header=""): header = header.replace("_", " ").title() + untouched_header - print format_text(header, 'cyan', 'underline') + "\n" + print(format_text(header, 'cyan', 'underline') + "\n") - print (texttable_obj.draw() + "\n").encode('utf-8') + print((texttable_obj.draw() + "\n").encode('utf-8')) if __name__ == "__main__": pass |