本文整理汇总了Python中subprocess.CalledProcessError类的典型用法代码示例。如果您正苦于以下问题:Python CalledProcessError类的具体用法?Python CalledProcessError怎么用?Python CalledProcessError使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了CalledProcessError类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: check_media_file
def check_media_file(self, filename):
valid_media_msg = '%s => OK' % filename
invalid_media_msg = '%s => INVALID' % filename
try:
# cmd = self.validate_cmd.format(filename)
cmd = self.validate_cmd
log.debug('cmd: %s %s', cmd, filename)
log.info('verifying {0}'.format(filename))
# capturing stderr to stdout because ffprobe prints to stderr in all cases
# Python 2.7+
#subprocess.check_output(cmd.split() + [filename], stderr=subprocess.STDOUT)
proc = subprocess.Popen(cmd.split() + [filename], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
(stdout, _) = proc.communicate()
returncode = proc.wait()
if returncode != 0 or (stdout is not None and 'Error' in stdout):
_ = CalledProcessError(returncode, cmd)
_.output = stdout
raise _
print(valid_media_msg)
except CalledProcessError as _:
if self.verbose > 2:
print(_.output)
if self.skip_errors:
print(invalid_media_msg)
self.failed = True
return False
die(invalid_media_msg)
示例2: check_output
def check_output(*popenargs, **kwargs):
r"""Run command with arguments and return its output as a byte string.
If the exit code was non-zero it raises a CalledProcessError. The
CalledProcessError object will have the return code in the returncode
attribute and output in the output attribute.
The arguments are the same as for the Popen constructor. Example:
>>> check_output(["ls", "-l", "/dev/null"])
'crw-rw-rw- 1 root root 1, 3 Oct 18 2007 /dev/null\n'
The stdout argument is not allowed as it is used internally.
To capture standard error in the result, use stderr=STDOUT.
>>> check_output(["/bin/sh", "-c",
... "ls -l non_existent_file ; exit 0"],
... stderr=STDOUT)
'ls: non_existent_file: No such file or directory\n'
"""
from subprocess import PIPE, CalledProcessError, Popen
if 'stdout' in kwargs:
raise ValueError('stdout argument not allowed, it will be overridden.')
process = Popen(stdout=PIPE, *popenargs, **kwargs)
output, unused_err = process.communicate()
retcode = process.poll()
if retcode:
cmd = kwargs.get("args")
if cmd is None:
cmd = popenargs[0]
err = CalledProcessError(retcode, cmd)
err.output = output
raise err
return output
示例3: __init__
def __init__(self, *, command: str, call_error: CalledProcessError) -> None:
super().__init__(command=command, exit_code=call_error.returncode)
CalledProcessError.__init__(
self,
returncode=call_error.returncode,
cmd=call_error.cmd,
output=call_error.output,
stderr=call_error.stderr,
)
示例4: test_fails_on_non_expected_exception
def test_fails_on_non_expected_exception(self):
mock_client = _get_time_noop_mock_client()
exp = CalledProcessError(-1, 'blah')
exp.stderr = '"" is not a valid tag'
controller_client = Mock()
controller_client.get_models.side_effect = [exp]
mock_client.get_controller_client.return_value = controller_client
with self.assertRaises(CalledProcessError):
amm.wait_until_model_disappears(
mock_client, 'test_model', timeout=60)
示例5: log_check_call
def log_check_call(*args, **kwargs):
kwargs['record_output'] = True
retcode, output = log_call(*args, **kwargs)
if retcode != 0:
cmd = kwargs.get('args')
if cmd is None:
cmd = args[0]
e = CalledProcessError(retcode, cmd)
e.output = output
raise e
return 0
示例6: check_output
def check_output(*args, **kwds):
process = Popen(stdout=PIPE, *args, **kwds)
output, _ = process.communicate()
retcode = process.poll()
if retcode:
cmd = kwds.get("args")
if cmd is None:
cmd = args[0]
error = CalledProcessError(retcode, cmd)
error.output = output
raise error
return output
示例7: check_output
def check_output(*popenargs, **kwargs):
"""Run command with arguments and return its output as a byte string."""
process = Popen(stdout=PIPE, *popenargs, **kwargs)
output, unused_err = process.communicate()
retcode = process.poll()
if retcode:
cmd = kwargs.get("args")
if cmd is None:
cmd = popenargs[0]
error = CalledProcessError(retcode, cmd)
error.output = output
raise error
return output
示例8: mockreturn
def mockreturn(*args, **kwargs):
if args == (['task', 'stats'],):
output = b"""
Category Data
-------- ----
Pending 0
Waiting 0"""
return output
elif args == (['task', 'overdue'],):
output = b'No matches.'
e = CalledProcessError(1, 'task')
e.output = output
raise e
示例9: call_for_stderr
def call_for_stderr(command, *args, **kwargs):
kwargs["stderr"] = _subprocess.PIPE
proc = start_process(command, *args, **kwargs)
output = proc.communicate()[1].decode("utf-8")
exit_code = proc.poll()
if exit_code != 0:
error = CalledProcessError(exit_code, proc.command_string)
error.output = output
raise error
return output
示例10: __run
def __run(self, *args, **kwargs):
_args = [i for i in args if i is not None]
argsLog = [self.__hidePassw(i) for i in args if i is not None ]
logging.debug("CMD: " + " ". join(argsLog))
process = Popen(
_args, stdout=kwargs.pop('stdout', PIPE),
stderr=kwargs.pop('stderr', PIPE),
close_fds=kwargs.pop('close_fds', True), **kwargs)
stdout, stderr = process.communicate()
if process.returncode:
exception = CalledProcessError(
process.returncode, repr(args))
exception.output = ''.join(filter(None, [stdout, stderr]))
raise Error('1001', err=exception.output)
return stdout
示例11: check_output
def check_output(run_args, *args, **kwargs):
kwargs['stdout'] = PIPE
kwargs['stderr'] = PIPE
process = Popen(run_args, *args, **kwargs)
stdout, stderr = process.communicate()
retcode = process.poll()
if retcode is not 0:
exception = CalledProcessError(retcode, run_args[0])
exception.stdout = stdout
exception.stderr = stderr
raise exception
return stdout, stderr
示例12: __lt__
def __lt__(self,other):
cmd = self.__get_recursive_name()
#print " ",cmd,"<",other
popen = subprocess.Popen(cmd,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
m = popen.communicate(other)
ret = popen.wait()
if ret:
e = CalledProcessError(ret,cmd)
e.stdout,e.stderr = m
raise e
class CommandOutput:
def __init__(self,stdout,stderr):
self.stdout = stdout
self.stderr = stderr
return CommandOutput(*m)
示例13: check_output
def check_output(arguments, stdin=None, stderr=None, shell=False):
temp_f = mkstemp()
returncode = call(arguments, stdin=stdin, stdout=temp_f[0], stderr=stderr, shell=shell)
close(temp_f[0])
file_o = open(temp_f[1], 'r')
cmd_output = file_o.read()
file_o.close()
remove(temp_f[1])
if returncode != 0:
error_cmd = CalledProcessError(returncode, arguments[0])
error_cmd.output = cmd_output
raise error_cmd
else:
return cmd_output
示例14: check_call_capturing
def check_call_capturing(arguments, input = None, preexec_fn = None):
"""Spawn a process and return its output."""
(stdout, stderr, code) = call_capturing(arguments, input, preexec_fn)
if code == 0:
return (stdout, stderr)
else:
from subprocess import CalledProcessError
error = CalledProcessError(code, arguments)
error.stdout = stdout
error.stderr = stderr
raise error
示例15: func
def func(*args):
"""Wrapper function used to append arguments to command."""
cmd = [command_line]
cmd += [str(arg) for arg in args]
proc = subprocess.Popen(cmd, stderr=PIPE, stdout=PIPE)
stdout, stderr = proc.communicate()
proc.wait()
if proc.returncode:
err = CalledProcessError(
returncode=proc.returncode,
cmd=" ".join(cmd),
output=stdout,
)
err.stderr = stderr
raise err
return stdout, stderr