@@ -188,51 +188,34 @@ def linkoutdir(src, tgt):
188188 break
189189 stageFiles (generatemapper , linkoutdir )
190190
191+ stdin_path = None
191192 if self .stdin :
192- stdin = open (self .pathmapper .reversemap (self .stdin )[1 ], "rb" )
193- else :
194- stdin = subprocess .PIPE
193+ stdin_path = self .pathmapper .reversemap (self .stdin )[1 ]
195194
195+ stderr_path = None
196196 if self .stderr :
197197 abserr = os .path .join (self .outdir , self .stderr )
198198 dnerr = os .path .dirname (abserr )
199199 if dnerr and not os .path .exists (dnerr ):
200200 os .makedirs (dnerr )
201- stderr = open (abserr , "wb" )
202- else :
203- stderr = sys .stderr
201+ stderr_path = abserr
204202
203+ stdout_path = None
205204 if self .stdout :
206205 absout = os .path .join (self .outdir , self .stdout )
207206 dn = os .path .dirname (absout )
208207 if dn and not os .path .exists (dn ):
209208 os .makedirs (dn )
210- stdout = open (absout , "wb" )
211- else :
212- stdout = sys .stderr
213-
214- sp = subprocess .Popen ([Text (x ).encode ('utf-8' ) for x in runtime + self .command_line ],
215- shell = False ,
216- close_fds = True ,
217- stdin = stdin ,
218- stderr = stderr ,
219- stdout = stdout ,
220- env = env ,
221- cwd = self .outdir )
222-
223- if sp .stdin :
224- sp .stdin .close ()
209+ stdout_path = absout
225210
226- rcode = sp .wait ()
227-
228- if isinstance (stdin , file ):
229- stdin .close ()
230-
231- if stderr is not sys .stderr :
232- stderr .close ()
233-
234- if stdout is not sys .stderr :
235- stdout .close ()
211+ rcode = _job_popen (
212+ [Text (x ).encode ('utf-8' ) for x in runtime + self .command_line ],
213+ stdin_path = stdin_path ,
214+ stdout_path = stdout_path ,
215+ stderr_path = stderr_path ,
216+ env = env ,
217+ cwd = self .outdir ,
218+ )
236219
237220 if self .successCodes and rcode in self .successCodes :
238221 processStatus = "success"
@@ -291,3 +274,53 @@ def linkoutdir(src, tgt):
291274 if move_outputs == "move" and empty_subtree (self .outdir ):
292275 _logger .debug (u"[job %s] Removing empty output directory %s" , self .name , self .outdir )
293276 shutil .rmtree (self .outdir , True )
277+
278+
279+ def _job_popen (
280+ commands ,
281+ stdin_path ,
282+ stdout_path ,
283+ stderr_path ,
284+ env ,
285+ cwd ,
286+ ):
287+ # type: (List[Text], Text, Text, Text, MutableMapping[Text, Text], Text) -> int
288+ if stdin_path is not None :
289+ stdin = open (stdin_path , "rd" )
290+ else :
291+ stdin = subprocess .PIPE
292+
293+ if stdout_path is not None :
294+ stdout = open (stdout_path , "wb" )
295+ else :
296+ stdout = sys .stderr
297+
298+ if stderr_path is not None :
299+ stderr = open (stderr_path , "wb" )
300+ else :
301+ stderr = sys .stderr
302+
303+ sp = subprocess .Popen (commands ,
304+ shell = False ,
305+ close_fds = True ,
306+ stdin = stdin ,
307+ stdout = stdout ,
308+ stderr = stderr ,
309+ env = env ,
310+ cwd = cwd )
311+
312+ if sp .stdin :
313+ sp .stdin .close ()
314+
315+ rcode = sp .wait ()
316+
317+ if isinstance (stdin , file ):
318+ stdin .close ()
319+
320+ if stdout is not sys .stderr :
321+ stdout .close ()
322+
323+ if stderr is not sys .stderr :
324+ stderr .close ()
325+
326+ return rcode
0 commit comments