예제 #1
0
async def check_call(context,
                     cmd,
                     *,
                     cwd=None,
                     env=None,
                     shell=False,
                     use_pty=None):
    """
    Run the command described by cmd.

    Post a `Command` event to the queue describing the exact invocation in
    order to allow reproducing it.
    All output to `stdout` and `stderr` is posted as `StdoutLine` and
    `StderrLine` events to the event queue.

    :param cmd: The command and its arguments
    :param cwd: the working directory for the subprocess
    :param env: a dictionary with environment variables
    :param shell: whether to use the shell as the program to execute
    :param use_pty: whether to use a pseudo terminal
    :returns: the result of the completed process
    """
    def stdout_callback(line):
        context.put_event_into_queue(StdoutLine(line))

    def stderr_callback(line):
        context.put_event_into_queue(StderrLine(line))

    context.put_event_into_queue(Command(cmd, cwd=cwd, env=env, shell=shell))
    rc = await run(cmd,
                   stdout_callback,
                   stderr_callback,
                   cwd=cwd,
                   env=env,
                   shell=shell,
                   use_pty=use_pty)
    context.put_event_into_queue(
        CommandEnded(cmd,
                     cwd=cwd,
                     env=env,
                     shell=shell,
                     returncode=rc.returncode))
    return rc
예제 #2
0
def test_command_to_string():
    cmd = ['executable', 'arg1', 'arg2']
    cwd = '/some/path'
    command = Command(cmd, cwd=cwd)
    assert command.to_string() == \
        "Invoking command in '/some/path': executable arg1 arg2"

    env = {
        '_TEST_NEW_KEY': 'new',
        '_TEST_SAME_VALUE': 'same',
        '_TEST_DIFFERENT_VALUE': 'different',
        '_TEST_PREPENDED_VALUE': 'before-base',
        '_TEST_APPENDED_VALUE': 'base-after',
    }
    if sys.platform != 'win32':
        env['PWD'] = '/other/path'
    command = Command(cmd, cwd=cwd, env=env)

    expected = "Invoking command in '/some/path': " \
        '_TEST_APPENDED_VALUE=${_TEST_APPENDED_VALUE}-after ' \
        '_TEST_DIFFERENT_VALUE=different ' \
        '_TEST_NEW_KEY=new ' \
        '_TEST_PREPENDED_VALUE=before-${_TEST_PREPENDED_VALUE} ' \
        'executable arg1 arg2'
    if sys.platform == 'win32':
        expected = expected.replace('${', '%')
        expected = expected.replace('}', '%')
    with EnvironmentContext(
            _TEST_SAME_VALUE='same',
            _TEST_DIFFERENT_VALUE='same',
            _TEST_PREPENDED_VALUE='base',
            _TEST_APPENDED_VALUE='base',
    ):
        assert command.to_string() == expected

    cmd = ['executable', '&&', 'other exec']
    command = Command(cmd, cwd=cwd)
    assert command.to_string() == \
        "Invoking command in '/some/path': executable && other exec"
    command = Command(cmd, cwd=cwd, shell=True)
    if sys.platform != 'win32':
        assert command.to_string() == \
            "Invoking command in '/some/path': executable && 'other exec'"
    else:
        assert command.to_string() == \
            "Invoking command in '/some/path': " \
            'executable && "other exec"'