@@ -148,10 +148,6 @@ def run(self, dry_run=False, pull_image=True, rm_container=True,
148148
149149 stageFiles (self .pathmapper , os .symlink )
150150
151- stdin = None # type: Union[IO[Any], int]
152- stderr = None # type: IO[Any]
153- stdout = None # type: IO[Any]
154-
155151 scr , _ = get_feature (self , "ShellCommandRequirement" )
156152
157153 if scr :
@@ -188,51 +184,34 @@ def linkoutdir(src, tgt):
188184 break
189185 stageFiles (generatemapper , linkoutdir )
190186
187+ stdin_path = None
191188 if self .stdin :
192- stdin = open (self .pathmapper .reversemap (self .stdin )[1 ], "rb" )
193- else :
194- stdin = subprocess .PIPE
189+ stdin_path = self .pathmapper .reversemap (self .stdin )[1 ]
195190
191+ stderr_path = None
196192 if self .stderr :
197193 abserr = os .path .join (self .outdir , self .stderr )
198194 dnerr = os .path .dirname (abserr )
199195 if dnerr and not os .path .exists (dnerr ):
200196 os .makedirs (dnerr )
201- stderr = open (abserr , "wb" )
202- else :
203- stderr = sys .stderr
197+ stderr_path = abserr
204198
199+ stdout_path = None
205200 if self .stdout :
206201 absout = os .path .join (self .outdir , self .stdout )
207202 dn = os .path .dirname (absout )
208203 if dn and not os .path .exists (dn ):
209204 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 ()
205+ stdout_path = absout
225206
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 ()
207+ rcode = _job_popen (
208+ [Text (x ).encode ('utf-8' ) for x in runtime + self .command_line ],
209+ stdin_path = stdin_path ,
210+ stdout_path = stdout_path ,
211+ stderr_path = stderr_path ,
212+ env = env ,
213+ cwd = self .outdir ,
214+ )
236215
237216 if self .successCodes and rcode in self .successCodes :
238217 processStatus = "success"
@@ -291,3 +270,58 @@ def linkoutdir(src, tgt):
291270 if move_outputs == "move" and empty_subtree (self .outdir ):
292271 _logger .debug (u"[job %s] Removing empty output directory %s" , self .name , self .outdir )
293272 shutil .rmtree (self .outdir , True )
273+
274+
275+ def _job_popen (
276+ commands ,
277+ stdin_path ,
278+ stdout_path ,
279+ stderr_path ,
280+ env ,
281+ cwd ,
282+ ):
283+ # type: (List[str], Text, Text, Text, Union[MutableMapping[Text, Text], MutableMapping[str, str]], Text) -> int
284+
285+ stdin = None # type: Union[IO[Any], int]
286+ stderr = None # type: IO[Any]
287+ stdout = None # type: IO[Any]
288+
289+ if stdin_path is not None :
290+ stdin = open (stdin_path , "rb" )
291+ else :
292+ stdin = subprocess .PIPE
293+
294+ if stdout_path is not None :
295+ stdout = open (stdout_path , "wb" )
296+ else :
297+ stdout = sys .stderr
298+
299+ if stderr_path is not None :
300+ stderr = open (stderr_path , "wb" )
301+ else :
302+ stderr = sys .stderr
303+
304+ sp = subprocess .Popen (commands ,
305+ shell = False ,
306+ close_fds = True ,
307+ stdin = stdin ,
308+ stdout = stdout ,
309+ stderr = stderr ,
310+ env = env ,
311+ cwd = cwd )
312+
313+ if sp .stdin :
314+ sp .stdin .close ()
315+
316+ rcode = sp .wait ()
317+
318+ if isinstance (stdin , file ):
319+ stdin .close ()
320+
321+ if stdout is not sys .stderr :
322+ stdout .close ()
323+
324+ if stderr is not sys .stderr :
325+ stderr .close ()
326+
327+ return rcode
0 commit comments