Exemplo n.º 1
0
def run_commands(position,
                 old_result,
                 commands,
                 work_dir,
                 timeout,
                 log_config,
                 env=None):
    name = "{}_{}".format(log_config[1], int(time.time()))
    name = log_config[1]
    formatter = logging.Formatter("%(asctime)s %(levelname)s %(message)s",
                                  datefmt="%Y-%m-%d %H:%M:%S")
    logger = construct_logger(log_config[0], log_config[1], file_fmt=formatter)
    if not commands:
        err = "Run task exit unexpected : {}, Log file at: {}.log".format(
            old_result[-1], log_config[0].get("dir") / name)
        logger.error(err)
        return position, old_result
    remain_time = timeout
    result = (PASS, None)
    logger.debug("Work directory: {}".format(work_dir))

    if platform.system() == "Windows":
        run_command = run_command_win
    else:
        run_command = run_command_linux

    commands_result = []
    for command in commands:
        start = timeit.default_timer()

        return_code, com_out, com_err = run_command(command, work_dir,
                                                    remain_time, logger, env)
        run_time = timeit.default_timer() - start
        remain_time = remain_time - run_time
        logger.debug("Run time: {:.2}, remain time: {:.2}".format(
            run_time, remain_time))
        command_result = {}
        command_result["cmd"] = command
        command_result["return_code"] = return_code
        command_result["stdout"] = com_out
        command_result["stderr"] = com_err
        commands_result.append(command_result)
        if return_code != 0:
            result = (FAIL, commands_result)
            err = "Failed, Log file at: {}.log".format(
                log_config[0].get("dir") / name)
            logger.error(err)
            break
        else:
            result = (PASS, commands_result)
    if result[0] == PASS:
        logger.debug("Task executed successfully")
    handlers = logger.handlers[:]
    for handler in handlers:
        handler.close()
        logger.removeHandler(handler)
    return position, result
Exemplo n.º 2
0
def run_commands(position,
                 old_result,
                 commands,
                 work_dir,
                 timeout,
                 log_config,
                 env=None):
    name = "{}_{}".format(log_config[1], int(time.time()))
    name = log_config[1]
    formatter = logging.Formatter("%(asctime)s %(levelname)s %(message)s",
                                  datefmt="%Y-%m-%d %H:%M:%S")
    logger = construct_logger(log_config[0], log_config[1], file_fmt=formatter)
    if not commands:
        err = "Run task exit unexpected : {}, Log file at: {}.log".format(
            old_result[-1], log_config[0].get("dir") / name)
        logger.error(err)
        return position, old_result
    remain_time = timeout
    result = (PASS, None)
    logger.debug("Work directory: {}".format(work_dir))
    if get_val("dry_run"):
        with (work_dir / "test.sh").open("w") as f:
            f.write("#!/bin/bash\n")
            for command in commands[:-1]:
                f.write(command)
                f.write(" && \\\n")
            f.write(commands[-1])
        return position, (NOT_RUN, None)

    for command in commands:
        start = timeit.default_timer()

        return_code, com_out, com_err = run_command(command, work_dir,
                                                    remain_time, logger, env)
        if return_code != 0:
            result = (FAIL, (return_code, command, shorten(com_err, width=84)))
            err = "Failed, Log file at: {}.log".format(
                log_config[0].get("dir") / name)
            logger.error(err)
            break

        run_time = timeit.default_timer() - start
        remain_time = remain_time - run_time
        logger.debug("Run time: {:.2}, remain time: {:.2}".format(
            run_time, remain_time))
    if result[0] == PASS:
        logger.debug("Task executed successfully")
    handlers = logger.handlers[:]
    for handler in handlers:
        handler.close()
        logger.removeHandler(handler)
    return position, result