@@ -188,51 +188,34 @@ def linkoutdir(src, tgt):
188
188
break
189
189
stageFiles (generatemapper , linkoutdir )
190
190
191
+ stdin_path = None
191
192
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 ]
195
194
195
+ stderr_path = None
196
196
if self .stderr :
197
197
abserr = os .path .join (self .outdir , self .stderr )
198
198
dnerr = os .path .dirname (abserr )
199
199
if dnerr and not os .path .exists (dnerr ):
200
200
os .makedirs (dnerr )
201
- stderr = open (abserr , "wb" )
202
- else :
203
- stderr = sys .stderr
201
+ stderr_path = abserr
204
202
203
+ stdout_path = None
205
204
if self .stdout :
206
205
absout = os .path .join (self .outdir , self .stdout )
207
206
dn = os .path .dirname (absout )
208
207
if dn and not os .path .exists (dn ):
209
208
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
225
210
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
+ )
236
219
237
220
if self .successCodes and rcode in self .successCodes :
238
221
processStatus = "success"
@@ -291,3 +274,53 @@ def linkoutdir(src, tgt):
291
274
if move_outputs == "move" and empty_subtree (self .outdir ):
292
275
_logger .debug (u"[job %s] Removing empty output directory %s" , self .name , self .outdir )
293
276
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, Union[MutableMapping[Text, Text], MutableMapping[str, str]], 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