diff options
author | Jan Gelety <jgelety@cisco.com> | 2019-11-12 05:27:43 +0100 |
---|---|---|
committer | Jan Gelety <jgelety@cisco.com> | 2019-11-28 18:26:21 +0100 |
commit | d68951ac245150eeefa6e0f4156e4c1b5c9e9325 (patch) | |
tree | 487554a7547218d27f0a61ec02b70502c32cdcb4 /resources/tools/scripts | |
parent | ed0258a440cfad7023d643f717ab78ac568dc59b (diff) |
Python3: resources and libraries
Change-Id: I1392c06b1d64f62b141d24c0d42a8e36913b15e2
Signed-off-by: Jan Gelety <jgelety@cisco.com>
Diffstat (limited to 'resources/tools/scripts')
-rw-r--r-- | resources/tools/scripts/compare_perpatch.py | 61 | ||||
-rwxr-xr-x | resources/tools/scripts/topo_reservation.py | 59 |
2 files changed, 57 insertions, 63 deletions
diff --git a/resources/tools/scripts/compare_perpatch.py b/resources/tools/scripts/compare_perpatch.py index 9c9bd20023..c2c165c76d 100644 --- a/resources/tools/scripts/compare_perpatch.py +++ b/resources/tools/scripts/compare_perpatch.py @@ -44,6 +44,7 @@ def hack(value_list): ret = tmp[3*eight:-eight] return tmp # ret + iteration = -1 parent_iterations = list() current_iterations = list() @@ -52,23 +53,25 @@ while 1: iteration += 1 parent_lines = list() current_lines = list() - filename = "csit_parent/{iter}/results.txt".format(iter=iteration) + filename = f"csit_parent/{iteration}/results.txt" try: with open(filename) as parent_file: parent_lines = parent_file.readlines() except IOError: break num_lines = len(parent_lines) - filename = "csit_current/{iter}/results.txt".format(iter=iteration) + filename = f"csit_current/{iteration}/results.txt" with open(filename) as current_file: current_lines = current_file.readlines() if num_lines != len(current_lines): - print "Number of tests does not match within iteration", iteration + print(f"Number of tests does not match within iteration {iteration}") sys.exit(1) if num_tests is None: num_tests = num_lines elif num_tests != num_lines: - print "Number of tests does not match previous at iteration", iteration + print( + f"Number of tests does not match previous at iteration {iteration}" + ) sys.exit(1) parent_iterations.append(parent_lines) current_iterations.append(current_lines) @@ -80,13 +83,13 @@ for test_index in range(num_tests): current_values = list() for iteration_index in range(len(parent_iterations)): parent_values.extend( - json.loads(parent_iterations[iteration_index][test_index])) + json.loads(parent_iterations[iteration_index][test_index]) + ) current_values.extend( - json.loads(current_iterations[iteration_index][test_index])) - print "Time-ordered MRR values for parent build: {p}".format( - p=parent_values) - print "Time-ordered MRR values for current build: {c}".format( - c=current_values) + json.loads(current_iterations[iteration_index][test_index]) + ) + print(f"Time-ordered MRR values for parent build: {parent_values}") + print(f"Time-ordered MRR values for current build: {current_values}") parent_values = hack(parent_values) current_values = hack(current_values) parent_max = BitCountingMetadataFactory.find_max_value(parent_values) @@ -97,35 +100,27 @@ for test_index in range(num_tests): current_factory = BitCountingMetadataFactory(val_max, parent_stats.avg) current_stats = current_factory.from_data(current_values) both_stats = factory.from_data(parent_values + current_values) - print "Value-ordered MRR values for parent build: {p}".format( - p=parent_values) - print "Value-ordered MRR values for current build: {c}".format( - c=current_values) + print(f"Value-ordered MRR values for parent build: {parent_values}") + print(f"Value-ordered MRR values for current build: {current_values}") difference = (current_stats.avg - parent_stats.avg) / parent_stats.avg - print "Difference of averages relative to parent: {d}%".format( - d=100 * difference) - print "Jumpavg representation of parent group: {p}".format( - p=parent_stats) - print "Jumpavg representation of current group: {c}".format( - c=current_stats) - print "Jumpavg representation of both as one group: {b}".format( - b=both_stats) + print(f"Difference of averages relative to parent: {100 * difference}%") + print(f"Jumpavg representation of parent group: {parent_stats}") + print(f"Jumpavg representation of current group: {current_stats}") + print(f"Jumpavg representation of both as one group: {both_stats}") bits = parent_stats.bits + current_stats.bits - both_stats.bits - compared = "longer" if bits >= 0 else "shorter" - print "Separate groups are {cmp} than single group by {bit} bits".format( - cmp=compared, bit=abs(bits)) + compared = u"longer" if bits >= 0 else u"shorter" + print( + f"Separate groups are {compared} than single group by {abs(bits)} bits" + ) classified_list = classifier.classify([parent_stats, current_stats]) if len(classified_list) < 2: - print "Test test_index {test_index}: normal (no anomaly)".format( - test_index=test_index) + print(f"Test test_index {test_index}: normal (no anomaly)") continue anomaly = classified_list[1].metadata.classification - if anomaly == "regression": - print "Test test_index {test_index}: anomaly regression".format( - test_index=test_index) + if anomaly == u"regression": + print(f"Test test_index {test_index}: anomaly regression") exit_code = 1 continue - print "Test test_index {test_index}: anomaly {anomaly}".format( - test_index=test_index, anomaly=anomaly) -print "Exit code {code}".format(code=exit_code) + print(f"Test test_index {test_index}: anomaly {anomaly}") +print(f"Exit code {exit_code}") sys.exit(exit_code) diff --git a/resources/tools/scripts/topo_reservation.py b/resources/tools/scripts/topo_reservation.py index e7e1ff6bab..73f6d0e815 100755 --- a/resources/tools/scripts/topo_reservation.py +++ b/resources/tools/scripts/topo_reservation.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python3 # Copyright (c) 2019 Cisco and/or its affiliates. # Licensed under the Apache License, Version 2.0 (the "License"); @@ -20,15 +20,15 @@ simultaneous use of nodes listed in topology file. As source of truth, TG node from the topology file is used. """ -import sys import argparse +import sys import yaml from resources.libraries.python.ssh import exec_cmd -RESERVATION_DIR = "/tmp/reservation_dir" -RESERVATION_NODE = "TG" +RESERVATION_DIR = u"/tmp/reservation_dir" +RESERVATION_NODE = u"TG" def diag_cmd(node, cmd): @@ -36,10 +36,10 @@ def diag_cmd(node, cmd): :param node: Node object as parsed from topology file to execute cmd on. :param cmd: Command to execute. - :type ssh: dict + :type node: dict :type cmd: str """ - print('+ {cmd}'.format(cmd=cmd)) + print(f"+ {cmd}") _, stdout, _ = exec_cmd(node, cmd) print(stdout) @@ -74,16 +74,18 @@ def main(): Python returns on encountering and unexcepted exception. """ parser = argparse.ArgumentParser() - parser.add_argument("-t", "--topo", required=True, - help="Topology file") - parser.add_argument("-c", "--cancel", help="Cancel reservation", - action="store_true") - parser.add_argument("-r", "--runtag", required=False, default="Unknown", - help="Identifier for test run suitable as filename") + parser.add_argument(u"-t", u"--topo", required=True, help=u"Topology file") + parser.add_argument( + u"-c", u"--cancel", help=u"Cancel reservation", action=u"store_true" + ) + parser.add_argument( + u"-r", u"--runtag", required=False, default=u"Unknown", + help=u"Identifier for test run suitable as filename" + ) args = parser.parse_args() with open(args.topo, "r") as topo_file: - topology = yaml.load(topo_file.read())['nodes'] + topology = yaml.safe_load(topo_file.read())[u"nodes"] # Even if TG is not guaranteed to be a Linux host, # we are using it, because testing shows SSH access to DUT @@ -91,39 +93,36 @@ def main(): try: node = topology[RESERVATION_NODE] except KeyError: - print("Topology file does not contain '{node}' node". - format(node=RESERVATION_NODE)) + print(f"Topology file does not contain '{RESERVATION_NODE}' node") return 1 # For system reservation we use mkdir it is an atomic operation and we can # store additional data (time, client_ID, ..) within reservation directory. if args.cancel: - ret, _, err = exec_cmd(node, "rm -r {dir}".format(dir=RESERVATION_DIR)) + ret, _, err = exec_cmd(node, f"rm -r {RESERVATION_DIR}") if ret: - print("Cancellation unsuccessful:\n{err}".format(err=err)) + print(f"Cancellation unsuccessful:\n{err!r}") return ret # Before critical section, output can be outdated already. - print("Diagnostic commands:") - # -d and * are to supress "total <size>", see https://askubuntu.com/a/61190 - diag_cmd(node, "ls --full-time -cd '{dir}'/*".format(dir=RESERVATION_DIR)) - print("Attempting testbed reservation.") + print(u"Diagnostic commands:") + # -d and * are to suppress "total <size>", see https://askubuntu.com/a/61190 + diag_cmd(node, f"ls --full-time -cd '{RESERVATION_DIR}'/*") + print(u"Attempting testbed reservation.") # Entering critical section. - ret, _, _ = exec_cmd(node, "mkdir '{dir}'".format(dir=RESERVATION_DIR)) + ret, _, _ = exec_cmd(node, f"mkdir '{RESERVATION_DIR}'") # Critical section is over. if ret: - _, stdo, _ = exec_cmd(node, "ls '{dir}'/*".format(dir=RESERVATION_DIR)) - print("Testbed already reserved by:\n{stdo}".format(stdo=stdo)) + _, stdo, _ = exec_cmd(node, f"ls '{RESERVATION_DIR}'/*") + print(f"Testbed already reserved by:\n{stdo}") return 2 # Here the script knows it is the only owner of the testbed. - print("Reservation success, writing additional info to reservation dir.") + print(u"Reservation success, writing additional info to reservation dir.") ret, _, err = exec_cmd( - node, "touch '{dir}/{runtag}'"\ - .format(dir=RESERVATION_DIR, runtag=args.runtag)) + node, f"touch '{RESERVATION_DIR}/{args.runtag}'") if ret: - print("Writing test run info failed, but continuing anyway:\n{err}". - format(err=err)) + print(f"Writing test run info failed, but continuing anyway:\n{err!r}") return 0 -if __name__ == "__main__": +if __name__ == u"__main__": sys.exit(main()) |