@@ -57,20 +57,22 @@ class BuildCommand(BuildCommandResultMixin):
57
57
or ``user``. Defaults to ``RTD_DOCKER_USER``.
58
58
:param build_env: build environment to use to execute commands
59
59
:param bin_path: binary path to add to PATH resolution
60
+ :param demux: Return stdout and stderr separately.
60
61
:param kwargs: allow to subclass this class and extend it
61
62
"""
62
63
63
64
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 ,
74
76
):
75
77
self .command = command
76
78
self .shell = shell
@@ -88,6 +90,7 @@ def __init__(
88
90
89
91
self .bin_path = bin_path
90
92
self .record_as_success = record_as_success
93
+ self .demux = demux
91
94
self .exit_code = None
92
95
93
96
# NOTE: `self.build_env` is not available when instantiating this class
@@ -147,13 +150,14 @@ def run(self):
147
150
if self .shell :
148
151
command = self .get_command ()
149
152
153
+ stderr = subprocess .PIPE if self .demux else subprocess .STDOUT
150
154
proc = subprocess .Popen (
151
155
command ,
152
156
shell = self .shell ,
153
157
cwd = self .cwd ,
154
158
stdin = None ,
155
159
stdout = subprocess .PIPE ,
156
- stderr = subprocess . STDOUT ,
160
+ stderr = stderr ,
157
161
env = environment ,
158
162
)
159
163
cmd_stdout , cmd_stderr = proc .communicate ()
@@ -302,8 +306,17 @@ def run(self):
302
306
stderr = True ,
303
307
)
304
308
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 )
307
320
cmd_ret = client .exec_inspect (exec_id = exec_cmd ['Id' ])
308
321
self .exit_code = cmd_ret ['ExitCode' ]
309
322
0 commit comments