class TestRunnerInMemory:
    def __init__(self):
        self.__cmd_utils = CmdUtils()

    def run_commands(self, commands):
        start_total = datetime.datetime.now()

        status_finished = "finished"
        status_in_progress = "in progress"
        command_dict = test_info_init

        command_dict['pid'] = os.getpid()
        input_data_dict = dict.fromkeys(commands, {
            "status": "scheduled",
            "details": {}
        })
        command_dict["started"] = "true"
        command_dict["commands"] = input_data_dict
        command_dict["startedat"] = str(datetime.datetime.now())

        details = {}
        for command in commands:
            start = datetime.datetime.now()
            command_dict['commands'][command.strip()] = {
                "status": "scheduled",
                "details": {}
            }
            command_dict['commands'][
                command.strip()]['status'] = status_in_progress
            command_dict['commands'][command.strip()]['startedat'] = str(start)
            try:
                if platform.system() == "Windows":
                    details[
                        command.strip()] = self.__cmd_utils.run_cmd_shell_true(
                            command.split())
                else:
                    details[
                        command.strip()] = self.__cmd_utils.run_cmd_shell_true(
                            [command.strip()])
            except Exception as e:
                details[command.strip()] = "Exception({0})".format(e.__str__())
            command_dict['commands'][
                command.strip()]['status'] = status_finished
            end = datetime.datetime.now()
            command_dict['commands'][command.strip()]['finishedat'] = str(end)
            command_dict['commands'][command.strip()]['duration'] = round(
                (end - start).total_seconds())
            command_dict['commands'][command.strip()]['details'] = details[
                command.strip()]

        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())

        return command_dict
Example #2
0
class CommandInMemory:
    def __init__(self):
        self.command_dict = {
            "finished": False,
            "started": False,
            "startedat": str(datetime.datetime.now()),
            "finishedat": str(datetime.datetime.now()),
            "duration": 0.000000,
            "id": "none",
            "pid": 0,
            "commands": {}
        }
        self.__cmd_utils = CmdUtils()

    def run_commands(self, commands):
        start_time = datetime.datetime.now()
        commands = list(map(lambda item: item.strip(), commands))

        self.command_dict['pid'] = os.getpid()
        input_data_dict = dict.fromkeys(commands, {
            "status": "scheduled",
            "details": {}
        })
        self.command_dict["started"] = True
        self.command_dict["commands"] = input_data_dict
        self.command_dict["startedat"] = str(datetime.datetime.now())

        self.__run_commands(commands)

        self.command_dict['finished'] = True
        self.command_dict['started'] = False
        end_time = datetime.datetime.now()
        self.command_dict['finishedat'] = str(end_time)
        self.command_dict['duration'] = (end_time - start_time).total_seconds()

        return self.command_dict

    def __run_commands(self, commands):
        details = {}
        status_finished = "finished"
        status_in_progress = "in progress"

        for command in commands:
            start_time = datetime.datetime.now()
            self.command_dict['commands'][command] = {
                "status": "scheduled",
                "details": {}
            }
            self.command_dict['commands'][command][
                'status'] = status_in_progress
            self.command_dict['commands'][command]['startedat'] = str(
                start_time)
            try:
                if platform.system() == "Windows":
                    details[command] = self.__cmd_utils.run_cmd_shell_true(
                        shlex.split(command))
                else:
                    details[command] = self.__cmd_utils.run_cmd_shell_true(
                        [command])
            except Exception as e:
                details[command] = "Exception({0})".format(e.__str__())
            self.command_dict['commands'][command]['status'] = status_finished
            end_time = datetime.datetime.now()
            self.command_dict['commands'][command]['finishedat'] = str(
                end_time)
            self.command_dict['commands'][command]['duration'] = (
                end_time - start_time).total_seconds()
            self.command_dict['commands'][command]['details'] = details[
                command]
Example #3
0
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