+import subprocess
+import sys
+import os
+import shutil
+import stat
+import time
+import glob
+
+load_types = ("cpu", "eth", "none")
+traffic_modes = ("oneatatime", "50", "flood")
+
+class settings:
+ """Wrapper class for "globals"
+ Used to keep track of current test settings for other functions.
+ """
+ hostname = None
+ targetname = "rtems4.10-midam"
+ traffic = None
+ load = None
+ testname = None
+
+def set_hostname(name):
+ settings.hostname = name
+
+def set_targetname(name):
+ settings.targetname = name
+
+def set_traffic(traffic):
+ settings.traffic = traffic
+
+def set_load(load):
+ settings.load = load
+
+def set_testname(name):
+ settings.testname = name
+
+def set_testenv(hostname = None, targetname = None, traffic = None, load = None, testname = None):
+ set_hostname(hostname)
+ set_targetname(targetname)
+ set_traffic(traffic)
+ set_load(load)
+ set_testname(testname)
+
+gateway_IP_addr = "192.168.2.3"
+ping_flood_cmd = "ping -f -s 60000 -q {}".format(gateway_IP_addr)
+
+#Prepares 755 flag for setting privileges
+file_flag = stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR | stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH
+
+base_plot_string = ('#!/bin/bash\n'
+ 'export kvers={0}\n'
+ 'export hostkvers={1}\n'
+ 'export traffic={2}\n'
+ 'export load={3}\n'
+ 'cd $(dirname $0)/../../../../../..\n'
+ 'exec ./{4}.sh --plot "$@"\n')
+
+tftpboot_path = "/var/lib/tftpboot/ryu/"
+load_image_path = tftpboot_path + "rtems4.10_{}_FULL_LOAD"
+no_load_image_path = tftpboot_path + "rtems4.10_{}_NO_LOAD"
+boot_image_path = tftpboot_path + "uImage"
+
+eth_process = None
+def start_eth_load():
+ """Starts eth load by ping_flood_cmd"""
+ global eth_process
+ eth_process = subprocess.Popen(ping_flood_cmd.split())
+
+def stop_eth_load():
+ """Stops eth load if active"""
+ global eth_process
+ if eth_process is not None:
+ eth_process.terminate()
+ eth_process = None
+ print("Eth load stopped")
+ else:
+ print("Couldn't stop eth load -- was not active")
+
+def restart_board():
+ """Physically restarts the board"""
+ result = subprocess.call("../scripts/restart")
+ print("Waiting on restart for 15s.")
+ time.sleep(15) # 15 seconds should be enough to boot up fully even when it runs slowly
+
+def start_cpu_load(test_name):
+ """Actually flashes different image, because I don't have working way of communicating commands to the board."""
+ shutil.copy(load_image_path.format(settings.testname), boot_image_path)
+ restart_board()
+
+def stop_cpu_load(test_name):
+ """Actually flashes different image, because I don't have working way of communicating commands to the board."""
+ shutil.copy(no_load_image_path.format(settings.testname), boot_image_path)
+ restart_board()
+
+def run_in_dir(directory, test):
+ """Runs given test in given directory and after it is finished, changes it back."""
+ old = os.getcwd()
+ os.chdir(directory)
+ test.run()
+ os.chdir(old)
+
+def run_test(test):
+ """Prepares proper directory for test and then runs it in the directory."""
+ target_dir = os.path.join("results", settings.hostname, settings.targetname, settings.traffic, settings.load, test.name)
+ prepare_directory(target_dir)
+ run_in_dir(target_dir, test)
+
+def prepare_directory(path):
+ """Creates given path if it does not already exist."""
+ if not os.path.exists(path):
+ os.makedirs(path)
+
+
+def create_plot_script():
+ """Creates plot.sh with correct format to be graphed."""
+ with open("plot.sh", "w+") as file:
+ file.write(base_plot_string.format(settings.targetname, settings.hostname, settings.traffic, settings.load, settings.testname))
+ os.chmod("plot.sh", file_flag)
+
+def get_latester_arg(msg_len):
+ """Returns first part of the latester arguments."""
+ if settings.traffic == "oneatatime":
+ return "-o"
+ elif settings.traffic == "flood":
+ return ""
+ elif settings.traffic == "50":
+ return "-p " + str(2 * (44 + 8 * msg_len))
+ else:
+ raise InvalidModeException("invalid argument for get_latester_arg")
+
+def call_latester(args):
+ """Calls latester with the supplied arguments"""
+ cmd = "latester " + args
+ subprocess.call(cmd.split())
+
+def symlink_results(orig_name):
+ """Symlinks all .txt files in path given by ../{orig_name}/ into current directory under their original names."""
+ files = glob.iglob("../{}/*.txt".format(orig_name))
+ for txt_file in files:
+ link_file = txt_file.split(os.path.sep)[-1]
+ if os.path.islink(link_file) or os.path.exists(link_file):
+ os.remove(link_file)
+ os.symlink(txt_file, link_file)
+
+def read_hostname():
+ """Reads uname from host kernel."""
+ k_call = subprocess.Popen("uname -r".split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+ output, error = k_call.communicate()
+ #we have to convert output from bytearray to string and strip trailing newline from output
+ return "host-" + output.decode(sys.stdout.encoding).rstrip()
+
+
+def original_name_from_histogram_name(hist_name):
+ """Retuns name of the original test from the name of its histogram."""
+ return "-".join(hist_name.split("-")[:-1])
\ No newline at end of file