mirror of
https://github.com/facebook/sapling.git
synced 2024-10-10 08:47:12 +03:00
fbdb46f5cb
Reviewed By: chadaustin Differential Revision: D17872966 fbshipit-source-id: cd60a364a2146f0dadbeca693b1d4a5d7c97ff63
42 lines
1.2 KiB
Python
42 lines
1.2 KiB
Python
#!/usr/bin/env python3
|
|
# Copyright (c) Facebook, Inc. and its affiliates.
|
|
#
|
|
# This software may be used and distributed according to the terms of the
|
|
# GNU General Public License version 2.
|
|
|
|
import shlex
|
|
import subprocess
|
|
|
|
|
|
class CommandError(subprocess.CalledProcessError):
|
|
"""
|
|
A wrapper around subprocess.CalledProcessError that also includes
|
|
includes the process's stderr when converted to a string.
|
|
"""
|
|
|
|
def __init__(self, orig: subprocess.CalledProcessError) -> None:
|
|
super().__init__(
|
|
orig.returncode, orig.cmd, output=orig.output, stderr=orig.stderr
|
|
)
|
|
|
|
def __str__(self) -> str:
|
|
if not self.stderr:
|
|
return super().__str__()
|
|
|
|
cmd_str = " ".join(shlex.quote(arg) for arg in self.cmd)
|
|
|
|
stderr_str = self.stderr
|
|
if isinstance(self.stderr, bytes):
|
|
stderr_str = self.stderr.decode("utf-8", errors="replace")
|
|
|
|
# Indent the stderr output just to help indicate where it starts
|
|
# and ends in the test output.
|
|
stderr_str = stderr_str.replace("\n", "\n ")
|
|
|
|
msg = "Command [%s] failed with status %s\nstderr:\n %s" % (
|
|
cmd_str,
|
|
self.returncode,
|
|
stderr_str,
|
|
)
|
|
return msg
|