Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix print stderr on failure at check_output tool. #5548

Merged
merged 2 commits into from Jul 29, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 9 additions & 4 deletions conans/client/tools/oss.py
Expand Up @@ -4,11 +4,13 @@
import subprocess
import sys
import tempfile
from subprocess import CalledProcessError, PIPE
from subprocess import PIPE

import six

from conans.client.tools.env import environment_append
from conans.client.tools.files import load, which
from conans.errors import ConanException
from conans.errors import ConanException, CalledProcessErrorWithStderr
from conans.model.version import Version
from conans.util.fallbacks import default_output

Expand Down Expand Up @@ -489,17 +491,20 @@ def get_gnu_triplet(os_, arch, compiler=None):
def check_output(cmd, folder=None, return_code=False, stderr=None):
tmp_file = tempfile.mktemp()
try:
# We don't want stderr to print warnings that will mess the pristine outputs
stderr = stderr or PIPE
cmd = cmd if isinstance(cmd, six.string_types) else subprocess.list2cmdline(cmd)
process = subprocess.Popen("{} > {}".format(cmd, tmp_file), shell=True,
stderr=stderr, cwd=folder)
process.communicate()

_, stderr = process.communicate()

if return_code:
return process.returncode

if process.returncode:
raise CalledProcessError(process.returncode, cmd)
# Only in case of error, we print also the stderr to know what happened
raise CalledProcessErrorWithStderr(process.returncode, cmd, stderr=stderr)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://docs.python.org/3/library/subprocess.html#subprocess.CalledProcessError

Changed in version 3.5: stdout and stderr attributes added

Will crash in other python versions and in 2.7

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, fixed.


output = load(tmp_file)
return output
Expand Down
10 changes: 10 additions & 0 deletions conans/errors.py
Expand Up @@ -9,8 +9,18 @@

"""
from contextlib import contextmanager
from subprocess import CalledProcessError

from conans.util.env_reader import get_env
from conans.util.files import decode_text


class CalledProcessErrorWithStderr(CalledProcessError):
def __str__(self):
ret = super(CalledProcessErrorWithStderr, self).__str__()
if self.stderr:
ret += "\n" + decode_text(self.stderr)
return ret


@contextmanager
Expand Down