#!/usr/bin/env python3 import json import os import sys from rest.utils.io_utils import IOUtils from rest.utils.testrunner import TestRunner if __name__ == '__main__': io_utils = IOUtils() WORKSPACE = os.environ.get('WORKSPACE') if os.environ.get('WORKSPACE') else "tmp" VARIABLES_PATH = WORKSPACE + "/variables" COMMAND_LOGGER_PATH = WORKSPACE + "/commandlogger.txt" file_path = VARIABLES_PATH + "/testinfo.json" io_utils.append_to_file(COMMAND_LOGGER_PATH, " ".join(sys.argv)) min_args = 3 if len(sys.argv) < min_args: raise Exception( "Error: Expecting at least {} args. Got {}, args={}".format(min_args, len(sys.argv), sys.argv)) test_id = sys.argv[1] commands_list = sys.argv[2:] test_runner = TestRunner() dictionary = test_runner.run_commands(file_path, test_id, commands_list) dictionary = io_utils.read_dict_from_file(file_path) print(json.dumps(dictionary) + "\n")
class TestRunnerParallel: def __init__(self): self.__cmd_utils = CmdUtils() self.__io_utils = IOUtils() def run_command(self, manager_dict, dictionary, command): print("Input json is: " + json.dumps(dictionary) + "\n") status_finished = "finished" status_in_progress = "in progress" dictionary['commands'][command.strip()]['status'] = status_in_progress start = datetime.datetime.now() dictionary['commands'][command.strip()]['startedat'] = str(start) if platform.system() == "Windows": details = self.__cmd_utils.run_cmd_shell_true(command.split()) else: details = self.__cmd_utils.run_cmd_shell_true([command.strip()]) dictionary['commands'][command.strip()]['status'] = status_finished end = datetime.datetime.now() dictionary['commands'][command.strip()]['finishedat'] = str(end) dictionary['commands'][command.strip()]['duration'] = round( (end - start).total_seconds()) dictionary['commands'][command.strip()]['details'] = details manager_dict[command.strip()] = {} manager_dict[command.strip()] = dictionary['commands'][command.strip()] def run_commands(self, json_file, commands): with Manager() as manager: try: manager_dict = manager.dict() command_dict = self.__io_utils.read_dict_from_file(json_file) command_dict['start_pid'] = os.getpid() start_total = datetime.datetime.now() procs = [ Process(target=self.run_command, args=( manager_dict, command_dict, command.strip(), )) for command in commands ] # start processes for p in procs: p.start() p.join() command_dict['commands'] = dict(manager_dict) self.__io_utils.write_to_file_dict(json_file, command_dict) command_dict['finished'] = "true" command_dict['started'] = "false" end_total = datetime.datetime.now() command_dict['finishedat'] = str(end_total) command_dict['duration'] = round( (end_total - start_total).total_seconds()) self.__io_utils.write_to_file_dict(json_file, command_dict) except Exception as e: raise e