本文整理汇总了Python中subprocess.py方法的典型用法代码示例。如果您正苦于以下问题:Python subprocess.py方法的具体用法?Python subprocess.py怎么用?Python subprocess.py使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类subprocess
的用法示例。
在下文中一共展示了subprocess.py方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_get_console_width_no_stderr_on_failure
# 需要导入模块: import subprocess [as 别名]
# 或者: from subprocess import py [as 别名]
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
示例2: check_output
# 需要导入模块: import subprocess [as 别名]
# 或者: from subprocess import py [as 别名]
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