Skip to content

Commit 04d20f3

Browse files
authored
BuildCommand: allow to merge or not stderr with stdout (#9184)
This is mainly for our internal commands, specifically for ls-remote, where a warning was getting in the output that we parse. We should use this option in more of our internal commands where we parse the output. Ref https://sentry.io/organizations/read-the-docs/issues/3227717706/?project=148442&query=is%3Aunresolved
1 parent 49b7ee8 commit 04d20f3

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

readthedocs/doc_builder/environments.py

+26-13
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,22 @@ class BuildCommand(BuildCommandResultMixin):
5757
or ``user``. Defaults to ``RTD_DOCKER_USER``.
5858
:param build_env: build environment to use to execute commands
5959
:param bin_path: binary path to add to PATH resolution
60+
:param demux: Return stdout and stderr separately.
6061
:param kwargs: allow to subclass this class and extend it
6162
"""
6263

6364
def __init__(
64-
self,
65-
command,
66-
cwd=None,
67-
shell=False,
68-
environment=None,
69-
user=None,
70-
build_env=None,
71-
bin_path=None,
72-
record_as_success=False,
73-
**kwargs,
65+
self,
66+
command,
67+
cwd=None,
68+
shell=False,
69+
environment=None,
70+
user=None,
71+
build_env=None,
72+
bin_path=None,
73+
record_as_success=False,
74+
demux=False,
75+
**kwargs,
7476
):
7577
self.command = command
7678
self.shell = shell
@@ -88,6 +90,7 @@ def __init__(
8890

8991
self.bin_path = bin_path
9092
self.record_as_success = record_as_success
93+
self.demux = demux
9194
self.exit_code = None
9295

9396
# NOTE: `self.build_env` is not available when instantiating this class
@@ -147,13 +150,14 @@ def run(self):
147150
if self.shell:
148151
command = self.get_command()
149152

153+
stderr = subprocess.PIPE if self.demux else subprocess.STDOUT
150154
proc = subprocess.Popen(
151155
command,
152156
shell=self.shell,
153157
cwd=self.cwd,
154158
stdin=None,
155159
stdout=subprocess.PIPE,
156-
stderr=subprocess.STDOUT,
160+
stderr=stderr,
157161
env=environment,
158162
)
159163
cmd_stdout, cmd_stderr = proc.communicate()
@@ -302,8 +306,17 @@ def run(self):
302306
stderr=True,
303307
)
304308

305-
cmd_output = client.exec_start(exec_id=exec_cmd['Id'], stream=False)
306-
self.output = self.sanitize_output(cmd_output)
309+
out = client.exec_start(
310+
exec_id=exec_cmd["Id"], stream=False, demux=self.demux
311+
)
312+
cmd_stdout = ""
313+
cmd_stderr = ""
314+
if self.demux:
315+
cmd_stdout, cmd_stderr = out
316+
else:
317+
cmd_stdout = out
318+
self.output = self.sanitize_output(cmd_stdout)
319+
self.error = self.sanitize_output(cmd_stderr)
307320
cmd_ret = client.exec_inspect(exec_id=exec_cmd['Id'])
308321
self.exit_code = cmd_ret['ExitCode']
309322

readthedocs/vcs_support/backends/git.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ def lsremote(self, include_tags=True, include_branches=True):
220220
cmd = ["git", "ls-remote", *extra_args, self.repo_url]
221221

222222
self.check_working_dir()
223-
_, stdout, _ = self.run(*cmd)
223+
_, stdout, _ = self.run(*cmd, demux=True, record=False)
224224

225225
branches = []
226226
# Git has two types of tags: lightweight and annotated.

0 commit comments

Comments
 (0)