예제 #1
0
파일: __init__.py 프로젝트: wja30/cortex
def run_cli(
    args: List[str],
    hide_output: bool = False,
) -> str:
    """
    Runs the Cortex binary with the specified arguments.

    Args:
        args: Arguments to use when invoking the Cortex CLI.
        hide_output: Flag to prevent streaming CLI output to stdout.

    Raises:
        CortexBinaryException: Cortex CLI command returned an error.

    Returns:
        The stdout from the Cortex CLI command.
    """

    env = os.environ.copy()
    env["CORTEX_CLI_INVOKER"] = "python"
    process = subprocess.Popen(
        [get_cli_path()] + args,
        stderr=subprocess.PIPE,
        stdout=subprocess.PIPE,
        encoding="utf8",
        env=env,
    )

    output = ""
    result = ""
    processing_result = False
    processed_result = False

    for c in iter(lambda: process.stdout.read(1), ""):
        output += c

        if not hide_output:
            sys.stdout.write(c)
            sys.stdout.flush()

        if processed_result == True:
            processing_result = False

    process.wait()

    if process.returncode == 0:
        return output

    if result != "":
        raise CortexBinaryException(result + "\n" + process.stderr.read())

    raise CortexBinaryException(process.stderr.read())
예제 #2
0
파일: __init__.py 프로젝트: xxlest/cortex
def run_cli(
    args: List[str],
    hide_output: bool = False,
    mixed_output: bool = False,
) -> str:
    """
    Runs the Cortex binary with the specified arguments.

    Args:
        args: Arguments to use when invoking the Cortex CLI.
        hide_output: Flag to prevent streaming CLI output to stdout.
        mixed_output: Used to handle CLI output that both prints to stdout and should be returned.

    Raises:
        CortexBinaryException: Cortex CLI command returned an error.

    Returns:
        The stdout from the Cortex CLI command, or the result if mixed_output output.
    """

    env = os.environ.copy()
    env["CORTEX_CLI_INVOKER"] = "python"
    process = subprocess.Popen(
        [get_cli_path()] + args,
        stderr=subprocess.PIPE,
        stdout=subprocess.PIPE,
        encoding="utf8",
        env=env,
    )

    output = ""
    result = ""
    processing_result = False
    processed_result = False

    for c in iter(lambda: process.stdout.read(1), ""):
        output += c

        if mixed_output:
            if output[-2:] == "\n~" or output == "~":
                processing_result = True
                output = output[:-1]
            if processing_result:
                result += c
                if (result[:len(MIXED_CORTEX_MARKER)] == MIXED_CORTEX_MARKER
                        and result[-len(MIXED_CORTEX_MARKER):]
                        == MIXED_CORTEX_MARKER
                        and len(result) > len(MIXED_CORTEX_MARKER)):
                    result = result[len(MIXED_CORTEX_MARKER
                                        ):-len(MIXED_CORTEX_MARKER)]
                    result = base64.b64decode(result).decode("utf8")
                    processed_result = True

                output = output[:-1]
        if not hide_output:
            if (not mixed_output) or (mixed_output and not processing_result):
                sys.stdout.write(c)
                sys.stdout.flush()

        if processed_result == True:
            processing_result = False

    process.wait()

    if process.returncode == 0:
        if mixed_output:
            return result
        return output

    if result != "":
        raise CortexBinaryException(result + "\n" + process.stderr.read())

    raise CortexBinaryException(process.stderr.read())