Python subprocess.py() Examples

The following are 2 code examples of subprocess.py(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module subprocess , or try the search function .
Example #1
Source File: test_example.py    From DockerMake with Apache License 2.0 6 votes vote down vote up
def test_get_console_width_no_stderr_on_failure():
    # run docker-make with a non-console standard input
    #   (inspiration from Python's subprocess.py)
    process = subprocess.Popen(
        "docker-make devbase --repo myrepo --tag mytag".split(),
        cwd=EXAMPLEDIR,
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )
    try:
        stdout, stderr = process.communicate(b"I am not a shell")
    except:
        process.kill()
        process.wait()
        raise
    retcode = process.poll()
    assert retcode == 0  # sanity check - did this build not work for some reason?

    assert b"ioctl for device" not in stderr 
Example #2
Source File: processutils.py    From releases with Apache License 2.0 4 votes vote down vote up
def check_output(*popenargs, timeout=None, **kwargs):
    # A variation of subprocess.check_output that captures stderr and
    # logs it instead of letting it go to the console directly to make
    # it easier for tests to capture it.

    # NOTE(dhellmann): copied from subprocess.py vv
    if 'stdout' in kwargs:
        raise ValueError('stdout argument not allowed, it will be overridden.')
    if 'input' in kwargs and kwargs['input'] is None:
        # Explicitly passing input=None was previously equivalent to passing an
        # empty string. That is maintained here for backwards compatibility.
        kwargs['input'] = '' if kwargs.get('universal_newlines', False) else b''
    # NOTE(dhellmann): end copied from subprocess.py ^^

    cmd = kwargs.get("args")
    if cmd is None:
        cmd = popenargs[0]
    if 'cwd' in kwargs:
        LOG.debug('cwd = {}'.format(kwargs['cwd']))
    LOG.debug('$ {}'.format(' '.join(cmd)))

    if 'stderr' not in kwargs:
        kwargs['stderr'] = subprocess.PIPE

    # Copy full environment to pass in so we can include any of our own
    # environment variables with it.
    env = os.environ.copy()
    env_extras = kwargs.pop('env', None)
    if env_extras:
        env.update(env_extras)

    completed = subprocess.run(*popenargs,
                               stdout=subprocess.PIPE,
                               timeout=timeout,
                               check=True,
                               env=env,
                               **kwargs)

    if completed.stderr:
        _multi_line_log(logging.WARNING, completed.stderr.decode('utf-8'))

    return completed.stdout