2018-03-21 01:04:59 +03:00
|
|
|
#!/usr/bin/env python3
|
2019-06-20 02:58:25 +03:00
|
|
|
# Copyright (c) Facebook, Inc. and its affiliates.
|
2018-03-21 01:04:59 +03:00
|
|
|
#
|
2019-06-20 02:58:25 +03:00
|
|
|
# This software may be used and distributed according to the terms of the
|
|
|
|
# GNU General Public License version 2.
|
2018-03-21 01:04:59 +03:00
|
|
|
|
|
|
|
import shlex
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
|
|
|
|
class CommandError(subprocess.CalledProcessError):
|
2018-05-10 07:33:49 +03:00
|
|
|
"""
|
2018-03-21 01:04:59 +03:00
|
|
|
A wrapper around subprocess.CalledProcessError that also includes
|
|
|
|
includes the process's stderr when converted to a string.
|
2018-05-10 07:33:49 +03:00
|
|
|
"""
|
|
|
|
|
2018-03-21 01:04:59 +03:00
|
|
|
def __init__(self, orig: subprocess.CalledProcessError) -> None:
|
2018-05-10 07:33:49 +03:00
|
|
|
super().__init__(
|
|
|
|
orig.returncode, orig.cmd, output=orig.output, stderr=orig.stderr
|
|
|
|
)
|
2018-03-21 01:04:59 +03:00
|
|
|
|
|
|
|
def __str__(self) -> str:
|
|
|
|
if not self.stderr:
|
|
|
|
return super().__str__()
|
|
|
|
|
2018-05-10 07:33:49 +03:00
|
|
|
cmd_str = " ".join(shlex.quote(arg) for arg in self.cmd)
|
2018-03-21 01:04:59 +03:00
|
|
|
|
|
|
|
stderr_str = self.stderr
|
|
|
|
if isinstance(self.stderr, bytes):
|
2018-05-10 07:33:49 +03:00
|
|
|
stderr_str = self.stderr.decode("utf-8", errors="replace")
|
2018-03-21 01:04:59 +03:00
|
|
|
|
|
|
|
# Indent the stderr output just to help indicate where it starts
|
|
|
|
# and ends in the test output.
|
2018-05-10 07:33:49 +03:00
|
|
|
stderr_str = stderr_str.replace("\n", "\n ")
|
2018-03-21 01:04:59 +03:00
|
|
|
|
2018-05-10 07:33:49 +03:00
|
|
|
msg = "Command [%s] failed with status %s\nstderr:\n %s" % (
|
2018-05-19 09:05:39 +03:00
|
|
|
cmd_str,
|
|
|
|
self.returncode,
|
|
|
|
stderr_str,
|
2018-05-10 07:33:49 +03:00
|
|
|
)
|
2018-03-21 01:04:59 +03:00
|
|
|
return msg
|